题目链接:http://acm.acmcoder.com/showproblem.php?pid=2594

题意:求最长的串 同一时候是s1的前缀又是s2的后缀。输出子串和长度。

思路:kmp

代码:

#include <vector>
#include <string>
#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <string.h> using namespace std; char s1[500010],s2[500010];
char ANS[500010]; void get_next(char x[], int m, int Next[])
{
int i, j;
j = Next[0] = -1;
i = 0;
while (i < m)
{
while (-1 != j && x[i] != x[j]) j = Next[j];
Next[++i] = ++j;
}
} int Next[1001000];
int KMP(char x[], int m, char y[], int n)//x模式串 y主串
{
int i, j;
i = j = 0;
get_next(x, m, Next);
if (n > m) i = n - m; while (i < n)
{
if (j == -1 || y[i] == x[j])
{
i++;
j++;
}
else j = Next[j];
}
return j;
} int main()
{
while (cin>>s1>>s2)
{
int len1 = strlen(s1);
int len2 = strlen(s2);
int ans = KMP(s1, len1, s2, len2);
if (ans !=0 )
{
for(int i=0;i<ans;i++) cout<<s1[i];cout<<" ";
}
cout<<ans<<endl;
}
return 0;
}

hdu 2594 Simpsons’ Hidden Talents 【KMP】的更多相关文章

  1. hdoj 2594 Simpsons’ Hidden Talents 【KMP】【求串的最长公共前缀后缀】

    Simpsons' Hidden Talents Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java ...

  2. HDU 2594 Simpsons’ Hidden Talents(KMP的Next数组应用)

    Simpsons’ Hidden Talents Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java ...

  3. HDU2594 Simpsons’ Hidden Talents 【KMP】

    Simpsons' Hidden Talents Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java ...

  4. HDU 2594 Simpsons’ Hidden Talents (KMP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2594 这题直接用KMP算法就能够做出来,只是我还尝试了用扩展的kmp,这题用扩展的KMP效率没那么高. ...

  5. hdu 2594 Simpsons’ Hidden Talents(扩展kmp)

    Problem Description Homer: Marge, I just figured out a way to discover some of the talents we weren’ ...

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

    HDU 2594 Simpsons’ Hidden Talents(辛普森一家的潜在天赋) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 3 ...

  7. hdu 2594 Simpsons’ Hidden Talents KMP

    Simpsons’ Hidden Talents Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java ...

  8. hdu 2594 Simpsons’ Hidden Talents KMP应用

    Simpsons’ Hidden Talents Problem Description Write a program that, when given strings s1 and s2, fin ...

  9. hdu 2594 Simpsons’ Hidden Talents(KMP入门)

    Simpsons’ Hidden Talents Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java ...

随机推荐

  1. 更好地认知Azure

    编辑人员注释:本文章由 Windows Azure 网站团队项目经理 Erez Benari 撰写. 我们的网站 http://www.windowsazure.com 包含大量信息,并且也在不断添加 ...

  2. LoadRunner 监控 Apache

    配置Apache部分 1.修改Apache中Httpd.conf文件 <Location /server-status> SetHandler server-status Order de ...

  3. Android面试题收集(有具体答案)

    Android面试题目及其答案 1.Android dvm的进程和Linux的进程, 应用程序的进程是否为同一个概念 DVM指dalivk的虚拟机.每个Android应用程序都在它自己的进程中执行,都 ...

  4. spring中bean的一些知识点

    知识点1: 实例化bean的3种方法. 1.      使用类构造器 (90%用这个方法) 2.      使用静态工厂方法 3.      使用实例化工厂 知识点2: 看这段代码 Applicati ...

  5. 关于for循环中i=0与i=arr.length容易被忽视的bug

    for循环中的这两种写法 for(var i=0,len=arr.length;i<len;i++){ } 上面这种是最为常见也是初学者经常写的 而下面这种写法,在性能上则是比上面的更好,然而我 ...

  6. 程序中非action获得spring容器

    java类: public class MySpringContext implements ApplicationContextAware{ private static ApplicationCo ...

  7. SRM 583 Div Level Two:IDNumberVerification

    题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12610 这道题比较有意思,估计是中国人出的吧,以前都不知道身份 ...

  8. Basic4android:多功能的Android应用软件快速开发平台

    Basic4android 是目前最简单.最强大的Android平台快速应用开发工具. ( "Basic4android is the simplest and most powerful ...

  9. crm使用FetchXml聚合查询

    /* 创建者:菜刀居士的博客  * 创建日期:2014年07月08号  */ namespace Net.CRM.FetchXml {     using System;     using Micr ...

  10. How to find configuration file MySQL uses?(转)

    http://www.dbasquare.com/2012/04/01/how-to-find-mysql-configuration-file/ A customer called me today ...