hdu2594

Simpsons’ Hidden Talents

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 2525    Accepted Submission(s): 960

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

分析:

next[j]=k:下标为j的前k个字符和开头的前k个字符完全一样

对于字符串abcuijkabc的长度为10,即next[10]=3;

所以这道题可以先把两个字符串合并,用next数组的性质可以更高效的求出;

注意:

aaaa和aaa答案是aaaaaa 6不符合题意

在合并的时候在两个字符串之间加上一个特殊字符如“#”

即新字符串为aaaa#aaa,此时的答案为 aaa 3

程序:

#include"stdio.h"
#include"string.h"
#define M 100009
int next[M];
void getNext(char *b)
{
int i,j;
i=0;
j=-1;
next[0]=-1;
while(b[i]!='\0')
{
if(j==-1||b[i]==b[j])
{
i++;
j++;
next[i]=j;
}
else
j=next[j];
}
}
char ch[M],ch1[M];
int main()
{
while(scanf("%s%s",ch,ch1)!=EOF)
{
char str[M];
strcat(ch,"0");
strcat(ch,ch1);
int m=strlen(ch);
getNext(ch);
strncpy(str,ch,next[m]);
str[next[m]]='\0';//注意
if(next[m])
printf("%s %d\n",str,next[m]);
else
printf("0\n");
}
}

KMP的next数组性质运用的更多相关文章

  1. 求最长公共前缀和后缀—基于KMP的next数组

    KMP算法最主要的就是计算next[]算法,但是我们知道next[]求的是当前字符串之前的子字符串的最大前后缀数,但是有的时候我们需要比较字符串中前后缀最大数,比如 LeetCode的shortest ...

  2. 【bzoj2384】[Ceoi2011]Match 特殊匹配条件的KMP+树状数组

    题目描述 给出两个长度分别为n.m的序列A.B,求出B的所有长度为n的连续子序列(子串),满足:序列中第i小的数在序列的Ai位置. 输入 第一行包含两个整数n, m (2≤n≤m≤1000000).  ...

  3. HDU - 4763 Theme Section (KMP的next数组的应用)

    给定一个字符串,求出一个前缀A,使得字符串的构成可以表示成ABABA的形式(B可以为空串). 输出这个前缀的最大长度. KMP算法Next数组的使用. 枚举中间的每个位置,可以根据Next数组求出这个 ...

  4. POJ 2752 KMP中next数组的应用

    题意: 让你从小到大输出给的字符串中既是前缀又是后缀的子串的长度. 思路: 先要了解这个东西: KMP中next数组表示的含义:记录着字符串匹配过程中失配情况下可以向前多跳几个字符,它描述的也是子串的 ...

  5. KMP(next数组的更新理解)Codeforces Round #578 (Div. 2)--Compress Words

    题目链接:https://codeforc.es/contest/1200/problem/E 题意: 有n串字符串,让你连起来:sample please ease in out   ---> ...

  6. UVA 11475 Extend to Palindrome (kmp || manacher || 后缀数组)

    题目链接:点击打开链接 题意:给你一个串,让你在串后面添加尽可能少的字符使得这个串变成回文串. 思路:这题可以kmp,manacher,后缀数组三种方法都可以做,kmp和manacher效率较高,时间 ...

  7. KMP中next数组的理解

    next数组是KMP的核心,但对于next数组我们总是有时候感觉明白了,但有时候又感觉没明白,现在我就说下我自己对KMP中next数组的理解,首先next[i]上的数字的意义,next[i]表示的是当 ...

  8. POJ1961(kmp中Next数组的性质)

    对于某个位置i,i - Next[i]是循环节长度,i整除(i - Next[i])时是完整的几个循环元. ; int n, kase, Next[maxn]; char ch[maxn]; inli ...

  9. KMP算法&next数组总结

    http://www.cnblogs.com/yjiyjige/p/3263858.html KMP算法应该是每一本<数据结构>书都会讲的,算是知名度最高的算法之一了,但很可惜,我大二那年 ...

随机推荐

  1. 基于Dedup的数据打包技术

    基于Dedup的数据打包技术 0.引言    Tar, winrar, winzip是最为常见的数据打包工具软件,它们把文件集体封装成一个单独的数据包,从而方便数据的分布.传输.归档以及持久保存等目的 ...

  2. 多媒体开发之h264的三种字节流格式---annexb 哥伦布/mp4 以及还有一种rtp传输流格式

    ------------------------------------author:pkf ------------------------------------------time:2015-1 ...

  3. sql字符串的拼接 (字符串和二进制,erlang的mysql驱动)

    1> list_to_binary(["select * from aa limit","1",",","97"] ...

  4. jQuery中return false,e.preventDefault(),e.stopPropagation()的区别

    e.stopPropagation()阻止事件冒泡 <head> <title></title> <script src="Scripts/jQue ...

  5. 在工程名.h头文件中写public:

    class CaccessimageApp : public CWinApp { public: _ConnectionPtr m_pConnection; CaccessimageApp(); // ...

  6. Mongodb 与sql 语句对照

    此处用mysql中的sql语句做例子,C# 驱动用的是samus,也就是上文中介绍的第一种. 引入项目MongoDB.dll //创建Mongo连接 var mongo = new Mongo(&qu ...

  7. C#读取Excel日期时间

    //如果为20171219 if (dt.Rows[i][title].ToString().Trim().Length == 8) { realDate = dt.Rows[i][title].To ...

  8. Burp Suite使用教程

    http://www.nxadmin.com/tools/689.html http://tech.idv2.com/2006/08/31/burp-suite/ http://www.securit ...

  9. ScrollView拉到尽头时出现阴影的解决方法

    <code class="hljs markdown has-numbering" style="display: block; padding: 0px; col ...

  10. Putty的安装和使用

     Putty是最简单易用的SSH工具,详细介绍其安装和使用 直接在百度里搜索Putty,可以找到百度软件中心,或者官方网站: PuTTY Download Page.  进入官方网站,直接可执行文件装 ...