您的位置:首页 > 其它

String Successor(ZOJ - 3490 )

2018-02-05 18:48 246 查看
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

啊又是最讨厌的模拟题,不过还好这题不是很难,具体看代码吧,写得很杂乱……

#include <iostream>
#include <string>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <stack>
#include <set>
#include <queue>
#include <map>
#include <algorithm>
using namespace std;
bool check(char ch) {
if (ch >= 'a' && ch <= 'z') return true;
else if (ch >= '0' && ch <= '9') return true;
else if (ch >= 'A' && ch <= 'Z') return true;
else return false;
}
bool carry(char ch) {
if (ch == 'Z' || ch == 'z' || ch == '9') return true;
else return false;
}
int main() {
int t, n;
string text;
cin >> t;
while (t--) {
cin >> text >> n;
vector <int> pos;
for (int i = text.size() - 1; i >= 0; i--)
if (check(text[i])) pos.push_back(i);
if (pos.empty()) pos.push_back(text.size() - 1);
while (n--) {
if (carry(text[pos[0]])) {
bool ok = false;
for (int i = 0; i < pos.size(); i++)
if (!carry(text[pos[i]])) {
ok = true;
text[pos[i]]++;
break;
}
else {
if (text[pos[i]] == 'Z') text[pos[i]] = 'A';
else if (text[pos[i]] == 'z') text[pos[i]] = 'a';
else if (text[pos[i]] == '9') text[pos[i]] = '0';
}
if (!ok) {
if (text[pos[pos.size() - 1]] == 'a') {
text.insert(text.begin() + pos[pos.size() - 1], 'a');
pos.push_back(pos[pos.size() - 1] - 1);
for (int i = 0; i < pos.size(); i++) pos[i]++;
}
else if (text[pos[pos.size() - 1]] == 'A') {
text.insert(text.begin() + pos[pos.size() - 1], 'A');
pos.push_back(pos[pos.size() - 1] - 1);
for (int i = 0; i < pos.size(); i++) pos[i]++;
}
else if (text[pos[pos.size() - 1]] == '0') {
text.insert(text.begin() + pos[pos.size() - 1], '1');
pos.push_back(pos[pos.size() - 1] - 1);
for (int i = 0; i < pos.size(); i++) pos[i]++;
}
}
}
else text[pos[0]]++;
cout << text << endl;
}
cout << endl;
pos.clear();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: