Test instructions: There are two couples, the names are A and b two strings, and then give the child a name, the name can be the father's name prefix and the Mother name suffix equal part, ask how many names can be given to the child.
Puzzle: KMP in the Next[i] array record the first I character prefix and suffix equal length of how much, then you can use this feature, first A and B stitching up the total length is Len, and then get next array, from Next[len] forward to find, If there is a string in the same prefix as the suffix, there is an equal string of strings, until next[i] = 0, and the result is added 1 because the strings of A and B are also eligible.
#include <stdio.h> #include <string.h>char str[200005];int next[200005], len;void getnext () {int pp =-1, k = 0 ; Next[0] = -1;while (k < len) {if (pp = =-1 | | str[pp] = str[k]) {k++;pp ++;next[k] = pp;} Elsepp = next[pp];}} int solve (int x) {if (x = = 0) return 0;return 1 + solve (next[x]);} int main () {int t;scanf ("%d", &t), while (t--) {memset (next, 0, sizeof (next)), scanf ("%s", str), scanf ("%s", str + strlen (str)); len = strlen (str), GetNext ();p rintf ("%d\n", Solve (Next[len]) + 1);} return 0;}
Acdream 1683 (KMP)