zoukankan      html  css  js  c++  java
  • ZOJ 3490 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

    代码写的太丑了

    #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;
    }
    
  • 相关阅读:
    Photoshop
    你会为了钱出售自己的个人资料吗?
    [ElasticSearch] 空间搜索 (一)
    hdu1584 A strange lift (电梯最短路径问题)
    Android API Guides---OpenGL ES
    Qt 推断一个IP地址是否有效
    bzoj1670【Usaco2006 Oct】Building the Moat 护城河的挖掘
    集成学习1-Boosting
    微信开发模式之自己定义菜单实现
    人件札记:开放式的办公室环境
  • 原文地址:https://www.cnblogs.com/dacc123/p/8228697.html
Copyright © 2011-2022 走看看