Description

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

题目大意:(题外话)George Orwell在《1984》中谈及的老大哥在2084年来到。政府在每个公民体内植入电脑芯片监视控制公民的一举一动,电脑芯片同时具备发射信号的功能。为了确定电脑ID号码,政府给出了一套规则:每个ID号由3个'a'两个'b'一个'c'构成。给定若干ID codes,求它们的下一个字典序全排列。

思路1:先求出当前排列要变化的子串的首字母下标,若不存在该字母,即已经完成了全排列,则输出No Successor,否则继续算法。再将首字母前一个字母同子串中从后到前第一个字典序大于前者的字母进行交换,之后对子串排序,得出目标(target)数组(array),算法结束。

思路1:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
char str[101];
bool cmp(const char x,const char y)
{return x<y;}
int main()
{
while(scanf("%s",str)&&str[0]!='#'){
int k;
for(k=strlen(str)-1;k>=1;k--)
if(str[k]>str[k-1])
break;
if(!k){puts("No Successor");continue;}
for(int i=strlen(str)-1;i>=k;i--)
if(str[i]>str[k-1])
{swap(str[i],str[k-1]);break;}
sort(str+k,str+strlen(str),cmp);
puts(str);
}
return 0;
}

思路2:直接使用c++ STL标准库中的next_permutation()函数。

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
char str[101];
int main()
{
while(scanf("%s",str)&&str[0]!='#'){
if(next_permutation(str,str+strlen(str)))
puts(str);
else puts("No Successor");
memset(str,0,sizeof(str));
}
return 0;
}

uva146 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. UVA-146 ID Codes

    It is 2084 and the year of Big Brother has finally arrived, albeit a century late. In order to exerc ...

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

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

  4. POJ 1146:ID Codes

    ID Codes Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 6281 Accepted: 3769 Description ...

  5. UVA 146 ID Codes(下一个排列)

    C - ID Codes Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Statu ...

  6. poj 1146 ID Codes (字符串处理 生成排列组合 生成当前串的下一个字典序排列 【*模板】 )

    ID Codes Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6229   Accepted: 3737 Descript ...

  7. POJ 1146 ID Codes 用字典序思想生成下一个排列组合

    ID Codes Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7644   Accepted: 4509 Descript ...

  8. 【个人训练】(UVa146)ID Codes

    题意与解析 这题其实特别简单,求给定排列的后继.使用stl(next_permutation)可以方便地解决这个问题.但是,想要自己动手解就是另外一回事了.我的解法是从后往前找到第一个$a_i$比$a ...

  9. POJ 1146 ID Codes (UVA146)

    // 求下一个排列// 如果已经是最后一个排列// 就输出 No Successor// stl 或 自己写个 生成排列 我测试了下 两个速率是一样的.只是代码长度不同 /* #include < ...

随机推荐

  1. 如何申请Autodesk ReCap 360 photo的云币(Cloud Credit)

    在之前的博客中我介绍过Autodesk的照片建模云服务—Autodesk ReCap 360 photo,通过Autodesk ReCap 360 photo,你可以非常方便的通过照片生成三维模型.如 ...

  2. Android 的系统架构

    Android 的系统架构 Android其本质就是在标准的Linux系统上增加了Java虚拟机Dalvik,并在Dalvik虚拟机上搭建了一个JAVA的application framework,所 ...

  3. ArrayList Vector LinkedList 区别与用法

    转载自: http://www.cnblogs.com/mgod/archive/2007/08/05/844011.html 最近用到了,所以依然是转载 ArrayList 和Vector是采用数组 ...

  4. __block和__weak的区别

    API Reference对__block变量修饰符有如下几处解释: //A powerful feature of blocks is that they can modify variables ...

  5. django 验证用户是否登陆

    第一步 指定一下登陆url. url(r'^accounts/login/$', include(xadmin.site.urls)), 由于我用的xadmin故而指向了xadmin,如果使用默认的a ...

  6. Nodejs的模块实现

    在Node中引入模块,需要经历如下3个步骤:(1)路径分析(2)文件定位(3)编译执行 Node中模块分为两类: 一是Node提供的模块——核心模块.这部分在Node源代码的编译过程中,编译进了二进制 ...

  7. C#复习⑧

    C#复习⑧ 2016年6月22日 13:50 Main Attribute & Threads 属性与线程 1.Conditional Attribute 条件属性 #define debug ...

  8. android 进程间通信数据(一)------parcel的起源

    关于parcel,我们先来讲讲它的“父辈” Serialize. Serialize 是java提供的一套序列化机制.但是为什么要序列化,怎么序列化,序列化是怎么做到的,我们将在本文探讨下. 一:ja ...

  9. Nexus Repository Manager 3.0 发布

    著名仓库管理工具Nexus,在2016年4月6日发布3.0版本(包括OSS版),相较2.*版本有很大的改变: 1. 从底层重构,从而提高性能,增强扩展能力,并改善用户体验 2. 升级界面,增加更多的浏 ...

  10. node模块的分类

    模块的分类 1.核心模块 2.文件模块 3.第三方模块(npm安装的) 模块的引用: 1.路径 2.模块名 模块的流程: 1.创建模块:teacher.js 2.导出模块:exports.add=fu ...