HDU
来源:互联网 发布:淘宝传奇一条龙可靠吗 编辑:程序博客网 时间:2024/06/06 00:34
利用KMP算法匹配一个串T在另一个串S中首次出现的位置,套模板即可。
代码如下:
#include <bits/stdc++.h>using namespace std;const int N = 1000005;int S[N], T[N]; // 下标从0开始int lens, lent;int Next[N]; // 下标从1开始void getNext(){ int i = 0; int k = -1; Next[0] = -1; while (i < lent) if (k == -1 || T[i] == T[k]) Next[++i] = ++k; else k = Next[k];}int KMP_index(){ int i = 0; int j = 0; getNext(); while (i < lens && j < lent) { if (j == -1 || S[i] == T[j]) { i++; j++; } else j = Next[j]; } if (j == lent) return i - lent + 1; // 输出的答案下标从1开始,所以要+1 else return -1;}int main(){ //freopen("test.txt", "r", stdin); //freopen("out.txt", "w", stdout); int t; scanf("%d", &t); while(t--) { scanf("%d%d", &lens, &lent); for (int i = 0; i < lens; i++) scanf("%d", &S[i]); for (int i = 0; i < lent; i++) scanf("%d", &T[i]); printf("%d\n", KMP_index()); } return 0;}
阅读全文
0 0
- hdu
- hdu
- HDU
- hdu ()
- hdu
- hdu
- HDU
- HDU
- hdu
- hdu
- HDU
- Hdu
- hdu
- hdu-
- hdu
- hdu
- hdu
- HDU
- 【poj】 2480 Longge's problem (欧拉函数)
- Gson封装
- Codeforces Round #426 (Div. 2) D. The Bakery(DP+线段树维护)
- 在cmd下运行Python脚本+如何使用Python Shell
- 解决python3 UnicodeEncodeError: 'gbk' codec can't encode character '\xXX' in position XX 标签: pythonco
- HDU
- vim常用操作
- (转)gzib等压缩算法原理详解
- Linux批量删除包含某个关键字的进程
- HP-Socket v5.0.1:支持 IPv6 及多 SSL 证书
- 深圳小组座谈会的优势
- 一些好用的jar
- Python中isinstance和type进行对比
- 《mastering opencv》第一章学习