EOJ #3441 唐纳德与子串 (Easy)【KMP】
来源:互联网 发布:2016年十大网络用语 编辑:程序博客网 时间:2024/06/15 10:41
题目链接
题目意思
给你一个文本串,然后给出Q次询问,每次给你两个数a,b和一个模式串,问你在区间【a,b】中有多少个模式串。
解题思路
简单的KMP算法。
代码部分
#include <iostream>#include <string.h>#include <stdio.h>#include <algorithm>#include <map>using namespace std;int nex[10001];void GetNext(string B){ int len_B=B.size(); nex[0]=-1; int k = -1,j = 0 ; while(j < len_B) { if(k == -1 || B[j] == B[k]) { ++ k; ++ j; nex[j] = k; } else k = nex[k]; }}int kmp(int a,int b,string A, string B){ int ans = 0, i = a, j = 0; int len_B = B.size(), n = A.size(); while(i<=b) { if(j==-1||A[i] == B[j]) { ++i; ++j; } else j = nex[j]; if(j == len_B) { ans++; j = nex[j]; } } return ans;}int main(){ string a,b; int l,r; int n; while(cin>>a) { scanf("%d",&n); while(n--) { scanf("%d%d",&l,&r); cin>>b; GetNext(b); int ans=kmp(l,r,a,b); cout<<ans<<endl; } } return 0;}
阅读全文
0 0
- EOJ #3441 唐纳德与子串 (Easy)【KMP】
- EOJ 3441-唐纳德与子串 (Easy)
- EOJ 3441 唐纳德与子串 (Easy)
- EOJ Monthly 2017.12 G1. 唐纳德与子串 (Easy)
- EOJ Monthly 2017.12 (暨 ECNU 12 月内部选拔)G1. 唐纳德与子串 (Easy)
- (substr strstr 的学习) EOJ Monthly 2017.12 (暨 ECNU 12 月内部选拔) G1. 唐纳德与子串 (Easy)
- G2. 唐纳德与子串 (Hard)kmp
- 唐纳德与子串 (Easy)(计算子串个数)
- EOJ 3452- 唐纳德先生和假骰子
- EOJ 3452 唐纳德先生和假骰子
- EOJ Monthly 2017.12 A. 唐纳德先生和假骰子
- EOJ #3452 唐纳德先生和假骰子【暴力】
- EOJ Monthly 2017.12 题解 3449. 唐纳德和他的数学老师
- KMP与最小覆盖子串
- KMP与最小覆盖子串
- KMP与最小覆盖子串
- 回文子串对(扩展kmp-kmp与回文子串)
- kmp 子串查找
- 650. 2 Keys Keyboard
- PostgreSQL操作
- 七牛云 from表单上传文件简单应用
- 不可变的String类
- filebeat源码分析之采集
- EOJ #3441 唐纳德与子串 (Easy)【KMP】
- 二分法插入paix
- Implement PatternToNumber
- C++可变参数宏
- 定义一个平面中的circle类
- 关于EasyRTSPClient、EasyPlayer RTSP流重连问题的解释
- Python基础之数据结构(元组,列表,字典)
- Eclipse快捷键 10个最有用的快捷键
- 实验二