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. 周末大礼:jQuery技巧总结

    一.简介 1.1.概述 随着WEB2.0及ajax思想在互联网上的快速发展传播,陆续出现了一些优秀的Js框架,其中比较著名的有Prototype.YUI.jQuery.mootools.Bindows ...

  2. Read from socket failed: Connection reset by peer 问题

    [FAILED] 解决方法:#chmod 600 sshd_config ssh_host_dsa_key ssh_host_key ssh_host_rsa_key#chmod 620 moduli ...

  3. ubuntu 16.04 安装pycharm

    Ubuntu16.04下安装Cuda8.0+Caffe+TensorFlow-gpu+Pycharm过程(Simple) ubuntu 16.04 安装pycharm 1.安装java  jdk 直接 ...

  4. win7语音识别开发(sapi)

    参考:http://msdn.microsoft.com/en-us/library/ee125663(v=vs.85).aspx    (sapi5.4 reference) http://msdn ...

  5. 使用Ultra Librarian转换芯片的Altium Designer封装格式

    第一步:找到对应芯片的CAD文件,以OPA350为例: http://www.ti.com/product/opa350   RE: 使用Ultra Librarian转换TI芯片的Altium De ...

  6. python cython 模块(2)

    cython 的主要用途是加速python 代码的执行速度,手段有很多种,最简单的一种就是将变量声明成静态类型: 比如用python 代码写的计算素数的函数,最大计算1000个: def primes ...

  7. 感谢各位亲们的大力支持,免费的HTML5学习课程《HTML5网页开发实例具体解释》连载已经结束了!

    感谢各位亲们的大力支持,免费的HTML5学习课程<HTML5网页开发实例具体解释>连载已经结束了.  有兴趣的读者能够看我的博客,也能够看以下的链接逐个学习: 当里个当.免费的HTML5连 ...

  8. Hasen的linux设备驱动开发学习之旅--时钟

    /** * Author:hasen * 參考 :<linux设备驱动开发具体解释> * 简单介绍:android小菜鸟的linux * 设备驱动开发学习之旅 * 主题:时钟 * Date ...

  9. 将android程序中的数据库导出到SD卡

    private void copyDBToSDcrad() { String DATABASE_NAME = "数据库文件名"; String oldPath = "da ...

  10. can not connect to MySQL server on "10.30.48.153"(13)

    国庆节前好好的程序,完了回来愣是不能跑了! 真是纳闷了,而且邮件别人都发出去了,等于这跟别人一边使用一遍救火一样子了.     查了一下午,一直以为是机器mysql服务的问题,或者是我代码的问题.该找 ...