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

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 of `b' 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

题意:

首先会给定一个字符串,因为开发商要通过交换字母位置生成新的字符串来节约刻印这些字符串的成本,假设现在已经按字典序将所有可能的排列全部找出来了,问你当前给定的这个字符串的下一个排列(按照字典序)是什么?例如假定一个识别码有三个a,两个b,一个c,在满足条件的60个编码中按字典序选出三个是:abaabc,abaacb,ababac,现在题目给定的字符串是abaacb,那么按字典序来讲,下个字符串是ababac,输出"ababac"即可。

分析:

本题利用的是字典序思想来对字符串进行改造,也就是在不改变字母组成的情况下生成该字符串的下一个字典序排列(没有的话输出“No Successor”)。

解法:

1.从后往前先找出一个正序。(找不到的话就是No Successor)。

2.(记这个正序的下标分别是id-1,id)从找出的的那个正序的下标(id)开始往后遍历,找到最后一个大于s[id-1]且小于s[id]的那个元素(记下标为 j)。

3.swap(s[id-1],s[j]);交换这两个数来破坏原来的正序,又因为新找到的元素字典序大于s[id-1]所以交换以后的字典序一定更大。

4.sort(s+id,s+len);对从id到末尾的所有元素从小到大排序来保证我们新得出的这个字符串按字典序排序的结果是紧邻在原串之后的。

AC code:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
char s[];
int solve()
{
int len=strlen(s);
int id=len-;
while(id>=)
{
if(s[id]>s[id-]) break;
else
{
id--;
}
}
if(id==) return ;
int mpre=id-,mnow=id;
for(int j=mnow+;j<len;j++)
{
if(s[j]<=s[mpre]) continue;
if(s[j]<s[mnow]) mnow=j;
}
swap(s[mnow],s[mpre]);
sort(s+id,s+len);
return ;
}
int main()
{
//freopen("input.txt","r",stdin);
while(~scanf("%s",s)&&s[]!='#')
{
if(solve()) printf("%s\n",s);
else printf("No Successor\n");
}
}

POJ 1146 ID Codes 用字典序思想生成下一个排列组合的更多相关文章

  1. POJ 1146 ID Codes (UVA146)

    // 求下一个排列// 如果已经是最后一个排列// 就输出 No Successor// stl 或 自己写个 生成排列 我测试了下 两个速率是一样的.只是代码长度不同 /* #include < ...

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

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

  3. ACM POJ 1146 ID Codes

    题目大意:输入一个字符串.输出它的下一个字典序排列. 字典序算法思想: 1.从右向左寻找字符串找出第一个a[i]<a[i+1]的位置i; 2.从右向左找出第一个大于a[i]的元素a[j]; 3. ...

  4. 31. Next Permutation (java 字典序生成下一个排列)

    题目: Implement next permutation, which rearranges numbers into the lexicographically next greater per ...

  5. (组合数学3.1.1.1)POJ 1146 ID Codes(字典序法)

    /* * POJ_1146.cpp * * Created on: 2013年10月8日 * Author: Administrator */ #include <iostream> #i ...

  6. UVA 10098 用字典序思想生成所有排列组合

    题目: Generating permutation has always been an important problem in computer science. In this problem ...

  7. 1146 ID Codes

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

  8. UVA 146 ID Codes(下一个排列)

    C - ID Codes Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Statu ...

  9. UVa-146 - ID Codes(下一个排列)

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

随机推荐

  1. NIO你真正了解多少?

    解释一下java.io.Serializable接口 类通过实现 Java.io.Serializable 接口以启用其序列化功能.未实现此接口的类将无法使其任何状态序列化或反序列化. IO操作最佳实 ...

  2. 【spring】自定义注解 custom annotation

    自定义注解 custom annotation 使用场景 类属性自动赋值. 验证对象属性完整性. 代替配置文件功能,像spring基于注解的配置. 可以生成文档,像java代码注释中的@see,@pa ...

  3. DesignPattern系列__07合成复用原则

    基本介绍 合成复用原则的核心,就是尽量去使用组合.聚合等方式,而不是使用继承. 核心思想 1.找出应用中可能需要变化之处,把它们独立出来,不要和那些不需要变化的代码混在一起. 2.针对接口编程,而不是 ...

  4. ios路线

    http://www.cocoachina.com/ios/20150303/11218.html

  5. 汇编之JCC指令

    版权声明:本文为博主原创文章,转载请附上原文出处链接和本声明.2019-09-06,21:59:16.作者By-----溺心与沉浮----博客园 JCC指令决定它跳不跳转跟别的没关系,只跟EFLAG标 ...

  6. elasticsearch 单节点出现unassigned_shards

    查看单节点Elasticsearch健康状态 使用head插件查看集群状态 从上面截图可以看出存在5个unassigned的分片,新建索引blog5的时候,分片数为5,副本数为1,新建之后集群状态成为 ...

  7. 关与 @EnableConfigurationProperties 注解

    @EnableConfigurationProperties注解的作用是: 让使用 @ConfigurationProperties 注解的类生效. 当@EnableConfigurationProp ...

  8. linux 网站目录权限设置

    Linux下Apache网站目录读写权限的设置 网站目录文件权限的设置对网站的安全至关重要,下面简单介绍网站目录文件权限的基本设定. 我们假设http服务器运行的用户和用户组是www,网站用户为cen ...

  9. 网络编程ssh,粘包

    1.什么是socket? TCP,可靠地,面向连接协议,有阻塞rect udp,不可靠的,无线连接的服务 这里因为不需要阻塞,所以速度会很快,但安全性不高 2.关于客户端退出而服务器未退出的解决办法 ...

  10. JS高阶---继承模式(借用构造函数继承+组合继承)

    (1)借用构造函数继承 案例如下: 验证: (2)组合继承 案例如下: 验证如下: 结果如右图所示 . .