[HDU] 1711 Number Sequence KMP
2015-08-03 13:51
501 查看
http://acm.hdu.edu.cn/showproblem.php?pid=1711
题意:很裸的KMP,只不过把字符换成数字了。
思路:KMP
题意:很裸的KMP,只不过把字符换成数字了。
思路:KMP
[code]#include <cstdio> #include <cstring> #include <iostream> using namespace std; const int maxn = 1000010; int n, m; int Next[maxn]; int arg[maxn],str[10010]; void GetNext() { Next[0] = -1; int i = 0, k = -1; while(i < m){ if(k == -1 || str[i] == str[k]){ i++,k++; Next[i] = k; } else{ k = Next[k]; } } } int Kmp() { int i = 0, k = 0; while(i < n){ if(k == -1 || arg[i] == str[k]){ i++,k++; if(k == m) return i-m+1; } else{ k = Next[k]; } } return -1; } int main() { int Test; cin>>Test; while(cin>>n>>m) { for(int i = 0; i < n; i++){ cin>>arg[i]; } for(int i = 0; i < m; i++){ cin>>str[i]; } GetNext(); cout<<Kmp()<<endl; } return 0; }
相关文章推荐
- 修改virtualbox的UUID,使virtualbox的虚拟磁盘可以复制
- 文字效果和表情图片:JAVA正则表达式 Pattern和Matcher、SpannableString与SpannableStringBuilder
- Robotium Instrumentation run failed due to 'java.lang.ClassNotFoundException'
- The type java.lang.String cannot be resolved. It is indirectly referenced from required .class files
- UITextField(输入框)
- Fixing common issues when hosting a .NET 4.0 WCF service in IIS 7
- 北大ACM1503——Integer Inquiry
- 前端UI框架
- Havok_2014-1-0_Pc_Xs_User_Guide(目录)
- apue 第十六章 套接字
- EQueue - 一个纯C#写的分布式消息队列介绍2
- NGUI初探之UIRoot
- DDS杂散频谱来源:谐波超Nyquist 折返
- EQueue - 详细谈一下消息持久化以及消息堆积的设计
- UE4与WEB服务器交互(json)
- CUint
- EQueue - 一个C#写的开源分布式消息队列的总体介绍
- [UE4.4.x] 虚幻4 安卓打包
- php array_unique优化
- UGUI实现街机摇杆及技能判定