POJ1146

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
void permutation(string val)
{
	int idx=-1;
	for(int i=val.size()-1;i>0;--i){
		if(val[i-1]<val[i]){
			idx=i-1;
			break;
		}
	}
	if(idx==-1){
		cout<<"No Successor"<<endl;
		return;
	}
    int C=0;
    char X='z';
	for(int k=val.size()-1;k>idx;--k){
		if((val[k]-'0')<=(val[idx]-'0'))continue;
		if((val[k]-'0')<=(X-'0')){
			C=k;
			break;
		}
	}
	swap(val[idx],val[C]);
	int len=val.size()-1-idx;
	for(int r=1;r<=(len/2);++r)
		swap(val[idx+r],val[val.size()-r]);
	for(int w=0;w<val.size();++w)
		cout<<val[w];
	cout<<endl;
}
int main()
{
	vector<string>V;
	string idx;
	while(cin>>idx){
		if(idx[0]=='#')
			break;
		else V.push_back(idx);
	}
	for(int w=0;w<V.size();++w)
		permutation(V[w]);
	return 0;
}

点赞