http://acm.sdut.edu.cn:8080/vjudge/contest/view.action?cid=91#problem/C
比赛时候一直没看懂题意,sad......
题意:给定一个字符串,把它变成n个“String Successor”,若一个字符串中没有字母或数字,直接在最右边的字符加1,否则找到从右边数第一个字母或数字加1,若是'9' 'z'或'Z',加1后边分别变为'0','a'和'A',并向前面的第一个数字或字母进位,若前面没有字母或数字,就直接在当前字母或数字前面加上'1','a'或'A'。
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <string>
#include <iostream>
#include <ctype.h>
using namespace std;
void dfs(string &str, int i)
{
if(str[i] == '9'+1 || str[i] == 'z'+1 || str[i] == 'Z'+1)//先判断是否应该进位
{
char ch;
if(str[i] == '9'+1)
{
ch = '1';
str[i] = '0';
}
else if(str[i] == 'z'+1)
{
ch = 'a';
str[i] = 'a';
}
else
{
ch = 'A';
str[i] = 'A';
}
int j;
for(j = i-1; j >= 0; j--)
if(isalpha(str[j]) || isdigit(str[j]) )
break;
if(j != -1) //若i之前有字母或数字,递归进位
{
str[j]++;
dfs(str,j);
}
else str.insert(str.begin()+i,ch);//若i之前没有字母或数字,就在i-1处增加'1''A'或‘a’。
}
}
int main()
{
int test;
string str;
int n;
cin >> test;
while(test--)
{
cin >> str >> n;
while(n--)
{
int i;
for( i = str.size()-1; i >= 0; i--)
if( isalpha(str[i]) || isdigit(str[i]) )
break;
if(i == -1) //没有字母或数字,直接在最右边加1
str[ str.size()-1 ] ++;
else
{
str[i]++;//加1后向前进位
dfs(str,i);
}
printf("%s\n",str.c_str());
}
printf("\n");
}
return 0;
}