1146 ID Codes

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

1146 ID Codes的相关文章

ACM POJ 1146 ID Codes

题目大意:输入一个字符串,输出它的下一个字典序排列. 字典序算法思想: 1.从右向左寻找字符串找出第一个a[i]<a[i+1]的位置i; 2.从右向左找出第一个大于a[j]的元素a[i]; 3.swap(a[i],a[j]) 4.将a[i]......到a[stelen(a)]倒序 5.输出a 代码如下: #include<iostream> #include<cstdio> #include <cstring> #include<algorithm>

POJ 1146 ID Codes(枚举排序)

[题意简述]:求下一个排列 [分析]:同1833一样,如果用STL 一下就解决了,最好自己写函数. // 192K 0Ms #include<iostream> #include<cstring> #include<algorithm> using namespace std; char alphabit[51]; int main() { int i,j,len; while(1) { cin>>alphabit; if(alphabit[0] == '#

POJ 1146:ID Codes

ID Codes Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 6281 Accepted: 3769 Description It is 2084 and the year of Big Brother has finally arrived, albeit a century late. In order to exercise greater control over its citizens and thereby

UVA - 146 - ID Codes (枚举排列)

UVA - 146 ID Codes Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description It is 2084 and the year of Big Brother has finally arrived, albeit a century late. In order to exercise greater control over its ci

UVA 146 ID Codes(下一个排列)

C - ID Codes Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Appoint description:  System Crawler  (2014-05-12) Description  ID Codes  It is 2084 and the year of Big Brother has finally arrived, albeit a century l

Brute Force &amp; STL --- UVA 146 ID Codes

 ID Codes  Problem's Link:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=3&problem=82&mosmsg=Submission+received+with+ID+14418598 Mean: 求出可重排列的下一个排列. analyse: 直接用STL来实现就可.自己手动写了一个,并不复杂.

寒假集训.ID Codes

ID Codes Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Description  ID Codes  It is 2084 and the year of Big Brother has finally arrived, albeit a century late. In order to exercise greater control over its citi

poj-1146 ID codes

Description It is 2084 and the year of Big Brother has finally arrived, albeit a century late. In order to exercise greater control over its citizens and thereby to counter a chronic breakdown in law and order, the Government decides on a radical mea

ID Codes UVA 146(求字典序比当前字符串小的最大字符串)

说说: 题意其实很简单,就是给你一个由小写英文字母组成的字符串,然后让你求字典序比当前字符串小的最大的字符串.解法的话,就是从字符串的末尾开始遍历,若得到的子串已经是该字串所能得到的最小字典序,则继续往前遍历.否则,先在子串中,找到比原字串的首字符小的最大字符,将两者交换位置.然后将除首字符以外的其他字串排列获取最大字典序的子串即可.具体方案,看源代码好了. 源代码: #include <stdio.h> #include <string.h> #define MAX 50+5 c