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 ...
随机推荐
- Android架构设计之MVP分析
转载请注明出处:http://blog.csdn.net/crazy1235/article/details/51471280
- online learning,batch learning&批量梯度下降,随机梯度下降
以上几个概念之前没有完全弄清其含义及区别,容易混淆概念,在本文浅析一下: 一.online learning vs batch learning online learning强调的是学习是实时的,流 ...
- PHP 时间戳 转时间 for ios 8*3600
// 时间戳 转时间 int intTimeString = [_model.date intValue]; NSDate *confromTimesp = [NSDate dateWithTimeI ...
- vuex(一)mutations
前言:vuex的使用,想必大家也都知道,类似于状态库的东西,存储某种状态,共互不相干的两个组件之间数据的共享传递等.我会分开给大家讲解vuex的使用,了解并掌握vuex的核心(state,mutati ...
- Java并发--阻塞队列
在前面几篇文章中,我们讨论了同步容器(Hashtable.Vector),也讨论了并发容器(ConcurrentHashMap.CopyOnWriteArrayList),这些工具都为我们编写多线程程 ...
- XML 的 XPath 语法
XPath 是 XML 路径语言(XML Path Language),用来确定XML文档中某部分位置的语言.无论是什么语言什么框架,几乎都可以使用 XPath 来高效查询 XML 文件. 本文将介绍 ...
- window10 下 php7.0 添加Sqlserver扩展
第一步.7.0.x 7.0.x的扩展下载地址: Microsoft Drivers for PHP for SQL Server https://www.microsoft.com/en-us/do ...
- scrapy模拟浏览器爬取验证码页面
使用selenium模块爬取验证码页面,selenium模块需要另外安装这里不讲环境的配置,我有一篇博客有专门讲ubuntn下安装和配置模拟浏览器的开发 spider的代码 # -*- coding: ...
- LeetCode 973. K Closest Points to Origin
原题链接在这里:https://leetcode.com/problems/k-closest-points-to-origin/ 题目: We have a list of points on th ...
- wpf中如何在xaml中绑定cs中类的属性
cs代码:/// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class MainWin ...