ZOJ-3490-String Successor【8th浙江省赛】【模拟】

来源:互联网 发布:我知天下之中央 编辑:程序博客网 时间:2024/05/01 21:47

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

题目链接:ZOJ-3490

题目大意:
1. 特殊情况:字符串中无字母和数字。此时的操作是,将最右边的非字母数字的字符ASC2码+1
2. 正常情况 : 字符串中含有字母和数字
① 从最右边的字母(数字)++,如果是9或者Z或者z则产生进位。
②如果进位完之后,前面已经没有数字或者字母可以++的,则在最左边的字母(数字)的左侧增加一个’1’或者’A’或者’a’视最左边的字母(数字)类型而定

题目思路:直接模拟.

我的bug样例是  *****7*****

以下是代码:

#include <iostream>#include <cstdio>#include <cmath>#include <vector>#include <cstring>#include <algorithm>#include <string>#include <set>#include <functional>#include <numeric>#include <sstream>#include <stack>#include <map>#include <queue>#include<iomanip>using namespace std;int main(){    int t;    cin >> t;    while(t--)    {        string s;        int k;        cin >>s >> k;        int pos = -1;        for (int i = 0; i < s.size(); i++)        {            if (isalpha(s[i]) || isdigit(s[i]))            {                pos = i;                break;            }        }        while(k--)        {            if (pos != -1)            {                int add = 0;                int i;                for (i = s.size() - 1; i >= pos; i--)                {                    if(isdigit(s[i]))                    {                        if (s[i] == '9') add = 1,s[i] = '0';                        else s[i]++,add = 0;                        if (add == 0) break;                    }                    if (isalpha(s[i]))                    {                        if (s[i] == 'z') add = 1,s[i] = 'a';                        else if (s[i] == 'Z') add = 1,s[i] = 'A';                        else s[i]++,add = 0;                        if (add == 0) break;                    }                }                if (add == 1)                {                    if (s[i + 1] >= 'A' && s[i+1] <= 'Z') s.insert(pos,"A");                    if (s[i+1] >= '0' && s[i+1] <= '9') s.insert(pos,"1");                    if (s[i+1] >= 'a' && s[i+1] <= 'z') s.insert(pos,"a");                }            }            else            {                int add = 0;                for (int i = s.size()- 1; i >= 0; i--)                {                    s[i]++;                    if (isalpha(s[i]) || isdigit(s[i])) pos = i;                    break;                }            }            cout << s << endl;         }         cout << endl;    }     return 0;}
0 0
原创粉丝点击