zoukankan      html  css  js  c++  java
  • ZOJ——String Successor(字符串模拟题目)

    ZOJ Problem Set - 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
    
    

    Author: WU, Zejun
    Contest: The 8th Zhejiang Provincial Collegiate Programming Contest
    Submit    Status
    //本来这题是用string来写的但是总是能在超时之间徘徊,勉强能AC,今天看到了老师的代码 10ms 差距太大了。
    #include<bits/stdc++.h> using namespace std; int isap(char ch){ return (ch>='0' && ch<='9') || (ch>='a' && ch<='z') || (ch>='A' && ch<='Z'); } int main(){ int T; scanf("%d",&T); getchar(); while(T--){ char str[110]; int n,i,j; scanf("%s",&str); scanf("%d",&n); int len = strlen(str); int flag=0; int k = len; for(i=len-1;i>=0;i--){ if(isap(str[i])){//找到最后面满足的 flag = 1; k=i; break; } } int s = -1; for(int i=0;i<len;i++){//找到最前面的满足的 if(isap(str[i])){ s=i; break; } } while(n--){ if(flag == 0){//如果没有的情况 str[len-1]++; printf("%s ",str); if(isap(str[len-1])){//如果加到了有的情况 flag = 1; s = len-1; k = len-1; } } else{ for(i=k;i>=s;i--){//从最后的数字到最前面 if((str[i]>='0' && str[i]<'9') || (str[i]>='a' && str[i]<'z') || (str[i]>='A' && str[i]<'Z')){ str[i]++; printf("%s ",str); break; } else if(str[i]=='9'){ str[i] = '0'; } else if(str[i]=='z'){ str[i] = 'a'; } else if(str[i]=='Z'){ str[i] = 'A'; } } if(i<s){//如果到结束了插入进去了 也就是9 z Z的情况 for(j=len+1;j>s;j--){//位移 一直到s的那位都往后推 str[j] = str[j-1]; } switch(str[s]){ case '0':str[s]='1';break; case 'a':str[s]='a';break; case 'A':str[s]='A';break; } len++,k++;//最后一位满足的情况 printf("%s ",str); } } } printf(" "); } return 0; }
  • 相关阅读:
    [Python Study Notes]with的使用
    [Python Study Notes]pynput实现对键盘控制与监控
    [Python Study Notes]pynput实现对鼠标控制
    [Python Study Notes]WdSaveFormat 枚举
    voip通话分析(含语音质量)
    纯Python给ulaw wav文件加头
    BAE+Python+Django+Wechatpy+Baidu weather api +微信订阅号 = 实现微信查询天气
    Python爬虫抓取某音乐网站MP3(下载歌曲、存入Sqlite)
    Python调用C++DLL函数出错String类型问题
    聊聊Python ctypes 模块(转载)
  • 原文地址:https://www.cnblogs.com/xiaonuolen/p/10687479.html
Copyright © 2011-2022 走看看