ZOJ——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
//本来这题是用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(字符串模拟题目)的更多相关文章
- ZOJ 3490 String Successor 字符串处理
一道模拟题,来模拟进位 暴力的从右往左扫描,按规则求后继就好了.除了Sample已给出的,还有一些需要注意的地方: 9的后继是10,而不是00: (z)的后继是(aa),而不是a(a): 输入虽然最长 ...
- ZOJ 3490 String Successor(模拟)
Time Limit: 2 Seconds Memory Limit: 65536 KB The successor to a string can be calculated by applying ...
- 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 ...
- [LeetCode] Reverse Words in a String 翻转字符串中的单词
Given an input string, reverse the string word by word. For example, Given s = "the sky is blue ...
- .NET面试题解析(03)-string与字符串操作
系列文章目录地址: .NET面试题解析(00)-开篇来谈谈面试 & 系列文章索引 字符串可以说是C#开发中最常用的类型了,也是对系统性能影响很关键的类型,熟练掌握字符串的操作非常重要. 常 ...
- 用字符串模拟两个大数相加——java实现
问题: 大数相加不能直接使用基本的int类型,因为int可以表示的整数有限,不能满足大数的要求.可以使用字符串来表示大数,模拟大数相加的过程. 思路: 1.反转两个字符串,便于从低位到高位相加和最高位 ...
- HDU-3787(字符串模拟)
Problem Description 给定两个整数A和B,其表示形式是:从个位开始,每三位数用逗号","隔开.现在请计算A+B的结果,并以正常形式输出. Input 输入包含 ...
- [LeetCode] Find And Replace in String 在字符串中查找和替换
To some string S, we will perform some replacement operations that replace groups of letters with ne ...
- HDU-Digital Roots(思维+大数字符串模拟)
The digital root of a positive integer is found by summing the digits of the integer. If the resulti ...
随机推荐
- 在MaxCompute中配置Policy策略遇到结果不一致的问题
背景信息: 本文以如下场景为基准进行编写,如下: 用户通过DataWorks-简单模式使用MaxCompute: 用户具有DataWorks默认角色,如DataWorks开发者角色: 用户通过cons ...
- Android Binder设计与实现 – 设计篇
摘要 Binder是Android系统进程间通信(IPC)方式之一.Linux已经拥有管道,system V IPC,socket等IPC手段,却还要倚赖Binder来实现进程间通信,说明Binder ...
- 仔细看看Javascript中的逻辑与(&&)和逻辑或(||)
学过Java和C的人,都知道逻辑与(&&)和逻辑或(||),他们都是短路运算符,也就是说,对于&&来说,只要左边的操作数是false,它就不会再去判断右边的操作数是tr ...
- 【JZOJ1611】Dining
题目描述 农夫JOHN为牛们做了很好的食品,但是牛吃饭很挑食.每一头牛只喜欢吃一些食品和饮料而别的一概不吃.虽然他不一定能把所有牛喂饱,他还是想让尽可能多的牛吃到他们喜欢的食品和饮料. 农夫JOHN做 ...
- oracle中常用的时间格式转换
1:取得当前日期是本月的第几周 select to_char(sysdate,'YYYYMMDD W HH24:MI:SS') from dual; TO_CHAR(SYSDATE,'YY') se ...
- CS第三方控件 标签: 总结 2016-04-09 11:51 1398人阅读 评论(27) 收藏
大家都知道,我现在在做CS的项目,现在是需求频变啊,心里好苦,做了这么久,还是涨了一点点见识的,下面就介绍一下自己最近用到的几款CS的第三方控件. DockPanel 想必大家都用过VS,那么想一下V ...
- ssh 出错 Permission denied (publickey,password).
将客户端的~/.ssh/know_hosts 文件删掉试试 ssh debug信息 ssh -vvv xxx@192.168.1.111
- python for 循环结构
- Python基础:03序列:字符串、列表和元组
一:序列 1:连接操作符(+) 这个操作符允许把一个序列和另一个相同类型的序列做连接,生成新的序列.语法如下:sequence1 + sequence2 该表达式的结果是一个包含sequence1和s ...
- Android 整合实现简单易用、功能强大的RecyclerView
之前总是会有人在一些开发群里问,有木有比较好使且功能强大些的RecyclerVew,比如支持下来刷新,加载更多等,还有人在问,如何为RecyclerView添加分割线,尤其是如何为网格布局添加分割线? ...