UVA 146 ID Codes
2015-08-05 15:15
211 查看
分析
求下一个排列,next_permutation()好好好。
举一个栗子,
aaabbc的排列是什么?编写一个简单的代码生成排列。
#include <cstdio> #include <cstring> #include <algorithm> using std::next_permutation; using std::sort; char s[50]; int main() { int t; scanf("%d", &t); while (t--) { scanf("%s", s); sort(s, s+strlen(s)); do { printf("%s\n", s); } while (next_permutation(s, s+strlen(s))); printf("\n"); } return 0; }
观察
abaacb前后的排列
aacbab aacbba abaabc abaacb < ababac ababca abacab
(建议绘制解答树)由于最后一位没有选择,所以从前一位考察,取出
c,和末位
b比较大,说明这一位已经排序过了(
abaabc),所以继续向前取出
a,比末位
b小,说明可以向后推,但是它的下一位是
c,但是
c的下一位是
b,更小,根据字典序于是把
a,
b交换,得到
ababca,但是这一位之后还需要字典序,所以再把后面部分排序,得到
ababac。以下是两种
AC解法。
所以大致思路是这样。从末尾开始取值,再寻找末尾比当前位大的集合里最小(只需要也从末尾向前滚,因为当前的排列也是字典序)的值。交换次序,将剩余部分按字典序排序。
代码
#include <cstdio> #include <cstring> #include <algorithm> using std::next_permutation; char s[50]; int main() { while (scanf("%s", s), s[0]!='#') if (next_permutation(s, s+strlen(s))) printf("%s\n", s); else printf("No Successor\n"); return 0; }
#include <cstdio> #include <cstring> #include <algorithm> using std::next_permutation; using std::sort; char s[50]; void solve() { bool flag = false; for (int i = strlen(s) - 2; !flag && i >= 0; i-- ) for (int j = strlen(s) - 1; j > i; j--) { if (s[j] > s[i]) { flag = true; char t = s[i]; s[i] = s[j]; s[j] = t; sort(s+i+1, s+strlen(s)); break; } } if (flag) printf("%s\n", s); else printf("No Successor\n"); } int main() { while (scanf("%s", s), s[0]!='#') solve(); return 0; }
题目
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
band 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 Successorif 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
相关文章推荐
- 南邮 OJ 1404 取数游戏
- 南邮 OJ 1401 乘车费用
- [转]构建C协程之setjmp/long_jmp篇
- HDU 5351 MZL's Border(规律)
- RTEMS-BBB(四):rki镜像
- c++中的explicit关键字
- 防止Block的循环引用
- UIView的图层关系
- 孙源面试题试解(更新完毕)
- android_移植memtester
- 低级绕过手段
- Pie(poj 3122)二分法
- 64bit需要做的改变
- linux命令--------------top
- 如何在Android开发中让你的代码更有效率
- 栈的应用 - 波兰式与逆波兰式
- Wireshark远程抓包
- C#中的异常处理
- php 反射
- Cocos2d-x 3.6 命令行创建项目