/* 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新老接口对比

    http://blog.csdn.net/leixiaohua1020/article/details/41013567

  2. Okhttp之RouteSelector简单解析

    继前面的几篇OKhttp的拦截器简单分析之后,对于后续Okhttp之间的分析自己也着实琢磨了一段时间,是分析RealConnection?还是ConnectionPool,随着对Okhttp源码的深入 ...

  3. MySQL的用户账户管理

    1.开启MySQL远程连接 1.sudo -i 2.cd /etc/mysql/mysql.conf.d/ 3.vim mysqld.cnf #bind-address = 127.0.0.1 把前面 ...

  4. 如何写一个LaTeX类文件,并设计你自己的简历

    2017/8/29 20:26:03 原文地址 https://www.sharelatex.com/blog/2011/03/27/how-to-write-a-latex-class-file-a ...

  5. HDU - 6397:Character Encoding (组合数&容斥)

    题意:T组数据,给次给出N,M,K,多少种方案,用[0,N-1]范围的数,表示一个M排列,其和为K: 思路:隔板法,不限制[0,N-1]的时候答案是C(M+K-1,M-1):那么我们减去至少一个> ...

  6. 在eclipse中使用git clone 别人共享在Github的代码和上传到自己的仓库!

    原文地址:http://www.itdaan.com/blog/2014/10/24/ff5e03b5cefef19e1b6fbb7b49809846.html1.在eclipse中安装git:打开e ...

  7. turtle海龟作图

    个人心得:python这么火是有他的原因的,突然发现他的语言很有趣,库函数也很多. 这次是第一步,简单了解了一下turtle函数 import turtle import time def drawS ...

  8. Machine Schedule(二分图匹配之最小覆盖点,匈牙利算法)

    个人心得:二分图啥的一点都不知道,上网借鉴了下,请参考http://blog.csdn.net/thundermrbird/article/details/52231639 加上自己的了解,二分图就是 ...

  9. Docker生态不会重蹈Hadoop的覆辙

    本文原作者是晏东(精灵云Ghostcould创始人),在读到<Docker生态会重蹈Hadoop的覆辙吗?>文章后的个人思考,里面的不少观点也是很不错的. 1.形态上的差异 2013年的时 ...

  10. 【转】ubuntu 12.04 /sbin/ldconfig.real: /usr/local/lib/*.so.8 不是符号连接 解决办法

    原文网址:http://blog.csdn.net/shulianghe/article/details/21176059 最近在ubuntu12.04下使用sudo apt-get install安 ...