您的位置:首页 > 其它

ZOJ 3490 String Successor(字符串处理)

2017-02-10 15:23 309 查看
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

题意:
给一串字符,按规则增加n次。
1.如果有字母数字符号,把最右边的数字或者字母+1, 否者就把最右边的其他符号Ascll码+1.
2.当字母或者数字到了 9 或者 Z 或者 z 再增加就向左进位(是指靠近在当前位置的左边最近的字母数字),自己变成0,A,a.
3.如果左边已经没有数字或者字母了,就在当前位置的紧贴着的左边加一位同类型的字符,字母加A或a,数字加1.

#include <iostream>
#include<cstdio>
#include<cstring>
#include<string>
using namespace std;
int t,k,j;
string ch;
bool flag;
int main()
{
scanf("%d",&t);
for(;t>0;t--)
{
cin>>ch>>k;
int l=ch.length();
for(;k>0;k--)
{
int j; //表示处理的字符的位置
for(j=l-1;j>=0;j--)
{
if (ch[j]>='a' && ch[j]<='z'
|| ch[j]>='A' && ch[j]<='Z'
|| ch[j]>='0' && ch[j]<='9') break;
}
if (j<0) j=l-1;  //如果字串中没有一个字母数字,直接从最后一个字符开始处理
char str;
flag=0; //判断是否要继续处理
while(!flag)
{
if (ch[j]=='Z' || ch[j]=='z' || ch[j]=='9')
{
if(ch[j]=='Z'){ch[j]='A';str='A';}
if(ch[j]=='z'){ch[j]='a';str='a';}
if(ch[j]=='9'){ch[j]='0';str='1';}
int t;
for(t=j-1;t>=0;t--)
if (ch[t]>='a' && ch[t]<='z' || ch[t]>='A' && ch[t]<='Z' || ch[t]>='0' && ch[t]<='9')
break;
if(t<0)  //左边没有字母数字,就要添加一位同类型的字符
{
ch.insert(ch.begin()+j,str);
l++;   //之前忘记长度要+1了
flag=1;
}
else j=t; //继续要向前进位
}else {ch[j]++; flag=1;}  //不是字母数字直接Ascll码+1
}
cout<<ch<<endl;
}
printf("\n"); //每组数据之间要换行之前也忘记了
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: