码迷,mamicode.com
首页 > 其他好文 > 详细

zoj 3490 String Successor 字符串 进制

时间:2015-04-18 19:15:36      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:字符串

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


题意:
给一串字符,按规则增加n次。

1.如果有字母数字符号,把最左边的数字或者字母+1, 否者就把最右边的其他符号asc码+1.
2.当字母或者数字到了 9 或者 Z 或者 z 再增加就向左进位,自己变成0,A,a.
3.  如果左边已经没有数字或者字母了,就在最左边的数字或字母左边,紧贴着加一位,字母加A或a,数字加1.

具体看案例就能明白了。

做法:模拟就好了,从右至左,看到数字字母,就加,没进位就结束,有进位就继续向左。

我用c++的string类做的, 我看了下一起比赛的同学代码,如果用c++普遍耗时600ms, 如果用char写,可以快到0ms。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <malloc.h>
#include <ctype.h> 
#include <string>
#include <stack>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <queue>
#include <map>
using namespace std;  
#define MAXM 1000000
#define MAXN 110
map<string ,int> my;
 

int is(char ch)
{
	if(ch>='0'&&ch<='9')
		return 1;
	if(ch>='a'&&ch<='z')
		return 2;
	if(ch>='A'&&ch<='Z')
		return 3;
	return 0; 
}

int upup(char &ch,int type)
{
	if(type==1)
	{
		if(ch=='9')
		{
			ch='0';
			return 1;//jin
		}
		else
		{
			ch++;
			return 0;
		}
	}
	if(type==2)
	{
		if(ch=='z')
		{
			ch='a';
			return 1;//jin
		}
		else
		{
			ch++;
			return 0;
		}
	}
	if(type==3)
	{
		if(ch=='Z')
		{
			ch='A';
			return 1;//jin
		}
		else
		{
			ch++;
			return 0;
		}
	} 
	return 0;
}
void update(string &str)
{
	int i;
	int flag=0;
	int jin=0;
	int id;
	int type;
	for(i=str.size()-1;i>=0;i--)
	{
		if(is(str[i]))
		{
			jin=upup(str[i],is(str[i]));
			flag=1;//you jia guo
			type=is(str[i]);
			id=i;
			if(jin==0)
				break;
		}
	}
	
	if(flag==0)//从来没有 数字 字母
	{
		str[str.size()-1]++;
	}
	else if(jin==1)
	{
		if(type==1)
			str.insert(id,"1");//insert(id,'1');
		else if(type==2)
			str.insert(id,"a");
		else if(type==3)
			str.insert(id,"A");
	}

	cout<<str<<endl;
}

int main()
{ 
	int t;
	cin>>t; 
	double num[10];
	while(t--)
	{
		string str;
		int n;
		cin>>str;
		cin>>n;

		while(n--)
		{
			update(str); 
		}
		printf("\n");
	}
	return 0; 
}









zoj 3490 String Successor 字符串 进制

标签:字符串

原文地址:http://blog.csdn.net/u013532224/article/details/45115459

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!