HDU2594 Simpsons’ Hidden Talents —— KMP next数组
题目链接: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
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.
The lengths of s1 and s2 will be at most 50000.
homer
riemann
marjorie
rie 3
题解:
给出两个字符串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数组的更多相关文章
- hdu2594 Simpsons' Hidden Talents【next数组应用】
Simpsons’ Hidden Talents Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java ...
- hdu2594 Simpsons’ Hidden Talents kmp
Simpsons’ Hidden Talents Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- HDU2594 Simpsons’ Hidden Talents 【KMP】
Simpsons' Hidden Talents Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java ...
- hdu 2594 Simpsons’ Hidden Talents KMP
Simpsons’ Hidden Talents Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java ...
- hdu 2594 Simpsons’ Hidden Talents KMP应用
Simpsons’ Hidden Talents Problem Description Write a program that, when given strings s1 and s2, fin ...
- hdu 2594 Simpsons’ Hidden Talents(KMP入门)
Simpsons’ Hidden Talents Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java ...
- 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 ...
- 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 ...
- HDU2594——Simpsons’ Hidden Talents
Problem Description Homer: Marge, I just figured out a way to discover some of the talents we weren’ ...
随机推荐
- git提交之后没有push,代码被覆盖之后恢复
git reflog 通过这个看commit id git reset [commit id] --hard 有时候要删除一个index.lock文件.
- Codevs 2855 游乐园的迷宫
2855 游乐园的迷宫 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 迷宫可是每个游乐园必不可少的项目,菜菜当然是要尝试一下啦. ...
- 降雨量 BZOJ 1067
降雨量 [问题描述] 我们常常会说这样的话:“X年是自Y年以来降雨量最多的”.它的含义是X年的降雨量不超过Y年,且对于任意Y<Z<X,Z年的降雨量严格小于X年.例如2002,2003,20 ...
- 球形空间产生器 BZOJ 1013
球形空间产生器 [问题描述] 有一个球形空间产生器能够在n维空间中产生一个坚硬的球体.现在,你被困在了这个n维球体中,你只知道球面上n+1个点的坐标,你需要以最快的速度确定这个n维球体的球心坐标,以便 ...
- linux的crontab定时任务命令
linux的crontab定时任务命令 cron是一个linux下的定时执行工具. 启动.停止.重启.重新载入配置/sbin/service crond start|stop|restart|relo ...
- Java使用IText(VM模版)导出PDF,IText导出word(二)
===============action=========================== //退款导出word public void exportWordTk() throws IOE ...
- R必学包之dplyr
http://www.360doc.com/content/17/1204/10/50223086_709726679.shtml
- 用canal监控binlog并实现mysql定制同步数据的功能
业务背景 写任何工具都不能脱离实际业务的背景.开始这个项目的时候是因为现有的项目中数据分布太零碎,零零散散的分布在好几个数据库中,没有统一的数据库来收集这些数据.这种情况下想做一个大而全的会员中心系统 ...
- 还在为不停build 烦恼么?看这里~~
如果你是一名开发者,还在为偶尔改一个坐标或者颜色值 就要重新build 好久,然后如果层次深 还要一步步进去看效果么?下面 为大家介绍一个很好的开源库 DYCI 他的github地址,首先下载到本 ...
- leetcode最长递增子序列问题
题目描写叙述: 给定一个数组,删除最少的元素,保证剩下的元素是递增有序的. 分析: 题目的意思是删除最少的元素.保证剩下的元素是递增有序的,事实上换一种方式想,就是寻找最长的递增有序序列.解法有非常多 ...