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. Seata 0.7.0 正式发布

    Seata 是 阿里巴巴 开源的 分布式事务中间件,以 高效 并且对业务 0 侵入 的方式,解决 微服务 场景下面临的分布式事务问题.Seata 0.7.0 已正式发布,本次共合并59pr,主要包括: ...

  2. hdu2516 博弈

    找规律,发现时斐波那契数列:打表上. #include<stdio.h> #include<string.h> #define maxn 2147483647 __int64 ...

  3. dva框架简单描述使用

    首先传统的create-router-app脚手架生成的脚手架我们写仓库的时候用reducers进行调用还有thunk进行异步操作的时候,需要多层函数进行调用,这样会让我们代码进行维护的时候变得麻烦, ...

  4. 给图片加ALT属性是个什么意思?有什么优点呢

    使用百度统计的seo建议当中一条图片ALT信息"存在没有alt信息的img标签"扣分较多.站点图片中加入ALT对seo有什么优点呢?这是非常多初学seo的朋友们都须要询问的问题.a ...

  5. qt 自定义窗口显示鼠标划过的轨迹

    鼠标事件分为四种: 1.按下 2.抬起 3.移动 4.双击 鼠标事件继承与QWidget void mouseDoubleClickEvent(QMouseEvent *event) void mou ...

  6. selenium 自动化点击页面

    #!/usr/bin/env python# -*- coding:utf-8 -*-from selenium import webdriverfrom selenium.webdriver.com ...

  7. 使用HSV色彩空间遮罩绿色区域

    HSV 颜色空间 导入资源 In []: import matplotlib.pyplot as plt import matplotlib.image as mpimg ​ import numpy ...

  8. epoll与fork

    使用epoll时,如果在调用epoll_create之后,调用了fork创建子进程,那么父子进程虽然有各自epoll实例的副本,但是在内核中,它们引用的是同一个实例.子进程向自己的epoll实例添加. ...

  9. SVN更换新的svn链接

    输入新的SVN地址即可:

  10. TP-admin即基于ThinkPHP5拿来即用高性能后台管理系统

    TP-Admin即基于ThinkPHP5的web后台管理系统(总结一套自己的后台管理系统,方便自己后续的项目开发.) 主要特性:自适应手机端.支持国际化.吸取其他CMF框架优点.多站点部署.日志记录. ...