一开始总是超时,后来发现还是方法没找对,这个跟普通KMP不太一样的就是,KMP匹配成功的时候会完全跳过已经匹配成功的匹配段,至少我掌握的是.那么如何避免这样的问题呢,举个栗子啊

原串为ABABA,模式串为ABA,当匹配成功的时候,只要跳转到模式串最大公共前后缀长度就行了,ABA的长度为1,所以就是从原串的第二个B开始进行匹配,这样就不会漏了,记录所有匹配成功的次数就是答案.

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
char a[],b[];
int Next[];
int get_next(const char *p)
{
Next[] = ;
int lenp = strlen(p);
for(int i = ,k = ;i < lenp;i++)
{
while(k > && p[k] != p[i])
k = Next[k-];
if(p[k] == p[i]) k++;
Next[i] = k;
}
}
int kmp(const char *t,const char *p)
{
int lent = strlen(t),lenp = strlen(p);
get_next(p);
int ans = ;
for(int i = ,q = ;i < lent;i++)
{
while(q > && p[q] != t[i])
q = Next[q-];
if(p[q] == t[i]) q++;
if(q == lenp)
{
q = Next[q-];///如上述
ans++;
}
}
return ans;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%s%s",b,a);
printf("%d\n",kmp(a,b));
}
return ;
}

HDU 1686 Oulipo(KMP+计算匹配成功次数)的更多相关文章

  1. HDU - 1686 Oulipo KMP匹配运用

    id=25191" target="_blank" style="color:blue; text-decoration:none">HDU - ...

  2. HDU 1686 Oulipo (KMP 可重叠)

    题目链接 Problem Description The French author Georges Perec (1936–1982) once wrote a book, La dispariti ...

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

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

  4. HDU 1686 - Oulipo - [KMP模板题]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1686 Time Limit: 3000/1000 MS (Java/Others) Memory Li ...

  5. hdu 1686 Oulipo kmp算法

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1686 题目: Problem Description The French author George ...

  6. hdu 1686 Oulipo (kmp)

    Problem Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, w ...

  7. HDU 1686 Oulipo kmp裸题

    kmp算法可参考 kmp算法 汇总 #include <bits/stdc++.h> using namespace std; const int maxn=1000000+5; cons ...

  8. 洛谷 P3375 【模板】KMP字符串匹配 || HDU 1686 Oulipo || kmp

    HDU-1686 P3375 kmp介绍: http://www.matrix67.com/blog/archives/115 http://www.cnblogs.com/SYCstudio/p/7 ...

  9. 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 ...

随机推荐

  1. UPX3.03+UpolyX.5 Shell v1.0 汉化绿色版

    软件名称:UPX3.03+UpolyX.5 Shell v1.0 汉化绿色版软件类别:汉化软件运行环境:Windows软件语言:简体中文授权方式:免费版软件大小:635 KB软件等级:整理时间:201 ...

  2. spring AOP 代理机制、执行过程、四种实现方式及示例详解

    1.加载过程 spring首先检测配置文件中的代理配置,然后去加载bean; 如果配置文件中没有配置代理,自然代理不会生效,如果配置了代理,但是代理还没有生效,那么有可能是加载顺序的问题,即在检测到代 ...

  3. IP子网掩码格式转换

    def exchange_maskint(mask_int): bin_arr = [' for i in range(32)] for i in range(mask_int): bin_arr[i ...

  4. A. Launch of Collider Codeforces Round #363 (Div2)

    A. Launch of Collider time limit per test 2 seconds memory limit per test 256 megabytes input standa ...

  5. 解决安装WordPress主题及插件需要输入FTP问题

    http://www.zhanghenglei.com/wordpress-ftp-update/ 使用Wordpress程序架构的网站如果需要在网站后台升级.安装主题或者插件的时候,总是会提示需要我 ...

  6. jquery操作HTML5 的data-*的用法实例分享

    .mm{width:256px; height:200px;} .mm[data-name='张含韵']{background:url(http://image.zhangxinxu.com/imag ...

  7. NetBox v2.8下载使用指南

    2008-09-20 11:21:05|  分类: ASP|举报|字号 订阅     NetBox v2.8下载地址: http://down.chinaz.com/soft/13211.htm 安装 ...

  8. 面向对象的特性-为String类型的变量扩展一个replaceAll()函数

    ———————————————————————————— <script type="text/javascript">                    //按钮 ...

  9. HTML 锚点链接,链接到同一个页面的不同位置

    <html>    <head>        <title></title>    </head>    <body>     ...

  10. java中list集合的内容,如何使用像数据库中group by形式那样排序

    java中list集合的内容,如何使用像数据库中group by形式那样排序,比如:有一个 List<JavaBean> 他中包含了一些如下的内容JavaBean:name    mone ...