HDU-4300 Clairewd's message (Extended KMP), hdu-4300clairewd

Source: Internet
Author: User

HDU-4300 Clairewd's message (Extended KMP), hdu-4300clairewd
Clairewd's message

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission (s): 7958 Accepted Submission (s): 2940

Problem Description Clairewd is a member of FBI. after several years concealing in BUPT, she intercepted some important messages and she was preparing for sending it to ykwd. they had agreed that each letter of these messages wocould be transfered to another one according to a conversion table.
Unfortunately, GFW (someone's name, not what you just think about) has detected their action. he also got their conversion table by some unknown methods before. clairewd was so clever and vigilant that when she realized that somebody was monitoring their action, she just stopped transmitting messages.
But GFW knows that Clairewd wowould always firstly send the ciphertext and then plaintext (Note that they won't overlap each other ). but he doesn' t know how to separate the text because he has no idea about the whole message. however, he thinks that recovering the shortest possible text is not a hard task for you.
Now GFW will give you the intercepted text and the conversion table. You shoshould help him work out this problem.
Input The first line contains only one integer T, which is the number of test cases.
Each test case contains two lines. the first line of each test case is the conversion table S. S [I] is the ith latin letter's cryptographic letter. the second line is the intercepted text which has n letters that you should recover. it is possible that the text is complete.

Hint

Range of test data:
T <= 100;
N <= 100000;

Output For each test case, output one line contains the shorest possible complete text.
Sample Input
 

2 abcdefghijklmnopqrstuvwxyz abcdab qwertyuiopasdfghjklzxcvbnm qwertabcde

Sample Output
 

Abcdabcd qwertabcde

The question is a little disgusting. Simply put, Enter 26 letters in the first line, representing abcd... z. The following line is ciphertext + plain text, but the plain text may not be fully displayed. The minimum ciphertext + plain text must be output. idea: Expand KMP. This article is quite good. Then we can store the original string in one array and the translation string in another, and find the extend array of the original string, then we start from len/2 and find the largest extend [I] and it is extend [I] = len-I, the first extend [I] = len-I is the first character of the Ming text.
Code:
# Include
 
  
# Include
  
   
# Include
   
    
# Include
    
     
# Include
     
      
# Include
      
        # Include
       
         # Include
        
          # Include
         # Define mem (a, B) memset (a, B, sizeof (a) # define mod 1000000007 using namespace std; typedef long ll; const int maxn = 1e5 + 5; const double esp = 1e-7; const int ff = 0x3f3f3f3f; int ne [maxn], ex [maxn]; char s [maxn], t [maxn], vis [30]; void get_next (char * s, int len) {ne [0] = len; int a = 0, p = 0; for (int I = 1; I <len; I ++) {if (I> = p | I + ne [I-a]> = p) {if (I> p) p = I; while (p <len & s [p] = s [p-I]) p ++; ne [I] = p-I; a = I ;} elsene [I] = ne [I-a] ;}} void get_extend (char * s, int len1, char * t, int len2) // remember the Board {int a = 0, p = 0; for (int I = 0; I <len1; I ++) {if (I> = p | I + ne [I-a]> = p) {if (I> = p) p = I; while (p <len1 & p-I <len2 & s [p] = t [p-I]) p ++; ex [I] = p-I; a = I;} elseex [I] = ne [I-a] ;}} int main () {int tt; cin> tt; while (tt --) {mem (ne, 0); mem (ex, 0); for (int I = 0; I <26; I ++) {char c; scanf ("% c", & c); vis [c-'a'] = 'A' + I;} scanf ("% s", t ); int len = strlen (t); for (int I = 0; I <len; I ++) s [I] = vis [t [I]-'a']; get_next (s, len); get_extend (t, len, s, len); int ans = len; for (int I = (len + 1)/2; I <len; I ++) {if (ex [I] = len-I) // the first character {ans = I; break;} of the plaintext is found ;}} for (int I = 0; I <ans; I ++) printf ("% c", t [I]); for (int I = 0; I <ans; I ++) printf ("% c", s [I]); printf ("\ 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.