题意:

  求子串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. Hibernate学习7—Hibernate 映射继承

    需求:学生有很多照片,分为生活照和工作照: 第一节:每个具体类对应一个表 Student.java: package com.cy.model; import java.util.Set; publi ...

  2. mysql索引之五:多列索引

    索引的三星原则 1.索引将相关的记录放到一起,则获得一星 2.如果索引中的数据顺序和查找中的排列顺序一致则获得二星 3.如果索引中的列包含了查询中的需要的全部列则获得三星 多列索引 1.1.多个单列索 ...

  3. Python中断多重循环的几种思路exit_flag

    常见方法 II. 跳出多重循环 事实上,Python的标准语法是不支持跳出多重循环的,所以只能利用一些技巧,大概的思路有:写成函数.利用笛卡尔积.利用调试. 写成函数 在Python中,函数运行到re ...

  4. javascript或node中的console用法总结

    //建立app.js页面 // 一:页面代码 console.log("log信息"); //在页面中执行(node app.js)这个文件会在控制台中看到log信息:" ...

  5. 【UVA】201 Squares(模拟)

    题目 题目     分析 记录一下再预处理一下.     代码 #include <bits/stdc++.h> int main() { int t=0,s,n; while(scanf ...

  6. CEF3中js调用delphi内部方法

    2015-01-20修改:以下方法不适合delphi7,在CEF3源码中限制了delphi_14 up,对于被我误导的朋友说声抱歉 在CEF1中JS调用delphi的方法已经贴过:http://www ...

  7. sqlldr加载 数据文件的字段超出最大长度

    SQLLOAD数据的时候,建表给的字段给够了,建表已经给到VHARCHAR2(1024)了,实际数据是VHARCHAR2(256).加载报错"数据文件的字段超出最大长度"查了半天, ...

  8. Django的路由层(URLconf)

    URL配置(URLconf)就像Django所支撑网站的目录.它的本质是URL与要为该URL调用的视图函数之间的映射表:你就是以这种方式告诉Django,对于客户端发来的某个URL调用哪一段逻辑代码对 ...

  9. rownum, row_number(), rank() , dense_rank(), partition by ,max() keep 语句的区别与用法

    rownum,rownumber(), rank(),dense_rank()都是用来为记录分配序号的, rownum只能在orderby语句排完序后,在外层嵌套查询才能获得正确的行号,用起来相当复杂 ...

  10. 第五章:Reminders实验:第一部分[Learn Android Studio 汉化教程]

    Learn Android Studio 汉化教程 By now you are familiar with the basics of creating a new project, program ...