//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);
}

最新文章

  1. C fgetc
  2. java父类与接口有相同的方法
  3. php mysql支持emoji表情方案
  4. 图解javascript中this指向
  5. Codevs 1669 运输装备
  6. oscgit
  7. 空对象模式(Null Object Pattern)
  8. [Angular 2] Dispatching Action with Payloads and type to Reducers
  9. IOS支付宝支付出现6002问题的解决办法
  10. Importing Product Images on Magento
  11. PostgresSQL中的限制和级联删除
  12. Web 项目报错No suitable driver found for jdbc:mysql://localhost:3306/book 的一个解决办法
  13. Failure sending mail: The user or group name 'xxx\xxxx' is not recognized.Mail will not be resent
  14. Neovim中NERDTree等多处cursorline不高亮
  15. 主席树+树链剖分——南昌邀请赛Distance on the tree
  16. 使用expect解决shell交互问题
  17. jqgrid传入的json数据,赋值了但是没有在表格上显示
  18. 使用CountDownLatch模拟高并发场景
  19. 数据库的完整性约束(ForeignKey ,Unique)
  20. 当点击onBackpress方法或者onKeyDown方法时出现不能响应的问题解决办法

热门文章

  1. iOS中NSAttributedString的使用--对关键字着色,以及处理html实例
  2. 字符串、散列--P1598 垂直柱状图
  3. Gym - 101670H Go Northwest!(CTU Open Contest 2017 思维题+map)
  4. session对象的使用
  5. linux常用命令大全(linux基础命令+命令备忘录+面试复习)
  6. 关于字符串不为空 错误:s!=null
  7. 添物零基础到大型全栈架构师 不花钱学计算机及编程(预备篇)— C语言编程基础
  8. HDU 1253 三维数组的图上找最短路
  9. NYOJ35 表达式求值
  10. [转][MEF插件式开发] 一个简单的例子