求助
查看原帖
求助
965233
_Oxygen_楼主2023/8/27 10:48

样例1中的1234不知道怎么加

#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;

const int N = 1010;
int p1, p2, p3, amo;
string str, temp;
string ans;

bool check(int a){ // 是否同为字母或数字 
	if ((isdigit(str[a - 1]) && isdigit(str[a + 1])) || (isalpha(str[a - 1]) && isalpha(str[a + 1])))
		if (str[a - 1] < str[a + 1])
			return true;
			
	return false;
}

void open(int a){ // 展开操作 
	switch (p1){
		case 1:{ // 我用的算法导致数字和字母是一样的 
			for (int i = (int)str[a - 1]; i <= (int)str[a + 1]; i ++)
				for (int j = 1; j <= p2; j ++)
					temp += (char)i;
				
				// 掐头去尾 
			temp.erase(temp.begin(), temp.begin() + p2);
			temp.erase(temp.end() - p2, temp.end());
			if (p3 == 2) reverse(temp.begin(), temp.end());
			ans = ans + temp + str[a + 1];
			temp.clear();
			break;
		}
		case 2:{
			if (isalpha(str[a + 1])){ // 填充大写字母 
				for (int i = (int)(str[a - 1]) - 32; i <= (int)(str[a + 1]) - 32; i ++)
					for (int j = 1; j <= p2; j ++)
						temp += (char)i;
				
				temp.erase(temp.begin(), temp.begin() + p2);
				temp.erase(temp.end() - 2, temp.end());
				if (p3 == 2) reverse(temp.begin(), temp.end());
				ans = ans + temp + str[a + 1];
			}
			else{
				for (int i = (int)str[a - 1]; i <= (int)str[a + 1]; i ++)
					for (int j = 1; j <= p2; j ++)
						temp += (char)i;
	
				temp.erase(temp.begin(), temp.begin() + p2);
				temp.erase(temp.end() - p2, temp.end());
				if (p3 == 2) reverse(temp.begin(), temp.end());
				ans = ans + temp + str[a + 1];
			}
			break;
		}
		case 3:{
			for (int i = (int)(str[a - 1]) - 32; i <= (int)(str[a + 1]) - 32; i ++)
				for (int j = 1; j <= p2; j ++)
					temp += '*';
			temp.erase(temp.begin(), temp.begin() + p2);
			temp.erase(temp.end() - 2, temp.end());
			ans = ans + temp + str[a + 1];
			break;
		}
	}
	temp.clear(); // 多测不清空 十年OI一场空 
	str[a] = ' ';
}

int main(){
	freopen("3.txt", "r", stdin);
	cin >> p1 >> p2 >> p3;
	cin >> str;
	if (str[0] == '-' && str[str.length() - 1]) ans += '-'; // 首尾是'-' 
	int a = str.find('-');
	ans += str.substr(0, a); // 把第一个减号和右边都去掉
	
	for (int i = 0; i <= str.length(); i ++)
		if (str[i] == '-')
			amo ++; // 表示'-'的数量 
	for (int i = 0; i < amo; i ++){
		a = str.find('-');
		if (check(a)){ // 需要展开 
			if ((int)str[a - 1] - (int)str[a + 1] == -1){
				// 右边的字符恰好是左边字符的后继
				str.erase(a, a);
				ans += str.substr(a, a + 2);
			}
			else open(a);
		}
		else{ 
			// 因为check函数里已经确定了 右边的ascii大于左边的 
			// 所以这里是 减号右边的字符按照 ASCII 码的顺序小于或等于左边字符
			ans += str.substr(a - 1, a + 2);
		}
	}
	cout << ans << endl;
	return 0;
}
2023/8/27 10:48
加载中...