题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4313
The successor to a string can be calculated by applying the following rules:
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‘).
For each test case, output the next n successors to the given string s in separate lines. Output a blank line after each test case.
4 :-( 1 cirno=8 2 X 3 /**********/ 4
:-) cirno=9 cirnp=0 Y Z AA /**********0 /**********1 /**********2 /**********3
代码如下:
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <iostream>
using namespace std;
string s;
int judge(char c)
{
if(c >= '0' && c <= '9')
{
return 1;
}
else if(c >= 'a' && c <= 'z')
{
return 2;
}
else if(c >= 'A' && c <= 'Z')
{
return 3;
}
return 0;
}
int check(char &c, int type)
{
if(type == 1)
{
if(c == '9')
{
c = '0';
return 1;
}
else
{
c++;
return 0;
}
}
if(type == 2)
{
if(c == 'z')
{
c = 'a';
return 1;
}
else
{
c++;
return 0;
}
}
if(type == 3)
{
if(c == 'Z')
{
c = 'A';
return 1;
}
else
{
c++;
return 0;
}
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n;
cin >> s >> n;
while(n--)
{
int len = s.size();
int flag = 0;
int pos;
for(int i = 0; i < len; i++)
{
if(judge(s[i]))//judge 检测是否为字母或数字
{
flag = 1;
break;
}
}
if(!flag)//全是符号
{
s[len-1]++;
cout << s <<endl;
continue;
}
int k = 0;
int type, tt;
for(int i = len-1; i >=0; i--)
{
if(judge(s[i]))//judge 检测是否为字母或数字
{
tt = check(s[i],judge(s[i]));
int k = 1;
type = judge(s[i]);
pos = i;
if(tt == 0)//没有进位
{
break;
}
}
}
if(tt == 1)
{
if(type == 1)
{
s.insert(pos,"1");
}
else if(type == 2)
{
s.insert(pos,"a");
}
else if(type == 3)
{
s.insert(pos,"A");
}
}
cout<<s<<endl;
}
printf("\n");
}
return 0;
}
ZOJ 3490 String Successor(模拟啊 )
原文:http://blog.csdn.net/u012860063/article/details/45116551