HDU 2594 Simpsons 'den den Talents KMP

Source: Internet
Author: User
Tags acos

Question: Give you two strings s1 and s2. Ask you to find a substring t with the maximum length. t is the prefix of s1 and is the suffix of s2. The length of t and t is output, if it does not exist, 0 is output directly.

 

 

1. directly request next


# Include <iostream> # include <cstdio> # include <cstdlib> # include <cmath> # include <cstring> # include <string> # include <vector> # include <list> # include <deque> # include <queue> # include <iterator> # include <stack> # include <map> # include <set> # include <algorithm> # include <cctype> using namespace std; typedef long LL; const int N = 50005; const ll ii = 100000000; const int INF = 0x3f3f3f; const Double PI = acos (-1.0); int next [2 * N], len; char str [2 * N], xh [N]; void getnext (char * p) {int j = 0, k =-1; next [0] =-1; while (j <len) // len is the length of p {if (k =-1 | p [j] = p [k]) {j ++; k ++; next [j] = k;} else k = next [k];} int main () {int I, j, T; while (scanf ("% s", str, xh )! = EOF) {int len1 = strlen (str), len2 = strlen (xh); len = len1 + len2; strcat (str, xh); getnext (str ); while (next [len]> len1 | next [len]> len2) {len = next [len];} str [next [len] = '\ 0 '; if (next [len] = 0) printf ("0 \ n"); else printf ("% s % d \ n", str, next [len]);} return 0 ;} # include <iostream> # include <cstdio> # include <cstdlib> # include <cmath> # include <cstring> # include <string> # include <vector> # include <list> # include <d Eque> # include <queue> # include <iterator> # include <stack> # include <map> # include <set> # include <algorithm> # include <cctype> using namespace std; typedef long LL; const int N = 50005; const ll ii = 100000000; const int INF = 0x3f3f3f; const double PI = acos (-1.0 ); int next [2 * N], len; char str [2 * N], xh [N]; void getnext (char * p) {int j = 0, k =-1; next [0] =-1; while (j <len) // len is the length of p {if (k =-1 | p [j] = p [k]) {j ++; k ++; Next [j] = k;} else k = next [k];} int main () {int I, j, T; while (scanf ("% s", str, xh )! = EOF) {int len1 = strlen (str), len2 = strlen (xh); len = len1 + len2; strcat (str, xh); getnext (str ); while (next [len]> len1 | next [len]> len2) {len = next [len];} str [next [len] = '\ 0 '; if (next [len] = 0) printf ("0 \ n"); else printf ("% s % d \ n", str, next [len]);} return 0 ;}


2. KMP match: Use s1 as the mode string and s2 as the main string, and directly use kmp


#include <cstdio>  #include <cstring>  using namespace std;  const int N = 50002; char str1[N]; char str2[N]; int next[N];  void get_next(int len_1); int kmp_search(int len_1, int len_2);  int main() {     int len;     while(scanf("%s%s", str1, str2) != EOF)     {         int len_1 = strlen(str1);         int len_2 = strlen(str2);         get_next(len_1);         len = kmp_search(len_1, len_2);         if(len == 0)         {             printf("0\n");         }         else         {             for(int i = 0; i < len; i++)             {                 printf("%c", str1[i]);             }             printf(" %d\n", len);         }     }     return 0; }  void get_next(int len_1) {     int i = 0;     int j = -1;     next[i] = -1;     while(i < len_1)     {         if(j == -1 || str1[j] == str1[i])         {             i++;             j++;             if(str1[i] == str1[j])             {                 next[i] = next[j];             }             else             {                 next[i] = j;             }         }         else         {             j = next[j];         }     } }  int kmp_search(int len_1, int len_2) {     int i = 0;     int j = 0;     while(i < len_2)     {         if(j == -1 || str1[j] == str2[i])         {             i++;             j++;         }         else         {             j = next[j];         }     }     if(j == -1)     {         return 0;     }     if(j == 0)     {         if(str1[0] == str2[len_2 - 1])         {             return 1;         }         else         {             return 0;         }     }     else     {         return j;     } } #include <cstdio>#include <cstring>using namespace std;const int N = 50002;char str1[N];char str2[N];int next[N];void get_next(int len_1);int kmp_search(int len_1, int len_2);int main(){    int len;    while(scanf("%s%s", str1, str2) != EOF)    {        int len_1 = strlen(str1);        int len_2 = strlen(str2);        get_next(len_1);        len = kmp_search(len_1, len_2);        if(len == 0)        {            printf("0\n");        }        else        {            for(int i = 0; i < len; i++)            {                printf("%c", str1[i]);            }            printf(" %d\n", len);        }    }    return 0;}void get_next(int len_1){    int i = 0;    int j = -1;    next[i] = -1;    while(i < len_1)    {        if(j == -1 || str1[j] == str1[i])        {            i++;            j++;            if(str1[i] == str1[j])            {                next[i] = next[j];            }            else            {                next[i] = j;            }        }        else        {            j = next[j];        }    }}int kmp_search(int len_1, int len_2){    int i = 0;    int j = 0;    while(i < len_2)    {        if(j == -1 || str1[j] == str2[i])        {            i++;            j++;        }        else        {            j = next[j];        }    }    if(j == -1)    {        return 0;    }    if(j == 0)    {        if(str1[0] == str2[len_2 - 1])        {            return 1;        }        else        {            return 0;        }    }    else    {        return j;    }}

 

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.