J - Simpsons’ Hidden Talents
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.
InputInput consists of two lines. The first line contains s1 and the second line contains s2. You may assume all letters are in lowercase.OutputOutput 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
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<vector>
#include <sstream>
#include<string>
#include<cstring>
#include<list>
using namespace std;
#define MAXN 51000
#define INF 0x3f3f3f3f
typedef long long LL;
/*
求两个串的最长相同前缀后缀匹配
那么可以将两个串连接起来用求Next数组的方法找出所有匹配,选可行(小于两个串长度的)的最大值
*/
char a[MAXN*],b[MAXN*];
int Next[MAXN*];
void kmp_pre(char t[])
{
int m = strlen(t);
int j,k;
j = ;k = Next[] = -;
while(j<m)
{
if(k==-||t[j]==t[k])
Next[++j] = ++k;
else
k = Next[k];
}
}
int main()
{
while(scanf("%s%s",a,b)!=EOF)
{
int l1 = strlen(a),l2 = strlen(b),L = l1+l2;
stringstream ss;
ss<<a<<b;
ss>>a;
kmp_pre(a);
int ans = Next[L],k = L;
if(ans>l1||ans>l2)
ans = min(l1,l2);
if(ans>)
{
for(int i=;i<ans;i++)
printf("%c",a[i]);
printf(" %d\n",ans);
}
else
printf("0\n");
}
return ;
}
J - Simpsons’ Hidden Talents的更多相关文章
- 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的Next数组应用)
Simpsons’ Hidden Talents Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java ...
- HDU 2594 Simpsons’ Hidden Talents(辛普森一家的潜在天赋)
HDU 2594 Simpsons’ Hidden Talents(辛普森一家的潜在天赋) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 3 ...
- hduoj------2594 Simpsons’ Hidden Talents
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 ...
- hdu 2594 Simpsons’ Hidden Talents KMP应用
Simpsons’ Hidden Talents Problem Description Write a program that, when given strings s1 and s2, fin ...
- hdoj 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【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 ...
随机推荐
- 数据库登陆失败原因: 未与信任 SQL Server 连接相关联
解决方案:用户 'sa' 登录失败.原因: 未与信任 SQL Server 连接相关联. 问题简述: 用户 'sa' 登录失败.原因: 未与信任 SQL Server 连接相关联. 说明: 执行当前 ...
- Redis学习和应用记录(2)--常用数据类型及命令
这一节主要介绍Redis支持的数据结构及常用命令. 数据类型 Redis支持多种数据类型的存储,包括字符,列表,集合,有续集合,哈希表,bit数组,超级日志等.下面分别介绍: strings:存储普通 ...
- PCB MS SQL 标量函数(CLR) 实现DataTable转Json方法
一.准备需转为json字符串的DataTable数据 在数据库中执行一段SQL返回的数据 需转换后的JSON字符串的效果 [{"TechName":"开料",& ...
- HDU4340 Capturing a country DP
自己原来写的两个维度的DP有错,看了半天这个大牛的blog.http://blog.csdn.net/cyberzhg/article/details/7840922 题意:A军队和B军队要一起占领一 ...
- C#模拟百度登录并到指定网站评论回帖(一)
核心信息: 请求网址: https://passport.baidu.com/v2/api/?login请求方法: POST状态码: HTTP/1.1 200 OK请求头 //用户代理 Use ...
- jQuery :even
此选择器匹配所有索引值为偶数的元素,从0开始计数. jQuery1.0版本添加. 语法结构: jQuery( ":even" ) 代码实例: <!doctype html&g ...
- centos7下手动制作trove镜像
获取镜像 [root@bldattet1 ~]# wget http://mirrors.aliyun.com/centos/7.5.1804/isos/x86_64/CentOS-7-x86_64 ...
- CSS——盒子居中显示
嵌套中个的子盒子使用了绝对定位,父盒子使用了相对定位.那么子盒子如何居中显示: 1.距离左偏离50% 2.margin-right子盒子宽度的一半 <!DOCTYPE html> < ...
- [Windows Server 2008] 安装IIS7.5及FTP
★ 欢迎来到[护卫神·V课堂],网站地址:http://v.huweishen.com★ 护卫神·V课堂 是护卫神旗下专业提供服务器教学视频的网站,每周更新视频.★ 本节我们将带领大家:安装IISII ...
- haproxy故障处理
1. haproxy 在配置健康检查的时候,默认没有配置页面检查 ,通过端口状态来检测.后端IIS web服务开始可能 是一个站点,或者采用了基于域名的配置方式,导致目前站点停了,后端主机不能被hap ...