hdu-1711(kmp算法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711
思路:kmp模板,注意用scanf,不然超时。
- #include<iostream>
- #include<cstdio>
- #include<cstring>
- using namespace std;
- const int maxn = ;
- int a[maxn],b[maxn],p[maxn],n,m;
- void pre()
- {
- int i,j;
- for(i=,j=;i<m;i++)
- {
- while(j>&&b[i+]!=b[j+]) j=p[j];
- if(b[i+]==b[j+]) j++;
- p[i+]=j;
- }
- }
- int kmp()
- {
- int i,j;
- for(i=,j=;i<n;i++)
- {
- while(j>&&a[i+]!=b[j+]) j=p[j];
- if(a[i+]==b[j+]) j++;
- if(j==m)
- {
- return i-j+;
- }
- }
- return -;
- }
- int main(void)
- {
- int t,i,j;
- scanf("%d",&t);
- while(t--)
- {
- scanf("%d%d",&n,&m);
- memset(p,,sizeof(p));
- for(i=;i<=n;i++) scanf("%d",&a[i]);
- for(i=;i<=m;i++) scanf("%d",&b[i]);
- pre();
- printf("%d\n",kmp());
- }
- return ;
- }
hdu-1711(kmp算法)的更多相关文章
- hdu 1711 KMP算法模板题
题意:给你两个串,问你第二个串是从第一个串的什么位置開始全然匹配的? kmp裸题,复杂度O(n+m). 当一个字符串以0为起始下标时.next[i]能够描写叙述为"不为自身的最大首尾反复子串 ...
- hdu 1686 KMP算法
题意: 求子串w在T中出现的次数. kmp算法详解:http://www.cnblogs.com/XDJjy/p/3871045.html #include <iostream> #inc ...
- hdu 4300 kmp算法扩展
Clairewd’s message Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- HDU 1711 kmp+离散化
http://acm.hdu.edu.cn/showproblem.php?pid=1711 Number Sequence Time Limit: 10000/5000 MS (Java/Other ...
- Number Sequence HDU 1711 KMP 模板
题目大意:两个数组匹配,求子串首次出现的位置. 题目思路:数组长度,比较大,朴素算法的时间复杂度为 m*n超时.KMP的时间复杂度为m+n可行. #include<iostream> #i ...
- hdu 3613 KMP算法扩展
Best Reward Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total ...
- HDU 2594 kmp算法变形
Simpsons’ Hidden Talents Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java ...
- HDU 1711 Number Sequence (字符串匹配,KMP算法)
HDU 1711 Number Sequence (字符串匹配,KMP算法) Description Given two sequences of numbers : a1, a2, ...... , ...
- 字符串_KMP算法(求next[]模板 hdu 1711)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711 问题描述:给两个序列a,b,长度分别为n,m(1<=n<=1000000,1< ...
- HDU 1711(KMP)字符串匹配
链接 HDU 1711 Number Sequence KMP 算法 我以自己理解写的,写的不对,不明白的地方海王子出来,一起共同学习: 字符串匹配 就是KMP,一般思想,用一个for循环找开头 ...
随机推荐
- maven错误
maven-enforcer-plugin (goal "enforce") is ignored by m2e. Plugin execution not covered by ...
- eclipse Jsp 自创建tags问题
Can not find the tag directory "/WEB-INF/tags" 网上的说法有三种情况: 1. jsp2.0开始支持自定义tag,因此 web.xml文 ...
- pandas datafram重命名列名称
方法一: 直接给column赋值 df.columns=["a", "b"], 所有的column全部重命名 example: import pandas as ...
- linux install redis-cli
ubuntu: sudo apt-get install redis-cli centos: sudo -i yum list redis yum install redis>>> ...
- insert NULL into mysql
https://stackoverflow.com/questions/36898130/python-how-to-insert-null-mysql-values You are insertin ...
- How to Pronounce OF
How to Pronounce OF Tagged With: OF Reduction Study the OF reduction. There are many reductions in ...
- conductor 事件处理程序
Introduction conductor中的事件提供工作流之间的松散耦合,并支持从外部系统生成和消耗事件. 包括: 1. 能够在外部系统像SQS或Conductor内部生成一个事件(消息). 2. ...
- ubuntu修改运行级别方法
Ubuntu系统设置启动级别的问题,因自己以前遇到过,故做过笔记记录了下来:Ubuntu.Debian系列与RedHat.CentOS启动级别含义有所区别:Ubuntu系列运行级别定义如下:0 – H ...
- Mysql 内部默认排序
mysql默认的排序: https://forums.mysql.com/read.php?21,239471,239688#msg-239688 Do not depend on order whe ...
- R画散点图、线型图、箱型图、直方图基本知识
1.导入数据 2.散点图 plot(iris[,1]~iris[,4],xlab='Length',ylab='Width',col='red',main='Length VS Width')