您的位置:首页 > 其它

zoj3490

2016-04-15 13:33 148 查看
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <queue>
#include <string>
#include <string.h>
#include <map>
#include <vector>
typedef long long LL ;

int isOk(char c){
return    '0' <= c && c <= '9'
|| 'a' <= c && c <= 'z'
|| 'A' <= c && c <= 'Z' ;
}

std::string nextSuccessor(std::string word){
int rightMost ;
for(rightMost = word.size()-1 ; rightMost >= 0 ; rightMost--){
if(isOk(word[rightMost])) break ;
}
if(rightMost >= 0){
if(('0'<= word[rightMost] && word[rightMost] < '9')
|| ('a' <= word[rightMost] && word[rightMost] < 'z')
|| ('A' <= word[rightMost] && word[rightMost] < 'Z') ) word[rightMost]++ ;
else if(word[rightMost] == '9'){
word[rightMost] = '0' ;
int fd = 0 ;
for(int j = rightMost - 1 ; j >= 0 ; j--){
if(isOk(word[j])) fd = 1 ;
}
if(! fd){
std::string head = word.substr(0 , rightMost) ;
std::string tail = word.substr(rightMost) ;
return head + "1" + tail ;
}
else{
std::string tail = word.substr(rightMost) ;
std::string head = word.substr(0 , rightMost) ;
return nextSuccessor(head) + tail ;
}
}
else if(word[rightMost] == 'z'){
word[rightMost] = 'a' ;
int fd = 0 ;
for(int j = rightMost - 1 ; j >= 0 ; j--){
if(isOk(word[j])) fd = 1 ;
}
if(! fd){
std::string head = word.substr(0 , rightMost) ;
std::string tail = word.substr(rightMost) ;
return head + "a" + tail ;
}
else{
std::string tail = word.substr(rightMost) ;
std::string head = word.substr(0 , rightMost) ;
return nextSuccessor(head) + tail ;
}
}
else if(word[rightMost] == 'Z'){
word[rightMost] = 'A' ;
int fd = 0 ;
for(int j = rightMost - 1 ; j >= 0 ; j--){
if(isOk(word[j])) fd = 1 ;
}
if(! fd){
std::string head = word.substr(0 , rightMost) ;
std::string tail = word.substr(rightMost) ;
return head + "A" + tail ;
}
else{
std::string tail = word.substr(rightMost) ;
std::string head = word.substr(0 , rightMost) ;
return nextSuccessor(head) + tail ;
}
}
}
else word[word.size()-1]++ ;
return word ;
}

char word[108] ;

int main(){
int t , k ;
scanf("%d" , &t) ;
while(t--){
scanf("%s%d" ,word , &k) ;
std::string s = std::string(word) ;
for(int i = 1 ; i <= k ; i++){
s = nextSuccessor(s) ;
printf("%s\n" , s.c_str()) ;
}
puts("") ;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: