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

Each test case contains a nonempty string s and an integer 1 ≤ n ≤ 100. The string s


Output



For each test case, output the next n successors to the given string s


Sample Input



4 :-( 1 cirno=8 2 X 3 /**********/ 4


Sample Output



:-) cirno=9 cirnp=0 Y Z AA /**********0 /**********1 /**********2 /**********3



有字母和数字的无视其他字符,而没有的时候从最右边开始。其实就是做加法和进位。



#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<string>
#include<cstring>
using namespace std;
const int maxn = 1005;
int t, n, f, j;
char s[200], c[200];

int check(char x)
{
if (x >= '0'&&x <= '9') return '1';
if (x >= 'A'&&x <= 'Z') return 'A';
if (x >= 'a'&&x <= 'z') return 'a';
return 0;
}

char add(char x)
{
if (x == '9') { f = 1; return '0'; }
else if (x == 'Z') { f = 1; return 'A'; }
else if (x == 'z') { f = 1; return 'a'; }
else { f = 0; return x + 1; }
}

int main(){
scanf("%d", &t);
while (t--)
{
cin >> s >> n;
for (int i = 0; i < strlen(s); i++) c[i] = s[strlen(s) - i - 1];
c[strlen(s)] = 0;
while (n--)
{
int i = 0;
while (!check(c[i]) && i < strlen(c)) i++;
if (i == strlen(c)) i = 0;
f = 0; c[i] = add(c[i]);
while (f)
{
j = i + 1;
while (!check(c[j]) && j < strlen(c)) j++;
if (j == strlen(c))
{
for (j = strlen(c); j > i; j--) c[j + 1] = c[j];
c[i + 1] = check(c[i]); f = 0;
}
else { c[j] = add(c[j]); i = j; }
}
for (i = strlen(c) - 1; i >= 0; i--) printf("%c", c[i]);
cout << endl;
}
printf("\n");
}
return 0;
}





温故而知新

#include<map>
#include<cmath>
#include<queue>
#include<string>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define ms(x,y) memset(x,y,sizeof(x))
#define rep(i,j,k) for(int i=j;i<=k;i++)
#define per(i,j,k) for(int i=j;i>=k;i--)
#define loop(i,j,k) for (int i=j;i!=-1;i=k[i])
#define inone(x) scanf("%d",&x)
#define intwo(x,y) scanf("%d%d",&x,&y)
#define inthr(x,y,z) scanf("%lf%lf%lf",&x,&y,&z)
typedef long long LL;
const int low(int x) { return x&-x; }
const int INF = 0x7FFFFFFF;
const int mod = 1e9 + 7;
const int N = 1e5 + 10;
int T, n, m;
char s[N], g[N];

int main()
{
for (inone(T); T--;)
{
scanf("%s", s);
n = strlen(s);
rep(i, 0, n - 1) g[n - i - 1] = s[i];
for (inone(m); m; m--)
{
int j = 1, k = 0;
rep(i, 0, n - 1)
{
if (g[i] >= '0'&&g[i] < '9' || g[i] >= 'A'&&g[i] < 'Z' || g[i] >= 'a'&&g[i] < 'z')
{
g[i] = g[i] + j; j = 0; k = 1; break;
}
else if (g[i] == '9') g[i] = '0', k = '1';
else if (g[i] == 'Z') g[i] = 'A', k = 'A';
else if (g[i] == 'z') g[i] = 'a', k = 'a';
}
if (j && k)
{
g[n++] = k;
per(i, n - 1, 1)
{
if (g[i - 1] >= '0'&&g[i - 1] <= '9' || g[i - 1] >= 'a'&&g[i - 1] <= 'z' || g[i - 1] >= 'A'&&g[i - 1] <= 'Z') break;
swap(g[i], g[i - 1]);
}
}
else if (!k) g[0]++;
per(i, n - 1, 0) putchar(g[i]); putchar(10);
}
putchar(10);
}
return 0;
}