hdu 1711( 模式串T在主串S中首次出现的位置)
Sample Input
2
13 5
1 2 1 2 3 1 2 3 1 3 2 1 2
1 2 3 1 3
13 5
1 2 1 2 3 1 2 3 1 3 2 1 2
1 2 3 2 1
Sample Output
6
-1
# include <iostream>
# include <cstdio>
# include <cstring>
using namespace std; int Next[];
int S[], T[]; //题目中为数字串
int slen, tlen; void getNext()
{
int j, k;
j = ; k = -; Next[] = -;
while(j < tlen)
if(k == - || T[j] == T[k])
Next[++j] = ++k;
else
k = Next[k]; } /*
返回模式串T在主串S中首次出现的位置
返回的位置是从1开始的。
*/
int KMP_Index()
{
int i = , j = ;
getNext(); while(i < slen && j < tlen)
{
if(j == - || S[i] == T[j])
{
i++; j++;
}
else
j = Next[j];
}
if(j == tlen)
return i - tlen + ;
else
return -;
}
int main()
{ int TT;
int i, cc;
scanf("%d",&TT);
while(TT--)
{
scanf("%d %d" ,&slen , &tlen);
for (i = ;i < slen ; i++)
scanf("%d" , &S[i]) ;
for (i = ;i < tlen ; i++)
scanf("%d" , &T[i]) ; printf("%d\n" , KMP_Index()) ; }
return ;
}
hdu 1711( 模式串T在主串S中首次出现的位置)的更多相关文章
- poj 3461 (模式串T在主串S中出现的次数)
求模式串在主串中出现的次数Sample Input 3BAPCBAPCAZAAZAZAZAVERDIAVERDXIVYERDIANSample Output 130 #include <iost ...
- KMP模版 && KMP求子串在主串出现的次数模版
求取出现的次数 : #include<bits/stdc++.h> ; char mo[maxn], str[maxn];///mo为模式串.str为主串 int next[maxn]; ...
- HDU 1711 Number Sequence(KMP裸题,板子题,有坑点)
Number Sequence Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 2087 剪花布条(模式串在主串中出现的次数主串中子串不可重叠)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2087 题意:求模式串在主串中出现的次数,与模式串匹配的子串之间不可重叠. 思路:用kmp算法解决,在匹 ...
- hdu2457(最少替换多少个字符使主串不包含模式串)ac自动机+dp
题:http://acm.hdu.edu.cn/showproblem.php?pid=2457 题意:给定n个模式串,给定一个主串,问最替换掉多少个字符使主串不包含模式串或输出“-1”表示没有可行的 ...
- POJ 3461 Oulipo(模式串在主串中出现次数 可重叠)
Oulipo [题目链接]Oulipo [题目类型]KMP &题意: 给你两个字符串p和s,求出p在s中出现的次数. &题解: kmpC函数就是解题的,其中也就j=nex[j]难理解一 ...
- hdu3065 病毒侵袭持续中 AC自动机入门题 N(N <= 1000)个长度不大于50的模式串(保证所有的模式串都不相同), 一个长度不大于2000000的待匹配串,求模式串在待匹配串中的出现次数。
/** 题目:hdu3065 病毒侵袭持续中 链接:http://acm.hdu.edu.cn/showproblem.php?pid=3065 题意:N(N <= 1000)个长度不大于50的 ...
- 模板—字符串—KMP(单模式串,单文本串)
模板—字符串—KMP(单模式串,单文本串) Code: #include <cstdio> #include <cstring> #include <algorithm& ...
- 模板—字符串—AC自动机(多模式串,单文本串)
模板—字符串—AC自动机(多模式串,单文本串) Code: #include <queue> #include <cstdio> #include <cstring> ...
随机推荐
- AbstractQueuedSynchronizer同步队列与Condition等待队列协同机制
概要: AQS维护了一个同步队列 Condition是JUC的一个接口,AQS的ConditionObject实现了这个接口,维护了一个等待队列(等待signal信号的队列) 线程调用reentran ...
- Jupyter 魔术命令(magic commands)
自动重新加载更改的模块 命令参数如下所示: %autoreload: 自动重载%aimport排除的模块之外的所有模块. %autoreload 0: 禁用自动重载 %autoreload 1: 自动 ...
- springboot(六)SpringBoot问题汇总
SpringBoot2.0整合Mybatis,取datetime数据类型字段出来时,发现少了8小时. 过程:mysql中注册时间查询出来结果是正确的,只是java程序运行出来后显示少了8小时.经前辈指 ...
- Spring中的Bean配置
IOC&DI概述 OPC(Inversion of Control):其思想是反转资源获取的方向.传统的资源查找方式要求组件向容器发起请求查找资源.作为回应,容器适时的返回资源.而应用了IOC ...
- 前段clam安装
前端模块化协同开发解决方案 —— clam 1. 打开后直接看最后一条https://blog.csdn.net/zhangwenwu2/article/details/581720422. node ...
- JavaScript之函数存储[摘]
引用文献:js忍者秘籍 var store = {//demo:函数存储----存储一组独立的函数 nextId:1, cache:{}, add:function(fn){ if(!fn.id){ ...
- python线程之condition
cond = threading.Condition() # 类似lock.acquire() cond.acquire() # 类似lock.release() cond.release() # 等 ...
- oracle_数据库对象
- mysql基本命令[转]
1.连接Mysql 格式: mysql -h主机地址 -u用户名 -p用户密码 1.连接到本机上的MYSQL.首先打开DOS窗口,然后进入目录mysql\bin,再键入命令mysql -u root ...
- Jetson tk1 刷机后要做的几件事
参考简书文章: http://www.jianshu.com/p/997ede860d74 1. 查看Jetson TK1 L4T版本 head -n 1 /etc/nv_tegra_release ...