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 ...
随机推荐
- docker的ubuntu镜像无ifconfig和ping命令
docker的ubuntu镜像无ifconfig和ping命令 或者 ubuntu系统中无ifconfig 和 ping 解决方案: 执行以下鸣冷: apt-get update apt-get in ...
- Nuxt.js打造旅游网站第2篇_首页开发
页面效果: 1.初始化默认布局 nuxtjs提供了一个公共布局组件layouts/default.vue,该布局组件默认作用于所有页面,所以我们可以在这里加上一些公共样式,在下一小结中还会导入公共组件 ...
- 巨蟒python全栈开发-第11阶段 ansible_project7
今日大纲 1.发布详情页面 2.前端页面获取分支信息 3.前端界面获取commit信息与tag信息 4.获取线上最新版本 5.发布之实现nginx下线 6.发布之实现server发布 7.前端页面按钮 ...
- POJ-3159_Candies
Candies Time Limit: 1500MS Memory Limit: 131072K Description During the kindergarten days, flymouse ...
- 从零学React Native之01创建第一个程序
本篇首发于简书 欢迎关注 上一篇文章是时候了解React Native了介绍了React Native.大家应该对React Native有个初步的认识. 接下来我们就可以初始化一个React Nat ...
- Python中json和eval的区别
>>> import json >>> s = '{"one":1,"two":2}' >>> json. ...
- android学习——android项目的的目录结构
然后我们看一下Helloword的程序目录: 我们可以看到 大致有的文件: 1. MainHelloWorld.java文件 2. R.java文件 3. android.jar文件 4. RE ...
- 网上很多laravel中cookie的使用方法。
https://blog.csdn.net/chen529834149/article/details/75244718 概述 Cookie的添加其实很简单,直接使用Cookie::make(),在使 ...
- 2-3-4 tree留坑
#include<bits/stdc++.h> #define LL long long #define pii pair<int,int> #define mp make_p ...
- CCPC final Cockroaches
算法假了,我想的是通过枚举x,删除y的影响,这样答案第一个是没有任何问题的,但是第二个会算重复. 因为我枚举每一个x的时候,得到的y,而算另外一个x的时候,可能已经通过其他的点选到了这个点y这就有点麻 ...