Simpsons’ Hidden Talents HDU - 2594(拓展kmp)

Sample Input

clinton
homer
riemann
marjorie

Sample Output

0
rie 3

看输出才题意。。。拓展kmp特征很明显嘛。。。。注意开始就匹配到尾的情况
#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#define rap(i, a, n) for(int i=a; i<=n; i++)
#define rep(i, a, n) for(int i=a; i<n; i++)
#define lap(i, a, n) for(int i=n; i>=a; i--)
#define lep(i, a, n) for(int i=n; i>a; i--)
#define rd(a) scanf("%d", &a)
#define rlld(a) scanf("%lld", &a)
#define rc(a) scanf("%c", &a)
#define rs(a) scanf("%s", a)
#define MOD 2018
#define LL long long
#define ULL unsigned long long
#define Pair pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define _  ios_base::sync_with_stdio(0),cin.tie(0)
//freopen("1.txt", "r", stdin);
using namespace std;
const int maxn = 50010, INF = 0x7fffffff;
int nex[maxn], ex[maxn];

void get_next(char *s)
{
    int i=0, j, po, len = strlen(s);
    nex[0] = len;
    while(i+1 < len && s[i] == s[i+1])
        i++;
    nex[1] = i;
    po = 1;
    for(int i=2; i<len; i++)
    {
        if(i+nex[i-po] < po + nex[po])
            nex[i] = nex[i-po];
        else
        {
            j = po + nex[po] - i;
            if(j < 0) j = 0;
            while(i + j < len && s[i+j] == s[j])
                j++;
            nex[i] = j;
            po = i;
        }
    }
}

int get_ex(char *s1, char *s2)
{
    int i=0, j, po, len1 = strlen(s1), len2 = strlen(s2);
    get_next(s2);
    while(s1[i] == s2[i] && i < len1 && i < len2)
        i++;
    ex[0] = i;
    if(i == len1)
        return 1;
    po = 0;
    for(int i=1; i<len1; i++)
    {
        if(i + nex[i - po] < po + ex[po])
            ex[i] = nex[i-po];
        else
        {
            j = po + ex[po] - i;
            if(j < 0) j = 0;
            while(i + j < len1 && j < len2 && s1[i+j] == s2[j])
                j++;
            ex[i] = j;
            po = i;
            if(ex[i] == len1 - i)
                return i + 1;
        }
    }
    return 0;
}

char s1[maxn], s2[maxn];
int main()
{
    while(~rs(s1))
    {
        rs(s2);
        int flag = get_ex(s2, s1);
        int len = strlen(s2);
        if(flag)
        {
            rep(i, flag-1, len)
                cout<<s2[i];
            cout<<" ";
            cout<< ex[flag-1] <<endl;
        }
        else
            cout<< "0" <<endl;

    }

    return 0;
}

原文地址:https://www.cnblogs.com/WTSRUVF/p/9475968.html

时间: 2024-10-16 11:38:59

Simpsons’ Hidden Talents HDU - 2594(拓展kmp)的相关文章

(KMP)Simpsons’ Hidden Talents -- hdu -- 2594

http://acm.hdu.edu.cn/showproblem.php?pid=2594 Simpsons’ Hidden Talents Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4756    Accepted Submission(s): 1732 Problem Description Homer: Marge, I j

Simpsons’ Hidden Talents - HDU 2594(求相同的前缀后缀)

题目大意:给你两个字符串,找出一个最大的子串,这个子串要是前面串的前缀并且是后面串的后缀...........   分析:next的简单运用吧,可以把两个串进行合并,中间加一个不能被匹配的字符,然后求出来next就行了.......确实很水 代码如下: ========================================================================================================= #include<stdio.h>

HDU 2594 Simpsons’ Hidden Talents (字符串-KMP)

Simpsons' Hidden Talents 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? M

HDU 2594 Simpsons’ Hidden Talents KMP题解

KMP的应用.直接使用s1产生next 数组,然后在s2中搜索s1,那么记录最后一个搜索到的数值,就是s1的前缀在s2中的最长后缀了. 本题应该不能直接调用strstr了吧. #include <stdio.h> #include <vector> #include <string.h> #include <algorithm> #include <iostream> #include <string> #include <li

HDU 2594 Simpsons’ Hidden Talents (KMP)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2594 这题直接用KMP算法就可以做出来,不过我还尝试了用扩展的kmp,这题用扩展的KMP效率没那么高. KMP算法: #include<stdio.h> #include<iostream> #include<string.h> using namespace std; int next[50001]; char p[50000],s[50000]; void getnex

HDU 2594 Simpsons’ Hidden Talents (KMP)

#include <stdio.h> #include <string.h> int next[50005]; char str1[50005],str2[50005]; void build_next(int len2) { int i=0,j=-1; next[0] = -1; while (i < len2) { if (j==-1 || str2[i] == str2[j]) { i++; j++; if (str2[i] != str2[j]) { next[i]

hdu 2594 Simpsons’ Hidden Talents(KMP入门)

Simpsons’ Hidden Talents Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4543    Accepted Submission(s): 1648 Problem Description Homer: Marge, I just figured out a way to discover some of the t

HDU 2594 Simpsons’ Hidden Talents(辛普森一家的潜在天赋)

HDU 2594 Simpsons’ Hidden Talents(辛普森一家的潜在天赋) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) [Description] [题目描述] Homer: Marge, I just figured out a way to discover some of the talents we weren’t aware we had. Marge:

HDU2594 Simpsons’ Hidden Talents 【KMP】

Simpsons' Hidden Talents Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2798    Accepted Submission(s): 1055 Problem Description Homer: Marge, I just figured out a way to discover some of the