题意:

  求子串w在T中出现的次数。

kmp算法详解:http://www.cnblogs.com/XDJjy/p/3871045.html

#include <iostream>
#include <cstring>
#include <string>
#include <cstdio> using namespace std;
int lenp;
int lens;
void getnext(int *next, char *p)
{
int j = , k = -;
next[] = -;
while(j < lenp)
{ if(k == - || p[j] == p[k])
{
j++;
k++;
next[j] = k;
}
else
k = next[k];
}
} char s[], p[];
int next[];
int main(void)
{
int N;
scanf("%d", &N);
while( N-- )
{ scanf("%s", p);
scanf("%s", s);
lens=(int)strlen(s);
lenp=(int)strlen(p); getnext(next, p);
int i = ;
int j = ;
int sum = ; while(i < lens&&j<lenp)
{
//printf("%d %d %c %c\n",i,j,s[i],p[j]);
if( j == - || s[i] == p[j] )
{ i++;
j++;
}
else
{
j = next[j];
}
//printf("#%d %d %c %c\n",i,j,s[i],p[j]);
if( j >=lenp)
{
sum++;
j = next[j];/////////!!!!!!!!!!
//printf("%d %c %c\n",sum,s[i],p[j]);
}
}
printf("%d\n", sum);
}
return ;
}
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <algorithm>
using namespace std;
#define L1 1000005
#define L2 10005 int next[L2], len1, len2, res;
char s[L1], p[L2]; void get_next ()
{
int j = , k = -;
next[] = -;
while (j < len2)
{
if (k == - || p[j] == p[k])
{
j++, k++;
next[j] = k;
}
else k = next[k];
}
}
void kmp (int pos)
{
int i = pos, j = ;
while (i < len1 && j < len2)
{
//printf("%d %d %c %c\n",i,j,s[i],p[j]);
if (j == - || s[i] == p[j])
{
i++, j++;
}
else j = next[j];
//printf("#%d %d %c %c\n",i,j,s[i],p[j]);
if (j >= len2)
res++, j = next[j]; //神奇之处,效率大增
}
}
int main()
{
int t;
scanf ("%d", &t);
while (t--)
{
res = ;
scanf ("%s%s", p, s);
len1 = strlen (s);
len2 = strlen (p);
get_next ();
kmp ();
printf ("%d\n", res);
}
return ;
}

hdu 1686 KMP算法的更多相关文章

  1. hdu 1686 KMP模板

    // hdu 1686 KMP模板 // 没啥好说的,KMP裸题,这里是MP模板 #include <cstdio> #include <iostream> #include ...

  2. hdu 1711 KMP算法模板题

    题意:给你两个串,问你第二个串是从第一个串的什么位置開始全然匹配的? kmp裸题,复杂度O(n+m). 当一个字符串以0为起始下标时.next[i]能够描写叙述为"不为自身的最大首尾反复子串 ...

  3. hdu 4300 kmp算法扩展

    Clairewd’s message Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  4. HDU 1686 & KMP

    题意: 求模板在匹配串所有子串中出现次数. SOL: 本题与普通kmp有一点不同,因为待匹配串中的模板串可能相互包含. 我们考虑正常的kmp是在怎么做的 i = 1 2 3 4 5 6 7 8 9 … ...

  5. HDU 1686 (KMP模式串出现的次数) Oulipo

    题意: 求模式串W在母串T中出现的次数,各个匹配串中允许有重叠的部分. 分析: 一开始想不清楚当一次匹配完成时该怎么办,我还SB地让i回溯到某个位置上去. 后来仔细想想,完全不用,直接让模式串向前滑动 ...

  6. Oulipo HDU 1686 KMP模板

    题目大意:求模式串在主串中的出现次数. 题目思路:KMP模板题 #include<iostream> #include<algorithm> #include<cstri ...

  7. hdu 3613 KMP算法扩展

    Best Reward Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  8. HDU 2594 kmp算法变形

    Simpsons’ Hidden Talents Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java ...

  9. hdu 1686 Oulipo KMP匹配次数统计

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1686 分析:典型的KMP算法,统计字符串匹配的次数. 用Next数组压缩时间复杂度,要做一些修改. / ...

随机推荐

  1. Java运算符,位运算

    注意:位运算符针对整数的补码进行运算,所以运算结果也是补码 &(与运算) 将数据转化为补码形式,然后将0看作false,将1看作true,按位进行与运算,最后将结果转化为十进制来显示     ...

  2. PDA后台运行、安装程序

    ////启动最新版本安装(后台安装模式),结束更新程序            //string cabPath = @"\Application Data\QY.DDM.PDA.CAB&qu ...

  3. TCP三次握手和四次挥手、HTTP协议

    TCP三次握手和四次挥手 首先我们知道HTTP协议通常承载于TCP协议之上,HTTPS承载于TLS或SSL协议层之上 通过上面这张图我们能够知道.     在Http工作之前,Web浏览器通过网络和W ...

  4. 【BZOJ】1007: [HNOI2008]水平可见直线(凸包)

    题目 传送门:QWQ 分析 在下面维护一个凸壳 好久没写博客了...... 代码 #include <bits/stdc++.h> using namespace std; ; ,INF= ...

  5. canvas绘制矩形

    canvas绘制矩形 方法 fillRect(x, y, width, height) 画一个实心的矩形 clearRect(x, y, width, height) 清除一块儿矩形区域 stroke ...

  6. GO 功能注释

    文章转载于 Original 2017-06-12 liuhui 生信百科 相似的基因在不同物种中,其功能往往保守的.显然,需要一个统一的术语用于描述这些跨物种的同源基因及其基因产物的功能,否则,不同 ...

  7. Windows系统版本型号MSDN版、OEM版、RTM版、VOL版区别

    我们常常听说操作系统的MSDN版.OEM版.RTM版.VOL版等等,它们到底是什么意思,有什么不同呢? (一)MSDN (Microsoft Developer Network)版MSDN软件是微软公 ...

  8. C语言学习笔记---好用的函数memcpy与memset

    这个主要用于我个人的学习笔记,便于以后查询,顺便分享给大家. 想必在用C的时候难免会与数组,指针,内存这几样东西打交道,先以数组为例,例如有一个数组int a[5] = {1, 2, 3, 4, 5} ...

  9. delphi IOS 通知 TNotification

    delphi  IOS 通知 TNotification http://blogs.embarcadero.com/ao/2013/05/01/39450 TNotification http://d ...

  10. Shiro异常处理总结

    出自:https://blog.csdn.net/goodyuedandan/article/details/62420120 一.Spring MVC处理异常有3种方式: (1)使用Spring-M ...