ZOJ 3490 String Successor

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
有字母和数字的无视其他字符,而没有的时候从最右边开始。其实就是做加法和进位。
#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;
}



你可能感兴趣的:(ZOJ)