题意:统计其中一个子串的出现次数

题解:即KMP算法中j==m的次数

//作者:1085422276
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
//#include<bits/stdc++.h>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
const int inf = ;
inline ll read()
{
ll x=,f=;
char ch=getchar();
while(ch<''||ch>'')
{
if(ch=='-')f=-;
ch=getchar();
}
while(ch>=''&&ch<='')
{
x=x*+ch-'';
ch=getchar();
}
return x*f;
}
ll exgcd(ll a,ll b,ll &x,ll &y)
{
ll temp,p;
if(b==)
{
x=;
y=;
return a;
}
p=exgcd(b,a%b,x,y);
temp=x;
x=y;
y=temp-(a/b)*y;
return p;
}
//*******************************
int p[];
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
string a,b;
cin>>b>>a;
a=" "+a;
b=" "+b;
int m=b.length();
int n=a.length();
n--,m--;
memset(p,,sizeof(p));
int j=;
for(int i=;i<=m;i++)
{
while(j>&&b[j+]!=b[i])j=p[j];
if(b[j+]==b[i])j++;
p[i]=j;
}
int ans=;
j=;
for(int i=;i<=n;i++)
{
while(j>&&b[j+]!=a[i])j=p[j];
if(b[j+]==a[i])j++;
if(j==m){
ans++;
}
}
cout<<ans<<endl;
}
return ;
}

代码

最新文章

  1. Git——1
  2. 开心网的账号登录及api操作
  3. 如何复制DataRow(dataTabel中的行)
  4. [SAP ABAP开发技术总结]Form(subroutine)、Function参数传值传址
  5. 更改Android AVD路径
  6. [求助]谁能给我讲解一下,iOS编程要如何实时显示采集到的图像???
  7. duilib中ListCtrl控件的实现
  8. C 语言的可变参数表函数的设计
  9. DE1-SOC的sof文件无法下载解决办法
  10. hdu1069(dp)
  11. mysqlclient和PyMySQL对比
  12. 个人作业3--------个人总结(Alpha版本)
  13. 我推荐的 Java Web 学习路线
  14. 哈密顿绕行世界问题(dfs+记录路径)
  15. RE模块疑问
  16. LeetCode(32)-Binary Tree Level Order Traversal
  17. pythonのsqlalchemy简单查询
  18. php同curl post 发送json并返回json数据实例
  19. Ubuntu 16.04及以上 安装/卸载 Docker-CE
  20. (转)Android数据的四种存储方式SharedPreferences、SQLite、Content Provider和File (三) —— SharePreferences

热门文章

  1. SpringMVC 和Struts2的区别
  2. 转:Java NIO系列教程(二) Channel
  3. 手机wifi密码的保存位置
  4. Entity Framework 简单增删改操作
  5. Spring AOP使用整理:使用@AspectJ风格的切面声明
  6. Python ===if while for语句 以及一个小小网络爬虫实例
  7. Android的webview加载本地html、本apk内html和远程URL
  8. git clone报错
  9. 在线调试和演示的前端开发工具------http://jsfiddle.net/
  10. 异常:The absolute uri: http://www.springframework.org/security/tags cannot be resolved in either web.xml or the jar files deployed with this application