题目链接:https://vjudge.net/problem/HDU-2594

Simpsons’ Hidden Talents

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 10647    Accepted Submission(s): 3722

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
 
Source
 
Recommend
lcy

题解:

给出两个字符串x和y,求出x的最长前缀,使得该前缀也是y的后缀。

1.把字符串y接到字符串x的后面,中间用个特殊符号隔开。

2.求出新字符串的next[]数组,然后next[len]就是满足该条件的x前缀。

代码如下:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <sstream>
#include <algorithm>
using namespace std;
typedef long long LL;
const double eps = 1e-;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = 1e5+; char x[MAXN], y[MAXN];
int Next[MAXN]; void get_next(char x[], int m)
{
int i, j;
j = Next[] = -;
i = ;
while(i<m)
{
while(j!=- && x[i]!=x[j]) j = Next[j];
Next[++i] = ++j;
}
} int main()
{
while(scanf("%s%s", x, y)!=EOF)
{
int n = strlen(x);
int m = strlen(y); x[n] = '$';
for(int i = ; i<m; i++)
x[n++i] = y[i];
x[n++m] = ; int len = n++m;
get_next(x, len);
if(Next[len]==) printf("0\n");
else
{
x[Next[len]] = ;
printf("%s %d\n", x, Next[len]);
}
}
}

HDU2594 Simpsons’ Hidden Talents —— KMP next数组的更多相关文章

  1. hdu2594 Simpsons' Hidden Talents【next数组应用】

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

  2. hdu2594 Simpsons’ Hidden Talents kmp

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

  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

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

  5. hdu 2594 Simpsons’ Hidden Talents KMP应用

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

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

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

  7. hdu2594 Simpsons’ Hidden Talents LCS--扩展KMP

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

  8. kuangbin专题十六 KMP&&扩展KMP HDU2594 Simpsons’ Hidden Talents

    Homer: Marge, I just figured out a way to discover some of the talents we weren’t aware we had. Marg ...

  9. HDU2594——Simpsons’ Hidden Talents

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

随机推荐

  1. windows创建任务计划(周期执行bat脚本)

    https://jingyan.baidu.com/article/ca00d56c767cfae99febcf73.html windows找到任务计划程序: 这台电脑->管理

  2. php生成压缩包

    $filename = "./" . date ( 'YmdH' ) . ".zip"; // 最终生成的文件名(含路径) // 生成文件 $zip = new ...

  3. Python入门--7--处理数据时学习到的东西

    一.数据导入(这里使用的是pands包) import pands as pd wenjian = pd.read_csv('路径') 二.数据变换 print wenjian.head()    # ...

  4. 微信小程序项目结构

  5. SPOJ 26108 TRENDGCD - Trending GCD

    Discription Problem statement is simple. Given A and B you need to calculate S(A,B) . Here, f(n)=n, ...

  6. linux 每天备份mysql数据

    原文:http://www.open-open.com/code/view/1433587311890 前言: 如果你的数据库文件较大,可能备份的时候需要占用很多服务器资源,所以站长要尽量选择访客最少 ...

  7. 23. 客户默认选项(Default Customer Options)

    Editing Email Templates Email Sender Contact Us

  8. insserv: warning: script 'lampp' missing LSB tags and overrides

    https://ubuntuforums.org/showthread.php?t=2327011 1.方法一,编辑rc.loacl脚本 Ubuntu开机之后会执行/etc/rc.local文件中的脚 ...

  9. 推断dxf文件的版本号

    打开DXF參考手冊,在DXF參考手冊中,点击"索引"-->输入"HEADER",在ACADVER字段有acd的版本号信息: 以下是用C语言,写的推断dxf ...

  10. 子组件跟随父组件re-render

    想象一下这种场景,一个父组件下面一大堆子组件.然后呢,这个父组件re-render.是不是下面的子组件都得跟着re-render.可是很多子组件里面是冤枉的啊!!很多子组件的props 和 state ...