ZOJ- 3490 String Successor 模拟
2017-02-12 21:12
447 查看
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).
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').
Author: WU, Zejun
Contest: The 8th Zhejiang Provincial Collegiate Programming Contest
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <math.h>
using namespace std;
int T;
string str;
int n;
char ch;
int flag;
int main()
{
cin>>T;
while(T--)
{
cin>>str>>n;
while(n--)
{
int i;
for(i=str.length()-1; i>=0; i--)
{
if(str[i]>='a'&&str[i]<='z'
||str[i]>='A'&&str[i]<='Z'
||str[i]>='0'&&str[i]<='9')
break;
}
int l=str.length();
if(i<0) i=l-1;
flag=0;
while(!flag)
{
if(str[i]=='z'||str[i]=='Z'||str[i]=='9')
{
if(str[i]=='z')
{
str[i]='a';
ch='a';
}
if(str[i]=='Z')
{
str[i]='A';
ch='A';
}
if(str[i]=='9')
{
str[i]='0';
ch='1';
}
int j;
for(j=i-1; j>=0; j--)
{
if(str[j]>='a'&&str[j]<='z'
||str[j]>='A'&&str[j]<='Z'
||str[j]>='0'&&str[j]<='9')
break;
}
if(j<0)
{
str.insert(str.begin()+i,ch);
flag=1;
l++;
}
else
i=j;
}
else
{
str[i]++;
flag=1;
}
}
cout<<str<<endl;
}
cout<<endl;
}
return 0;
}
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
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <math.h>
using namespace std;
int T;
string str;
int n;
char ch;
int flag;
int main()
{
cin>>T;
while(T--)
{
cin>>str>>n;
while(n--)
{
int i;
for(i=str.length()-1; i>=0; i--)
{
if(str[i]>='a'&&str[i]<='z'
||str[i]>='A'&&str[i]<='Z'
||str[i]>='0'&&str[i]<='9')
break;
}
int l=str.length();
if(i<0) i=l-1;
flag=0;
while(!flag)
{
if(str[i]=='z'||str[i]=='Z'||str[i]=='9')
{
if(str[i]=='z')
{
str[i]='a';
ch='a';
}
if(str[i]=='Z')
{
str[i]='A';
ch='A';
}
if(str[i]=='9')
{
str[i]='0';
ch='1';
}
int j;
for(j=i-1; j>=0; j--)
{
if(str[j]>='a'&&str[j]<='z'
||str[j]>='A'&&str[j]<='Z'
||str[j]>='0'&&str[j]<='9')
break;
}
if(j<0)
{
str.insert(str.begin()+i,ch);
flag=1;
l++;
}
else
i=j;
}
else
{
str[i]++;
flag=1;
}
}
cout<<str<<endl;
}
cout<<endl;
}
return 0;
}
相关文章推荐
- ZOJ 3490 String Successor(模拟啊 )
- ZOJ Problem Set - 3490 String Successor(字符串模拟)
- ZOJ 3490 String Successor(模拟)
- ZOJ 3490 String Successor(模拟)
- ZOJ-3490-String Successor【8th浙江省赛】【模拟】
- ZOJ 3490 String Successor(模拟)
- ZOJ - 3490 String Successor 递归模拟
- zoj 3490 String Successor 模拟
- ZOJ-3326-An Awful Problem【7th浙江省赛】【模拟】
- ZOJ 3674 Search in the Wiki 【C++STL大法尽情地模拟】
- UVALive 3490 (LA 3940) || ZOJ 2619 Generator AC自动机(或KMP) + 整数高斯消元 + 数学期望
- ZOJ 3780 Paint the Grid Again-贪心模拟/拓扑排序
- ZOJ 3862 Intersection【贪心】【几何】【模拟】
- ZOJ 3804 YY's Minions (简单模拟)
- ZOJ 3985 String Of CCPC 字符串,模拟
- zoj 3627#模拟#枚举
- ZOJ 3778 Talented Chef(计算器模拟)
- [ACM_模拟] ZOJ 3713 [In 7-bit 特殊输出规则 7bits 16进制]
- ZOJ 3814:模拟和状态压缩BFS
- ZOJ 3872 Beauty of Array(模拟)