HDOJ-2594 Simpsons’ Hidden Talents(KMP)
2016-03-13 20:02
393 查看
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <vector> #include <string> using namespace std; #define maxn 50005 int next1[maxn<<1]; void KMP(string &s) { next1[0] = -1; int j = -1, i = 0; while(i < s.size()) { if(j == -1 || s[i] == s[j]) { i++; j++; next1[i] = j; } else j = next1[j]; } } int main() { // freopen("in.txt", "r", stdin); string s1, s2; while(cin >> s1 >> s2) { s1 += s2; KMP(s1); int t = next1[s1.size()]; if(t == 0) cout << 0; else { while(t > s1.size() - s2.size() || t > s2.size()) t = next1[t]; for(int i = 0; i < t; i++) cout << s1[i]; cout << " " << t; } cout << endl; } return 0; }
相关文章推荐
- Java程序中多线程的优先级设置方法
- int和Integer的区别
- 我的考核项,你的年终奖
- Linux内核启动分析过程
- 安卓学习笔记 1(初学笔记)
- java程序设计基础_陈国君版第五版_第十一章例题
- Linux学习计划
- mysql urlencode 支持中文
- Class.forName()做了什么?
- 机房收费系统VB.NET版——七层登录
- 数据结构笔记-----递归与栈
- C# winform多线程的小例子
- ADO.NET常用几点
- iptables 路由转发, 将一台服务器的IP转发到另一个另一台服务器上面(远程序防护)
- 对比:通过实现Runnable接口和继承thread类来生成多线程
- 用C读取INI配置文件
- 3周《机电传动控制》学习笔记
- Photoshop的评价
- 深入Java虚拟机读书笔记之执行子系统
- Constructing module maps for integrated analysis of heterogeneous biological networks