我这段时间因为字符串太差而被关了起来了(昨晚打cf不会处理字符串现场找大佬模板瞎搞,差点就凉了),所以决定好好补一下字符串的知识QAQ,暂时先学习kmp算法吧~

题目链接:https://www.luogu.org/problemnew/show/P3375

题目:

思路:本题是kmp模板题,不会kmp的看官可以看本题在洛谷的题解,题解区有大量的讲解,我顺便在这里推荐一篇大佬的文章,帮助大家理解kmp算法,http://www.ruanyifeng.com/blog/2013/05/Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm.html。

代码实现如下:

 #include <set>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;;
typedef pair<int, int> pii;
typedef unsigned long long ull; #define lson i<<1
#define rson i<<1|1
#define bug printf("*********\n");
#define FIN freopen("D://code//in.txt", "r", stdin);
#define debug(x) cout<<"["<<x<<"]" <<endl;
#define IO ios::sync_with_stdio(false),cin.tie(0); const double eps = 1e-;
const int mod = ;
const int maxn = 1e6 + ;
const double pi = acos(-);
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f; int lens1, lens2;
string s1, s2;
int nex[maxn]; void get_next() {
nex[] = -;
for(int i = , k = -; i < lens2; ) {
if(k == - || s2[i] == s2[k]) {
++k;++i;
nex[i] = k;
} else k = nex[k];
}
} void kmp() {
get_next();
int i = , j = ;
while(i < lens1 && j < lens2) {
if(j == - || s1[i] == s2[j]) {
i++,j++;
if(j == lens2) {
printf("%d\n", i - j + );
j = nex[j];
}
} else j = nex[j];
}
} int main() {
//FIN;
cin >>s1 >>s2;
lens1 = s1.size(), lens2 = s2.size();
kmp();
for(int i = ; i <= lens2; i++) {
printf("%d%c", nex[i], i == lens2 ? '\n' : ' ');
}
return ;
}

洛谷 P3375 【模板】KMP字符串匹配的更多相关文章

  1. 洛谷P3375 [模板]KMP字符串匹配

    To 洛谷.3375 KMP字符串匹配 题目描述 如题,给出两个字符串s1和s2,其中s2为s1的子串,求出s2在s1中所有出现的位置. 为了减少骗分的情况,接下来还要输出子串的前缀数组next.如果 ...

  2. P3375 模板 KMP字符串匹配

    P3375 [模板]KMP字符串匹配 来一道模板题,直接上代码. #include <bits/stdc++.h> using namespace std; typedef long lo ...

  3. 【洛谷】3375 KMP字符串匹配

    [算法]KMP [题解][算法]字符串 #include<cstdio> #include<algorithm> #include<cstring> using n ...

  4. [模板]KMP字符串匹配

    洛谷P3375 注意:两次过程大致相同,故要熟读熟记,切勿搞混 可以看看其他的教程:http://www.cnblogs.com/c-cloud/p/3224788.html 本来就不太熟,若是在记不 ...

  5. 算法模板——KMP字符串匹配

    功能:输入一个原串,再输入N个待匹配串,在待匹配串中找出全部原串的起始位置 原理:KMP算法,其实这个东西已经包含了AC自动机的思想(fail指针/数组),只不过适用于单模板匹配,不过值得一提的是在单 ...

  6. [模板] KMP字符串匹配标准代码

    之前借鉴了某个模板的代码.我个人认为这份代码写得很好.值得一背. #include<bits/stdc++.h> using namespace std; const int N=1000 ...

  7. KMP字符串匹配 模板 洛谷 P3375

    KMP字符串匹配 模板 洛谷 P3375 题意 如题,给出两个字符串s1和s2,其中s2为s1的子串,求出s2在s1中所有出现的位置. 为了减少骗分的情况,接下来还要输出子串的前缀数组next.(如果 ...

  8. 洛谷—— P3375 【模板】KMP字符串匹配

    P3375 [模板]KMP字符串匹配 题目描述 如题,给出两个字符串s1和s2,其中s2为s1的子串,求出s2在s1中所有出现的位置. 为了减少骗分的情况,接下来还要输出子串的前缀数组next. (如 ...

  9. 洛谷 P3375 【模板】KMP字符串匹配 || HDU 1686 Oulipo || kmp

    HDU-1686 P3375 kmp介绍: http://www.matrix67.com/blog/archives/115 http://www.cnblogs.com/SYCstudio/p/7 ...

  10. 洛谷P3375 - 【模板】KMP字符串匹配

    原题链接 Description 模板题啦~ Code //[模板]KMP字符串匹配 #include <cstdio> #include <cstring> int cons ...

随机推荐

  1. kafka启动出现:Unsupported major.minor version 52.0 错误

    具体的错误输出: Exception in thread "main" java.lang.UnsupportedClassVersionError: kafka/Kafka : ...

  2. 第三部分shell编程3(shell脚本2)

    7. if 判断一些特殊用法 if [ -z $a ] 这个表示当变量a的值为空时会怎么样if grep -q '123' 1.txt; then 表示如果1.txt中含有'123'的行时会怎么样if ...

  3. C# 中的语法糖

    1.   using 代替了 try-catch-finally 因为之前是学 Java 的,在连接数据库或者进行文件读写操作时很自然的就使用了 try-catch-finally-,在 C# 中这样 ...

  4. 使用mac电脑,对Github客户端的简单操作1----开源项目

    工作之余自己也会一写一些小的程序项目,由于一直没时间“折腾”开源,之前写博客都是直接粘代码片段,今天看别人写技术博客大都会放出项目Github地址,突然感觉自己有点点out and low,作为一个励 ...

  5. 【bzoj4491】我也不知道题目名字是什么 离线扫描线+线段树

    题目描述 给定一个序列A[i],每次询问l,r,求[l,r]内最长子串,使得该子串为不上升子串或不下降子串 输入 第一行n,表示A数组有多少元素接下来一行为n个整数A[i]接下来一个整数Q,表示询问数 ...

  6. 【bzoj2190】[SDOI2008]仪仗队 欧拉函数

    题目描述 作为体育委员,C君负责这次运动会仪仗队的训练.仪仗队是由学生组成的N * N的方阵,为了保证队伍在行进中整齐划一,C君会跟在仪仗队的左后方,根据其视线所及的学生人数来判断队伍是否整齐(如下图 ...

  7. JS面向对象基础讲解(工厂模式、构造函数模式、原型模式、混合模式、动态原型模式)

    什么是面向对象?面向对象是一种思想. 面向对象可以把程序中的关键模块都视为对象, 而模块拥有属性及方法. 这样如果我们把一些属性及方法封装起来,日后使用将非常方便,也可以避免繁琐重复的工作.   工厂 ...

  8. Python 源码剖析(三)【字符串对象】

    三.字符串对象 1.PyStringObject与PyString_Type 2.创建PyStringObject对象 3.Intern 机制 4.字符缓冲池 5.PyStringObject 效率相 ...

  9. 用select模拟一个socket server成型版

    1.你往output里面放什么,下次循环就出什么.  2. 1.服务器端:实现了收和发的分开进行 import select,socket,queue server=socket.socket() s ...

  10. POJ1273:Drainage Ditches——题解

    http://poj.org/problem?id=1273 题目大意: n点m边网络流,求1-n最大流. —————————————— 网络流板子,切了. #include <cstdio&g ...