洛谷 P3375 【模板】KMP字符串匹配 题解
KMP模板,就不解释了
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#define int long long
#define maxn 1000000+10
#define INF 9187201950435737471
#define rep(i,s,e) for(register int i=s;i<=e;++i)
#define dwn(i,s,e) for(register int i=s;i>=e;--i)
using namespace std;
inline int read()
{
int x=;
bool f=;
char c=getchar();
for(; !isdigit(c); c=getchar()) if(c=='-') f=;
for(; isdigit(c); c=getchar()) x=(x<<)+(x<<)+c-'';
if(f) return x;
return -x;
}
inline void write(int x)
{
if(x<){putchar('-');x=-x;}
if(x>)write(x/);
putchar(x%+'');
}
int j,len1,len2;
char A[maxn],B[maxn];
int p[maxn];
void init()
{
j=;
p[]=;
rep(i,,len2)
{
while(j>&&B[i]!=B[j+]) j=p[j];
if(B[i]==B[j+]) ++j;
p[i]=j;
}
}
signed main()
{
scanf("%s",A+);
scanf("%s",B+);
len1=strlen(A+);
len2=strlen(B+);
init();
j=;
rep(i,,len1)
{
while(j>&&A[i]!=B[j+]) j=p[j];
if(A[i]==B[j+]) ++j;
if(j==len2)
{
write(i-len2+);
putchar('\n');
j=p[j];
}
}
rep(i,,len2) write(p[i]),putchar(' ');
return ;
}
请各位大佬斧正(反正我不认识斧正是什么意思)
洛谷 P3375 【模板】KMP字符串匹配 题解的更多相关文章
- 洛谷P3375 [模板]KMP字符串匹配
To 洛谷.3375 KMP字符串匹配 题目描述 如题,给出两个字符串s1和s2,其中s2为s1的子串,求出s2在s1中所有出现的位置. 为了减少骗分的情况,接下来还要输出子串的前缀数组next.如果 ...
- P3375 模板 KMP字符串匹配
P3375 [模板]KMP字符串匹配 来一道模板题,直接上代码. #include <bits/stdc++.h> using namespace std; typedef long lo ...
- 【洛谷】3375 KMP字符串匹配
[算法]KMP [题解][算法]字符串 #include<cstdio> #include<algorithm> #include<cstring> using n ...
- [模板]KMP字符串匹配
洛谷P3375 注意:两次过程大致相同,故要熟读熟记,切勿搞混 可以看看其他的教程:http://www.cnblogs.com/c-cloud/p/3224788.html 本来就不太熟,若是在记不 ...
- KMP算法详解&&P3375 【模板】KMP字符串匹配题解
KMP算法详解: KMP算法是一种改进的字符串匹配算法,由D.E.Knuth,J.H.Morris和V.R.Pratt(雾)提出的. 对于字符串匹配问题(such as 问你在abababb中有多少个 ...
- 算法模板——KMP字符串匹配
功能:输入一个原串,再输入N个待匹配串,在待匹配串中找出全部原串的起始位置 原理:KMP算法,其实这个东西已经包含了AC自动机的思想(fail指针/数组),只不过适用于单模板匹配,不过值得一提的是在单 ...
- [模板] KMP字符串匹配标准代码
之前借鉴了某个模板的代码.我个人认为这份代码写得很好.值得一背. #include<bits/stdc++.h> using namespace std; const int N=1000 ...
- KMP字符串匹配 模板 洛谷 P3375
KMP字符串匹配 模板 洛谷 P3375 题意 如题,给出两个字符串s1和s2,其中s2为s1的子串,求出s2在s1中所有出现的位置. 为了减少骗分的情况,接下来还要输出子串的前缀数组next.(如果 ...
- 洛谷—— P3375 【模板】KMP字符串匹配
P3375 [模板]KMP字符串匹配 题目描述 如题,给出两个字符串s1和s2,其中s2为s1的子串,求出s2在s1中所有出现的位置. 为了减少骗分的情况,接下来还要输出子串的前缀数组next. (如 ...
随机推荐
- XGBoost对波士顿房价进行预测
import numpy as np import matplotlib as mpl mpl.rcParams["font.sans-serif"] = ["SimHe ...
- 18年10月 python 中出现 ValueError: need more than 1 value to unpack 解决办法 (笨办法)
eg:a,b = argv :错误,我的理解也许不正确,但是能解决办法 a,b= argv,argv 正确 :经测试不会出现错误. ------------------------------ ...
- homebrew 使用代理
ALL_PROXY=socks5://127.0.0.1:1086 brew cask install aerial
- 3.02定义常量之const
[注:本程序验证是使用vs2013版] #include <stdio.h> #include <stdlib.h> #include <string.h> #pr ...
- 【1】BIO与NIO、AIO的区别
一.BIO 在JDK1.4出来之前,我们建立网络连接的时候采用BIO模式,需要先在服务端启动一个ServerSocket,然后在客户端启动Socket来对服务端进行通信,默认情况下服务端需要对每个请求 ...
- Java内存模型——方法区
方法区(Method Area) ① 对每个加载的类型,JVM必须在方法区中存储以下类信息: 1) 这个类型的完整有效名(类型信息) 类型名称在Java类文件和JVM中都以完整 ...
- 把时间戳转换为 datatime 格式
使用time timeStamp = 1381419600 timeArray = time.localtime(timeStamp) otherStyleTime = time.strftime(& ...
- 在SQL Server中,为何都建议禁止 VIA 协议,VIA协议具体内容是什么?
在SQL Server 在SQL Server中,为何都建议禁止 VIA 协议,VIA协议具体内容是什么? 中,为何都建议禁止 VIA 协议,VIA协议具体内容是什么? 在SQL Server中,为何 ...
- Django流程图(精简版)
网址: https://www.processon.com/view/link/5dddb0f8e4b074c442e5c68c
- go语言 goquery爬虫
goquery 类似ruby的gem nokogiri goquery的选择器功能很强大,很好用.地址:https://github.com/PuerkitoBio/goquery 这是一个糗百首 ...