前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ZOJ 3490 String Successor(模拟)

ZOJ 3490 String Successor(模拟)

作者头像
ShenduCC
发布2018-04-26 16:33:04
4830
发布2018-04-26 16:33:04
举报
文章被收录于专栏:算法修养算法修养

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

代码写的太丑了

代码语言:javascript
复制
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
char a[105];
char b[105];
int n;
int len;
int judge(char x)
{
    if((x<='9'&&x>='0')||(x<='z'&&x>='a')||(x<='Z'&&x>='A'))
        return 1;
    return 0;
}
void fun(int &pos)
{
     if(a[pos]=='9')
     {
        int j;
        for( j=pos-1;j>=0;j--)
        if(judge(a[j])) break;
        if(j==-1)
        {
            len++;
            for(int i=len-1;i>=pos+1;i--)
                a[i]=a[i-1];
            a[pos+1]='0';
            a[pos]='1';
        }
        else
        {
           a[pos]='0';
           fun(j);

        }
     }
     else if(a[pos]=='z')
     {
        int j;
        for( j=pos-1;j>=0;j--)
        if(judge(a[j])) break;
        if(j==-1)
        {
            len++;
            for(int i=len-1;i>=pos+1;i--)
                a[i]=a[i-1];
            a[pos+1]='a';
            a[pos]='a';
        }
        else
        {
            a[pos]='a';
            fun(j);


        }
     }
     else if(a[pos]=='Z')
     {
        int j;
        for( j=pos-1;j>=0;j--)
        if(judge(a[j])) break;
        if(j==-1)
        {
            len++;
            for(int i=len-1;i>=pos+1;i--)
                a[i]=a[i-1];
            a[pos+1]='A';
            a[pos]='A';
        }
        else
        {
            a[pos]='A';
            fun(j);


        }
     }
     else
     {
         a[pos]++;
     }

}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%s%d",a,&n);
        len=strlen(a);
        int num2=len;
        int i;
        for( i=len-1;i>=0;i--)
        {
            if(judge(a[i]))
                break;
        }
        if(i==-1)
        {
            int num=n;
            int pos=len-1;
            while(num>=1)
            {
               fun(pos);
               if(len>num2)
               {
                   pos+=len-num2;
                   num2=len;
               }

               num--;
               for(int j=0;j<=len-1;j++)
                 printf("%c",a[j]);
               cout<<endl;
            }
            cout<<endl;

        }
        else
        {
            int num=n;
            int pos=i;
            while(num>=1)
            {
                fun(pos);
                if(len>num2)
               {
                   pos+=len-num2;
                   num2=len;
               }
                num--;
                for(int j=0;j<=len-1;j++)
                    printf("%c",a[j]);
                cout<<endl;
            }
            cout<<endl;
        }
    }
    return 0;
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016-04-22 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档