HDU 4300 Clairewd's message KMP

Source: Internet
Author: User

The question is too difficult to understand.

The first line gives you a piece of ciphertext with 26 letters. The corresponding plaintext is from a-z.

The second line is followed by a plaintext string. The ciphertext must be complete, but either or not.

Ask for the shortest ciphertext + plaintext.

 


Example 1: abcdab

Shortest ciphertext: abcd, which corresponds to abcd.

Therefore, the shortest ciphertext + plaintext is abcdabcd.

Example 2: qwertabcde

Shortest ciphertext: qwert, whose plaintext is abcde


Therefore, the shortest ciphertext + plaintext is qwertabcde.


Difficult to understand.

 


Idea: the plaintext length must be smaller than or equal to len/2, and then match the later half with the corresponding plaintext

For example, in the first example, the maximum matching of dab and abcdab is t = 2, indicating that two are already in plain text, and the output from t to len-t is not displayed in plain text,

 


AC code:

 

# Include <iostream> # include <cstdio> # include <cstring> # include <string> # include <cstdlib> # include <cmath> # include <vector> # include <list> # include <deque> # include <queue> # include <iterator> # include <stack> # include <map> # include <set> # include <algorithm> # include <cctype> using namespace std; typedef _ int64 LL; const int N = 100090; const ll ii = 1000000007; const int INF = 0x3f3f3f3f; const double PI = acos (-1.0); char word [N], yi [26], mi [26], xh [N]; int wlen, next [N]; void getnext (char * p) {int j = 0, k =-1; next [0] =-1; while (j <wlen) {if (k =-1 | p [j] = p [k]) {j ++; k ++; next [j] = k;} else k = next [k] ;}} int kmp (char * text, char * word) {int I = 0, j = 0, tlen = strlen (text); while (I <tlen) {if (j =-1 | text [I] = word [j]) j ++, I ++; else j = next [j];} return j; // return the maximum number of overlapped prefixes between the suffix of the parent string and the pattern string} int main () {int I, j, T; scanf ("% d ", & T); while (T --) {scanf ("% s", yi, word); printf ("% s", word); for (I = 0; I <26; I ++) mi [yi [I]-'a'] = I + 'a'; // plaintext wlen = strlen (word) corresponding to the ciphertext ); strcpy (xh, word + (wlen + 1)/2); for (I = 0; I <wlen; I ++) word [I] = mi [word [I]-'a']; getnext (word); int t = kmp (xh, word ); // int p = wlen-t where the plain text overlaps; // the word [p] To be output = '\ 0'; printf ("% s \ n ", word + t); // from t to p} return 0 ;} # include <iostream> # include <cstdio> # include <cstring> # include <string> # include <cstdlib> # include <cmath> # include <vector> # include <list> # include <deque> # include <queue> # include <iterator> # include <stack> # include <map> # include <set> # include <algorithm> # include <cctype> using namespace std; typedef _ int64 LL; const int N = 100090; const ll ii = 1000000007; const int INF = 0x3f3f3f3f; const double PI = acos (-1.0); char word [N], yi [26], mi [26], xh [N]; int wlen, next [N]; void getnext (char * p) {int j = 0, k =-1; next [0] =-1; while (j <wlen) {if (k =-1 | p [j] = p [k]) {j ++; k ++; next [j] = k;} else k = next [k] ;}} int kmp (char * text, char * word) {int I = 0, j = 0, tlen = strlen (text); while (I <tlen) {if (j =-1 | text [I] = word [j]) j ++, I ++; else j = next [j];} return j; // return the maximum number of overlapped prefixes between the suffix of the parent string and the pattern string} int main () {int I, j, T; scanf ("% d ", & T); while (T --) {scanf ("% s", yi, word); printf ("% s", word); for (I = 0; I <26; I ++) mi [yi [I]-'a'] = I + 'a'; // plaintext wlen = strlen (word) corresponding to the ciphertext ); strcpy (xh, word + (wlen + 1)/2); for (I = 0; I <wlen; I ++) word [I] = mi [word [I]-'a']; getnext (word); int t = kmp (xh, word ); // int p = wlen-t where the plain text overlaps; // the word [p] To be output = '\ 0'; printf ("% s \ n ", word + t); // from t to p} 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.