ZOJ 3490 String Successor(模拟)
Time Limit: 2 Seconds Memory Limit: 65536 KB
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’).
Output
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.
Sample Input
4
:-( 1
cirno=8 2
X 3
/****/ 4
Sample Output
:-)
cirno=9
cirnp=0
Y
Z
AA
/**********0
/**********1
/**********2
/**********3
代码写的太丑了
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
char a[105];
char b[105];
int n;
int len;
int judge(char x)
{
if((x<='9'&&x>='0')||(x<='z'&&x>='a')||(x<='Z'&&x>='A'))
return 1;
return 0;
}
void fun(int &pos)
{
if(a[pos]=='9')
{
int j;
for( j=pos-1;j>=0;j--)
if(judge(a[j])) break;
if(j==-1)
{
len++;
for(int i=len-1;i>=pos+1;i--)
a[i]=a[i-1];
a[pos+1]='0';
a[pos]='1';
}
else
{
a[pos]='0';
fun(j);
}
}
else if(a[pos]=='z')
{
int j;
for( j=pos-1;j>=0;j--)
if(judge(a[j])) break;
if(j==-1)
{
len++;
for(int i=len-1;i>=pos+1;i--)
a[i]=a[i-1];
a[pos+1]='a';
a[pos]='a';
}
else
{
a[pos]='a';
fun(j);
}
}
else if(a[pos]=='Z')
{
int j;
for( j=pos-1;j>=0;j--)
if(judge(a[j])) break;
if(j==-1)
{
len++;
for(int i=len-1;i>=pos+1;i--)
a[i]=a[i-1];
a[pos+1]='A';
a[pos]='A';
}
else
{
a[pos]='A';
fun(j);
}
}
else
{
a[pos]++;
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%s%d",a,&n);
len=strlen(a);
int num2=len;
int i;
for( i=len-1;i>=0;i--)
{
if(judge(a[i]))
break;
}
if(i==-1)
{
int num=n;
int pos=len-1;
while(num>=1)
{
fun(pos);
if(len>num2)
{
pos+=len-num2;
num2=len;
}
num--;
for(int j=0;j<=len-1;j++)
printf("%c",a[j]);
cout<<endl;
}
cout<<endl;
}
else
{
int num=n;
int pos=i;
while(num>=1)
{
fun(pos);
if(len>num2)
{
pos+=len-num2;
num2=len;
}
num--;
for(int j=0;j<=len-1;j++)
printf("%c",a[j]);
cout<<endl;
}
cout<<endl;
}
}
return 0;
}