UVa-146 - ID Codes(下一个排列)
/* 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(下一个排列)的更多相关文章
- Brute Force & STL --- UVA 146 ID Codes
ID Codes Problem's Link:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&a ...
- UVA 146 ID Codes(下一个排列)
C - ID Codes Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit Statu ...
- POJ 1146 ID Codes 用字典序思想生成下一个排列组合
ID Codes Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 7644 Accepted: 4509 Descript ...
- [LeetCode] Next Permutation 下一个排列
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- lintcode:next permutation下一个排列
题目 下一个排列 给定一个整数数组来表示排列,找出其之后的一个排列. 样例 给出排列[1,3,2,3],其下一个排列是[1,3,3,2] 给出排列[4,3,2,1],其下一个排列是[1,2,3,4] ...
- C++构造 下一个排列 的函数
今天围观刘汝佳神犇的白书发现了一个好用的函数: next_permutation(); 可以用于可重, 或者不可重集, 寻找下一个排列. 时间复杂度尚不明. //适用于不可重和可重集的排列. # in ...
- LinkCode 下一个排列、上一个排列
http://www.lintcode.com/zh-cn/problem/next-permutation-ii/# 原题 给定一个若干整数的排列,给出按正数大小进行字典序从小到大排序后的下一个排列 ...
- Next Permutation 下一个排列
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- [Swift]LeetCode31. 下一个排列 | Next Permutation
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
随机推荐
- StringUtils.isEmpty和StringUtils.isBlank的区别
两个方法都是判断字符是否为空的.前者是要求没有任何字符,即str==null 或 str.length()==0:后者要求是空白字符,即无意义字符.其实isBlank判断的空字符是包括了isEmpty ...
- python常用模块之json、pickle模块
python常用模块之json.pickle模块 什么是序列化? 序列化就是把内存里的数据类型转换成字符,以便其能存储到硬盘或者通过网络进行传输,因为硬盘或网络传输时只接受bytes. 为什么要序列化 ...
- Mac 配置前端基本环境
一,sublime 下载一个版本,替换packages,要想shift command p管用,得在sublime里面control -,然后把 import urllib.request,os,h ...
- python中读取文件的f.seek()方法
用于二进制文件中F.seek方法 作用: 设置读写位置 F.seek(偏移量, whence=相对位置) 偏移量 大于0的数代表向文件末尾方向移动的字节数 小于0的数代表向文件头方向中移动的字节数 相 ...
- C的文件操作函数
fgetc(FILE *)意为从文件指针stream指向的文件中读取一个字符,读取一个字节后,光标位置后移一个字节fputc(char,FILE*)将字符ch写到文件指针fp所指向的文件的当前写指针的 ...
- hiho1613 墨水滴
对不起,太弱了.................想了一下午
- Codeforces 1030E 【暴力构造】
LINK 题目大意:给你n个数,你可以交换一个数的任意二进制位,问你可以选出多少区间经过操作后异或和是0 思路 充分必要条件: 区间中二进制1的个数是偶数 区间中二进制位最多的一个数的二进制个数小于等 ...
- BZOJ1002 FJOI2007 轮状病毒 【基尔霍夫矩阵+高精度】
BZOJ1002 FJOI2007 轮状病毒 Description 轮状病毒有很多变种,所有轮状病毒的变种都是从一个轮状基产生的.一个N轮状基由圆环上N个不同的基原子和圆心处一个核原子构成的,2个原 ...
- BZOJ4057 [Cerc2012]Kingdoms
题意 有一些王国陷入了一系列的经济危机.在很多年以前,他们私底下互相借了许多钱.现在,随着他们的负债被揭发,王国的崩溃不可避免地发生了--现在有n个王国,对于每对王国A和B,A欠B的钱被记为d_AB( ...
- elixir 几种安装方式
1. yum yum 源(直接放弃,版本太老) 2. 使用预编译包 wget https://github.com/elixir-lang/elixir/releases/download/v1. ...