题目大意 : 在一个字符串中找出目标单词的个数

代码:

      #include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<queue>
#include<algorithm>
#include<cmath>
#include<map>
using namespace std;
#define INF 0x7fffffff char t[1000010],w[10010];
int next[10010];
void getnext(int wlen){
int i,j;
next[0] = 0;
next[1] = 0;
for(i=1;i<wlen;i++){
j = next[i];
while(j && w[i] != w[j]) j = next[j];
if(w[i] == w[j]) next[i+1] = j+1 ;
else next[i+1] = 0 ;
}
} int KMP(){
int i,j,tlen,wlen,cnt = 0;
tlen = strlen(t);
wlen = strlen(w);
j = 0;
getnext(wlen);
for(i=0;i<tlen;i++){
while(j && w[j] != t[i]) j = next[j];
if(w[j] == t[i]) j++;
if(j>=wlen)
cnt ++;
}
return cnt ;
} int main(){
int i,j,n,cnt;
cin >> n;
for(i=1;i<=n;i++){
scanf("%s%s",w,t);
printf("%d\n",KMP());
} return 0;
}

POJ Oulipo (KMP)的更多相关文章

  1. POJ Oulipo KMP 模板题

    http://poj.org/problem?id=3461 Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4 ...

  2. POJ Oulipo(KMP模板题)

    题意:找出模板在文本串中出现的次数 思路:KMP模板题 #include<cstdio> #include<cstring> #include<cmath> #in ...

  3. poj3461 Oulipo —— KMP

    题目链接:http://poj.org/problem?id=3461 代码如下: #include<cstdio>//poj 3461 kmp #include<cstring&g ...

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

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

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

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

  6. POJ 3641 Oulipo KMP 水题

    http://poj.org/problem?id=3461 直接KMP就好.水题 #include<cstdio> #include<cstring> const int M ...

  7. POJ 3461 Oulipo KMP

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

  8. POJ 3461 Oulipo KMP算法题解

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

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

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

随机推荐

  1. eclipse js中 选中方法按F3快捷键不能跳转到对应方法的解决方案

    这种情况很可能是m2e-wtp插件没有安装的,安装插件成功后即可解决. m2e-wtp插件安装参照相应随笔.

  2. android图标设计事宜

    1.Launcher图标 图标的最佳宽高是48x48 dp. ldpi:36*36px,0.75倍密度,一般不用提供,系统会从hdpi取图缩小1倍. mdpi:48*48px, 1倍密度 hdpi:7 ...

  3. MongoDB 操作手冊CRUD插入

    插入操作 插入记录 1.插入一条记录 db.testData.insert({num:1,name:'a'}); 结果 WriteResult({ "nInserted" : 1 ...

  4. samba后台进程及安全模式简介

    识别 Samba 后台程序Linux 服务器通常作为守护程序(daemon) 来实现,这一词源于希腊神话,其中守护神(daemon)是超自然生物.Linux 守护程序在后台运行以便执行一些有用的任务. ...

  5. iptables 实现centos内网机器访问外网

    环境:一台带外网和内网的机器,另一台只有内网,默认不能上网.两台机器都是centos系统带外网机器的外网ip为 123.221.20.11, 内网ip为 192.168.15.100内网机器的内网ip ...

  6. sql获取第n条数据

    select * from (select top n * from students) aa where not exists(select * from (select top n-1 * fro ...

  7. Action重定向总结

    [HttpPost] public ActionResult StudentList( string StudName, string studName, DateTime BirthDay, For ...

  8. Android应用中使用Popupmenu

    最终效果: 页面布局: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns: ...

  9. CaseFile

    出处 http://tools.kali.org/information-gathering/casefile CaseFile is the little brother to Maltego. I ...

  10. ORA-00257: archiver error. Connect internal only, until freed 错误的处理方法

    转帖:原文地址http://blog.csdn.net/panys/article/details/3838846 archive log 日志已满ORA-00257: archiver error. ...