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. 使用tortoisegit访问git@oschina

    转自:http://www.3lian.com/edu/2014/01-03/121350.html 首先,如果你想使用git@oschina ,你的电脑上必须先有git工具:你可以从这里获取谷歌提供 ...

  2. iOS开源项目:asi-http-request

    使用CFNetwork实现的http库,能同时在iphone和macos下使用:http://allseeing-i.com/ASIHTTPRequest/ 他提供以下功能: 向服务器发送或者从服务器 ...

  3. awk排序作业

    输入:给定一个hotelinfo文件,文件格式如下: shanghai_city_7208      上海全季酒店淮海路店 shanghai_city_14744     锦江之星上海金山城市沙滩店 ...

  4. c#抓去网页

    c#利用WebClient和WebRequest获取网页源代码的比较 2011-11-28 10:26:42     我来说两句 收藏 我要投稿 C#中一般是可以利用WebClient类和WebReq ...

  5. 一个Loading 遮罩效果

    1.需要两个DIV,一个用来遮罩,另一个用来显示Loading图片和文字(初始时它们是隐藏的) .gdiv_over { display: none; position: absolute; top: ...

  6. 使用CSS3建立不可选的的文字

    下面的例子展示了在HTML5中你如何使用CSS建立不可选的文字. <!DOCTYPE HTML> <html> <head> <title>Creati ...

  7. SWFUpload 已上传成功数量控制 插件(用于解决队列满问题)

    当我们在使用 SWFUpload 做文件上传时,我们需要把已经上传的文件列表做一个删除, 但在我们把已上传列表删除后,再重新上传时,会发现提示 上传队列满 的问题,原因就是有一个状态对象中的一个 成功 ...

  8. Windows-设置系统服务不开机启动

    设置为手动,则开机不会自动启动了

  9. 【转】6 Reasons Why JavaScript’s Async/Await Blows Promises Away (Tutorial)

    原文:https://hackernoon.com/6-reasons-why-javascripts-async-await-blows-promises-away-tutorial-c7ec105 ...

  10. WinForm 窗口缩放动画效果

    using System; using System.Collections.Generic; using System.Text; using System.Threading; using Sys ...