ZOJ-3490-String Successor 模拟
ZOJ-3490-String Successor
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
题意:
字符串中无字母和数字:此时将最右边的非字母数字的字符ASC2码+1
字符串中含有字母和数字 :从最右边的字母/数字++,如果是9或者Z或者z则产生进位。 如果进位完之后,前面已经没有数字或者字母可以++的,则在最左边的字母/数字的左侧增加一个’1’或者’A’或者’a’视最左边的字母/数字类型而定
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<iostream>
#include<string>
#include<sstream>
#include<algorithm>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<vector>
using namespace std;
#define inf 0x3f3f3f3f
#define LL long long
int main()
{
int n,i,j,k;
string s;
scanf("%d",&n);
while(n--)
{
cin>>s>>k;
int pos=-1;//标记是否出现字母数字
for(i=0;i<s.length();i++)
{
if(isalpha(s[i])||isdigit(s[i]))
{
pos=i;
break;
}
}
int f;//标记进位
while(k--)
{
if(pos!=-1)
{
f=0;
for(i=s.length()-1;i>=pos;i--)
{
if(isdigit(s[i]))
{
if(s[i]=='9')
{
f=1;
s[i]='0';
}
else
{
s[i]++;
f=0;
}
if(f==0)break;
}
if (isalpha(s[i]))
{
if(s[i]=='Z')
{
f=1;
s[i]='A';
}
else if(s[i]=='z')
{
f=1;
s[i]='a';
}
else
{
s[i]++;
f=0;
}
if(f==0)break;
}
}
if(f) //进位
{
if(s[i+1]>='A'&&s[i+1]<='Z')
s.insert(pos,"A");
if(s[i+1]>='a'&&s[i+1]<='z')
s.insert(pos,"a");
if(s[i+1]>='0'&&s[i+1]<='9')
s.insert(pos,"1");
}
}
else
{
//字符串中无字母和数字
//此时将最右边的非字母数字的字符ASC2码+1
f=0;
int l=s.length()-1;
s[l]++;
if(isalpha(s[l])||isdigit(s[l]))
pos=l;
}
cout<<s<<endl;
}
cout<<endl;
}
}
还没有评论,来说两句吧...