Acdream 1683 (KMP)

Source: Internet
Author: User

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)

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.