Skip to content

Instantly share code, notes, and snippets.

@wand125
Last active August 29, 2015 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wand125/87ea2ab95d4ae2e2da3a to your computer and use it in GitHub Desktop.
Save wand125/87ea2ab95d4ae2e2da3a to your computer and use it in GitHub Desktop.
PKU1146.cpp
#include<algorithm>
#include<iostream>
#include<cstdio>
#include<vector>
using namespace std;
int main(){
vector<int>kazu;
vector<int>kouhan;
int end=0;
while(1){
kazu.clear();
kouhan.clear();
int p;
while((p=getchar())!='\n'){
if(p=='#'){
end=1;
break;
}
kazu.push_back(p);
// printf("%c \n",p);
}
if(end==1)break;
// printf("nya\n");
int flag=0;
int n=kazu.size();
kouhan.push_back(kazu[n-1]);
int hozon;
// printf("hey\n");
for(int a=n-2;a>=0;a--){
kouhan.push_back(kazu[a]);
if(kazu[a]<kazu[a+1]) {
flag=1,hozon=kazu[a];
break;
}
}
// printf("%d %c %d\n",n,hozon,flag);
if(flag==0)printf("No Successor\n");
else{
sort(kouhan.begin(),kouhan.end());
int m=kouhan.size();
for(int a=0;a<n-m;a++)printf("%c",kazu[a]);
// cout<<endl;
for(int a=0;a<m;a++) if(kouhan[a]>hozon){
printf("%c",kouhan[a]);
kouhan.erase(kouhan.begin()+a);
break;
}
for(int a=0;a<kouhan.size();a++)printf("%c",kouhan[a]);
printf("\n");
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment