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 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'.
abaacb cbbaa #
ababac No Successor
题意:
雖然慢了一個世紀,在2048年的時候,老大時代終究還是來了!為了管理這許多的公民,而且為了可以在法律上的長期計數。所以政府決定了一個極端的方法---所有居民都有一個超微小電腦晶片殖在他們的左手脕上。這個電腦將包含這個居民的所有個人資訊並且它也是一個小型發射機,將人們的行動記錄在一個中央電腦中。
每台電腦的本質元件上都將有一個唯一的識別碼。包含有最多50個字元的26個小寫字母。對任一個字元的選擇都是隨意的。為了使壓印這些識別碼到晶片中的複雜方法簡化一些,所以對不同製造者都將分配到不同區段的英文字,並用來產生新碼。因此每選擇一組英文字,在他換到其他組英文字之前,是可以推論出所有可能的碼。例如,如果決定的一組英文字剛好包含3個a,2個b, 1個c, 那麼在允許的60個可能的編碼中的三個如下所示:
abaabc abaacb ababac這三個碼按英文字母順序依序印出,而且在全部可能產生的碼當中是連續的。
現在你的任務就是要寫一個程序來幫忙識別碼的發行。你的程式將接受一系列不超50個小寫字母(有可能會重覆)而且印出他的下一個碼。假如是最後一個碼,也就是沒有下一個,就要印出"No Successor"
Input
每組測試資料1列,有1個識別碼字串。字串"#"代表輸入結束。
Output
每組測試資料輸出一個字串,代表輸入識別碼的下一個識別碼字串。如果此字串不存在,請輸出"No Successor"
这题显示了STL的强大魅力!
#include <iostream> #include <algorithm> #include <cstdio> #include <cstring> using namespace std; const int MAX=1000; int main() { // freopen("in.txt","r",stdin); char ch[MAX]; while(cin>>ch) { if(ch[0]=='#') break; if(next_permutation(ch, ch+strlen(ch))) cout << ch << endl; else cout << "No Successor" << endl; } return 0; }