题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4313
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 <cstdio> #include <cstring> #include <string> #include <algorithm> #include <iostream> using namespace std; string s; int judge(char c) { if(c >= '0' && c <= '9') { return 1; } else if(c >= 'a' && c <= 'z') { return 2; } else if(c >= 'A' && c <= 'Z') { return 3; } return 0; } int check(char &c, int type) { if(type == 1) { if(c == '9') { c = '0'; return 1; } else { c++; return 0; } } if(type == 2) { if(c == 'z') { c = 'a'; return 1; } else { c++; return 0; } } if(type == 3) { if(c == 'Z') { c = 'A'; return 1; } else { c++; return 0; } } } int main() { int t; scanf("%d",&t); while(t--) { int n; cin >> s >> n; while(n--) { int len = s.size(); int flag = 0; int pos; for(int i = 0; i < len; i++) { if(judge(s[i]))//judge 检测是否为字母或数字 { flag = 1; break; } } if(!flag)//全是符号 { s[len-1]++; cout << s <<endl; continue; } int k = 0; int type, tt; for(int i = len-1; i >=0; i--) { if(judge(s[i]))//judge 检测是否为字母或数字 { tt = check(s[i],judge(s[i])); int k = 1; type = judge(s[i]); pos = i; if(tt == 0)//没有进位 { break; } } } if(tt == 1) { if(type == 1) { s.insert(pos,"1"); } else if(type == 2) { s.insert(pos,"a"); } else if(type == 3) { s.insert(pos,"A"); } } cout<<s<<endl; } printf("\n"); } return 0; }
时间: 2024-08-23 22:59:25