您的位置:首页 > 其它

ZCMU—1170

2017-01-01 13:09 232 查看

1170: String Successor

Time Limit: 1 Sec  Memory Limit: 128 MB

[Submit][Status][Web
Board]

Description

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

【分析】

题目写的真不怎么样。。。一下子没怎么读懂。。还是样例来的靠谱...
题意:每次在最右边一个数字或字母的位置+1,如果产生进位,那么就把这一位变为当前字符范围的最小比如'9‘→'0','Z'→'A'...然后再向前一位字母或数字进位,如果无法进位就在右边插入一个字母进位,比如'9'无法进位就变成"10",'Z'变成"AA"...类似数学的加法。。考读题的一道题吧...另外分享一个命令 
isalnum(x)判断字符x是否为字母或数字字符,如果是则返回一个非零数,否则返回0
同样还有两个命令:
isalpha(x)判断字符x是否为字母,如果是则返回一个非零数,否则返回0
isdigit(x)判断字符x是否为数字,如果是则返回一个非零数,否则返回0
【代码】
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;

string s;

bool judge(int l)
{
for(int i=0;i<=l;i++)
if(isalnum(s[i]))
return 0;
return 1;
}

void add(int len)
{
for(int i=len;i>=0;i--)
{
if(isalnum(s[i]))
{
if(s[i]=='9')
{
s[i]='0';
if(i==0||judge(i-1))
s.insert(i,"1");
else
add(i-1);
}
else
if(s[i]=='z')
{
s[i]='a';
if(i==0||judge(i-1))
s.insert(i,"a");
else
add(i-1);
}
else
if(s[i]=='Z')
{
s[i]='A';
if(i==0||judge(i-1))
s.insert(i,"A");
else
add(i-1);
}
else
s[i]++;
return;
}
}
}

int main()
{
int pp;scanf("%d",&pp);
while(pp--)
{
int n;
cin>>s>>n;
for(int i=0;i<n;i++)
{
if(judge(s.size()-1))
s[s.size()-1]++;
else
add(s.size()-1);
cout<<s<<endl;
}
puts("");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: