本题就是给出非常多对字符串,然后问一个字符串在另外一个字符串出现的次数。

就是所谓的Strstr函数啦。

Leetcode有这道差点儿一模一样的题目。

使用KMP算法加速。算法高手必会的算法了。

另外看见讨论说什么使用KMP还超时,最大可能是没有真正理解next table的含义,写了错误的代码,故此尽管自己执行结果正确,可是却没有真正发挥next table的作用。使得算法退化为暴力法了,所以执行正确,但超时。

KMP參考: http://blog.csdn.net/kenden23/article/details/14178121

#include <stdio.h>
#include <string.h> const int MAX_N = 10001;
const int MAX_T = 1000001;
char word[MAX_N];
char text[MAX_T];
int nextTbl[MAX_N];
int wn, tn; int getTimes()
{
int ans = 0;
int i = 0, j = 0; //i为text的当前下标,j为word的当前下标
for (; i-j <= tn-wn; i++)
{
if (text[i] == word[j])
{
j++;
if (j == wn)
{
ans++;
j = nextTbl[j-1];
}
}
else if (j > 0)
{
j = nextTbl[j-1];
i--;
}
}
return ans;
} void genTbl()
{
memset(nextTbl, 0, sizeof(int) * (wn)); int i = 1, j = 0;
while (i < wn)
{
if (word[i] == word[j]) nextTbl[i++] = ++j;
else if (j > 0) j = nextTbl[j-1];
else i++;
}
} int main()
{
int T;
scanf("%d", &T);
getchar();
while (T--)
{
gets(word);//fgets(word, MAX_N, stdin);//fgets会在末尾保留'\n'
gets(text);//fgets(text, MAX_T, stdin);
wn = strlen(word);
tn = strlen(text);
genTbl();
printf("%d\n", getTimes());
}
return 0;
}

POJ 3461 Oulipo KMP算法题解的更多相关文章

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

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

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

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

  3. POJ 3461 Oulipo KMP

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

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

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

  5. POJ 3461 Oulipo(KMP,模式串在主串中出现次数 可重叠)

    题意:给你两个字符串p和s,求出p在s中出现的次数. 显然,我们要先把模式串放到前面,之后主串放后面,中间隔开,这样就可以根据前缀数组的性质来求了. 我先想直接把p接到s前面,之后求Next数组对st ...

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

  7. POJ 3461 Oulipo(乌力波)

    POJ 3461 Oulipo(乌力波) Time Limit: 1000MS   Memory Limit: 65536K [Description] [题目描述] The French autho ...

  8. POJ 3461 Oulipo[附KMP算法详细流程讲解]

      E - Oulipo Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit ...

  9. POJ 3461 Oulipo(字符串匹配,KMP算法)

    题意:给出几组数据,每组有字符串W和T,问你W在T中出现几次. 思路:字符串长度很大,用KMP算法. 一开始写的是:调用KMP算法查找W在T中是否匹配,若匹配,则个数+1.则接下来T的索引移动相应的距 ...

随机推荐

  1. Centos7不修改默认交换分区下添加交换分区

    交换分区介绍 Linux系统中的交换分区是当物理内存(RAM)被充满时,作为物理内存的缓存来使用. 当系统需要更多的内存资源而物理内存已经充满,内存中不活跃的页就会被移动到交换分区上. 交换分区位于硬 ...

  2. 【Codeforces Beta Round #45 D】Permutations

    [题目链接]:http://codeforces.com/problemset/problem/48/D [题意] 给你n个数字; 然后让你确定,这n个数字是否能由若干个(1..x)的排列连在一起打乱 ...

  3. 转载-- Qt Creator编译时make: arm-linux-g++: command not found 错误!

    前提是已经配置好交叉编译器,但是qt creator找不到. 解决方法: 修改 /usr/local/Trolltech/QtEmbedded-4.7.0-arm/mkspecs/qws/linux- ...

  4. IOS &#39;NSInternalInconsistencyException&#39;

    今天想写一个请求的天气.好的.废话不多说.先贴代码: 使用AFNetWorking 发送get请求,可是一直报错  IOS 'NSInternalInconsistencyException', re ...

  5. poj--2007--Scrambled Polygon(数学几何基础)

    Scrambled Polygon Time Limit: 1000MS   Memory Limit: 30000KB   64bit IO Format: %I64d & %I64u Su ...

  6. Android CardView卡片布局 标签: 控件

    CardView介绍 CardView是Android 5.0系统引入的控件,相当于FragmentLayout布局控件然后添加圆角及阴影的效果:CardView被包装为一种布局,并且经常在ListV ...

  7. Atcoder B - Moderate Differences

    http://agc017.contest.atcoder.jp/tasks/agc017_b B - Moderate Differences Time limit : 2sec / Memory ...

  8. 我的头上碧空晴朗——数据库存datetime问题

    今天遇到一个问题,数据库mysql存的datetime类型数据.取出来数据居然耍流氓,好好的日期在秒后多了个小数点0 当我用正常的方法, SimpleDateFormat myFmt=new Simp ...

  9. github连接报"ssh: connect to host github.com port 22: Connection timed out"错误

    1. 异常 在连接github时,执行"ssh -T git@github.com" 命令时,出现 ssh: connect to host github.com port 22: ...

  10. vue.js 第一课:实例化vue

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...