POJ 2159 Ancient Cipher
2012-04-30 11:43
405 查看
[align=center]AncientCipher[/align]
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
SampleOutput
TimeLimit:1000MS | MemoryLimit:65536K | |
TotalSubmissions:20788 | Accepted:7010 |
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");
}
}
}
相关文章推荐
- POJ 2159 Ancient Cipher(古老的密码)
- POJ-2159-Ancient Cipher-解题报告
- POJ 2159 Ancient Cipher(字符串排列替换)
- POJ 2159 Ancient Cipher
- POJ2159——Ancient Cipher
- poj 2159 Ancient Cipher
- Ancient Cipher (UVA-10340 && POJ-2159)
- 【哈希】POJ-2159 Ancient Cipher
- POJ-2159 Ancient Cipher
- (Relax ST1.11)POJ 2159 Ancient Cipher(判断一个字符串是否是另外一个字符串的原串)
- poj 2159 Ancient Cipher(水)
- POJ 2159 Ancient Cipher
- POJ 2159 Ancient Cipher
- POJ - 2159 Ancient Cipher 水题 字符统计
- POJ 2159 Ancient Cipher 难度:0
- poj-2159-Ancient Cipher
- poj - 2159 - Ancient Cipher(统计)
- Poj 2159 / OpenJudge 2159 Ancient Cipher
- poj 2159 Ancient Cipher
- POJ 2159 Ancient Cipher