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
思路:连接连个字符串求next数组。注意next数组的值不能超过len1或者len2
#include <iostream>
#include<string.h>
using namespace std;
const int maxn = ,maxn2=*maxn;
char a[maxn],b[maxn],c[maxn2];
int ne[maxn2];
int main()
{
while(scanf("%s",a+)!=EOF)
{
scanf("%s",b+);
int len1=strlen(a+),len2=strlen(b+);
int num=;
for(int i=;i<=len1;i++)
{
c[num]=a[i];
num++;
}
for(int i=;i<=len2;i++)
{
c[num]=b[i];
num++;
}
num--;
for(int i=,j=;i<=num;i++)
{
while(j&&c[i]!=c[j+]) j=ne[j];
if(c[i]==c[j+]) j++;
ne[i]=j;
}
if(ne[num]==)
printf("0\n");
else
{
int ans=ne[num];
if(ans>len1) ans=len1;
if(ans>len2) ans=len2;
for(int i=;i<=ans;i++)
printf("%c",a[i]);
printf(" %d\n",ans);
}
}
return ;
}
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 ...
- (KMP)Simpsons’ Hidden Talents -- hdu -- 2594
http://acm.hdu.edu.cn/showproblem.php?pid=2594 Simpsons’ Hidden Talents Time Limit: 2000/1000 MS (Ja ...
随机推荐
- xgboost调参过程
from http://blog.csdn.net/han_xiaoyang/article/details/52665396
- SQLite为何要用C语言来开发?
SQLite 选择 C 语言的理由是?为什么不选择 Go 或者 Rust? C 语言是最好的 SQLite 在 2000 年 5 月 29 日发布,并一直使用 C 语言实现.C 语言一直是实现 SQL ...
- NOIP2018 D1T3赛道修建
题目链接:Click here Solution: 最小值最大,考虑二分一个答案\(k\) 考虑在子树内先匹配,最后传递一个值给自己的父亲(因为每条边只能用一次,所以一颗子树最多传递一个值) 那么我们 ...
- Quartz监听器
1.概念Quartz的监听器用于当任务调度中你所关注事件发生时,能够及时获取这一事件的通知.类似于任务执行过程中的邮件.短信类的提醒.Quartz监听器主要有JobListener.TriggerLi ...
- Oracle数据库表空间查看和更改
set linesize 200 --设置输出一行字符个数为200 1.查看表空间名称和大小(单位MB) SELECT t.tablespace_name, round(SUM(bytes / (1 ...
- springboot学习日志(二)-- thymeleaf学习
本次学习如何使用thymeleaf以及相关语法1.在上一章写的那样 引入jar包到maven工程 <dependency> <groupId>org.springframewo ...
- Oracle开发:创建一个用户并分配表空间和分配权限
-- 创建一个用户并分配表空间和分配权限 -- 以sysdba登录 oracle@sha-col-oracle-2:~> sqlplus / as sysdba SQL*Plus: Releas ...
- nodejs 简单的搭建一个服务器
前言: nodejs 主要是后台语言 node 是在终端运行的,所以他可以进行 dos 命令 模块 自定义 JavaScript 是前台语言 nodejs 是 也是使用我们的 JavaScript ...
- C语言字符数组详解
字符串的存储方式有字符数组和字符指针,我们先来看看字符数组. 因为字符串是由多个字符组成的序列,所以要想存储一个字符串,可以先把它拆成一个个字符,然后分别对这些字符进行存储,即通过字符数组存储.字符数 ...
- Leon-ai on WSL
这几天尝试了下Windows10的WSL,的确是良心产品,虽然还有很多待改进的地方(比如iptabeles之类的功能还未支持). 使用了WSL就避免安装双系统的繁琐,节省很多时间,而且因为是微软自家产 ...