首页 > 其他 > 详细

1146 ID Codes

时间:2015-10-17 11:58:35      阅读:140      评论:0      收藏:0      [点我收藏+]

题目链接: 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 }

 

1146 ID Codes

原文:http://www.cnblogs.com/roger9567/p/4887215.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!