/* 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 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 and Output 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 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 发现其实next_permutation可以给字符排序
*/
#include <iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<cmath>
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
#define maxn 100
char a[maxn];
int v[maxn];
char s[maxn];
int main()
{
int ok,i;
while(scanf("%s",s),strcmp(s,"#"))
{
bool bb=false;
//printf("%d\n",strlen(s));
ok=;
for(i=;i<strlen(s);i++)
v[i]=(int)(s[i]-'a');
//sort(v,v+strlen(s));
do
{
for(i=;i<strlen(s);i++)
a[i]=(char)(v[i]+'a');
a[i]='\0';//忘记加反斜杠了
//printf("---%s----\n",a);
if(ok==)
{
bb=true;//开始用ok判断,发现当是最后一个的时候ok也会是1
/*for(i=0;i<strlen(s);i++)
printf("%c",a[i]);
printf("\n");*/
printf("%s\n",a);
break;
}
if(strncmp(a,s,strlen(s))==)
{
ok=;
}
}while(next_permutation(v,v+strlen(s)));
//printf("%d\n",ok);
if(!bb)
printf("No Successor\n");
}
return ;
}
/*还可以如此简单*/
#include <iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<cmath>
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
#define maxn 100
char s[maxn];
int main()
{
int i;
while(scanf("%s",s),strcmp(s,"#"))
{
i=;
do
{
if(i==)
{
i=-;
printf("%s\n",s);
break;
}
i++;
}
while(next_permutation(s,s+strlen(s)));
if(i!=-)
printf("No Successor\n");
}
return ;
}

UVa-146 - ID Codes(下一个排列)的更多相关文章

  1. Brute Force & STL --- UVA 146 ID Codes

     ID Codes  Problem's Link:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&a ...

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

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

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

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

  4. [LeetCode] Next Permutation 下一个排列

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

  5. lintcode:next permutation下一个排列

    题目 下一个排列 给定一个整数数组来表示排列,找出其之后的一个排列. 样例 给出排列[1,3,2,3],其下一个排列是[1,3,3,2] 给出排列[4,3,2,1],其下一个排列是[1,2,3,4] ...

  6. C++构造 下一个排列 的函数

    今天围观刘汝佳神犇的白书发现了一个好用的函数: next_permutation(); 可以用于可重, 或者不可重集, 寻找下一个排列. 时间复杂度尚不明. //适用于不可重和可重集的排列. # in ...

  7. LinkCode 下一个排列、上一个排列

    http://www.lintcode.com/zh-cn/problem/next-permutation-ii/# 原题 给定一个若干整数的排列,给出按正数大小进行字典序从小到大排序后的下一个排列 ...

  8. Next Permutation 下一个排列

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

  9. [Swift]LeetCode31. 下一个排列 | Next Permutation

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

随机推荐

  1. (二) ffmpeg filter学习--混音实现

    Audio 混音实现 从FFMPEG原生代码doc/examples/filtering_audio.c修改而来. ffmpeg版本信息 ffmpeg version N-82997-g557c0df ...

  2. (转)MapReduce Design Patterns(chapter 6 (part 1))(十一)

    Chapter 6. Metapatterns 这种模式不是解决某个问题的,而是处理模式的关系的.可以理解为“模式的模式”.首先讨论的是job链,把几个模式联合起来解决复杂的,有多个阶段要处理的问题. ...

  3. localhost与127.0.0.1之间的区别

    Localhost的意思是本地服务器,而127.0.0.1是本机地址,他们的关系是通过操 作系统中的hosts文件,将Localhost解析为127.0.0.1.而实际工作中,Localhost是不经 ...

  4. [Github] 本地git push免用户名和密码的配置

    在终端通过git config --global命令进行配置 git config --global user.email "xxx@xxmail.com" git config ...

  5. crm--01

    需求: 将课程名称与班级综合起来 class ClassListConfig(ModelSatrk): # 自定义显示方式 def display_class(self,obj=None,is_hea ...

  6. Ubuntu网络代理问题

    问题描述 新开机的电脑,不开lantern就上不了网.很气. 解决过程 首先当然是寻求解决方案了.未果 然后就是妥协,每次先开一次lantern.(其实也不是很麻烦是吧,哎,是的是的) 今天早晨友人来 ...

  7. 封装 一下 php sql 的存储语句

    function get_insert_sql($obj){ $str1 =""; $str2 =""; foreach($obj as $key => ...

  8. BZOJ5281: [Usaco2018 Open]Talent Show(01分数规划&DP)

    5281: [Usaco2018 Open]Talent Show Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 166  Solved: 124[S ...

  9. HDU - 5628:Clarke and math (组合数&线性筛||迪利克雷卷积)

    题意:略. 思路:网上是用卷积或者做的,不太会. 因为上一题莫比乌斯有个类似的部分,所以想到了每个素因子单独考虑. 我们用C(x^p)表示p次减少分布在K次减少里的方案数,由隔板法可知,C(x^p)= ...

  10. Associations marked as mappedBy must not define database mappings like @JoinTable or @JoinColumn【报错】

    自己的项目没有测通  可能是自己项目原因——因为自己项目中级联关系的类涉及太多 自己的项目[这样的配置报错] @OneToMany(fetch=FetchType.LAZY,cascade = { C ...