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. 用实现ajax读博客rss示例代码

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xht ...

  2. 回调方法介绍之中国好室友篇(Java示例)

    前言 在Java社区的各种开源工具中,回调方法的使用俯拾即是.所以熟悉回调方法无疑能加速自己对开源轮子的掌握.网上搜了一些文章,奈何对回调方法的介绍大多只停留在什么是回调方法的程度上.本篇文章尝试从回 ...

  3. e680. 使三元色图像变明变暗

    This example demonstrates how to brighten or darken an RGB buffered image by scaling the red, green, ...

  4. RTC终于tm的通了

     ITDS(1316336566) 2014-1-16 10:34:36我们板子上用的是pcf8563默认没使用这个,用图形界面选择下这个完以后,在配置下就这两步骤ITDS(1316336566) 2 ...

  5. (转)Linux下/etc/rc.local与/etc/init.d的区别与联系

    Linux下/etc/rc.local与/etc/init.d的区别与联系 2012-10-13 20:14:52|  分类: Linux学习|字号 订阅     1./etc/rc.local 这是 ...

  6. R语言中两个数组(或向量)的外积怎样计算

    所谓数组(或向量)a和b的外积,指的是a的每个元素和b的每个元素搭配在一起相乘得到的新元素.当然运算规则也可自己定义.外积运算符为 %o%(注意:百分号中间的字母是小写的字母o).比如: > a ...

  7. Unity使用JsonFX插件进行序列化

    孙广东  2015.6.25 Unity and JSON – Quick Guide: 相比較XML的沉重和密集,Json更加高效. Introduction: 什么是 Json ?假设你从未使用过 ...

  8. C语言简单选择排序

    #include <stdio.h> int main(int argc, char const *argv[]) { // 将数组按照从小到大排序 , , , , , }; int i, ...

  9. 【Java集合的详细研究7】Set和List 的关系与区别

    两个接口都是继承自Collection. List (inteface) 次序是List 的最重要特点,它确保维护元素特定的顺序. --ArrayList 允许对元素快速随机访问. --LinkedL ...

  10. struts2零配置參考演示样例

    <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2 ...