hdu2594 Simpsons' Hidden Talents【next数组应用】
Simpsons’ Hidden Talents
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 15015 Accepted Submission(s): 5151
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
题意:
给定字符串$s1$$s2$,在$s1$中找一个前缀和$s2$的后缀匹配的长度最长。
思路:
$next$数组的定义。
所以把$s1$$s2$拼起来求$next$就可以了。需要考虑一下越过他们的边界的情况。
#include<iostream>
//#include<bits/stdc++.h>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
#include<set>
#include<climits>
#include<map>
using namespace std;
typedef long long LL;
typedef unsigned long long ull;
#define pi 3.1415926535
#define inf 0x3f3f3f3f const int maxn = ;
char s1[maxn * ], s2[maxn];
int nxt[maxn * ]; void getnxt(char *s)
{
int len = strlen(s);
nxt[] = -;
int k = -;
int j = ;
while(j < len){
if(k == - || s[j] == s[k]){
++k;++j;
if(s[j] != s[k]){
nxt[j] = k;
}
else{
nxt[j] = nxt[k];
}
}
else{
k = nxt[k];
}
}
} bool kmp(char *s, char *t)
{
getnxt(s);
int slen = strlen(s), tlen = strlen(t);
int i = , j = ;
while(i < slen && j < tlen){
if(j == - || s[i] == t[j]){
j++;
i++;
}
else{
j = nxt[j];
}
}
if(j == tlen){
return true;
}
else{
return false;
}
} int main()
{
while(scanf("%s", s1) != EOF){
//getchar();
scanf("%s", s2);
//cout<<s1<<endl<<s2<<endl;
int len1 = strlen(s1), len2 = strlen(s2);
strcat(s1, s2);
//cout<<s1<<endl;
getnxt(s1);
//cout<<nxt[len1 + len2]<<endl;
if(nxt[len1 + len2] > min(len1, len2)){
for(int i = ; i < min(len1, len2); i++){
printf("%c", s1[i]);
}
printf(" %d\n", min(len1, len2));
}
else{
int ans = nxt[len1 + len2];
if(ans){
for(int i = ; i < ans; i++){
printf("%c", s1[i]);
}
printf(" ");
} printf("%d\n", ans);
}
}
return ;
}
hdu2594 Simpsons' Hidden Talents【next数组应用】的更多相关文章
- HDU2594 Simpsons’ Hidden Talents —— KMP next数组
题目链接:https://vjudge.net/problem/HDU-2594 Simpsons’ Hidden Talents Time Limit: 2000/1000 MS (Java/Oth ...
- HDU2594 Simpsons’ Hidden Talents 【KMP】
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
Problem Description Homer: Marge, I just figured out a way to discover some of the talents we weren’ ...
- 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 字符串哈希
最近在学习字符串的知识,在字符串上我跟大一的时候是没什么区别的,所以恶补了很多基础的算法,今天补了一下字符串哈希,看的是大一新生的课件学的,以前觉得字符串哈希无非就是跟普通的哈希没什么区别,倒也没觉得 ...
- hdu2594 Simpsons’ Hidden Talents
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2594 思路: 其实就是求相同的最长前缀与最长后缀 KMP算法的简单应用: 假设输入的两个字符串分别是s ...
- HDU 2594 Simpsons’ Hidden Talents(KMP的Next数组应用)
Simpsons’ Hidden Talents Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java ...
随机推荐
- sublime text3中文文件名显示为框框,怎么解决
点击Preferences选项——settings { "font_size": 20, "ignored_packages": [ ...
- 以太坊(Ethereum)智能合约NodeJS/Web3 使用
一.概述 运行环境:Node.js.npm.Truffle.Solidity等 root@keke:~/go-ethereum# node -v v8.9.4 root@keke:~/go-ether ...
- [转]oracle 常用的指令
1.显示当前用户名 select user from dual; show user 2.显示当然用户有哪些表 select * from tab; 3.显示当所有用户的表 select * from ...
- SNF快速开发平台MVC-EasyUI3.9之-WebApi身份验证问题解决方案
在我们的整体bs框架当中前端采用的是MVC+WebApi的处理方式.WebApi使用起来确实很方便但也会有新的麻烦事,就是身份验证. 如果没有启用身份认证,那么任何匿名用户只要知道了我们服务的url, ...
- HttpURLConnection与HttpClient比较和使用示例
1. GET请求与POST请求 HTTP协议是现在Internet上使用得最多.最重要的协议了,越来越多的Java应用程序需要直接通过HTTP协议来访问网络资源. 在介绍HttpURLConnecti ...
- [k8s]k8s-ceph-statefulsets-storageclass-nfs 有状态应用布署实践
k8s stateful sets storageclass 有状态应用布署实践v2 Copyright 2017-05-22 xiaogang(172826370@qq.com) 参考 由于网上的文 ...
- Xbox One手柄 + Xbox Wireless Adapter PC无线适配器驱动安装、配对全流程
以下步骤在Windows 7系统中操作.XBox One手柄+无线适配器并非仅只能在Windows 10中使用. 一点感想:微软的XBoxOne手柄实在是好东西,但产品使用说明与文档实在太垃圾,翻遍官 ...
- C++:重载全局new/delete实现跨平台多线程内存检测
Reference: https://blog.csdn.net/u014023615/article/details/39551191 Reference: https://blog.csdn.ne ...
- yum只下载软件不安装的两种方法
1 通过yum自带一个工具:yumdownloader rpm -qa |grep yum-utils yum -y install yum-utils* rpm -ql yum-utils 安装好后 ...
- 机器学习&深度学习基础(目录)
从业这么久了,做了很多项目,一直对机器学习的基础课程鄙视已久,现在回头看来,系统的基础知识整理对我现在思路的整理很有利,写完这个基础篇,开始把AI+cv的也总结完,然后把这么多年做的项目再写好总结. ...