kmp的练习们
2024-10-01 01:05:35
//poj3461 Oulipo
//kmp模板 统计子串在母串中的位置
#include<iostream>
#include<cstdio>
#include<cstring> using namespace std;
int next[];
char p[],s[];
int t,num,l1,l2; void get_next()
{
int j=;
int k=-;
next[]=-;
while(j<l2)
{
if(k==-||p[k]==p[j])
{
k++;
j++;
next[j]=k;
}
else
{
k=next[k];
}
}
} int kmp()
{
int i=;
int j=;
get_next();
while(i<l1)
{
if(j==-||p[j]==s[i])
{
j++;
i++;
}
else
{
j=next[j];
}
if(j==l2)
{
num++;
j=next[j];
}
}
return num;
} int main()
{
cin>>t;
while(t--)
{
num=;
cin>>p>>s;
l1=strlen(s);
l2=strlen(p);
cout<<kmp()<<endl;
}
}
//洛谷3375 字符串匹配
#include<iostream>
#include<cstdio>
#include<cstring> using namespace std;
int next[];
string p,s; void get_next(string p)
{
next[]=-;
int j=;
int k=-;
int l=p.length();
while(j<l)
{
if(k==-||p[j]==p[k])
{
j++;
k++;
next[j]=k;
}
else
{
k=next[k];
}
}
} void kmp(string s,string p)
{
get_next(p);
int i=,j=;
int l=s.length();
while(i<l)
{
if(j==-||p[j]==s[i])
{
i++;
j++;
}
else
{
j=next[j];
}
if(j==p.length())
{
printf("%d\n",i-p.length()+);
j=,i--;
}
}
for(int a=;a<=p.length();a++)
{
cout<<next[a]<<" ";
}
return;
} int main()
{
cin>>s>>p;
kmp(s,p);
}
最新文章
- C fgetc
- java父类与接口有相同的方法
- php mysql支持emoji表情方案
- 图解javascript中this指向
- Codevs 1669 运输装备
- oscgit
- 空对象模式(Null Object Pattern)
- [Angular 2] Dispatching Action with Payloads and type to Reducers
- IOS支付宝支付出现6002问题的解决办法
- Importing Product Images on Magento
- PostgresSQL中的限制和级联删除
- Web 项目报错No suitable driver found for jdbc:mysql://localhost:3306/book 的一个解决办法
- Failure sending mail: The user or group name 'xxx\xxxx' is not recognized.Mail will not be resent
- Neovim中NERDTree等多处cursorline不高亮
- 主席树+树链剖分——南昌邀请赛Distance on the tree
- 使用expect解决shell交互问题
- jqgrid传入的json数据,赋值了但是没有在表格上显示
- 使用CountDownLatch模拟高并发场景
- 数据库的完整性约束(ForeignKey ,Unique)
- 当点击onBackpress方法或者onKeyDown方法时出现不能响应的问题解决办法
热门文章
- iOS中NSAttributedString的使用--对关键字着色,以及处理html实例
- 字符串、散列--P1598 垂直柱状图
- Gym - 101670H Go Northwest!(CTU Open Contest 2017 思维题+map)
- session对象的使用
- linux常用命令大全(linux基础命令+命令备忘录+面试复习)
- 关于字符串不为空 错误:s!=null
- 添物零基础到大型全栈架构师 不花钱学计算机及编程(预备篇)— C语言编程基础
- HDU 1253 三维数组的图上找最短路
- NYOJ35 表达式求值
- [转][MEF插件式开发] 一个简单的例子