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 ...
随机推荐
- 如何利用 Webshell 诊断 EDAS Serverless 应用
本文主要介绍 Serverless 应用的网络环境以及 Serverless 应用容器内的环境,了解背景知识以及基本的运维知识后可以利用 Webshell 完成基本的运维需求. Webshell 简介 ...
- 洛谷P3455 [POI2007]ZAP-Queries (莫比乌斯反演)
题意:求$\sum_{i=1}^{a}\sum_{j=1}^{b}[gcd(i,j)==d]$(1<=a,b,d<=50000). 很套路的莫比乌斯反演. $\sum_{i=1}^{n}\ ...
- BZOJ5259/洛谷P4747: [Cerc2017]区间
BZOJ5259/洛谷P4747: [Cerc2017]区间 2019.8.5 [HZOI]NOIP模拟测试13 C.优美序列 思维好题,然而当成NOIP模拟题↑真的好吗... 洛谷和BZOJ都有,就 ...
- 2019-3-13-win10-uwp-使用-ScaleTransform-放大某个元素
title author date CreateTime categories win10 uwp 使用 ScaleTransform 放大某个元素 lindexi 2019-3-13 19:5:56 ...
- UVa 926【简单dp,递推】
UVa 926 题意:给定N*N的街道图和起始点,有些街道不能走,问从起点到终点有多少种走法. 很基础的dp.递推,但是有两个地方需要注意,在标记当前点某个方向不能走时,也要同时标记对应方向上的对应点 ...
- C++第一次作业(循环语句的使用)
一.知识点 循环结构 二.教学目的 掌握while和do...while循环语句在C++中的写法 三.教学内容 1.while语句 (1)执行顺序:先判断表达式(循环控制条件)的值,若表达式的值为tr ...
- 三级分销会员一次查询出来的SQL语句
SELECT p.id AS partyId, p.parent_id AS parentId, pul.username AS userName, p.city, p.birth_date AS b ...
- IO流实现文件及文件夹的复制
TestCopyDocuments.java package com.sxt.parc; /* * 复制文件夹 包含文本 视频 音频 用字节流 */ import java.io.BufferedIn ...
- Redis源码解析:06整数集合
整数集合(intset)是集合键的底层实现之一,当一个集合只包含整数值元素,并且这个集合的元素数量不多时,Redis就会使用整数集合作为集合键的底层实现. intset可以保存类型为int16_t,i ...
- 从开源小白到 Apache Member,我的成长之路
我们走过的每一步路,都会留下印记,越坚实,越清晰. 近日,Apache 软件基金会(ASF)官方 Blog 宣布全球新增 40 位 Apache Member,张乎兴有幸成为其中一位. 目前,全球共有 ...