hdu_2594_求串s1的前缀和串s2后缀最大相同部分
来源:互联网 发布:wow采集助手数据库 编辑:程序博客网 时间:2024/06/03 22:19
Simpsons’ Hidden Talents
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
题意:
求串s1的前缀和串s2后缀最大相同部分
解:
想一下你可以直接把s1作为子串一直匹配到最后
#include<bits/stdc++.h>using namespace std;string st,s;int nxt[55555],n,m;void get(){ int i=0,j=-1; nxt[0]=-1; while(i<m) { if(j==-1||st[i]==st[j]) nxt[++i]=++j; else j=nxt[j]; }}void kmp(){ get(); int i=0,j=0; while(i<n) { if(j==-1||s[i]==st[j]) i++,j++; else j=nxt[j]; } if(!j) puts("0"); else { for(i=0;i<j;i++) printf("%c",st[i]); cout<<" "<<j<<endl; }}int main(){ while(cin>>st>>s) { m=st.length(),n=s.length(); kmp(); } return 0;}
- hdu_2594_求串s1的前缀和串s2后缀最大相同部分
- 转帖-POJ 2774 后缀数组 题目要求:求s1,s2的最大子串
- hdu 2594 kmp水题 求s1的前缀和s2的后缀重复度的最大值
- kmp求前缀和后缀的最大重复部分
- 09给定任意俩组字符串S1和S2,请编程输出他们间的最大相同子串
- 给定任意俩组字符串S1和S2,请编程输出他们间的最大相同子串。
- squeeze(s1,s2),把字符串s1中与s2字符的相同的部分去掉
- HDU 2594 Simpsons’ Hidden Talents(s1的前缀是s2的后缀)
- 动态规划经典题:给出两个字符串s1和s2,返回其中最大的公共子串
- HDU3746 KMP相同公共前缀和公共后缀的最大长度应用及KMP
- 最大子串和之前缀后缀可反转
- char *s1和char s2[]的区别
- poj 2774求两个串的公共前缀 后缀数组
- 40.给字符串s1、s2,在s1中找包含s2里所有字符的最小子串
- 给字符串s1、s2,在s1中找包含s2里所有字符的最小子串
- python编程求字符串s1和s2共同元素
- MySQL填充字符串的函数LPAD(s1,len,s2)和RPAD(s1,len,s2)
- 给定s1,s2,s3,发现是否通过s1和s2的交错形成s3。
- Reverse Linked List II
- 周中训练笔记+POJ2777+2828(9.28)
- 大型网站架构与分布式架构
- idea安装ZK framework plugin
- [Android study note]安装Apache服务器
- hdu_2594_求串s1的前缀和串s2后缀最大相同部分
- ACM周中总结—9月28日
- 初学使用ViewPager组件
- 多线程代码实例
- LCD接口总结
- laravel框架数据迁移、填充(简单示例)--学习笔记
- string 类主要函数操作笔记
- 对数据进行哈希加盐加密
- Stm32的TFT LCD显示器控制学习笔记