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 ...
随机推荐
- 直接使用FileSystem以标准输出格式显示hadoop文件系统中的文件
package com.yoyosys.cebbank.bdap.service.mr; import java.io.IOException; import java.io.InputStream; ...
- html5做的一个激光条
<!DOCTYPE HTML><html lang="zh-cn"><head> <title>CSS3激光加载条</titl ...
- [App Store Connect帮助]二、 添加、编辑和删除用户(1)用户帐户和职能概述
您可以在 App Store Connect 的“用户和访问”部分管理用户.添加沙盒测试员以及管理 API 密钥. 用户职能决定了用户对 App Store Connect 和 Apple Devel ...
- 取消安卓listview,scrollview,gridview滑动时候边缘模糊问题
只需在xml文件里面声明: android:faddingEdge = "none" android:faddingEdgelenth = "0dp" andr ...
- [转]mysql触发器的作用及语法
转自:http://blog.csdn.net/cloudday/article/details/6905590 触发器是一种特殊的存储过程,它在插入,删除或修改特定表中的数据时触发执行,它比数据库本 ...
- VS开发C语言系列(零)-VS2013写C语言错误汇总
错误代码 error C3861:调用函数前未引用 error C4996:调用不安全的函数 error C2668:重载函数不明确 error C3861:"文件名" 找不到标识 ...
- Struts之 拦截器配置 ( Interceptor )
struts2体系结构的核心就是拦截器. 以链式执行,对真正要执行的方法(execute())进行拦截.首先执行Action配置的拦截器,在Action和Result执行之后,拦截器再一次执行(与先前 ...
- String字符串的完美度
题目详情: 我们要给每个字母配一个1-26之间的整数,具体怎么分配由你决定,但不同字母的完美度不同, 而一个字符串的完美度等于它里面所有字母的完美度之和,且不在乎字母大小写,也就是说字母F和f的完美度 ...
- Hive扩展功能(五)--HiveServer2服务高可用
软件环境: linux系统: CentOS6.7 Hadoop版本: 2.6.5 zookeeper版本: 3.4.8 主机配置: 一共m1, m2, m3这五部机, 每部主机的用户名都为centos ...
- CAD在网页中如何得到用户自定义事件的参数?
主要用到函数说明: _DMxDrawX::CustomEventParam 得到用户自定义事件的参数. js代码实现如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ...