hdu 2594 Simpsons’ Hidden Talents 【KMP】
题目链接:http://acm.acmcoder.com/showproblem.php?pid=2594
题意:求最长的串 同一时候是s1的前缀又是s2的后缀。输出子串和长度。
思路:kmp
代码:
#include <vector>
#include <string>
#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
char s1[500010],s2[500010];
char ANS[500010];
void get_next(char x[], int m, int Next[])
{
int i, j;
j = Next[0] = -1;
i = 0;
while (i < m)
{
while (-1 != j && x[i] != x[j]) j = Next[j];
Next[++i] = ++j;
}
}
int Next[1001000];
int KMP(char x[], int m, char y[], int n)//x模式串 y主串
{
int i, j;
i = j = 0;
get_next(x, m, Next);
if (n > m) i = n - m;
while (i < n)
{
if (j == -1 || y[i] == x[j])
{
i++;
j++;
}
else j = Next[j];
}
return j;
}
int main()
{
while (cin>>s1>>s2)
{
int len1 = strlen(s1);
int len2 = strlen(s2);
int ans = KMP(s1, len1, s2, len2);
if (ans !=0 )
{
for(int i=0;i<ans;i++) cout<<s1[i];cout<<" ";
}
cout<<ans<<endl;
}
return 0;
}
hdu 2594 Simpsons’ Hidden Talents 【KMP】的更多相关文章
- 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(KMP的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 ...
- HDU 2594 Simpsons’ Hidden Talents (KMP)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2594 这题直接用KMP算法就能够做出来,只是我还尝试了用扩展的kmp,这题用扩展的KMP效率没那么高. ...
- hdu 2594 Simpsons’ Hidden Talents(扩展kmp)
Problem Description Homer: Marge, I just figured out a way to discover some of the talents we weren’ ...
- 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
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 ...
- hdu 2594 Simpsons’ Hidden Talents(KMP入门)
Simpsons’ Hidden Talents Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java ...
随机推荐
- Mapper XML Files详解
扫扫关注"茶爸爸"微信公众号 坚持最初的执着,从不曾有半点懈怠,为优秀而努力,为证明自己而活. Mapper XML Files The true power of MyBatis ...
- Oracle百问百答(二)
Oracle百问百答(二) 11. nvl函数有什么用? NVL( string1, replace_with) 功能:如果string1为NULL,则NVL函数返回replace_with的值,否则 ...
- 开源OCR光学字符识别
纸张在 许多地方已日益失宠,无纸化办公谈论40多年,办公环境正限制纸山的生成.而过去几年,无纸化办公的概念发生了显着的转变.在计算机软件的帮助 下,包含大量重要管理数据和资讯的文档可以更方便的以电子形 ...
- boost库中thread多线程详解2——mutex与lock
1. mutex对象类 mutex类主要有两种:独占式与共享式的互斥量.▲ 独占式互斥量:mutex: 独占式的互斥量,是最简单最常用的一种互斥量类型try_mutex: 它是mutex的同义词,为了 ...
- cairo graphics.org
cairographics.org Latest news: 2013-08-26: cairo 1.12.16 snapshot available 2013-02-10: cairo 1.12.1 ...
- 如何使用不同dll的相同namespace下的相同接口
问题: 程序里加载了2个dll,这2个dll里都声明了同样的命名空间(这个不违法),然后在这个同样的命名空间下,他俩又定义了同名的interface. 然后我程序里直接using这个命名空间,使用这个 ...
- MDK的优化应用
MDK的优化应用 http://blog.163.com/zhaojun_xf/blog/static/300505802011291384721/ 使用Keil/MDK这么多年了,一直都没有使用它的 ...
- C++中实现 time_t, tm 相互转换
time_t -> tm: localtime tm -> time_t: mktime time_t curTime; time(&curTime); dwCurTime = c ...
- “Clang” CFE Internals Manual---中文版---"Clang"C语言前端内部手册
原文地址:http://clang.llvm.org/docs/InternalsManual.html 译者:史宁宁(snsn1984) "Clang"C语言前端内部手册 简介 ...
- SilkTest Q&A 12
111. 谁能告诉我,正在执行的SilkTest的log是存放在哪里? 答案1: 用下面的命令可以导出文本格式的log "c:/program files/segue/silktest/pa ...