ZOJ 3490 String Successor(模拟啊 )

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4313

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 <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <iostream>
using namespace std;
string s;
int judge(char c)
{
    if(c >= '0' && c <= '9')
    {
        return 1;
    }
    else if(c >= 'a' && c <= 'z')
    {
        return 2;
    }
    else if(c >= 'A' && c <= 'Z')
    {
        return 3;
    }
    return 0;
}

int check(char &c, int type)
{
    if(type == 1)
    {
        if(c == '9')
        {
            c = '0';
            return 1;
        }
        else
        {
            c++;
            return 0;
        }
    }
    if(type == 2)
    {
        if(c == 'z')
        {
            c = 'a';
            return 1;
        }
        else
        {
            c++;
            return 0;
        }
    }
    if(type == 3)
    {
        if(c == 'Z')
        {
            c = 'A';
            return 1;
        }
        else
        {
            c++;
            return 0;
        }
    }
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n;
        cin >> s >> n;
        while(n--)
        {
            int len = s.size();
            int flag = 0;
            int pos;
            for(int i = 0; i < len; i++)
            {
                if(judge(s[i]))//judge 检测是否为字母或数字
                {
                    flag = 1;
                    break;
                }
            }
            if(!flag)//全是符号
            {
                s[len-1]++;
                cout << s <<endl;
                continue;
            }
            int k = 0;
            int type, tt;
            for(int i = len-1; i >=0; i--)
            {
                if(judge(s[i]))//judge 检测是否为字母或数字
                {
                    tt = check(s[i],judge(s[i]));
                    int k = 1;
                    type = judge(s[i]);
                    pos = i;
                    if(tt == 0)//没有进位
                    {
                        break;
                    }
                }
            }
            if(tt == 1)
            {
                if(type == 1)
                {
                    s.insert(pos,"1");
                }
                else if(type == 2)
                {
                    s.insert(pos,"a");
                }
                else if(type == 3)
                {
                    s.insert(pos,"A");
                }
            }
            cout<<s<<endl;
        }
        printf("\n");
    }
    return 0;
}
时间: 2024-08-23 22:59:25

ZOJ 3490 String Successor(模拟啊 )的相关文章

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 charac

ZOJ 3490 String Successor

简单的模拟题,类似于高精度加法运算.理解题意即可. #include<stdio.h> #include<string.h> #include<math.h> #include<algorithm> using namespace std; const int maxn = 111; char s[maxn]; char ji[maxn]; int gg[maxn]; void RevStr(char *str) { int len; char *ptr;

String结构模拟

我们开始模拟一下大家最熟悉的String数据结构的模拟,这个相信做java的没有不熟悉的吧.那我们开始 1.操作接口 public interface IString { public void clear();          //将一个已经存在的串置成空串 public boolean isEmpty();    //判断当前串是否为空,为空则返回true,否则返回false public int length();         //返回字符串的长度 public char charA

HDU 1986 &amp; ZOJ 2989 Encoding(模拟)

题目链接: HDU: http://acm.hdu.edu.cn/showproblem.php?pid=1986 ZOJ: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1988 HDU 1987 & ZOJ 2990 和这题刚好相反,也是比较容易模拟: Chip and Dale have devised an encryption method to hide their (written) text messages

[ACM] ZOJ 3844 Easy Task (模拟+哈希)

Easy Task Time Limit: 2 Seconds      Memory Limit: 65536 KB You are given n integers. Your task is very easy. You should find the maximum integer a and the minimum integer b among these n integers. And then you should replace both a and bwith a-b. Yo

ZOJ How Many Nines 模拟 | 打表

How Many Nines Time Limit: 1 Second      Memory Limit: 65536 KB If we represent a date in the format YYYY-MM-DD (for example, 2017-04-09), do you know how many 9s will appear in all the dates between Y1-M1-D1 and Y2-M2-D2 (both inclusive)? Note that

ZOJ 1760 &amp;&amp;POJ1552 Doubles (模拟)

链接:click here 题意:叫你求一个数是另一个数的二倍的这样的组合有多少个. 思路:纯模拟,一重循环:读入当前数据组a,并累积数据元素个数n,循环的结束标志是读入数据0,两重循环结构枚举组内所有数据对a[i] a[j] 判断是否成两倍关系 代码: #include <stdio.h> #include <string.h> #include <math.h> #include <iostream> #include <algorithm>

zoj 3826 Hierarchical Notation(模拟)

题目链接:zoj 3826 Hierarchical Notation 题目大意:给定一些结构体.结构体有value值和key值,Q次询问,输出每一个key值相应的value值. 解题思路:思路非常easy.写个类词法的递归函数,每次将key值映射成一个hash值,用map映射每一个key的value起始终止位置,预处理完了查询就非常easy了. 这题是最后10分钟出的.由于没有考虑value为{}的情况,导致RE了.可是zoj上显示的是SE,表示不理解什么意思,事实上就是RE.只是另一个地方会

ZOJ 3180 Number Game(模拟,倒推)

题目 思路: 先倒推!到最后第二步,然后: 初始状态不一定满足这个状态.所以我们要先从初始状态构造出它出发的三种状态.那这三种状态跟倒推得到的状态比较即可. #include<stdio.h> #include<string.h> #include <algorithm> using namespace std; int t,a[5],b[5]; int main() { scanf("%d",&t); while(t--) { scanf(