During the competition, I had to read the question for a long time...
The algorithm is wrong twice:
Error Algorithm:
1. Use binary to find the ciphertext length ..
2. I used the next function only once to find the longest prefix to match the suffix, ignoring its possible intersection. This is not allowed in this question ..
Correct algorithm:
After so many twists and turns, I finally got back to the right path. I can solve it by asking for a KMP.
Use the second half of the intercepted text as the main string and the first half as the pattern string.
Use KMP to obtain the maximum length of the matched primary string.
View code
# Include <stdio. h> # include <string. h> # include <stdlib. h> char STR [101000]; char st [101000]; char temp1 [101000]; char temp2 [101000]; char hash [101000]; int next [101000]; void get_next () {int Len = strlen (ST + 1); int I = 1, j = 0; next [1] = 0; while (I <Len) {If (ST [I] = sT [J] | j = 0) {++ I, ++ J; next [I] = J ;} else J = next [J] ;}// record the maximum number of matching characters int KMP (char * a, char * B) {int len1 = strlen (a + 1); int len2 = strlen (B + 1); int I = 1, j = 1; while (I <= len1 & J <= len2) {if (a [I] = B [J] | j = 0) {if (I = len1) return J; ++ I, ++ J;} else {J = next [J] ;}} return 0 ;} void Hash () {int Len = strlen (STR + 1); int num = 0; For (INT I = 1; I <= Len; I ++) {hash [STR [I]-'a'] = 'A' + num ++ ;}} int main () {int N; scanf ("% d ", & N); While (n --) {scanf ("% S % s", STR + 1, ST + 1); Hash (); memset (next, 0, sizeof (next); get_next (); int Len = strlen (ST + 1); For (INT I = 1; I <= Len/2 + 1; I ++) temp1 [I] = hash [st [I]-'a']; temp1 [Len + 1] = '\ 0';/* int n = 1; if (LEN = 1) {printf ("% C \ n", St [1], hash [st [1]-'a']); continue ;} for (INT I = (LEN + 1)/2; I <= Len; I ++) temp2 [n ++] = sT [I]; temp2 [N] = '\ 0'; */strcpy (temp2 + 1, ST + (LEN + 1)/2 + 1); int n = KMP (temp2, temp1); printf ("% s", ST + 1); For (INT I = n + 1; I <= len-N; I ++) {printf ("% C", hash [st [I]-'a']);} puts ("") ;}return 0 ;}