/* 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. 转:java c/s tomcat 三层架构

    概述在c/s结构的发展历程中,产生了两层c/s结构和三层c/s结构之分.在两层c/s结构中,在信息管理系统的管理上主要分为客户端和数据库服务器. 缺点有两个: 1)开销高昂.在两层c/s结构中,为了维 ...

  2. PHPStorm 使用正则批量查询替换并自动转换大小写的方法

    PHPStorm 的项目查询替换功能那是非常非常强大的, 速度也很快, 配合正则更加灵活强大. 一般的正则查询替换没什么太多好说的, 这里主要说说比较少用的 大小写自动转换的问题, 也是比较少用但很有 ...

  3. Buildroot 使用默认配置

    /******************************************************************************** * Buildroot 使用默认配置 ...

  4. 【javascript】base.js

    作为一个好的脚手架使用 /* Base.js, version 1.1a Copyright 2006-2010, Dean Edwards License: http://www.opensourc ...

  5. debian 8.1 安装idempiere 2.1 X64 笔记

    接上文.当虚拟服务器和虚拟机搭建完成后.登陆debian 8.1 X64. 进入虚拟服务器控制台.打开虚拟机.root登陆.(留好初始状态系统快照.以便系统恢复.) 由于之前debian8.1X64默 ...

  6. ssm框架实现图片上传显示并保存地址到数据库

    本案例是通过springmvc+spring+mybatis框架以商品上传为例,实现的图片上传功能,并把图片的地址保存到数据库并在前台显示上传的图片. 本项目是使用maven搭建的项目,首先看下项目结 ...

  7. Uoj 22 外星人

    Uoj 22 外星人 注意到一个数只有 \(\%\) 了小于等于自己的数时,才可能有变化,否则可以随意安排,不会对最后最优解造成影响. 用 \(f[x]\) 表示给一个数 \(x\) ,仅用 \(a[ ...

  8. ubuntu16安装最新版docker

    ubuntu16.04安装最新版docker.docker-compose.docker-machine https://www.cnblogs.com/tianhei/p/7802064.html ...

  9. python之wheel 包命名规则、abi 兼容和安装

    一.windows安装python包,遇见的问题 1.python3以后的版本,安装python包,可以直接使用pip安装,但是安装时偶尔报错 2.安装python源码包,如何确定自己该安装哪个版本, ...

  10. vw+vh+rem响应式布局

    科普下: 平时很少用的css单位: 1.长度单位: rem:相对长度单位.相对于根元素(即html元素)font-size计算值的倍数; vw:相对于视口的宽度.视口被均分为100单位的vw; vh: ...