POJ 3461 && HDU 1686 Oulipo(kmp)
2015-09-02 08:41
597 查看
Description
求a串在b串中出现的次数
Input
第一行为用例组数t,每组用例占两包括两个字符串b串和a串
Output
对于每组用例,输出a串在b串中出现的次数
Sample Input
3
BAPC
BAPC
AZA
AZAZAZA
VERDI
AVERDXIVYERDIAN
Sample Output
1
3
0
Solution
kmp基础题
Code
求a串在b串中出现的次数
Input
第一行为用例组数t,每组用例占两包括两个字符串b串和a串
Output
对于每组用例,输出a串在b串中出现的次数
Sample Input
3
BAPC
BAPC
AZA
AZAZAZA
VERDI
AVERDXIVYERDIAN
Sample Output
1
3
0
Solution
kmp基础题
Code
#include<cstdio> #include<cstring> #include<iostream> using namespace std; char a[1111111],b[11111]; int next[1111111]; int main() { int t; cin>>t; while(t--) { scanf("%s",b); scanf("%s",a); int la=strlen(a); int lb=strlen(b); int sum=0; for(int i=0,j=-1;i<=lb;i++,j++) { next[i]=j; while(~j&&b[i]!=b[j]) j=next[j]; } for(int i=0,j=0;i<=la;i++,j++) { if(j==lb) { sum++; j=next[j]; } while(~j&&a[i]!=b[j]) j=next[j]; } printf("%d\n",sum); } return 0; }
相关文章推荐
- POJ 2406 Power Strings(kmp)
- POJ 2185 Milking Grid(二维kmp)
- web service与远程调用(RPC)的区别
- onbeforeunload与onunload事件
- POJ 1961 && HDU 1358 Period(kmp)
- GridView认识(一)
- 最近学习笔记
- Mybatis-spring基本配置
- Empty、Error、Nothing 及 Null
- 需要在AndroidManifest中添加的条件
- GridView中实现CheckBox的全选
- 递归树 TreeList
- cookie和session的区别
- UVA Live 3713 Astronauts (2-SAT)
- NPOI相关
- 虚拟文件系统中的数据结构(fs_struct、files_struct) http://xd03071149.blog.163.com/blog/static/1235063632012912114
- 知行合一——断舍离
- CoreAnimation-08-CATransition
- 如何在 Windows 上通过 Kitematic 使用 Docker
- activity的四种启动模式,隐式意图