C - ID Codes

Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld
& %llu

Appoint description: 
System Crawler  (2014-05-12)

Description

 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
#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<queue>
#include<vector>
#include<string.h>
#include<map>
using namespace std;
int main()
{
//freopen("in.txt","r",stdin);
char s[60];
while(gets(s)!=NULL){
if(!strcmp(s,"#"))return 0;
int len=strlen(s);
int solve=0;
for(int i=len-2;!solve&&i>=0;i--)
for(int j=len-1;j>i;j--)
{
if(s[j]>s[i]){
solve=1;
char t=s[i];
s[i]=s[j];
s[j]=t;
sort(s+i+1,s+len);
break;
}
}
if(solve)puts(s);
else puts("No Successor");
}
return 0;
}

STL版

#include<iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
char s[50];
int main()
{
while(gets(s) != NULL && s[0] != '#')
{
if(next_permutation(s, s + strlen(s)))
printf("%s\n", s);
else
printf("No Successor\n");
}
return 0;
}

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

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

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

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

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

  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. nolock的使用

    在SQL Server 2005数据库查询时,为了提高查询的性能,我们往往会在表后面加一个nolock,或者是with(nolock),让数据库在查询时不锁定表,从而提高查询的速度.本文我们就介绍SQ ...

  2. Python学习(四)数据结构 —— list tuple range

    序列类型 list   tuple   range list 和 tuple list: 列表,由 [] 标识: 有序:可改变列表元素 tuple: 元组,由 () 标识: 有序:不可改变元组元素(和 ...

  3. Informatica 常用组件Source Qualifier之五 User Defined Join

    User defined join :      输入用户定义的联接与输入自定义 SQL 查询类似.但是,只需输入 WHERE 子句的内容,而不是整个查询. 添加用户定义的联接时,源限定符转换包括默认 ...

  4. environmentmap in unity

    真崩溃之前明明找到这个api了 然后没存 然后我就找不到了... 刚刚遇到个特别邪门的问题 调着调着 vs的断点都显示无效 重启unity vs 电脑都不好使 之后我双击了breakpoint窗口.. ...

  5. 项目笔记:list页面展示与交互设计

    1.前台页面: 因为要展示正版和非正版,所以传个Type值过去: //正版序列号库列表 var type = $("input[name='serialNumber']:checked&qu ...

  6. build path libraries java基础--Jar包添加到build path方式说明--01

    摘自: http://blog.csdn.net/haolongabc/article/details/7007701 java基础--Jar包添加到build path方式说明--01 前言:这段短 ...

  7. win10 修改cmd终端编码格式为utf8

    最近在使用ssh 连接服务器时,好多中文显示为乱码,查明原因,是因为自己cmd终端编码给是为gbk,而服务器编码格式为utf8,所以需要修改cmd终端编码格式为utf8,但是网上看到好多解决方案是 1 ...

  8. JS辨别访问浏览器

    项目中需要扫描二维码之后自动分辨出是android还是ios系统,针对于不同的系统进行不同的下载. <script type="text/javascript"> /* ...

  9. LDA基本介绍以及LDA源码分析(BLEI)

    基本介绍: topic model,主题模型介绍:http://www.cnblogs.com/lixiaolun/p/4455764.html  以及 (http://blog.csdn.net/h ...

  10. 算法笔记_139:二分图的最大权匹配(Java)

    目录 1 问题描述 2 解决方案   1 问题描述 何为二分图的最大权匹配问题? 最大权二分匹配问题就是给二分图的每条边一个权值,选择若干不相交的边,得到的总权值最大. 2 解决方案 对于此问题的讲解 ...