题目链接: http://poj.org/problem?id=1146
题意: 给定一个字符串(长度不超过50), 求这个字符串的下一个字典序的字符串, 如果已经是最大字典序, 那么输出 "No successor".
分析: <algorithm>中有一个现成的next_permutation(begin, end), 对输入的字符串进行排列.
原型如下:
template<class BidirectionalIterator> bool next_permutation( BidirectionalIterator _First, BidirectionalIterator _Last ); template<class BidirectionalIterator, class BinaryPredicate> bool next_permutation( BidirectionalIterator _First, BidirectionalIterator _Last, BinaryPredicate _Comp );
AC代码:
#include <iostream> #include <algorithm> #include <string> using namespace std; string line; bool comp(char a, char b){ return a>b; } int main(){ while(cin>>line){ if(line.at(0) == '#') break; string s(line); sort(s.begin(),s.end(),comp); if(line == s){ cout<<"No Successor"<<endl; continue; } next_permutation(line.begin(),line.end()); cout<<line<<endl; } return 0; }