hdu 1711
读入优化有3s多。
#include <cstdio>
#include <cctype>
#define maxn 1000010
#define maxm 10010 int n, m;
int aa[maxn], bb[maxm], f[maxm]; void gn( int &rt ) {
char ch;
char opt;
while( !isdigit(ch=getchar()) ) opt=ch;
rt=ch-'';
while( isdigit(ch=getchar()) )
rt=rt*+ch-'';
if( opt=='-' ) rt=-rt;
} void getfail() {
f[] = f[] = ;
for( int i=, j; i<m; i++ ) {
j = f[i];
while( j && bb[i]!=bb[j] ) j=f[j];
f[i+] = bb[i]==bb[j] ? j+ : ;
}
}
int kmp() {
for( int i=,j=; i<n; i++ ) {
while( j && aa[i]!=bb[j] ) j=f[j];
if( aa[i]==bb[j] ) j++;
if( j==m ) return i-m+;
}
return -;
} int main() {
int T;
scanf( "%d", &T );
while( T-- ) {
scanf( "%d%d", &n, &m );
for( int i=; i<n; i++ )
gn(aa[i]);
for( int i=; i<m; i++ )
gn(bb[i]);
getfail();
printf( "%d\n", kmp() );
}
}
(注意,getfail()中f[i+1] = P[i]==P[j] ? j+1 : 0;)
hdu 1711的更多相关文章
- HDU 1711 Number Sequence(数列)
HDU 1711 Number Sequence(数列) Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...
- HDU 1711 Number Sequence (字符串匹配,KMP算法)
HDU 1711 Number Sequence (字符串匹配,KMP算法) Description Given two sequences of numbers : a1, a2, ...... , ...
- HDU 1711(KMP)字符串匹配
链接 HDU 1711 Number Sequence KMP 算法 我以自己理解写的,写的不对,不明白的地方海王子出来,一起共同学习: 字符串匹配 就是KMP,一般思想,用一个for循环找开头 ...
- hdu 1711 KMP算法模板题
题意:给你两个串,问你第二个串是从第一个串的什么位置開始全然匹配的? kmp裸题,复杂度O(n+m). 当一个字符串以0为起始下标时.next[i]能够描写叙述为"不为自身的最大首尾反复子串 ...
- HDU - 1711 A - Number Sequence(kmp
HDU - 1711 A - Number Sequence Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1 ...
- HDU 1711 Number Sequence(KMP)附带KMP的详解
题目代号:HDU 1711 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711 Number Sequence Time Limit: 10000/ ...
- Number Sequence HDU 1711(KMP)
http://acm.hdu.edu.cn/showproblem.php?pid=1711 首次接触KMP,自己都不是特别理解.在网上百度看了好几个帖子之后,对KMP也有了初步的理解. #inclu ...
- 字符串_KMP算法(求next[]模板 hdu 1711)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711 问题描述:给两个序列a,b,长度分别为n,m(1<=n<=1000000,1< ...
- HDU 1711 Number Sequence KMP
题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=1711 AC代码: #include <iostream> #include <cs ...
- HDU 1711 Number Sequence(KMP裸题,板子题,有坑点)
Number Sequence Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
随机推荐
- html 中的列表
html 中列表可以分为 1. 无序列表(ul--li 的形式) 2. 有序列表(ol li的形式) 3. 定义列表(dl 的形式) 下面来看几种列表的具体内容: 1.无序列表. 无序列表的格式 ...
- three.js轨道控制器OrbitControls.js
https://blog.csdn.net/qq_37338983/article/details/78575333 文章地址
- git push multiple repo
git remote add xxx https://git.github.com
- (转)什么是CDC类(Communication Device Class)
全文地址:http://justmei.blog.163.com/blog/static/1160998532010321112522467/ 什么是CDC类 (Communication Devic ...
- C# 操作IitextPdf
1.读取PDF内容 public string ReadPdfFile(string fileName) { StringBuilder text = new StringBuilder(); if ...
- WebBrowser中运行js
HtmlElement script = wf.WebBrowser.Document.CreateElement("script"); script.SetAttribute(& ...
- 【转载】selenium with PhantomJs wait till page fully loaded?
I use Selenium with Phantomjs, and want to get the page content after the page fully loaded. I tried ...
- sicily 1193. Up the Stairs
Time Limit: 1sec Memory Limit:32MB Description John is moving to the penthouse of a tall sky-scr ...
- 设置Git远程仓库
1,注册一个GitHub账户,登陆GitHub账户,添加一个储存库 2,进入Ubuntu命令窗口,创建文件夹.如 mkdir git echo "# first_git" ...
- SSD回归类物体检测
本宝宝最近心情不会,反正这篇也是搬用别人博客的了:(SSD就是YOLO+anchor(不同feature map 作为input)) 引言 这篇文章是在YOLO[1]之后的一篇文章,这篇文章目前是一篇 ...