poj3461kmp
2024-05-26 10:40:08
求模式串在原串出现次数。
#include <cstdio> #include <cstring> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #include <cstdlib> #include <list> #include <set> #include <queue> #include <stack>using namespace std;const int maxn=1111111;int next[maxn];void getnext(char *s) {int len=strlen(s);int j=-1;int i=0;next[0]=-1;while(i<len){if(j==-1||s[i]==s[j]){i++;j++;next[i]=j;}else{j=next[j];}} }//求next 数组函数 next[j] 表示从 str[j-1]向前和从头str[0]向后 最长相同串的长度 int ans; void solve(char *s,char *s1) {int j=0;int len=strlen(s);int len1=strlen(s1);for(int i=0;i<len1;i++){if(j>0&&s1[i]!=s[j]) j= next[j]; //失配之后 将 str[next[j]] 与 str1[i] 比较if(s[j]==s1[i]) j++;if(j==len){ans++;j=next[j]; //找到答案 后继续失配。。 }}cout<<ans<<endl; } char str[maxn];char str1[maxn]; int main() {int Icase;cin>>Icase;while(Icase--){scanf("%s",str);scanf("%s",str1);ans=0;getnext(str);solve(str,str1);}return 0; }
转载于:https://www.cnblogs.com/yigexigua/p/3870170.html
poj3461kmp相关推荐
最新文章
- 和尚挖井故事给程序员的启示!
- python编程语言继承_python应用:学习笔记(Python继承)
- 【bzoj2154】Crash的数字表格 莫比乌斯反演
- 剑指offer:39-42记录
- python基本对象类型
- Python中math模块的使用
- EC600 QuecPython开发环境搭建、固件下载,最方便的OpenCPU物联网4G通信解决方案
- 给我一个软件,我将操控一个机器人军团!
- JVM 大厂面试都会问,都会这么问,你能顶住么?
- 分水岭算法(Watershed)
- 记2019届阿里校招第一面
- MSDB数据库置疑的解决方法
- 官方文件出炉!北京住建委权威解读商住限购
- sdkman 的酷炫 Logo 欢迎界面: 安装 kscript (Kotlin Shell)
- [生存志] 第79节 国语述诸国
- Transformer28
- 为高效学习神器 Anki 部署一个专属同步服务器
- 人工智能的影响是大众还是小众,对我们的职业有什么影响?
- 展锐UDX710:MMC概述、SD Card驱动解析及调试
- win10 无法连接打印机 报0x00000520错误解决办法!