ZOJ3490-String Successor

100 篇文章 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


Author:  WU, Zejun
Contest:  The 8th Zhejiang Provincial Collegiate Programming Contest


题意: 
1. 特殊情况:字符串中无字母和数字。此时的操作是,将最右边的非字母数字的字符ASC2码+1 
2. 正常情况 : 字符串中含有字母和数字 
① 从最右边的字母(数字)++,如果是9或者Z或者z则产生进位。 
②如果进位完之后,前面已经没有数字或者字母可以++的,则在最左边的字母(数字)的左侧增加一个’1’或者’A’或者’a’视最左边的字母(数字)类型而定

题目思路:模拟

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <climits>

using namespace std;

#define LL long long
const int INF=0x3f3f3f3f;

bool check1(char ch[])
{
    int len=strlen(ch);
    for(int i=0; i<len; i++)
    {
        if('0'<=ch[i]&&ch[i]<='9') return false;
        if('a'<=ch[i]&&ch[i]<='z') return false;
        if('A'<=ch[i]&&ch[i]<='Z') return false;
    }
    return true;
}

bool check2(char a)
{
    if('0'<=a&&a<='9') return true;
    if('a'<=a&&a<='z') return true;
    if('A'<=a&&a<='Z') return true;
    return false;
}

int jin(char a)
{
    if(a=='9') return 1;
    if(a=='z') return 2;
    if(a=='Z') return 3;
    return 0;
}

void add(char ch[])
{
    if(check1(ch))
    {
        ch[0]=ch[0]+1;
        return ;
    }
    int i=0,len=strlen(ch);
    while(!check2(ch[i])) i++;
    int k=jin(ch[i]);
    if(k>0)
    {
        while(k)
        {
            if(k==1) ch[i++]='0';
            if(k==2) ch[i++]='a';
            if(k==3) ch[i++]='A';
            while(i<len&&!check2(ch[i])) i++;
            if(i>=len) break;
            k=jin(ch[i]);
        }
        if(k==0) ch[i++]++;
    }
    else {ch[i]++;return ;}
    if(k>0)
    {
        int p;
        for(int j=len-1; j>=0; j--)
        {
            if(check2(ch[j]))
            {
                p=j;
                break;
            }
        }
        for(int j=len; j>p; j--)
            ch[j]=ch[j-1];
        if(k==1) ch[p+1]='1';
        if(k==2) ch[p+1]='a';
        if(k==3) ch[p+1]='A';
        ch[len+1]='\0';
    }
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        char ch[1000],s[1000];
        int n,k=0;
        scanf("%s %d",ch,&n);
        int len=strlen(ch);
        for(int i=len-1; i>=0; i--)
            s[k++]=ch[i];
        s[k]='\0';
        for(int i=0; i<n; i++)
        {
            add(s);
            int len=strlen(s);
            for(int i=len-1; i>=0; i--)
                printf("%c",s[i]);
            printf("\n");
        }
        printf("\n");
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值