题目链接: http://poj.org/problem?id=1146
题意: 给定一个字符串(长度不超过50), 求这个字符串的下一个字典序的字符串, 如果已经是最大字典序, 那么输出 "No successor".
分析: <algorithm>中有一个现成的next_permutation(begin, end), 对输入的字符串进行排列.
原型如下:
1 template<class BidirectionalIterator> 2 bool next_permutation( 3 BidirectionalIterator _First, 4 BidirectionalIterator _Last 5 ); 6 template<class BidirectionalIterator, class BinaryPredicate> 7 bool next_permutation( 8 BidirectionalIterator _First, 9 BidirectionalIterator _Last, 10 BinaryPredicate _Comp 11 );
AC代码:
1 #include <iostream> 2 #include <algorithm> 3 #include <string> 4 using namespace std; 5 string line; 6 7 bool comp(char a, char b){ 8 return a>b; 9 } 10 11 int main(){ 12 while(cin>>line){ 13 if(line.at(0) == ‘#‘) 14 break; 15 string s(line); 16 sort(s.begin(),s.end(),comp); 17 if(line == s){ 18 cout<<"No Successor"<<endl; 19 continue; 20 } 21 next_permutation(line.begin(),line.end()); 22 cout<<line<<endl; 23 } 24 return 0; 25 }
时间: 2024-11-17 07:29:57