D - String Successor

D - String Successor
Crawling in process... Crawling failed Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu

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

 这道题目的大致意思是:

1.如果没有字母或是数字那么就忽视它,增加最右边字符串中的字符;

2.else 增加从最右边的字母或是数字开始;

3.如果生成了一个进位(即为9->0; z->a; Z->A)这样叫做进位,那么该字母数字的左边的那个字母数字就增加1;

4.是最重要的一步,如果该字母数字的左边没有其他东西,那么就在最左边插进去一个相对应形式的字母数字;('1' for digit, 'A' for upper case and 'a' for lower case).

(其实也就相当于长度只有1的情况);

 

参考一个大神写下如下代码,

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<ctype.h>
using namespace std;
string a;
int flag=0,len=0;
int ju(char c){
	//判断其中是否还有字符或数字 
	if(islower(c)||isupper(c)||('0'<=c&&c<='9')){
			return 1;
	}
	return 0;
}
void change(string& a,int i){
	char c;
	if(a[i]=='z'||a[i]=='Z'||a[i]=='9'){
		if(a[i]=='z')  {a[i]='a';  c='a';} 
		else if(a[i]=='Z') {a[i]='A';  c='A';}
		else if(a[i]=='9') {a[i]='0'; c='1';}	//注意这里的c要改成c='1'; 
	}
	else{
		a[i]++; 
		return;
	}
	int j;
	for(j=i-1;j>=0;j--)
	if(ju(a[j]))  break;
	string::iterator it=a.begin();
	if(j>=0)  change(a,j);
	//insert: 在i前面的那个位置前面插入一个字符串c; 
	else if(j<0)  a.insert(i+it,c);
}
int main(){
	int T,i,j,n,t;
	cin>>T;
	while(T--){
		cin>>a>>n;
		for(i=1;i<=n;i++){
			//注意每次都要求一次它的长度; 
			len=a.size();
			flag=0;
			for(j=len-1;j>=0;j--){
				if(ju(a[j])){
					flag=1; break;
				}
			}
			if(!flag){
				a[len-1]++;
				cout<<a<<endl;
				continue;
			}
			change(a,j);
			cout<<a<<endl;
		}
		cout<<endl;
	}
}

*注意这里用c++函数的string比较方便;因为它可以随时在前端插入
ju函数是用来判断该字符串中是否还有字符或是字符数字; 如果有的话,那么就在main函数中保存那个位置;

change函数的目的是:

1.判断是否要进位,如果要进位的话,那么把要进位的字符数字保存一个在c中,以便之后插入字符串a中的最前端;

2.如果不用进位的话,那么就进行自增就好;此时要注意return直接返回,那么就不会进行下面的语句了;

3.如果1条件成立的话,那么就for一遍,判断在i之前是否还有字符或是字符数字,如果有的话,那么j肯定是大于等于0的,然后再次进行change函数的调用 ; 如果没有,那么就把c字符插入到i的前一位中去;

4.如果全是其他乱七八糟的字符的话,那么就直接在最后一位加1,这里十分巧妙,当n十分大的时候,会产生字符或是数字,此时flag就会判断为1,然后就会自动的进行到change的函数中去了;

 

思路十分巧妙,多看看就能更好的去理解,加油吧。终有一天你能成为girl的骄傲@all girls!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值