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. OpenShift 如何获取bearer Token以便进行各种API调用

    Openshift 需要通过bearer token的方式和API进行调用,比如基于Postman就可以了解到,输入bearer token后 1.如何获取Bearer Token 但Bearer T ...

  2. OpenJudge 8782 乘积最大——S.B.S

    8782:乘积最大 总时间限制: 1000ms 内存限制:  65536kB 描述 今年是国际数学联盟确定的“2000——世界数学年”,又恰逢我国著名数学家华罗庚先生诞辰90周年.在华罗庚先生的家乡江 ...

  3. C# 弹出USB外接硬盘(U盘)

    最近一个项目需要通过代码来弹出USB外接硬盘设备,经过google找到了下面这个类库: http://www.codeproject.com/Articles/13530/Eject-USB-disk ...

  4. 加解密 3DES AES RSA 简介 示例 MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  5. http://m.blog.csdn.net/article/details?id=49132747

    http://m.blog.csdn.net/article/details?id=49132747

  6. html input type=date 赋值问题 必须yyyy-mm-dd格式

    type=date ,日期类型默认格式是yyyy-mm-dd 因此必须给该控件赋值yyyy-mm-dd格式的数据 错误的赋值 <input type="date" id=&q ...

  7. 恢复计算机崩溃数据的五款最佳Linux发行版

    嗨,Linux 新手们!你们在尝试运行命令时有没有搞坏过计算机系统?我相信你们有过这种经历.这一幕经常发生:你想尝试运行命令,或者安装测试更新版,结果下一次重启时计算机就崩溃了.我在本文将逐一介绍五款 ...

  8. [Functional Programming] Working with two functors(Applicative Functors)-- Part2 --liftAN

    Let's examine a pointfree way to write these applicative calls. Since we know map is equal to of/ap, ...

  9. springMVC之增删改查

    一.核心原理 1. 用于发送请求给server: /home.htm 2. 请求被DispatchServlet拦截到 3. DispatchServlet通过HandleMapping检查url有没 ...

  10. centos版本7以上网卡名修改

    1.初始状态网卡 2.首先,先编辑网卡的配置文件 vi /etc/sysconfig/network-scripts/ifcfg-eno16777736 将里面的NAME项修改为eth0 3.然后,禁 ...