The successor to a string can be calculated by applying the following rules:
- Ignore the nonalphanumerics unless there are no alphanumerics, in this case, increase the rightmost character in the string.
- The increment starts from the rightmost alphanumeric.
- Increase a digit always results in another digit ('0' -> '1', '1' -> '2' ... '9' -> '0').
- Increase a upper case always results in another upper case ('A' -> 'B', 'B' -> 'C' ... 'Z' -> 'A').
- Increase a lower case always results in another lower case ('a' -> 'b', 'b' -> 'c' ... 'z' -> 'a').
- If the increment generates a carry, the alphanumeric to the left of it is increased.
- Add an additional alphanumeric to the left of the leftmost alphanumeric if necessary, the added alphanumeric is always of the same type with the leftmost alphanumeric ('1' for digit, 'A' for upper case and 'a' for lower case).
Input
There are multiple test cases. The first line of input is an integer T ≈ 10000 indicating the number of test cases.
Each test case contains a nonempty string s and an integer 1 ≤ n ≤ 100. The string s consists of no more than 100 characters whose ASCII values range from 33('!') to 122('z').
OutputFor each test case, output the next n successors to the given string s in separate lines. Output a blank line after each test case.
Sample Input4 :-( 1 cirno=8 2 X 3 /**********/ 4Sample Output
:-) cirno=9 cirnp=0 Y Z AA /**********0 /**********1 /**********2 /**********3
题意:按所给规则求出字符串的后1到n个
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 250;
char str[N];
int judge(int len){
for(int i=0;i<len;i++)
if(str[i]>='0'&&str[i]<='9'||str[i]>='a'&&str[i]<='z'||str[i]>='A'&&str[i]<='Z') return 1;
return 0;
}
void update(){
int len=strlen(str);
if(judge(len)){
int flag=1; //在最后一位加一
int carry=0; //进位
for(int i=len-1;i>=0;i--){
if(str[i]>='0'&&str[i]<='9'||str[i]>='a'&&str[i]<='z'||str[i]>='A'&&str[i]<='Z'){
if(str[i]>='0'&&str[i]<='9'){
char temp=str[i]+carry+flag;
flag=0;
if(temp>'9'){
carry=1;
str[i]='0';
}
else{
carry=0;
str[i]=temp;
break;
}
}
else if(str[i]>='a'&&str[i]<='z'){
char temp=str[i]+carry+flag;
flag=0;
if(temp>'z'){
carry=1;
str[i]='a';
}
else{
carry=0;
str[i]=temp;
break;
}
}
else if(str[i]>='A'&&str[i]<='Z'){
char temp=str[i]+carry+flag;
flag=0;
if(temp>'Z'){
carry=1;
str[i]='A';
}
else{
carry=0;
str[i]=temp;
break;
}
}
}
else continue;
}
if(carry){ //如果还有进位,在第一个符合条件的位置前加同类型的最小字符
char a[200];
int bound;
for(int i=0;i<len;i++){
if(str[i]>='0'&&str[i]<='9'||str[i]>='a'&&str[i]<='z'||str[i]>='A'&&str[i]<='Z'){
bound=i;
break;
}
}
int cnt=0;
for(int i=bound;i<len;i++) //向后移动一位
a[cnt++]=str[i];
if(str[bound]>='0'&&str[bound]<='9') str[bound]='1';
else if(str[bound]>='a'&&str[bound]<='z') str[bound]='a';
else if(str[bound]>='A'&&str[bound]<='Z') str[bound]='A';
int i,j;
for(i=bound+1,j=0;j<cnt;j++,i++)
str[i]=a[j];
str[i]='\0';
}
}
else str[len-1]++;
}
int main(){
int T;
scanf("%d",&T);
while(T--){
int n;
scanf("%s%d",str,&n);
for(int i=0;i<n;i++){
update();
printf("%s\n",str);
}
puts("");
}
return 0;
}