ZOJ - 3490 String Successor 递归模拟

String Successor

Time Limit: 2 Seconds       Memory Limit: 65536 KB

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


题意:给你一个没有空格的字符串和变换的次数,然后在变换中如果该字符串里面存在字母或数字时将最右边的字符ascll码加1,当该字符等于‘9’、‘z'、’Z'时,下次加1的时候就需要进位,若该字符以前的字符中不存在数字或字母是就分别在前加上‘1’、‘a'、’Z';如果该字符串里面不存在字母或数字,直接把最右边的字符加1。


思路:使用string按照题意来递归模拟就可以。。。


代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <cstring>
#include <string>
using namespace std;
string s;
int Findalphanumeric(int res)
{
	for(int i = res - 1; i>= 0; i--)
	{
		if(isalnum(s[i])) return i;
	}
	return -1;
}
void FindZore(int res)
{
	if(s[res] == '9')//当s[res] == '9'时,在前面插入的是'1'而不是'0'
	{
		s[res] = '0';
		if(Findalphanumeric(res) == -1){//如果满了,前面没有字母或数字则直接在前面插入
			s.insert(res,1,'1');//第一次用string里面的insert,顺便记录一下
			//s.insert(在字符串s下标为res的前面插入,插入字符的数量,插入什么字符);
		}
		else{//如果满了,而且前面有字母或数字向前递归进位
			FindZore(Findalphanumeric(res));
		}
	}
	else if(s[res] == 'z')
	{
		s[res] = 'a';
		if(Findalphanumeric(res) == -1){
			s.insert(res,1,'a');
		}
		else{
			FindZore(Findalphanumeric(res));
		}
	}
	else if( s[res] == 'Z')
	{
		s[res] = 'A';
		if(Findalphanumeric(res) == -1){
			s.insert(res,1,'A');
		}
		else{
			FindZore(Findalphanumeric(res));
		}
	}
	else{
		s[res]++;
	}
}
int main()
{
#ifdef CDZSC_June
	freopen("t.txt","r",stdin);
#endif
	int t,n;
	ios::sync_with_stdio(false);
	cin >> t;
	while(t--)
	{
		cin >> s >> n;
		while(n--)
		{
			if(!isalnum(s[s.length() -1])){//如果最右边的字符不是数字或字母
				if(Findalphanumeric(s.length()-1) != -1){//如果最右边的字符前面存在数字或字母就进入递归函数
					FindZore(Findalphanumeric(s.length()-1));
				}
				else{//如果最右边的字符前面不存在数字或字母,直接把该字符的值++
					s[s.length()-1]++;
				}
			}
			else{//如果最右边的字符是数字或字母,直接把该字符的值++
				FindZore(s.length()-1);
			}
			cout << s<<endl;
		}
		cout << endl;//感觉题目有点错误,明明说在测试实例之间存在空行,数据却变成了每个测试实例后面都有一个空行
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值