一、题目
ID Codes
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 4558   Accepted: 2753

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: 
      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

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

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

Source

二、分析

       对某个包含某些特定数字(0~9)的数,求包含这些数字且比已知数大的最小数。这道题目只不过是把具体的数字(0~9)换成了小写字母(a~z),其具体算法如下:

1,  从字符串末尾开始,对某个字符str[i],找出它前面离它最近的,且比它小的字符str[j](显然j<i)。若不存在这样的字符,则i--,继续寻找满足条件的j, 若i== 0,则输出”No Successor”; 否则:

2,  交换str[i]和str[j];对str[i+1]以及其后的字母按从小到大的次序进行排列,并将排列的结果接到str[i]之后,则新的str就是所有的结果。

三、AC源代码

 1 #include <iostream>
2 #include <algorithm>
3 #include <string>
4 using namespace std;
5 int main()
6 {
7 string str;
8 while(cin>>str&&str[0]!='#')
9 {
10 bool flag=true;
11 if(next_permutation(str.begin(),str.end()))
12 {
13 flag=false;
14 cout<<str<<endl;
15 }
16 if (flag)
17 cout<<"No Successor\n";
18 }
19 return 0;
20 }
 1 #include <stdio.h> 
2 #include <string.h>
3 #include <stdlib.h>
4 #define len 60
5 char s[len];
6 int cmp(const void *a,const void *b){
7 return *(char *)a>*(char *)b?1:-1;
8 }
9 void swap(char &a,char &b){
10 a^=b;
11 b^=a;
12 a^=b;
13 }
14 int main(void){
15 int n,i,j;
16 bool flag;
17 while(scanf("%s",s)==1&&s[0]!='#'){
18 n=(int)strlen(s);
19 for(i=n-1,flag=false;i>=0;i--){
20 for(j=i;j>=0;j--)
21 if(s[i]>s[j]){
22 swap(s[i],s[j]);
23 flag=true;
24 qsort(s+j+1,n-j-1,sizeof(s[0]),cmp);
25 break;
26 }
27 if(flag)
28 break;
29 }
30 printf("%s\n",flag?s:"No Successor");
31 }
32 return 0;
33 }




posted on 2011-12-13 20:49  小狗狗ACM  阅读(430)  评论(0编辑  收藏  举报