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. ioctl参数cmd=2错误

    在写内核驱动的时候,用到了ioctl.自己定义cmd作为ioctl的参数.如下: enum CMD { LEVEL_DOWN, LEVEL_UP, GPIO_INPUT, GPIO_OUTPUT, G ...

  2. unity3d绘画手册-------地形各个参数解释

    关于Unity3D是什么.我就不多做解释了.由于工作原因,该系列原创教程不定期更新.每月必然有更新.谢谢各位. Unity地形:: 新建地形: <ignore_js_op> 如图在菜单中新 ...

  3. R read.table函数的check.names参数

    今天用cummeRbund 对cuffdiff的结果进行可视化, 一直报错,之前跑的好好的,找了半天原因, 原来出现在read.table这个函数上: read.table有一个参数check.nam ...

  4. mongodb group php 操作

    紧接着上篇来,这篇主要讲,mongodb的group功能,做的还是挺强大的,相当对于find(),skip(),distinct()等,用法比较复杂. 测试数据 > db.fruit.find( ...

  5. 3th January 2014

    I owe my girl so much, i want to pay her, i know this is impossible,but I still try hard.

  6. 第四章 Spring.Net 如何管理您的类___让对象了解自己的容器

    我们在开发中,经常需要让对象了解自己所在的容器的信息,例如,有时我们需要让对象知道,对象所在容器的引用是什么,或者是对象在容器中的名称是什么 .Spring.Net 中提供了两个接口,我们使用这两个接 ...

  7. JavaScript------字符串与HTML格式相互转换

    转载: http://blog.sina.com.cn/s/blog_4cb0b0fc0100aoo1.html 代码:: 1.将字符转换成Html function encodeHtml(str){ ...

  8. 7 -- Spring的基本用法 -- 3... Spring 的核心机制 : 依赖注入

    7.3 Spring 的核心机制 : 依赖注入 Spring 框架的核心功能有两个. Spring容器作为超级大工厂,负责创建.管理所有的Java对象,这些Java对象被称为Bean. Spring容 ...

  9. ssh&scp指定密钥

    scp时指定密钥: scp -P22   -r -i ~/.ssh/dongjing-shanghai.pem root@kiri_pro01:/data/backup/back_from_japan ...

  10. nagios监控mysql

    在nagios上部署check_mysql_health 监控mysql 博客分类: 架构   本监控为基于nagios服务器主动监控方法,利用check_mysql_health实现多种监控模式:  ...