您的位置:首页 > 其它

ZOJ- 3490 String Successor 模拟

2017-02-12 21:12 447 查看
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

Author: WU, Zejun
Contest: The 8th Zhejiang Provincial Collegiate Programming Contest

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <math.h>

using namespace std;
int T;
string str;
int n;
char ch;
int flag;

int main()
{
cin>>T;
while(T--)
{
cin>>str>>n;
while(n--)
{
int i;
for(i=str.length()-1; i>=0; i--)
{
if(str[i]>='a'&&str[i]<='z'
||str[i]>='A'&&str[i]<='Z'
||str[i]>='0'&&str[i]<='9')
break;
}
int l=str.length();
if(i<0) i=l-1;
flag=0;
while(!flag)
{
if(str[i]=='z'||str[i]=='Z'||str[i]=='9')
{
if(str[i]=='z')
{
str[i]='a';
ch='a';
}
if(str[i]=='Z')
{
str[i]='A';
ch='A';
}
if(str[i]=='9')
{
str[i]='0';
ch='1';
}
int j;
for(j=i-1; j>=0; j--)
{
if(str[j]>='a'&&str[j]<='z'
||str[j]>='A'&&str[j]<='Z'
||str[j]>='0'&&str[j]<='9')
break;
}
if(j<0)
{
str.insert(str.begin()+i,ch);
flag=1;
l++;
}
else
i=j;
}
else
{
str[i]++;
flag=1;
}
}
cout<<str<<endl;
}
cout<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ACM zoj string