zoukankan      html  css  js  c++  java
  • ZOJ 3490 String Successor




    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



    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <string>
    #include <algorithm>

    using namespace std;

    string str;

    bool noalmun(int id)
    {
        bool OK=true;
        for(int i=0;i<=id;i++)
        {
            if(isalnum(str))
            {
                OK=false;  break;
            }
        }
        return OK;
    }

    void cpp(int pos)
    {
    for(int i=pos;i>=0;i--)
    {
        if(isalnum(str))
        {
            if(isdigit(str))
            {
                ///
                if((str+1)>'9')
                {
                    ///
                    str='0';
                    if(i==0||noalmun(i-1)) str.insert(i,"1");
                    else cpp(i-1);
                }
                else str++;
            }
            else if(isalpha(str))
            {
                if(isupper(str))
                {
                    ///
                    if((str+1)>'Z')
                    {
                        ///
                        str='A';
                        if(i==0||noalmun(i-1)) str.insert(i,"A");
                        else  cpp(i-1);
                    }
                    else str++;
                }
                else if(islower(str))
                {
                    ///
                    if((str+1)>'z')
                    {
                        ///
                        str='a';
                        if(i==0||noalmun(i-1)) str.insert(i,"a");
                        else  cpp(i-1);
                    }
                    else str++;
                }
            }
            return ;
        }
    }
    }

    int main()
    {
        int T;
        scanf("%d",&T);
    while(T--)
    {
        int n;
        cin>>str;
        scanf("%d",&n);
        while(n--)
        {
            int pos=-1;
            int len=str.size();
            for(int i=len-1;i>=0;i--)
            {
                if(isalnum(str)&&pos==-1) pos=i;
            }
            if(pos==-1)
            {
                str[len-1]=str[len-1]+1;
            }
            else   cpp(pos);
            cout<<str<<endl;
        }
        cout<<endl;
    }

    }

  • 相关阅读:
    多个类定义attr属性重复的问题:Attribute "xxx" has already been defined
    好用的批量改名工具——文件批量改名工具V2.0 绿色版
    得到ImageView中drawable显示的区域的计算方法
    得到view坐标的各种方法
    实现类似于QQ空间相册的点击图片放大,再点后缩小回原来位置
    Material Designer的低版本兼容实现(五)—— ActivityOptionsCompat
    Android 自带图标库 android.R.drawable
    解决 Attempting to destroy the window while drawing!
    解决Using 1.7 requires compiling with Android 4.4 (KitKat); currently using API 4
    Material Designer的低版本兼容实现(四)—— ToolBar
  • 原文地址:https://www.cnblogs.com/CKboss/p/3350926.html
Copyright © 2011-2022 走看看