ZOJ 3490 String Successor(模拟)
2016-04-22 13:40
423 查看
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
代码写的太丑了
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
代码写的太丑了
#include<cstdio> #include<cmath> #include<cstring> #include<algorithm> #include<iostream> using namespace std; char a[105]; char b[105]; int n; int len; int judge(char x) { if((x<='9'&&x>='0')||(x<='z'&&x>='a')||(x<='Z'&&x>='A')) return 1; return 0; } void fun(int &pos) { if(a[pos]=='9') { int j; for( j=pos-1;j>=0;j--) if(judge(a[j])) break; if(j==-1) { len++; for(int i=len-1;i>=pos+1;i--) a[i]=a[i-1]; a[pos+1]='0'; a[pos]='1'; } else { a[pos]='0'; fun(j); } } else if(a[pos]=='z') { int j; for( j=pos-1;j>=0;j--) if(judge(a[j])) break; if(j==-1) { len++; for(int i=len-1;i>=pos+1;i--) a[i]=a[i-1]; a[pos+1]='a'; a[pos]='a'; } else { a[pos]='a'; fun(j); } } else if(a[pos]=='Z') { int j; for( j=pos-1;j>=0;j--) if(judge(a[j])) break; if(j==-1) { len++; for(int i=len-1;i>=pos+1;i--) a[i]=a[i-1]; a[pos+1]='A'; a[pos]='A'; } else { a[pos]='A'; fun(j); } } else { a[pos]++; } } int main() { int t; scanf("%d",&t); while(t--) { scanf("%s%d",a,&n); len=strlen(a); int num2=len; int i; for( i=len-1;i>=0;i--) { if(judge(a[i])) break; } if(i==-1) { int num=n; int pos=len-1; while(num>=1) { fun(pos); if(len>num2) { pos+=len-num2; num2=len; } num--; for(int j=0;j<=len-1;j++) printf("%c",a[j]); cout<<endl; } cout<<endl; } else { int num=n; int pos=i; while(num>=1) { fun(pos); if(len>num2) { pos+=len-num2; num2=len; } num--; for(int j=0;j<=len-1;j++) printf("%c",a[j]); cout<<endl; } cout<<endl; } } return 0; }
相关文章推荐
- GridView如何使得item之间的距离和左右侧距离相等
- ZOJ 3490 String Successor(模拟)
- django中的字段类型
- java中的IO一
- notebook kernels
- jquery 的(function(){});表示什么?
- ORA-12516: TNS: 监听程序无法找到匹配协议栈的可用句柄
- Linux Kernel Release Versions
- 线程
- 作业调度
- ThinkPHP集成阿里支付
- MySQL 批量插入 Update时Replace
- java.lang.OutOfMemoryError: PermGen space及其解决方法
- document.body / document.ducumentElement /等获取高度和宽度的区别 ----转载
- Python yield 使用浅析
- 【学习C++】C++ Primer Plus (第六版)第八章编程练习1-7
- .net如何发送格式化的文本内容
- jcenter 的一点总结
- js跨域访问,No ‘Access-Control-Allow-Origin‘ header is present on
- leetcode——337—— House Robber III