UvaOJ 146
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
本题说了好多无关的话,感觉自己就在做阅读理解一样..
意思要点如下:
1.produce codes which are rearrangements of other codes than to produce new codes with a different selection of letters.意思在已给的字母中进行重排序
2.once a set of letters has been chosen all possible codes derivable from it are used before changing the set.所给字母每个都要用上,就是题意那样,用的字母个数什么的都一样
最后,题意就是 给你一组字符(在50个以内),你为它排序成示例那样(用数字更形象哈),如:
12345
你应该给出它的下一个排序,仅次于它,比它大的输出应该是12354 ( >12345)
再来一个哈:
12543
这时应该是13254,由于543已经是最大的了,开始在进一位把2换成仅比它大的3,后面的应该542应该重排序变成最小的组合,这样才能满足刚好比12543大的数
什么时候出现No successor呢?就是没有比它还大的数啦,就如54321。
自己多写写就能找到规律啦。建议自己写写,感觉这个算法没难度,感觉就是在找规律...
已经通过了呦---------------------------------------------------------------------------------------
#include<stdio.h> #include<string.h> void sort(char *p,int n,int L) //习惯用冒泡排序(^-^)V,从小到大呦~ { int i,j;char c; for(i=n;i<L;i++) { for(j=n;j<L-i+n;j++) { if(p[j]>p[j+1]) { c = p[j]; p[j] =p[j+1]; p[j+1]=c; } } } } int main() { char str[52],c;int L,i,j; while(1) { scanf("%s",&str); if(str[0]=='#')break; L = strlen(str)-1; for(i=L;i>0;i--) { if(str[i]>str[i-1]) break; } if(i==0)printf("No Successor\n"); else if(i==L) { c = str[i]; str[i] = str[i-1]; str[i-1] = c; printf("%s\n",str); } else { for(j=L;j>=i;j--) { if(str[j]>str[i-1]) break; } c = str[i-1]; str[i-1] = str[j]; str[j] = c; sort(str,i,L); printf("%s\n",str); } } return 0; }
--------------------------------------------------------------------------------------------------
其实刚开始用的是深搜,懒得动脑子,,直接从小到大枚举个种情况,如果与这个输入的数一样,就直接输出它的下一种情况,,但是最后结果是Time limit exceeded....
估计大于字符个数30的就不行了吧,深搜也是有极限的,慎重用哈
#include<stdio.h>
#include<string.h>
char str1[52];
char str2[52];
void sort(int n,char *p)
{
int i,j;char t;
for(i=0;i<n-1;i++)
for(j=0;j<n-i-1;j++)
{
if(p[j]>p[j+1])
{
t = p[j];
p[j] = p[j+1];
p[j+1]= t;
}
}
}
char copy[52];
int ok ,d;
int dfs(int l , int n)
{
int i,j ,c1=0,c2 =0;
if(n == l)
{
copy[n]='\0';
if(ok == 1)
{
printf("%s\n",copy);
return 1;
}
if(memcmp(copy,str2,l)==0)ok =1;
}
else
{
for(i=0;i<l;i++)
{
c1=0;c2=0;
if(i==0 || (str1[i] != str1[i-1]))
{
for(j=0;j<l;j++)if(str1[j]==str1[i])c1++;
for(j=0;j<n;j++)if(copy[j]==str1[i])c2++;
if(c2 < c1 )
{
copy[n]=str1[i];
d = dfs(l,n+1);
if(d==1)return 1;
}
}
}
}
return 0;
}
int main()
{
int length;
while(1)
{
scanf("%s",&str1);
if(str1[0]=='#')break;
length = strlen(str1);
memcpy(str2,str1,length);
sort(length,str1);
ok = 0;
if(dfs(length , 0)==0)
printf("No Successor\n");
}
return 0;
}