PROBLEM LINK:
Author: Rahul Johari
Tester: Rahul Johari
DIFFICULTY:
EASY
PREREQUISITES:
Sorting, Strings
PROBLEM:
Two friends are playing a game. One friend gives a number N ans asks his friend to swap the digits in the number such that a smallest number M is obtained without leading zeroes.
EXPLANATION:
Input the two numbers N and M as strings. Store the second number M in some variable (as this is final answer to be verified). Sort first string (N). Check if first character of N is not equal to 0. Find the next permutations of N store the smallest permutations in any variable (say ans).
Check if M is equal to ans and output the result accordingly.
Pseudocode -
cin >> N >> M;
ans = N;
sort(N.begin(),N.end());
do
{
if ( N[0]!='0' )
ans = min(ans,N);
}
while(next_permutation(N.begin(),N.end()));
if ( M==ans )
printf("AC\n");
else
printf("WA\n");
AUTHOR’S AND TESTER’S SOLUTIONS:
Author’s solution can be found here.