uva 146 - ID Codes
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 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
, , ,
#include <stdio.h>
#include <string.h>
int main()
{char s[100],ch;
int l,i,j,pos,min;
while (gets(s))
{if (s[0]=='#') break;
l=strlen(s);
pos=l-1;
while ((pos>0)&&(s[pos]<=s[pos-1])) --pos;
if (pos)
{min=pos;
for (i=pos+1;i<l;i++)
if ((s[i]<s[min])&&(s[i]>s[pos-1])) min=i;
ch=s[pos-1];s[pos-1]=s[min];s[min]=ch;
for (i=pos;i<l-1;i++)
for (j=i+1;j<l;j++)
if (s[i]>s[j]) {ch=s[i];s[i]=s[j];s[j]=ch;}
puts(s);
}
else printf("No Successor
");
}
return 0;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Access Request, Session and Application in Struts2
If we want to use request, Session and application in JSP, what should we do?
We can obtain Map type objects such as Req...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.
abaabc
abaacb
ababac
abaacb
cbbaa
#
ababac
No Successor
, , ,
#include <stdio.h>
#include <string.h>
int main()
{char s[100],ch;
int l,i,j,pos,min;
while (gets(s))
{if (s[0]=='#') break;
l=strlen(s);
pos=l-1;
while ((pos>0)&&(s[pos]<=s[pos-1])) --pos;
if (pos)
{min=pos;
for (i=pos+1;i<l;i++)
if ((s[i]<s[min])&&(s[i]>s[pos-1])) min=i;
ch=s[pos-1];s[pos-1]=s[min];s[min]=ch;
for (i=pos;i<l-1;i++)
for (j=i+1;j<l;j++)
if (s[i]>s[j]) {ch=s[i];s[i]=s[j];s[j]=ch;}
puts(s);
}
else printf("No Successor
");
}
return 0;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Access Request, Session and Application in Struts2If we want to use request, Session and application in JSP, what should we do? We can obtain Map type objects such as Req...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.