首页 技术 正文
技术 2022年11月13日
0 收藏 663 点赞 5,064 浏览 870 个字

传送门

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define re register
using namespace std;
const int maxn = 1000005;inline long long read() {
char ch = getchar();
long long f = 1 , x = 0;
while(ch > '9' || ch < '0') {if(ch == '-') f = -1 ;ch = getchar();}
while(ch >= '0' && ch <= '9') {x = (x << 1) + ( x << 3) + ch - '0';ch = getchar();}
return x * f;
}char s1[maxn],s2[maxn];
int len1,len2;
int nxt[maxn];
//用于记录当匹配到模式串的第 i 位之后失配,该跳转到模式串的哪个位置int main(){
cin >> s1 + 1 >> s2 + 1 ;
len1 = strlen(s1 + 1) , len2 = strlen(s2 + 1);
nxt[0] = nxt[1] = 0;
int j ;//j可以看做表示当前已经匹配完的模式串的最后一位的位置
for(re int i = 2 ; i <= len2 ; ++i) {
while(j && s2[i] != s2[j + 1]) j = nxt[j];
//如果失配 ,那么就不断向回跳,直到可以继续匹配
if(s2[j + 1] == s2[i]) j++;
//如果匹配成功,那么对应的模式串位置++
nxt[i] = j ;
}
j = 0;
for(re int i = 1 ; i <= len1 ; ++i) {
while(j && s2[j + 1] != s1[i]) j = nxt[j];
//此处判断j是否为0的原因在于,如果回跳到第一个字符就不 用再回跳
if(s2[j + 1] == s1[i]) j++ ;
if(j == len2) {
printf("%d\n",i - len2 + 1) ;
j = nxt[j];
}
}
for(re int i = 1 ; i <= len2 ; ++i)
printf("%d " , nxt[i]);
return 0;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,107
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,583
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,430
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,201
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,836
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,919