HDU2594(简单KMP)
Simpsons’ Hidden Talents
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4976 Accepted Submission(s): 1815
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.
The lengths of s1 and s2 will be at most 50000.
/*
ID: LinKArftc
PROG: 2594.cpp
LANG: C++
*/ #include <map>
#include <set>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <cstdio>
#include <string>
#include <utility>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define eps 1e-8
#define randin srand((unsigned int)time(NULL))
#define input freopen("input.txt","r",stdin)
#define debug(s) cout << "s = " << s << endl;
#define outstars cout << "*************" << endl;
const double PI = acos(-1.0);
const double e = exp(1.0);
const int inf = 0x3f3f3f3f;
const int INF = 0x7fffffff;
typedef long long ll; const int maxn = ; char str[maxn], str1[maxn];
int Next[maxn]; void getNext(char *p) {
int len = strlen(p);
int i = , j = -;
Next[i] = j;
while (i < len) {
if (j == - || p[i] == p[j]) {
i ++; j ++;
Next[i] = j;
} else j = Next[j];
}
} int main() {
while (~scanf("%s %s", str, str1)) {
int len1 = strlen(str);
int len2 = strlen(str1);
strcat(str, str1);
getNext(str);
int len = strlen(str);
int cnt = Next[len];
while (cnt > len1 || cnt > len2) {
cnt = Next[cnt];
}
if (cnt == ) printf("0\n");
else {
for (int i = ; i < cnt; i ++) printf("%c", str[i]);
printf(" %d\n", cnt);
}
} return ;
}
HDU2594(简单KMP)的更多相关文章
- hdu2594 简单KMP
题意: 给你两个串,问你s1的前缀和s2的后缀最长公共部分是多少. 思路: 根据KMP的匹配形式,我们求出s1的next,然后用s1去匹配s2,输出当匹配到s2的最后一个的时候的匹 ...
- 简单kmp算法(poj3461)
题目简述: 给你两个字符串p和s,求出p在s中出现的次数. 思路简述: 在介绍看BF算法时,终于了解到了大名鼎鼎的KMP算法,结果属于KMP从入门到放弃系列,后来看了几位大神的博客,似乎有点懂了.此题 ...
- HDU 2087 剪花布条 (简单KMP或者暴力)
剪花布条 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- POJ2406简单KMP
题意: 给一个字符串,求最大的前缀循环周期,就是最小的循环节对应的最大的那个周期. 思路: KMP的简单应用,求完next数组后有这样的应用:next[i] :是最大循环节的第几位 ...
- HDU 1358 简单kmp
题目大意: 找到所有的可组成连续字符串相连的位置,和循环字符串的个数 #include <cstdio> #include <cstring> #include <alg ...
- 剪花布条 - HDU 2087(简单KMP | 暴力)
分析:基础的练习............... ============================================================================ ...
- CSU 1598 最长公共前缀 (简单KMP或者暴力)
Submit Page Summary Time Limit: 1 Sec Memory Limit: 128 Mb Submitted: 226 Solved: ...
- POJ 2406 Power Strings 简单KMP模板 strcmp
http://poj.org/problem?id=2406 只是模板,但是有趣的是一个strcmp的字符串比较函数,学习到了... https://baike.baidu.com/item/strc ...
- HDU2594 【KMP】
题意: 给两个字符串s1,s2,求最长的s1前缀匹配s2后缀的字符串,以及长度 思路: 利用KMP看下最终匹配到了哪个位置:一个是利用常规匹配,另一个是利用next数组的跳转. #include< ...
随机推荐
- Sql面试题之四(难度:中等 | 含答案 | 有逻辑题)
Sql面试题之四(难度:中等 | 含答案 | 有逻辑题)
- react实现页面切换动画效果
一.前情概要 注:(我使用的路由是react-router4) 如下图所示,我们需要在页面切换时有一个过渡效果,这样就不会使页面切换显得生硬,用户体验大大提升: but the 问题是 ...
- java 读取配置文件 与更新
笔记 public class Config { private static Properties props = new Properties(); static File configFile ...
- liunx运维必备150个基础命令
经过上次的面试,总结了一下的linux系统常用命令: 命令 功能说明 线上查询及帮助命令(2个) man 查看命令帮助,命令的词典,更复杂的还有info,但不常用. help 查看Linux内置命令的 ...
- LightGBM的算法介绍
LightGBM算法的特别之处 自从微软推出了LightGBM,其在工业界表现的越来越好,很多比赛的Top选手也掏出LightGBM上分.所以,本文介绍下LightGBM的特别之处. LightGBM ...
- 装机、UEFI双系统安装
装机设置 设置默认中图标显示查看-选项-查看-应用到文件夹 控制面板-语言-管理输入法 word-选项-取消输入法设置处于活动状态word-字体-设置默认值 高DPI的显示屏,需要使用125%的缩放, ...
- hibernate延时加载机制
延迟加载: 延迟加载机制是为了避免一些无谓的性能开销而提出来的,所谓延迟加载就是当在真正需要数据的时候,才真正执行数据加载操作.在Hibernate中提供了对实体对象的延迟加载以及对集合的延迟加载,另 ...
- 基于SDN的IP RAN网络虚拟化技术
http://www.zte.com.cn/cndata/magazine/zte_technologies/2014/2014_4/magazine/201404/t20140421_422858. ...
- Python创建目录文件夹
Python对文件的操作还算是方便的,只需要包含os模块进来,使用相关函数即可实现目录的创建. 主要涉及到三个函数 1.os.path.exists(path) 判断一个目录是否存在 2.os.mak ...
- ui-grid表格怎么实现内容居中
这次是思想落后了,只关注怎么使用原生的ui-grid样式来实现这一需求,后来发现可以通过此列的cellTemplate来为列指定内容,从而可以使用css调整样式. ps:其实有时候换种思路,豁然开朗. ...