【思路】:按照题意来就行。注意当p1=2时,字母串和数字串的区别。推荐使用string,采用replace函数替换就行,char[]移动大量数据。
【AC代码】:
#include <iostream> #include <algorithm> #include <string> #include <cstdio> #include <cstring> using namespace std; #define MAX 10000+5 int isLower(char c) { if (c >= 'a' && c <= 'z') return 1; return 0; } int isNum(char c) { if (c >= '0' && c <= '9') return 1; return 0; } void reverse(char repstr[]) { int i = 0; for (i = 0; i < strlen(repstr)/2; i++) { char temp; temp = repstr[i]; repstr[i] = repstr[strlen(repstr)-1-i]; repstr[strlen(repstr)-1-i] = temp; } } int main() { freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout); int p1 = 0, p2 = 0, p3 = 0, i = 0, j = 0, k = 0; string str; //input cin >> p1 >> p2 >> p3; cin >> str; //handle for (i = 1; i < str.length()-1; i++) //注意范围 { if ('-' == str[i]) { int flag = 0; char repstr[MAX]; if (isLower(str[i-1]) && isLower(str[i+1]) && str[i+1]>str[i-1]) flag = 1; else if (isNum(str[i-1]) && isNum(str[i+1]) && str[i+1]>str[i-1]) flag = 2; if (0==flag) continue; //replace p1 p2 int cnt = 0; for (j = str[i-1]+1; j < str[i+1]; j++) { for (k = 0; k < p2; k++) { if (1 == p1) { repstr[cnt++] = j; } else if (2 == p1) { if (1 == flag) repstr[cnt++] = j+'A'-'a'; else if (2 == flag) repstr[cnt++] = j; } else if (3 == p1) { repstr[cnt++] = '*'; } } } repstr[cnt] = '\0'; //p3 if (2 == p3) reverse(repstr); //replace string rep = repstr; str.replace(i, 1, rep); } } //output cout << str; return 0; }
备注:
原文地址:http://blog.csdn.net/weijj6608/article/details/44681631