pok與pom特性區別,POJ3461 【KMP(粗糙模板)】

 2023-11-19 阅读 27 评论 0

摘要:題意: 給你兩個字符串p和s,求出p在s中出現的次數。 這道題,abababa中aba出現了3次。 pok與pom特性區別、有其他題是求abababa,aba就是2次。 需注意。 KMP 模板 //#include<bits/stdc++.h> //using namespace std; #include<cst

題意:

給你兩個字符串p和s,求出p在s中出現的次數。

這道題,abababa中aba出現了3次。

pok與pom特性區別、有其他題是求abababa,aba就是2次。

需注意。

KMP 模板

//#include<bits/stdc++.h>
//using namespace std;
#include<cstdio>
#include<iostream>
#include<queue>
#include<algorithm>
#include<string.h>
using namespace std;
typedef long long LL;const int N=1e6+10;
const int M=1e4+10;int next[10010];
char s[1000010];
char p[10010];
int lenp,lens;void print()
{for(int i=0;i<lenp;i++)printf("%3d",next[i]);puts("");
}void GetNext()
{int i,j;next[0]=-1;i=0;j=-1;while(i<lenp){if(j==-1||p[i]==p[j]){i++;j++;next[i]=j;}elsej=next[j];}
//	print();
}int KMP()
{int ans=0,i=0,j=0;while(i<lens&&j<lenp){if(j==-1||p[j]==s[i]){i++;j++;}elsej=next[j];if(j==lenp){ans++;j=next[j];}}return ans;
}int main()
{int T,ans;scanf("%d",&T);while(T--){scanf("%s%s",p,s);lens=strlen(s);lenp=strlen(p);GetNext();//getnext();ans=KMP();printf("%d\n",ans);}return 0;
}


轉載于:https://www.cnblogs.com/keyboarder-zsq/p/6777451.html

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/2/180175.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息