UVA 146 ID Codes

来源:互联网 发布:数据交换是什么意思 编辑:程序博客网 时间:2024/06/08 13:34

分析

求下一个排列,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前后的排列

aacbabaacbbaabaabcabaacb <ababacababcaabacab

(建议绘制解答树)由于最后一位没有选择,所以从前一位考察,取出c,和末位b比较大,说明这一位已经排序过了(abaabc),所以继续向前取出a,比末位b小,说明可以向后推,但是它的下一位是c,但是c的下一位是b,更小,根据字典序于是把ab交换,得到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 b and 1 of c, then three of the allowable 60 codes under these conditions are:

abaabcabaacbababac

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

abaacbcbbaa#

Sample output

ababacNo Successor
0 0