• hdu 2594 kmp


    Problem Description
    Homer: Marge, I just figured out a way to discover some of the talents we weren’t aware we had.
    Marge: Yeah, what is it?
    Homer: Take me for example. I want to find out if I have a talent in politics, OK?
    Marge: OK.
    Homer: So I take some politician’s name, say Clinton, and try to find the length of the longest prefix
    in Clinton’s name that is a suffix in my name. That’s how close I am to being a politician like Clinton
    Marge: Why on earth choose the longest prefix that is a suffix???
    Homer: Well, our talents are deeply hidden within ourselves, Marge.
    Marge: So how close are you?
    Homer: 0!
    Marge: I’m not surprised.
    Homer: But you know, you must have some real math talent hidden deep in you.
    Marge: How come?
    Homer: Riemann and Marjorie gives 3!!!
    Marge: Who the heck is Riemann?
    Homer: Never mind.
    Write a program that, when given strings s1 and s2, finds the longest prefix of s1 that is a suffix of s2.
     
    Input
    Input consists of two lines. The first line contains s1 and the second line contains s2. You may assume all letters are in lowercase.
     
    Output
    Output consists of a single line that contains the longest string that is a prefix of s1 and a suffix of s2, followed by the length of that prefix. If the longest such string is the empty string, then the output should be 0.
    The lengths of s1 and s2 will be at most 50000.
     
    Sample Input
    clinton homer riemann marjorie
     
    Sample Output
    0 rie 3
     
    题目的意思是求输入两个字符串的最大共工前缀 后缀子串
    这题用kmp的next求解 一开始根本就没有想到这个点  然后理解了下 next数组的意思
    我们next数组跳转的对象便是以跳转位置为终点的一个串的前缀串与后缀串相同的部分

    如图 next【j】对应的跳转位置便是k了

    那么再回过头来看这道题目 题目是要求最大的共用部分对吧  那么 我们可以把两个串拼接起来 然后用next数组的性质不久解决了

    上代码

    #include <map>
    #include <set>
    #include <list>
    #include <cmath>
    #include <queue>
    #include <stack>
    #include <cstdio>
    #include <vector>
    #include <string>
    #include <cctype>
    #include <complex>
    #include <cassert>
    #include <utility>
    #include <cstring>
    #include <cstdlib>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    typedef pair<int,int> PII;
    typedef long long ll;
    #define lson l,m,rt<<1
    #define pi acos(-1.0)
    #define rson m+1,r,rt<<11
    #define All 1,N,1
    #define N 50010
    #define read freopen("in.txt", "r", stdin)
    const ll  INFll = 0x3f3f3f3f3f3f3f3fLL;
    const int INF= 0x7ffffff;
    const int mod =  1000000007;
    char a[N*2],b[N];
    int f[N*2];
    void getnext(int n){
        int i=0,j=-1;
        f[0]=-1;
        while(i<n){
            if(j==-1||a[i]==a[j]){
                i++;
                j++;
                f[i]=j;
            }
            else
                j=f[j];
        }
    }
    void solve(){
        int len1=strlen(a);
        int len2=strlen(b);
        strcat(a,b);
         int tmp=len1+len2;
        getnext(tmp);
        //必须是两串的子串
        while(f[tmp]>len1)tmp=f[tmp];
        while(f[tmp]>len2)tmp=f[tmp];
        if(f[tmp]==0)
            printf("0
    ");
        else{
            for(int i=0;i<f[tmp];++i)
                printf("%c",a[i]);
            printf(" %d
    ",f[tmp]);
        }
    }
    int main()
    {
        while(~scanf("%s%s",a,b)){
            solve();
        }
    return 0;
    }
  • 相关阅读:
    nuxt实践
    安卓H5软键盘遮挡输入框
    h5复制粘贴板,打开APP功能
    MVC3
    MVC3
    C#高编
    接口的显式实现(转)
    E-Retail 框架学习
    C#高编
    实现DIV居中布局三种途径(转)
  • 原文地址:https://www.cnblogs.com/z1141000271/p/5783094.html
Copyright © 2020-2023  润新知