String Successor(字符串处理)

http://acm.sdut.edu.cn:8080/vjudge/contest/view.action?cid=91#problem/C

比赛时候一直没看懂题意,sad......

题意:给定一个字符串,把它变成n个“String Successor”,若一个字符串中没有字母或数字,直接在最右边的字符加1,否则找到从右边数第一个字母或数字加1,若是'9' 'z'或'Z',加1后边分别变为'0','a'和'A',并向前面的第一个数字或字母进位,若前面没有字母或数字,就直接在当前字母或数字前面加上'1','a'或'A'。


#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <string>
#include <iostream>
#include <ctype.h>
using namespace std;

void dfs(string &str, int i)
{
	if(str[i] == '9'+1 || str[i] == 'z'+1 || str[i] == 'Z'+1)//先判断是否应该进位
	{
		char ch;
		if(str[i] == '9'+1)
		{
			ch = '1';
			str[i] = '0';
		}
		else if(str[i] == 'z'+1)
		{
			ch = 'a';
			str[i] = 'a';
		}
		else
		{
			ch = 'A';
			str[i] = 'A';
		}
		int j;
		for(j = i-1; j >= 0; j--)
			if(isalpha(str[j]) || isdigit(str[j]) )
				break;

		if(j != -1) //若i之前有字母或数字,递归进位
		{
			str[j]++;
			dfs(str,j);
		}
		else str.insert(str.begin()+i,ch);//若i之前没有字母或数字,就在i-1处增加'1''A'或‘a’。
	}

}

int main()
{
	int test;
	string str;
	int n;
	cin >> test;
	while(test--)
	{
		cin >> str >> n;

		while(n--)
		{
			int i;
			for( i = str.size()-1; i >= 0; i--)
				if( isalpha(str[i]) || isdigit(str[i]) )
					break;

			if(i == -1)	//没有字母或数字,直接在最右边加1
				str[ str.size()-1 ] ++;
			else
			{
				str[i]++;//加1后向前进位
				dfs(str,i);
			}
			printf("%s\n",str.c_str());
		}
		printf("\n");
	}
	return 0;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值