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;
    }
    
  • 相关阅读:
    ajax标准写法
    javascript序列化表单追加参数
    javascript获取url参数的方式
    jmeter实现跨线程组传递参数
    代码扫描工具SonarQube+Scanner的基本安装与应用
    给定一个非空正整数的数组,把数值与出现的次数存在一个新的字典中,并按照出现的次数排序
    定义一个函数,实现整数列表中偶数在左边,奇数在右边(不借助其它列表)
    python-leetcode1在一个整数数组中找出两数之和等于目标值,并把两数下标存在list中返回
    python根据key对字典进行排序
    python实现对浮点型的四舍五入
  • 原文地址:https://www.cnblogs.com/dacc123/p/8228697.html
Copyright © 2011-2022 走看看