题意:统计子串出现在主串中的次数

思路:典型kmp

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std; int next[]; void GetNext(char t[]){//求next数组
int j,k,len;
j=;
k=-;
next[]=-;
len=strlen(t);
while(j<len){
if(k==-||t[j]==t[k]){
++j;
++k;
next[j]=k;//此句可由优化替代
/*优化(仅保证求KMPIndex时可用。谨慎使用。)
if(t[j]!=t[k])next[j]=k;
else next[j]=next[k];
*/
}
else k=next[k];
}
} int KMPIndex(char s[],char t[]){//求子串首次出现在主串中的位置
int i,j,lens,lent;
i=j=;
lens=strlen(s);
lent=strlen(t); while(i<lens&&j<lent){
if(j==-||s[i]==t[j]){
++i;
++j;
}
else j=next[j];
}
if(j>=lent)return i-lent;
else return -;
} int KMPCount(char s[],char t[]){//统计子串在主串中的出现次数,可重叠
int i,j,lens,lent,cnt;
i=j=;
lens=strlen(s);
lent=strlen(t);
cnt=; while(i<lens){
if(j==-||s[i]==t[j]){
++i;
++j;
}
else j=next[j];
if(j==lent)++cnt;
}
return cnt;
} void KMPCount2(char t[]){//统计单串中从某个位置以前有多少重复的串
int i,lent,tmp;
lent=strlen(t); for(i=;i<=lent;++i){
tmp=i-next[i];
if(i%tmp==&&i/tmp>)
printf("\t位置:%d 个数:%d\n",i,i/tmp);
}
} int main(){
char str1[],str2[];
int i;
int t;
scanf("%d",&t);
while(t--){
scanf("%s%s",str2,str1);
GetNext(str2);//求子串的next数组
printf("%d\n",KMPCount(str1,str2));
}
return ;
}

poj 3461 Oulipo(kmp统计子串出现次数)的更多相关文章

  1. POJ 3461 Oulipo(KMP,模式串在主串中出现次数 可重叠)

    题意:给你两个字符串p和s,求出p在s中出现的次数. 显然,我们要先把模式串放到前面,之后主串放后面,中间隔开,这样就可以根据前缀数组的性质来求了. 我先想直接把p接到s前面,之后求Next数组对st ...

  2. POJ 3080 Blue Jeans、POJ 3461 Oulipo——KMP应用

    题目:POJ3080 http://poj.org/problem?id=3080 题意:对于输入的文本串,输出最长的公共子串,如果长度相同,输出字典序最小的. 这题数据量很小,用暴力也是16ms,用 ...

  3. POJ 3461 Oulipo KMP

    题意:统计其中一个子串的出现次数 题解:即KMP算法中j==m的次数 //作者:1085422276 #include <cstdio> #include <cmath> #i ...

  4. [POJ] 3461 Oulipo [KMP算法]

    Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 23667   Accepted: 9492 Descripti ...

  5. POJ 3461 Oulipo KMP算法(模板)

    题意: 给两组字符串a和b,求a在b中出现的次数 关于KMP: 马拉车算法是处理回文串,而KMP是处理前后缀的相同字符串的最长长度. a | a | b | a | a | f | a | a 数组 ...

  6. POJ 3461 Oulipo KMP算法题解

    本题就是给出非常多对字符串,然后问一个字符串在另外一个字符串出现的次数. 就是所谓的Strstr函数啦. Leetcode有这道差点儿一模一样的题目. 使用KMP算法加速.算法高手必会的算法了. 另外 ...

  7. HDU 1686 Oulipo / POJ 3461 Oulipo / SCU 2652 Oulipo (字符串匹配,KMP)

    HDU 1686 Oulipo / POJ 3461 Oulipo / SCU 2652 Oulipo (字符串匹配,KMP) Description The French author George ...

  8. POJ 3461 Oulipo(乌力波)

    POJ 3461 Oulipo(乌力波) Time Limit: 1000MS   Memory Limit: 65536K [Description] [题目描述] The French autho ...

  9. POJ 3461 Oulipo 【KMP统计子串数】

    传送门:http://poj.org/problem?id=3461 Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submission ...

随机推荐

  1. 【索引】理解MySQL——索引与优化

    MySQL 索引 MySQL索引的建立对于MySQL的高效运行是很重要的,索引可以大大提高MySQL的检索速度. 打个比方,如果合理的设计且使用索引的MySQL是一辆兰博基尼的话,那么没有设计和使用索 ...

  2. zoj 2081 BFS 延迟标记 读入问题

    Mission Impossible Time Limit: 2 Seconds                                     Memory Limit: 65536 KB  ...

  3. Codeforces Round #291 (Div. 2) C. Watto and Mechanism [字典树]

    传送门 C. Watto and Mechanism time limit per test 3 seconds memory limit per test 256 megabytes input s ...

  4. POJ 2002 Squares [hash]

    Squares Time Limit: 3500MS   Memory Limit: 65536K Total Submissions: 16631   Accepted: 6328 Descript ...

  5. 531. Lonely Pixel I

    Given a picture consisting of black and white pixels, find the number of black lonely pixels. The pi ...

  6. 湘潭oj1203/邀请赛A题 数论+java大数

    求 n%1+n%2+n%3+n%4+.........n%n=,n<=10^12次. 开始时盲目地找规律,结果一无所获.后来经学长点拨,天资愚钝,搞了半天才明白. 先上图: 对于该题,在求区间( ...

  7. 生成PDF文档

    byte[] buffer = context.Response.Clear(); context.Response.ClearHeaders(); context.Response.ClearCon ...

  8. vue之组件理解(一)

    组件是可复用的 Vue 实例,所以它们与 new Vue 接收相同的选项,例如 data.computed.watch.methods 以及生命周期钩子等.仅有的例外是像 el 这样根实例特有的选项. ...

  9. Centos常用命名

    1.关机 (系统的关机.重启以及登出 ) 的命令 shutdown -h now 关闭系统(1) init 0 关闭系统(2) telinit 0 关闭系统(3) shutdown -h hours: ...

  10. VS调试时不加载符号

    加载符号,能够让调试时进入系统的dll中,甚至能达到汇编语言的深度,让你调试时能够逐行进行.但是加载符号的话,会使得每次调试,增加进入到首个断点的时间. 而且有时候也非必要加载符号,它的取消可以使用下 ...