HDU 1015 safecracker (enumeration)

Source: Internet
Author: User
Safecracker

Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 3713 accepted submission (s): 1919

Problem description = Op Tech briefing, 2002/11/02 CST =
"The item is locked in a Klein safe behind a painting in the second-floor library. klein safes are extremely rare; most of them, along with Klein and his factory, were destroyed in World War II. fortunately old brumbaugh from research knew Klein's secrets and wrote them down before he died. A Klein safe has two distinguishing features: a combination lock that uses letters instead of numbers, and an engraved quotation on the door. A Klein quotation always contains between five and twelve distinct uppercase letters, usually at the beginning of sentences, and mentions one or more numbers. five of the uppercase letters form the combination that opens the safe. by combining the digits from all the numbers in the appropriate way you get a numeric target. (The details of constructing the target number are classified .) to find the combination you must select five letters V, W, X, Y, and Z that satisfy the following equation, where each letter is replaced by its ordinal position in the alphabet (a = 1, B = 2 ,..., z = 26 ). the combination is then vwxyz. if there is more than one solution then the combination is the one that is lexicographically greatest, I. E ., the one that wowould appear last in a dictionary."

V-w ^ 2 + x ^ 3-y ^ 4 + Z ^ 5 = Target

"For example, given target 1 and letter set abcdefghijkl, one possible solution is fiecb, since 6-9 ^ 2 + 5 ^ 3-3 ^ 4 + 2 ^ 5 = 1. there are actually several solutions in this case, and the combination turns out to be lkeba. klein thought it was safe to encode the combination within the engraving, because it cocould take months of effort to try all the possibilities even if you knew the secret. but of course computers didn't exist then."

=== Op Tech directive, computer division, 2002/11/02 CST ====

"Develop a program to find Klein combinations in preparation for field deployment. use standard test methodology as per departmental regulations. input consists of one or more lines containing a positive integer target less than twelve million, a space, then at least five and at most twelve distinct uppercase letters. the last line will contain in a target of zero and the letters end; This signals the end of the input. for each line output the Klein combination, break ties with Lexicographic Order, or 'no solution' if there is no correct combination. use the exact format shown below."

 

Sample input1 abcdefghijkl 11700519 zayexiwovu 3072997 sought 1234567 thequickfrog 0 end

 

Sample outputlkeba yoxuz ghost no solution

 

Sourcemid-Central USA 2002

 

Recommendjgshining is more violent and involves 5 repeated loops of direct enumeration.
# Include <stdio. h>
# Include < String . H>
# Include <algorithm>
# Include <iostream>
Using Namespace STD;
Char STR [ 25 ];
Bool CMP ( Char A, Char B)
{
Return A> B;
}
Int Main ()
{
Int Target;
Int A, B, C, D, E;
Int Ta, TB, TC, TD, Te;
Int Flag;
While (Scanf ( " % D % s " , & Target, & Str ))
{
If (Target = 0 ) Break ;
Int Len = strlen (STR );
Flag = 0 ;
Sort (STR, STR + Len, CMP );
For (A = 0 ; A <Len; A ++)
{
For (B = 0 ; B <Len; B ++)
{
If (B =) Continue ;
For (C = 0 ; C <Len; C ++)
{
If (C = B | C =) Continue ;
For (D = 0 ; D <Len; D ++)
{
If (D = A | D = B | D = C) Continue ;
For (E = 0 ; E <Len; e ++)
{
If (E = A | E = B | E = c | E = D) Continue ;
Ta = STR [a]- ' A ' + 1 ;
TB = STR [B]- ' A ' +1 ;
TC = STR [c]- ' A ' + 1 ;
TD = STR [d]- ' A ' + 1 ;
Te = STR [e]- ' A ' + 1 ;
If (Ta-TB * TB + Tc * TC-TD * TD + Te * te * TE = target)
{
Flag = 1 ;

Printf ( " % C \ n " , STR [a], STR [B], STR [c], STR [d], STR [e]);
Break ;
}

}
If (FLAG)Break ;
}
If (FLAG) Break ;
}
If (FLAG) Break ;
}
If (FLAG) Break ;
}

If (Flag = 0 ) Printf (" No solution \ n " );

}
Return 0 ;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.