POJ 1146 ID Codes 用字典序思想生成下一个排列组合
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 7644 | Accepted: 4509 |
Description
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
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
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 用字典序思想生成下一个排列组合的更多相关文章
- POJ 1146 ID Codes (UVA146)
// 求下一个排列// 如果已经是最后一个排列// 就输出 No Successor// stl 或 自己写个 生成排列 我测试了下 两个速率是一样的.只是代码长度不同 /* #include < ...
- poj 1146 ID Codes (字符串处理 生成排列组合 生成当前串的下一个字典序排列 【*模板】 )
ID Codes Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 6229 Accepted: 3737 Descript ...
- ACM POJ 1146 ID Codes
题目大意:输入一个字符串.输出它的下一个字典序排列. 字典序算法思想: 1.从右向左寻找字符串找出第一个a[i]<a[i+1]的位置i; 2.从右向左找出第一个大于a[i]的元素a[j]; 3. ...
- 31. Next Permutation (java 字典序生成下一个排列)
题目: Implement next permutation, which rearranges numbers into the lexicographically next greater per ...
- (组合数学3.1.1.1)POJ 1146 ID Codes(字典序法)
/* * POJ_1146.cpp * * Created on: 2013年10月8日 * Author: Administrator */ #include <iostream> #i ...
- UVA 10098 用字典序思想生成所有排列组合
题目: Generating permutation has always been an important problem in computer science. In this problem ...
- 1146 ID Codes
题目链接: http://poj.org/problem?id=1146 题意: 给定一个字符串(长度不超过50), 求这个字符串的下一个字典序的字符串, 如果已经是最大字典序, 那么输出 " ...
- UVA 146 ID Codes(下一个排列)
C - ID Codes Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit Statu ...
- UVa-146 - ID Codes(下一个排列)
/* ID Codes It is 2084 and the year of Big Brother has finally arrived, albeit a century late. In or ...
随机推荐
- Python - 错误和异常 - 第十九天
Python 错误和异常 作为 Python 初学者,在刚学习 Python 编程时,经常会看到一些报错信息,在前面我们没有提及,这章节我们会专门介绍. Python 有两种错误很容易辨认:语法错误和 ...
- Flask笔记:cookie
在网站中,HTTP请求是无状态的:第一次请求成功后,第二次请求时服务器依然不知道这次请求的所属用户是谁.为了解决这个问题,在第一次请求成功后,服务器会生成并返回对应的cookie信息给浏览器,而浏览器 ...
- vuePress自动部署到Github Page脚本踩坑
背景 照着官网的教程来就行了,踩了个小坑,记录一下,希望对你有帮助 这是部署后的效果 小坑1 如图所示,官网推荐部署命令 然而windows 没有bash 指令, 直接运行报错 两个解决方法: 项目根 ...
- php取整的几种方式,四舍五入,舍去法取整,进一法取整
php取整的几种方式,四舍五入,舍去法取整,进一法取整方式一:round 对浮点数进行四舍五入语法:float round ( float val [, int precision] ) echo r ...
- java--正则校验
java--正则校验 // boolearn matches(String regex):判断当前字符串是否匹配指定的正则表达式true/false demo: String qq = "1 ...
- 浅谈 Flask 框架
一.框架对比 Django —— 教科书式框架 优势:组件全,功能全,教科书 劣势:占用资源,创建复杂度高 Flask —— 以简单为基准开发,一切从简,能省则省 优势:轻,块 劣势:先天不足,第三方 ...
- MySQL整形手工注入
0x1 判断注入点: http://www.xxx.org/members.php?id=1 and 1=1 --+ # ture http://www.xxx.org/members.php?id= ...
- Java基础之 集合体系结构(Collection、List、ArrayList、LinkedList、Vector)
Java基础之 集合体系结构详细笔记(Collection.List.ArrayList.LinkedList.Vector) 集合是JavaSE的重要组成部分,其与数据结构的知识密切相联,集合体系就 ...
- git tag介绍
我们常常在代码发版时,使用git 创建一个tag ,这样一个不可修改的历史代码版本就像被我们封存起来一样,不论是运维发布拉取,或者以后的代码版本管理,都是十分方便的. git的tag功能git 下打标 ...
- Asp.Net六大内置对象
前面学习mvc管道处理模型的时候,我们晓的HttpContext是贯穿全文的一个对象,在HttpRuntime产生,现在我们所谓的Asp.Net六大内置对象,其实就是HttpContext的属性.具体 ...