您的位置:首页 > 其它

POJ 2159 Ancient Cipher

2012-04-30 11:43 405 查看
[align=center]AncientCipher[/align]

TimeLimit:1000MSMemoryLimit:65536K
TotalSubmissions:20788Accepted:7010
Description
AncientRomanempirehadastronggovernmentsystemwithvariousdepartments,includingasecretservicedepartment.Importantdocumentsweresentbetweenprovincesandthecapitalinencryptedformtopreventeavesdropping.The
mostpopularciphersinthosetimesweresocalledsubstitutioncipherandpermutationcipher.

Substitutioncipherchangesalloccurrencesofeachlettertosomeotherletter.Substitutesforalllettersmustbedifferent.Forsomeletterssubstitutelettermaycoincidewiththeoriginalletter.Forexample,applyingsubstitutioncipherthatchanges
alllettersfrom'A'to'Y'tothenextonesinthealphabet,andchanges'Z'to'A',tothemessage"VICTORIOUS"onegetsthemessage"WJDUPSJPVT".

Permutationcipherappliessomepermutationtothelettersofthemessage.Forexample,applyingthepermutation<2,1,5,4,3,7,6,10,9,8>tothemessage"VICTORIOUS"onegetsthemessage"IVOTCIRSUO".

Itwasquicklynoticedthatbeingappliedseparately,bothsubstitutioncipherandpermutationcipherwereratherweak.Butwhenbeingcombined,theywerestrongenoughforthosetimes.Thus,themostimportantmessageswerefirstencryptedusingsubstitution
cipher,andthentheresultwasencryptedusingpermutationcipher.Encryptingthemessage"VICTORIOUS"withthecombinationoftheciphersdescribedaboveonegetsthemessage"JWPUDJSTVP".

Archeologistshaverecentlyfoundthemessageengravedonastoneplate.Atthefirstglanceitseemedcompletelymeaningless,soitwassuggestedthatthemessagewasencryptedwithsomesubstitutionandpermutationciphers.Theyhaveconjecturedthepossible
textoftheoriginalmessagethatwasencrypted,andnowtheywanttochecktheirconjecture.Theyneedacomputerprogramtodoit,soyouhavetowriteone.
Input
Inputcontainstwolines.Thefirstlinecontainsthemessageengravedontheplate.Beforeencrypting,allspacesandpunctuationmarkswereremoved,sotheencryptedmessagecontainsonlycapitallettersoftheEnglishalphabet.
Thesecondlinecontainstheoriginalmessagethatisconjecturedtobeencryptedinthemessageonthefirstline.ItalsocontainsonlycapitallettersoftheEnglishalphabet.

Thelengthsofbothlinesoftheinputareequalanddonotexceed100.
Output
Output"YES"ifthemessageonthefirstlineoftheinputfilecouldbetheresultofencryptingthemessageonthesecondline,or"NO"intheothercase.
SampleInput
JWPUDJSTVP
VICTORIOUS

SampleOutput
YES


java代码:

importjava.util.Arrays;
importjava.util.Scanner;
publicclassMain{
publicstaticvoidmain(String[]args){
Scannerin=newScanner(System.in);
while(in.hasNext()){
Stringcipher=in.next();
Stringoriginal=in.next();
intci[]=newint[26];
intor[]=newint[26];
for(inti=0;i<cipher.length();i++){
ci[cipher.charAt(i)-'A']++;
}
for(inti=0;i<original.length();i++){
or[original.charAt(i)-'A']++;
}
Arrays.sort(ci);
Arrays.sort(or);
intflag=1;
for(inti=0;i<26;i++){
if(ci[i]!=or[i]){
System.out.println("NO");
flag=0;
break;
}
}
if(flag==1)
System.out.println("YES");
}
}
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: