题意:

  求子串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. 减少TIME_WAIT时间的优化配置

    减少TIME_WAIT时间的优化配置 建立TCP需要三次握手才能建立,而断开连接则需要四次握手.整个过程如下图所示: net.ipv4.tcp_max_syn_backlog=8192 增加TCP S ...

  2. Java 中的this关键字

    1.this关键字代表当前对象 this.属性 操作当前对象的属性 this.方法 调用当前对象的方法 2.封装对象的属性的时候,经常会使用this关键字 public class Telphone ...

  3. POJ 3684 Physics Experiment(弹性碰撞)

    Physics Experiment Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2936   Accepted: 104 ...

  4. html2pdf 中文支持问题

    系统用的是HTML2PDF V4.0.3 版本 百度后 http://blog.sina.com.cn/s/blog_6b0ce0310101fdv6.html 发现中文支持不好 还是有乱码问题 解决 ...

  5. Centos下Apache+Tomcat集群--搭建记录

    一.目的 利用apache的mod_jk模块,实现tomcat集群服务器的负载均衡以及会话复制,这里用到了<Cluster>. 二.环境 1.基础:3台主机,系统Centos6.5,4G内 ...

  6. bbbbb

  7. Julia - 字符串判断函数

    isascii() 判断是否是 ascii 码,返回 Bool 值 julia> isascii('a') true julia> isascii('α') false julia> ...

  8. spring 4.0+quartz2.2 实现持久化

    最近在搭建框架 用到quartz持久化这块 查了一些文档  如下配置即可. 这里是quartz官方提供配置步骤 http://www.quartz-scheduler.org/ Quartz包含三个抽 ...

  9. VC6.0打开文件是卡死的解决办法

    删除工程目录下的 .ncb .opt 文件,然后就OK了!

  10. 什么是事件代理?DOM2.0标准事件模型的三个阶段

    体验更优排版请移步原文:http://blog.kwin.wang/programming/js-event-delegation.html 事件代理,又称事件委托(Delegation),就是将处理 ...