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>='' && ch<='') || (ch>='a' && ch<='z') || (ch>='A' && ch<='Z');
}
int main(){
int T;
scanf("%d",&T);
getchar();
while(T--){
char str[];
int n,i,j;
scanf("%s",&str);
scanf("%d",&n);
int len = strlen(str);
int flag=;
int k = len;
for(i=len-;i>=;i--){
if(isap(str[i])){//找到最后面满足的
flag = ;
k=i;
break;
}
}
int s = -;
for(int i=;i<len;i++){//找到最前面的满足的
if(isap(str[i])){
s=i;
break;
}
}
while(n--){
if(flag == ){//如果没有的情况
str[len-]++;
printf("%s\n",str);
if(isap(str[len-])){//如果加到了有的情况
flag = ;
s = len-;
k = len-;
}
}
else{
for(i=k;i>=s;i--){//从最后的数字到最前面
if((str[i]>='' && str[i]<'') || (str[i]>='a' && str[i]<'z') || (str[i]>='A' && str[i]<'Z')){
str[i]++;
printf("%s\n",str);
break;
}
else if(str[i]==''){
str[i] = '';
}
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+;j>s;j--){//位移 一直到s的那位都往后推
str[j] = str[j-];
}
switch(str[s]){
case '':str[s]='';break;
case 'a':str[s]='a';break;
case 'A':str[s]='A';break; }
len++,k++;//最后一位满足的情况
printf("%s\n",str); }
}
}
printf("\n");
} return ;
}

ZOJ——String Successor(字符串模拟题目)的更多相关文章

  1. ZOJ 3490 String Successor 字符串处理

    一道模拟题,来模拟进位 暴力的从右往左扫描,按规则求后继就好了.除了Sample已给出的,还有一些需要注意的地方: 9的后继是10,而不是00: (z)的后继是(aa),而不是a(a): 输入虽然最长 ...

  2. ZOJ 3490 String Successor(模拟)

    Time Limit: 2 Seconds Memory Limit: 65536 KB The successor to a string can be calculated by applying ...

  3. Codeforces Round #425 (Div. 2) B. Petya and Exam(字符串模拟 水)

    题目链接:http://codeforces.com/contest/832/problem/B B. Petya and Exam time limit per test 2 seconds mem ...

  4. [LeetCode] Reverse Words in a String 翻转字符串中的单词

    Given an input string, reverse the string word by word. For example, Given s = "the sky is blue ...

  5. .NET面试题解析(03)-string与字符串操作

      系列文章目录地址: .NET面试题解析(00)-开篇来谈谈面试 & 系列文章索引 字符串可以说是C#开发中最常用的类型了,也是对系统性能影响很关键的类型,熟练掌握字符串的操作非常重要. 常 ...

  6. 用字符串模拟两个大数相加——java实现

    问题: 大数相加不能直接使用基本的int类型,因为int可以表示的整数有限,不能满足大数的要求.可以使用字符串来表示大数,模拟大数相加的过程. 思路: 1.反转两个字符串,便于从低位到高位相加和最高位 ...

  7. HDU-3787(字符串模拟)

    Problem Description 给定两个整数A和B,其表示形式是:从个位开始,每三位数用逗号","隔开.现在请计算A+B的结果,并以正常形式输出.   Input 输入包含 ...

  8. [LeetCode] Find And Replace in String 在字符串中查找和替换

    To some string S, we will perform some replacement operations that replace groups of letters with ne ...

  9. HDU-Digital Roots(思维+大数字符串模拟)

    The digital root of a positive integer is found by summing the digits of the integer. If the resulti ...

随机推荐

  1. 洛谷 P2146 [NOI2015]软件包管理器 树链剖分

    目录 题面 题目链接 题目描述 输入输出格式 输入格式: 输出格式: 输入输出样例 输入样例#1: 输出样例#1: 输入样例#2: 输出样例#2: 说明 说明 思路 AC代码 总结 题面 题目链接 P ...

  2. Leetcode806.Number of Lines To Write String写字符串需要的行数

    我们要把给定的字符串 S 从左到右写到每一行上,每一行的最大宽度为100个单位,如果我们在写某个字母的时候会使这行超过了100 个单位,那么我们应该把这个字母写到下一行.我们给定了一个数组 width ...

  3. Inno Setup生成桌面快捷方式

    在做项目的时候,需要打包成exe安装包.先前使用的是vs来打包,生成了setup.exe 和 *.msi的安装文件,不过也算顺利. 后因为要求采取 Inno Setup来打包程序,其中遇到个创建快捷方 ...

  4. 【JZOJ4869】【NOIP2016提高A组集训第9场11.7】平均数

    题目描述 数据范围 解法 二分答案. 对于一个答案mid,要求出区间平均数小于mid的个数ans. 给所有数减去mid,那么问题转化为求出所有区间和为负数的个数. 对于一个区间[l,r],如果sum[ ...

  5. .net 数据表格显示控件

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/chenjinge7/article/details/30470609 1. GridView 控件 ...

  6. 【NS2】How to remove Cygwin completely from Windows

    How to remove Cygwin completely from Windows 9th September 2012. 31243 views. Software Remember need ...

  7. 使用 Javascript 将二进制字符串转成数字

    使用 Javascript 将二进制字符串转成数字 Javascript 转成 数学太简单了. 原来 parseInt 还有这样的用法. function binaryAgent(str) { str ...

  8. Gym - 101480A_ASCII Addition

    题目链接 题解:普通的a+b才怪问题,需要绘制出来,方法有点麻烦. #include <iostream> #include <string.h> #include <s ...

  9. Java练习 SDUT-2504_多项式求和

    多项式求和 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 多项式描述如下: 1 - 1/2 + 1/3 - 1/4 + ...

  10. git 练习

    删除文件 git rm test.txt git  commit -m 'remove test.txt' 回复到最新版本 git checkout -- test.txt git checkout ...