【原】 POJ 2159 Ancient Cipher 题意万岁 解题报告

 

http://poj.org/problem?id=2159

 

题意万岁:判断两数组a、b中的所有数是否一样(一个数组是另一个数组的排列)
两种方法:
    1、可以先排序再依次判断两数组元素,复杂度nlgn+n
    2、由于此题中每个字符串的长度最大为100,则数组中的每个元素都不会超过100,那么可以利用一个附加数组c[101],
       在一个循环中 ++c[a[i]] , --c[b[i]] 。若最终c中所有元素为0,则a、b相同。复杂度n

Description

Ancient Roman empire had a strong government system with various departments, including a secret service department. Important documents were sent between provinces and the capital in encrypted form to prevent eavesdropping. The most popular ciphers in those times were so called substitution cipher and permutation cipher. 
Substitution cipher changes all occurrences of each letter to some other letter. Substitutes for all letters must be different. For some letters substitute letter may coincide with the original letter. For example, applying substitution cipher that changes all letters from 'A' to 'Y' to the next ones in the alphabet, and changes 'Z' to 'A', to the message "VICTORIOUS" one gets the message "WJDUPSJPVT". 
Permutation cipher applies some permutation to the letters of the message. For example, applying the permutation <2, 1, 5, 4, 3, 7, 6, 10, 9, 8> to the message "VICTORIOUS" one gets the message "IVOTCIRSUO". 
It was quickly noticed that being applied separately, both substitution cipher and permutation cipher were rather weak. But when being combined, they were strong enough for those times. Thus, the most important messages were first encrypted using substitution cipher, and then the result was encrypted using permutation cipher. Encrypting the message "VICTORIOUS" with the combination of the ciphers described above one gets the message "JWPUDJSTVP". 
Archeologists have recently found the message engraved on a stone plate. At the first glance it seemed completely meaningless, so it was suggested that the message was encrypted with some substitution and permutation ciphers. They have conjectured the possible text of the original message that was encrypted, and now they want to check their conjecture. They need a computer program to do it, so you have to write one.

Input

Input contains two lines. The first line contains the message engraved on the plate. Before encrypting, all spaces and punctuation marks were removed, so the encrypted message contains only capital letters of the English alphabet. The second line contains the original message that is conjectured to be encrypted in the message on the first line. It also contains only capital letters of the English alphabet. 
The lengths of both lines of the input are equal and do not exceed 100.

Output

Output "YES" if the message on the first line of the input file could be the result of encrypting the message on the second line, or "NO" in the other case.

Sample Input

JWPUDJSTVP

VICTORIOUS

Sample Output

YES

 

   1: #include "sort.h"
   2:  
   3: using namespace std ;
   4:  
   5:  
   6: bool checkCipher(const string& s1 , const string& s2)
   7: {
   8:     int len1 = s1.size() ;
   9:     int len2 = s2.size() ;
  10:     if(len1 != len2)
  11:         return false;
  12:  
  13:     int i ;
  14:  
  15:     //A~Z --- 65~90
  16:     int a[26];
  17:     int b[26];
  18:     for( i=0 ; i<26 ; ++i )
  19:         a[i]=b[i]=0 ;
  20:  
  21:     for( i=0 ; i<len1 ; ++i )
  22:     {
  23:         ++a[s1[i]-'A'] ;
  24:         ++b[s2[i]-'A'] ;
  25:     }
  26:  
  27:     QuickSort(a,26);
  28:     QuickSort(b,26);
  29:  
  30:     for( i=0 ; i<26 ; ++i )
  31:     {
  32:         if( a[i]!=b[i] )
  33:             return false ;
  34:     }
  35:  
  36:     return true ;
  37: }
  38:  
  39: void run2159()
  40: {
  41:     //ifstream in("in.txt");
  42:     string s1,s2 ;
  43:     getline(cin,s1);
  44:     getline(cin,s2);
  45:     if(checkCipher(s1,s2)==true)
  46:         cout<<"YES"<<endl;
  47:     else
  48:         cout<<"NO"<<endl;
  49: }
  50:  
  51:  
  52: //**************************
  53:  
  54: bool checkCipher2(const string& s1 , const string& s2)
  55: {
  56:     int len1 = s1.size() ;
  57:     int len2 = s2.size() ;
  58:     if(len1 != len2)
  59:         return false;
  60:  
  61:     int i ;
  62:  
  63:     //A~Z --- 65~90
  64:     int a[26];
  65:     int b[26];
  66:     for( i=0 ; i<26 ; ++i )
  67:         a[i]=b[i]=0 ;
  68:  
  69:     int res[101] ;
  70:     for( i=0 ; i<101 ; ++i )
  71:         res[i]=0 ;
  72:  
  73:     for( i=0 ; i<len1 ; ++i )
  74:     {
  75:         ++a[s1[i]-'A'] ;
  76:         ++b[s2[i]-'A'] ;
  77:     }
  78:  
  79:     for( i=0 ; i<26 ; ++i )
  80:     {
  81:         ++res[a[i]];
  82:         --res[b[i]];
  83:     }
  84:  
  85:     for( i=0 ; i<101 ; ++i )
  86:     {
  87:         if( res[i]!=0 )
  88:             return false ;
  89:     }
  90:  
  91:     return true ;
  92: }
  93:  
  94: void run2159_2()
  95: {
  96:     //ifstream in("in.txt");
  97:     string s1,s2 ;
  98:     getline(cin,s1);
  99:     getline(cin,s2);
 100:     if(checkCipher2(s1,s2)==true)
 101:         cout<<"YES"<<endl;
 102:     else
 103:         cout<<"NO"<<endl;
 104: }
posted @ 2010-11-05 18:23  Allen Sun  阅读(401)  评论(0编辑  收藏  举报