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 to counter a chronic breakdown in law and order, the Government decides on a radical measure–all citizens are to have a tiny microcomputer surgically implanted in their left wrists. This computer will contains all sorts of personal information as well as a transmitter which will allow people’s movements to be logged and monitored by a central computer. (A desirable side effect of this process is that it will shorten the dole queue for plastic surgeons.)

An essential component of each computer will be a unique identification code, consisting of up to 50 characters drawn from the 26 lower case letters. The set of characters for any given code is chosen somewhat haphazardly. The complicated way in which the code is imprinted into the chip makes it much easier for the manufacturer to produce codes which are rearrangements of other codes than to produce new codes with a different selection of letters. Thus, once a set of letters has been chosen all possible codes derivable from it are used before changing the set.

For example, suppose it is decided that a code will contain exactly 3 occurrences of a', 2 ofb’ and 1 of `c’, then three of the allowable 60 codes under these conditions are:

  abaabc

  abaacb

  ababac

These three codes are listed from top to bottom in alphabetic order. Among all codes generated with this set of characters, these codes appear consecutively in this order.

Write a program to assist in the issuing of these identification codes. Your program will accept a sequence of no more than 50 lower case letters (which may contain repeated characters) and print the successor code if one exists or the message `No Successor’ if the given code is the last in the sequence for that set of characters.

Input

Input will consist of a series of lines each containing a string representing a code. The entire file will be terminated by a line consisting of a single #.

Output

Output will consist of one line for each code read containing the successor code or the words ‘No Successor’.

Sample Input

abaacb

cbbaa

#

Sample Output

ababac

No Successor

这道题是要输出给定字符串的字典顺序的下一个,假设没有下一个。则输出No Successor。

字典序例如以下:

设P是1~n的一个全排列:p=p1p2……pn=p1p2……pj-1pjpj+1……pk-1pkpk+1……pn

1)从排列的右端開始。找出第一个比右边数字小的数字的序号j(j从左端開始计算)。即 j=max { i | pi < pi+ 1}

2)在pj的右边的数字中。找出全部比pj大的数中最小的数字pk,即 k=max{i|pi>pj}(右边的数从右至左是递增的,因此k是全部大于pj的数字中序号最大者)

3)对换pj,pk

4)再将pj+1……pk-1pkpk+1……pn倒转得到排列p’=p1p2…..pj-1pjpn…..pk+1pkpk-1…..pj+1。这就是排列p的下一个排列。

上面这一段话摘自百度百科,之前的理解仅仅是知道例如说 123

字典顺序就是

1 2 3

1 3 2

2 1 3

2 3 1

3 1 2

3 2 1

但依据上面的方法事实上就能够写出求下一个字典顺序的字符串了。12345是起始,54321是终止。

例如说:13254

想找下一个字典顺序,找符合大小关系的。

发现5>4,pass。

2<5,perfect。

就是2了,从2这里入手。

之后仅仅须要在后面的5 4 中找一个比2大的最小的数,对换。

此时序列变为1 3 4 5 2。

再然后,对4后面的序列,进行正常从小到大排列。就是要找的下一个字典排序。

1 3 4 2 5。

所以,能够直接依据这个思想。写出代码

#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std; int front,behind;
string a; bool find()
{
int len =a.length()-1;
while(len>0)
{
if(a[len-1] >= a[len])
{
}
else
{
front = len-1;
return true;
}
len--;
}
return false;
} void change()
{
int len =a.length()-1;
int min=200; while(len>front)
{
if(a[len]>a[front]&&(int)a[len]<min)
{
min=len;
}
len--;
}
char temp=a[min];
a[min]=a[front];
a[front]=temp;
} void sort()
{
int len = a.length();
int i,j;
char temp; for(i=front+1;i<len;i++)
{
for(j=i;j<len;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
} int main()
{
while(cin>>a&&a!="#")
{
if(find())
{
change();
sort();
cout<<a<<endl;
}
else
{
cout<<"No Successor"<<endl;
}
}
return 0;
}

当然这个题,在algorithm.h文件里,有直接能够使用的字典顺序的函数next_permutation(),详细代码例如以下

#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std; int main()
{
string a;
while(cin>>a&&a!="#")
{
if(next_permutation(a.begin(),a.end()))
{
cout<<a<<endl;
}
else
{
cout<<"No Successor"<<endl;
}
}
return 0;
}

这里面的next_permutation()函数就是求其下一个字典顺序。和prev_permutation()函数相相应,使用使用方法有一些像sort函数。能够对int数组 ,char数组,string类型进行字典顺序查找。也能够在第三个參数加上cmp函数。自己自己定义的一个排序函数。详细使用方法能够參考POJ 1256这道题,体现的比較明显。

POJ 1146:ID Codes的更多相关文章

  1. poj 1146 ID Codes (字符串处理 生成排列组合 生成当前串的下一个字典序排列 【*模板】 )

    ID Codes Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6229   Accepted: 3737 Descript ...

  2. POJ 1146 ID Codes 用字典序思想生成下一个排列组合

    ID Codes Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7644   Accepted: 4509 Descript ...

  3. 1146 ID Codes

    题目链接: http://poj.org/problem?id=1146 题意: 给定一个字符串(长度不超过50), 求这个字符串的下一个字典序的字符串, 如果已经是最大字典序, 那么输出 " ...

  4. POJ 1459:Power Network(最大流)

    http://poj.org/problem?id=1459 题意:有np个发电站,nc个消费者,m条边,边有容量限制,发电站有产能上限,消费者有需求上限问最大流量. 思路:S和发电站相连,边权是产能 ...

  5. uva146 ID codes

    Description It is 2084 and the year of Big Brother has finally arrived, albeit a century late. In or ...

  6. POJ 3436:ACM Computer Factory(最大流记录路径)

    http://poj.org/problem?id=3436 题意:题意很难懂.给出P N.接下来N行代表N个机器,每一行有2*P+1个数字 第一个数代表容量,第2~P+1个数代表输入,第P+2到2* ...

  7. POJ 2195:Going Home(最小费用最大流)

    http://poj.org/problem?id=2195 题意:有一个地图里面有N个人和N个家,每走一格的花费是1,问让这N个人分别到这N个家的最小花费是多少. 思路:通过这个题目学了最小费用最大 ...

  8. POJ 3281:Dining(最大流)

    http://poj.org/problem?id=3281 题意:有n头牛,f种食物,d种饮料,每头牛有fnum种喜欢的食物,dnum种喜欢的饮料,每种食物如果给一头牛吃了,那么另一个牛就不能吃这种 ...

  9. POJ 3580:SuperMemo(Splay)

    http://poj.org/problem?id=3580 题意:有6种操作,其中有两种之前没做过,就是Revolve操作和Min操作.Revolve一开始想着一个一个删一个一个插,觉得太暴力了,后 ...

随机推荐

  1. 在VC下显示JPEG、GIF格式图像的一种简便方法

    在VC下显示JPEG.GIF格式图像的一种简便方法 一. 引言  JPEG图像压缩标准随然是一种有损图像压缩标准,但由于人眼视觉的不敏感,经压缩后的画质基本没有发生变化,很快便以较高的压缩率得到了广泛 ...

  2. [读书笔记]设计原本[The Design of Design]

    第1章 设计之命题 1.设计首先诞生于脑海里,再慢慢逐步成形(实现) 2.好的设计具有概念完整性:统一.经济.清晰.优雅.利落.漂亮... 第2章 工程师怎样进行设计思维——理性模型 1.有序模型的有 ...

  3. 再说Java EE

    说到JavaEE(曾经叫J2EE)是什么,你可能回答:JavaEE是一组规范,这么说是没错,可是自己不认为这个答案非常大.非常空么?什么又是规范?规范能组成应用么?能在JVM中跑起来么?要理解这些,先 ...

  4. 高级Bash脚本编程指南(27):文本处理命令(三)

    高级Bash脚本编程指南(27):文本处理命令(三) 成于坚持,败于止步 处理文本和文本文件的命令 tr 字符转换过滤器. 必须使用引用或中括号, 这样做才是合理的. 引用可以阻止shell重新解释出 ...

  5. Abot 爬虫

    Abot 爬虫分析-整体结构 1. 引言 在Github 上搜索下Web Crawler 有上千个开源的项目,但是C#的仅仅只有168 个,相比于Java 或者Python 确实少的可怜.如果按照St ...

  6. 谈VC++对象模型

    一个C++程序员,想要进一步提升技术水平的话,应该多了解一些语言的语意细节.对于使用VC++的程序员来说,还应该了解一些VC++对于C++的诠释.Inside the C++ Object Model ...

  7. 浅析Delphi Container库(有开源的DCLX)

    与Java和C++相比,Delphi对容器的支持实在少得可怜.Java有强大的集合框架,C++更有STL,Delphi有什么呢,不就是TList几个小巧的列表类,而TCollection系列的类更多只 ...

  8. http staus汇总

    参考http://www.cnblogs.com/cxd4321/archive/2008/11/20/1337776.html 常见HTTP状态码 200 OK 301 Moved Permanen ...

  9. SQL Server 数据的添加修改删除和查询

    数据的添加: 首先建立一个数据库,点击新建查询,然后用代码建立一个表,表里写上列名和数据类型,约束可加可不加 然后使用insert语句往表里添加数据 insert [into] 表名 (列名1,列名2 ...

  10. SpringMVC存取Session的两种方法

    方法一:使用servlet-api @Controller public class ManagerController { @Resource private ManagerService mana ...