【hdu 2594】Simpsons’ Hidden Talents
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 8010 Accepted Submission(s): 2837
Problem Description
Homer: Marge, I just figured out a way to discover some of the talents we weren’t aware we had.
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.
Input
Input consists of two lines. The first line contains s1 and the second line contains s2. You may assume all letters are in lowercase.
Output
Output 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
【题目链接】:http://acm.hdu.edu.cn/showproblem.php?pid=2594
【题解】
很巧妙的题;
把s1当成匹配串;
s2当成被匹配串;
在s2中匹配s1;
然后做KMP;
可知到了s1的最后一个字符的时候匹配到s2中的第几个字符;
则这些字符的个数就是题目所需要的长度;
(做KMP的时候,每次往移动都是最优的.匹配出来的肯定也是最长的了;)
做KMP的时候始终保持匹配串的前缀即s1[1..j]是和s2[i-j+1..i]相同的;这里i是被匹配串扫描到的位置;
如果i==length(s2)了;
那么s2[i-j+1..i]就是s2的后缀了即s2[len2-j+1..len2]
而在做KMP的时候会让j最大吧?
【完整代码】
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 5e4+10;
string s1,s2;
int l1,l2;
int f[MAXN];
int main()
{
//freopen("F:\\rush.txt","r",stdin);
while (cin >> s1 >> s2)
{
l1 = s1.size(),l2 = s2.size();
s1 = ' '+s1,s2 = ' '+s2;
//s1 zicuan s2 zhucuan
f[1] = f[2] = 1;
for (int i = 2;i <= l1;i++)
{
int t = f[i];
while (t>1 && s1[i]!=s1[t]) t = f[t];
f[i+1] = s1[i]==s1[t]?t+1:1;
}
//kmp
int j = 1;
for (int i = 1;i <= l2;i++)
{
while (j>1 && s2[i]!=s1[j]) j = f[j];
if (s2[i]==s1[j])
j++;
if (j>l1)
{
if (i==l2)
break;
j = f[j];
}
}
if (j==1)
puts("0");
else
{
for (int i = 1;i <= j-1;i++)
putchar(s1[i]);
putchar(' ');
printf("%d\n",j-1);
}
}
return 0;
}
【hdu 2594】Simpsons’ Hidden Talents的更多相关文章
- 【HDU 3746】Simpsons’ Hidden Talents(KMP求循环节)
求next数组,(一般有两种,求循环节用的见代码)求出循环节的长度. #include <cstdio> #define N 100005 int n,next[N]; char s[N] ...
- hdoj 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(辛普森一家的潜在天赋)
HDU 2594 Simpsons’ Hidden Talents(辛普森一家的潜在天赋) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 3 ...
- HDU 2594 Simpsons’ Hidden Talents(KMP的Next数组应用)
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 ...
- 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【next数组应用】
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 ...
随机推荐
- 微信小程序弹框提示绑定手环实例
今天想聊一聊小程序里面存在的一些逻辑问题,拿手上的这个小程序来说,(这个小程序是开发出来玩的,每个人手上有一个手环,带着手环时候的心率,运动步数,血压数据都会展现在这个小程序里面,一目了然)用户第一次 ...
- C#之菜单控件、主窗体打开子窗体、GroupBox控件使用
一.背景 一年前有学习过C#,但没有在项目中去实际做APP,重新捡起来应用到项目中.我同事本来做好一个CANOPEN设备管理的界面,由于近期搜索了别人的开发的界面,我觉得有很多东西要重新安排,以及我已 ...
- vector转数组
vector转数组 由于vector内部的数据是存放在连续的存储空间,vector转数组事实上只需要获取vector中第一个数据的地址和数据的长度即可.如果仅仅是传参,无需任何操作,直接传地址即可,如 ...
- Android触摸事件(五)-CropBitmapActivity关于裁剪工具的使用
文件夹 文件夹 概述 传递数据 裁剪流程 其他事项 使用方式 图片辅助功能 图片缩略图载入 图片旋转 源代码 GitHub地址 演示样例GIF 概述 这个Activity是为了裁剪图片的.使用时须要提 ...
- ES5, ES6, ES2016, ES.Next: JavaScript 的版本是怎么回事?
原网址:http://huangxuan.me/2015/09/22/js-version/ JavaScript 有着很奇怪的命名史. 1995 年,它作为网景浏览器(Netscape Naviga ...
- Codeforces #144 (Div. 1) B. Table (组合数学+dp)
题目链接: B.Table 题意: \(n*m\)的矩阵使每个\(n*n\)矩阵里面准确包含\(k\)个点,问你有多少种放法. \((1 ≤ n ≤ 100; n ≤ m ≤ 10^{18}; 0 ≤ ...
- python3 登录验证小程序,同一用户输错三次密码,锁定账户
''' 让用户输入用户名密码 认证成功后显示欢迎信息用户3次认证失败后,退出程序,再次启动程序尝试登录时,还是锁定状态''' # !/usr/bin/env python # -*- coding:u ...
- 使用PHP实现双向队列
使用PHP实现双向队列 一.总结 就是几个array函数 push pop shift unshift n. 移动:变化:手段:轮班 vi. 移动:转变:转换 vt. 转移:改变:替换 二.使用PHP ...
- (转)利用openfiler实现iSCSI
转自:http://czmmiao.iteye.com/blog/1735417 openfiler openfiler是一个基于浏览器的网络存储管理工具.来自于Linux系统.openfiler在一 ...
- OC学习篇之---Foundation框架中的其他类(NSNumber,NSDate,NSExcetion)
1.NSNumber 这个类主要是用来封装基本类型的,说到这里,就不得不说一下了: OC中的集合是不允许存入基本类型的,所以NSNumber类就诞生了,需要将基本类型封装一下,然后存进去,这个类似于J ...