KMP & amp; EKMP of hdu4300

Source: Internet
Author: User


Clairewd's message
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission (s): 2398 Accepted Submission (s): 942


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

It is hard to understand the meaning of the question, that is, given two strings, the first group has only 26 tables corresponding to a, B, c, d .... z can be converted to the first and the second... the password contains 26th characters,

The second string is the given ciphertext + plain text. The plain text may be incomplete (missing or absent), so that the complete ciphertext + plain text is the shortest.

If multiple types of plain text exist, the maximum plaintext is obtained.

Analysis: because there may be a variety of plain text and ciphertext> = plain text, the first half of the string (must be ciphertext) is converted into the plain text and the last part is matched, if the part can be matched to the end from a position in the end, it indicates that the part is in plain text, and then the complete output is supplemented. // here, the ciphertext obtained from the key is converted to the key B of the plain text, you do not need to convert plaintext to ciphertext because there may be multiple types of plaintext. The later part is not necessarily plain text. According to B, the ciphertext is converted to plain text (it must be the largest plain text, because the characters in plain text converted into B must be the largest, such as key: aa ...., b: B .....)

EKMP:


[Cpp]
# Include <iostream>
# Include <cstdio>
# Include <cstdlib>
# Include <cstring>
# Include <string>
# Include <queue>
# Include <algorithm>
# Include <map>
# Include <iomanip>
# Define INF 999999999
Using namespace std;
 
Const int MAX = 100000 + 10;
Char s1 [MAX], s2 [MAX], a [27], B [27];
Int next [MAX];
 
Void get_next (char * a, int len ){
Int k = 0, I = 1;
Next [0] = len; // This question does not work
While (k + 1 <len & a [k] = a [k + 1]) ++ k;
Next [1] = k;
K = 1;
While (++ I <len ){
Int maxr = k + next [k]-1;
Next [I] = min (next [I-k], max (maxr-I + 1, 0 ));
While (I + next [I] <len & a [next [I] = a [I + next [I]) ++ next [I];
If (I + next [I]> k + next [k]) k = I;
}
}
 
Int main (){
Int T, k;
Cin> T;
While (T --){
Cin> a> s1;
For (int I = 0; I <26; ++ I) B [a [I]-'a'] = I + 'a ';
Int len = strlen (s1 );
For (int I = 0; I <(len + 1)/2; ++ I) s2 [I] = B [s1 [I]-'a']; // convert the ciphertext to plain text. The ciphertext length is greater than or equal to the plaintext length.
For (int I = (len + 1)/2; I <= len; ++ I) s2 [I] = s1 [I];
Get_next (s2, len );
For (k = (len + 1)/2; k <len; ++ k ){
If (next [k] = len-k) break;
}
Cout <s1;
For (int I = len-k; I <k; ++ I) cout <B [s1 [I]-'a'];
Cout <endl;
}
Return 0;
}

# Include <iostream>
# Include <cstdio>
# Include <cstdlib>
# Include <cstring>
# Include <string>
# Include <queue>
# Include <algorithm>
# Include <map>
# Include <iomanip>
# Define INF 999999999
Using namespace std;

Const int MAX = 100000 + 10;
Char s1 [MAX], s2 [MAX], a [27], B [27];
Int next [MAX];

Void get_next (char * a, int len ){
Int k = 0, I = 1;
Next [0] = len; // This question does not work
While (k + 1 <len & a [k] = a [k + 1]) ++ k;
Next [1] = k;
K = 1;
While (++ I <len ){
Int maxr = k + next [k]-1;
Next [I] = min (next [I-k], max (maxr-I + 1, 0 ));
While (I + next [I] <len & a [next [I] = a [I + next [I]) ++ next [I];
If (I + next [I]> k + next [k]) k = I;
}
}

Int main (){
Int T, k;
Cin> T;
While (T --){
Cin> a> s1;
For (int I = 0; I <26; ++ I) B [a [I]-'a'] = I + 'a ';
Int len = strlen (s1 );
For (int I = 0; I <(len + 1)/2; ++ I) s2 [I] = B [s1 [I]-'a']; // convert the ciphertext to plain text. The ciphertext length is greater than or equal to the plaintext length.
For (int I = (len + 1)/2; I <= len; ++ I) s2 [I] = s1 [I];
Get_next (s2, len );
For (k = (len + 1)/2; k <len; ++ k ){
If (next [k] = len-k) break;
}
Cout <s1;
For (int I = len-k; I <k; ++ I) cout <B [s1 [I]-'a'];
Cout <endl;
}
Return 0;
}
KMP:


[Cpp]
# Include <iostream>
# Include <cstdio>
# Include <cstdlib>
# Include <cstring>
# Include <string>
# Include <queue>
# Include <algorithm>
# Include <map>
# Include <iomanip>
# Define INF 999999999
Using namespace std;
 
Const int MAX = 100000 + 10;
Char s1 [MAX], s2 [MAX], a [27], B [27];
Int next [MAX];
 
Void get_next (char * a, int len ){
Int I =-1, j = 0;
Next [0] =-1;
While (j <len ){
If (I =-1 | a [I] = a [j]) next [++ j] = ++ I;
Else I = next [I];
}
}
 
Int main (){
Int T;
Cin> T;
While (T --){
Cin> a> s1;
For (int I = 0; I <26; ++ I) B [a [I]-'a'] = I + 'a ';
Int len = strlen (s1), k = len;
For (int I = 0; I <(len + 1)/2; ++ I) s2 [I] = B [s1 [I]-'a']; // convert the ciphertext to plain text. The ciphertext length is greater than or equal to the plaintext length.
For (int I = (len + 1)/2; I <= len; ++ I) s2 [I] = s1 [I];
Get_next (s2, len );
While (next [k]> len/2) k = next [k];
Cout <s1;
For (int I = next [k]; I <len-next [k]; ++ I) cout <B [s1 [I]-'a'];
Cout <endl;
}
Return 0;
}

# Include <iostream>
# Include <cstdio>
# Include <cstdlib>
# Include <cstring>
# Include <string>
# Include <queue>
# Include <algorithm>
# Include <map>
# Include <iomanip>
# Define INF 999999999
Using namespace std;

Const int MAX = 100000 + 10;
Char s1 [MAX], s2 [MAX], a [27], B [27];
Int next [MAX];

Void get_next (char * a, int len ){
Int I =-1, j = 0;
Next [0] =-1;
While (j <len ){
If (I =-1 | a [I] = a [j]) next [++ j] = ++ I;
Else I = next [I];
}
}

Int main (){
Int T;
Cin> T;
While (T --){
Cin> a> s1;
For (int I = 0; I <26; ++ I) B [a [I]-'a'] = I + 'a ';
Int len = strlen (s1), k = len;
For (int I = 0; I <(len + 1)/2; ++ I) s2 [I] = B [s1 [I]-'a']; // convert the ciphertext to plain text. The ciphertext length is greater than or equal to the plaintext length.
For (int I = (len + 1)/2; I <= len; ++ I) s2 [I] = s1 [I];
Get_next (s2, len );
While (next [k]> len/2) k = next [k];
Cout <s1;
For (int I = next [k]; I <len-next [k]; ++ I) cout <B [s1 [I]-'a'];
Cout <endl;
}
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.