HDU 1618 Oulipo KMP题解

给出两个字符串,寻找一个字符串在另外一个字符串出现的频率。

原来kmp还有一个陷阱,下面注释出了,下标没步进好,就有一定几率出现超时的,也有一定几率出现错误,视具体的串而定。

修改一下就好了,kmp速度是很快的。

#include <stdio.h>
#include <string.h>

const int MAX_TXT = 1000001;
const int MAX_WORD = 10001;
int gWlen, gTlen;
char word[MAX_WORD];
int next[MAX_WORD];
char txt[MAX_TXT];

void getNext()
{
    memset(next, 0, sizeof(int) * gWlen);
    for (int i = 1, j = 0; i < gWlen; )
    {
        if (word[i] == word[j]) next[i++] = ++j;
        else if (j > 0) j = next[j-1];
        else i++;
    }
}

int kmpSearch()
{
    int i = 0, j = 0, ans = 0;
    for (; gWlen-j <= gTlen-i; )
    {
        if (word[j] == txt[i])
        {
            j++; i++;
            if (j == gWlen)
            {
                ans++;
                j = next[j-1];
            }
            //else i++; i++放这里会超时,是一定几率会出现超时,注意了!
        }
        else if (j > 0) j = next[j-1];
        else i++;
    }
    return ans;
}

int main()
{
    int T;
    scanf("%d", &T);
    getchar();
    while (T--)
    {
        gets(word);
        gWlen = strlen(word);
        gets(txt);
        gTlen = strlen(txt);

        getNext();
        printf("%d\n", kmpSearch());
    }
    return 0;
}

HDU 1618 Oulipo KMP题解,布布扣,bubuko.com

时间: 2024-10-29 10:46:00

HDU 1618 Oulipo KMP题解的相关文章

HDU - 1686 Oulipo KMP匹配运用

HDU - 1686 Oulipo Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u Submit Status Description The French author Georges Perec (1936?1982) once wrote a book, La disparition, without the letter 'e'. He was a member of the Ouli

hdu 1686 Oulipo kmp算法

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1686 题目: Problem Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter 'e'. He was a member of the Oulipo group. A quote from the book: Tout avait

HDU - 1686 Oulipo (KMP)

Problem Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter 'e'. He was a member of the Oulipo group. A quote from the book: Tout avait Pair normal, mais tout s'affirmait faux. Tout avait Fair

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

HDU-1686 P3375 kmp介绍: http://www.cnblogs.com/SYCstudio/p/7194315.html http://blog.chinaunix.net/uid-8735300-id-2017161.html(mp&kmp) http://www-igm.univ-mlv.fr/~lecroq/string/node8.html(mp&kmp,看上去很正确的例程) http://blog.csdn.net/joylnwang/article/detai

POJ 3461 Oulipo KMP算法题解

本题就是给出很多对字符串,然后问一个字符串在另外一个字符串出现的次数. 就是所谓的Strstr函数啦. Leetcode有这道几乎一模一样的题目. 使用KMP算法加速,算法高手必会的算法了. 另外看见讨论说什么使用KMP还超时,最大可能是没有真正理解next table的含义,写了错误的代码,故此虽然自己运行结果正确,但是却没有真正发挥next table的作用,使得算法退化为暴力法了,所以运行正确,但超时. KMP参考: http://blog.csdn.net/kenden23/articl

HDU 1711 Number Sequence KMP题解

KMP查找整数数列,不是查找字符串. 原理是一样的,不过把字符串转换为数列,其他基本上是一样的. #include <stdio.h> #include <string.h> const int MAX_N = 1000001; const int MAX_M = 10001; int strN[MAX_N], strM[MAX_M], next[MAX_M], N, M; void getNext() { memset(next, 0, sizeof(int) * M); for

HDU 1686 Oulipo(KMP)

Problem Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter 'e'. He was a member of the Oulipo group. A quote from the book: Tout avait Pair normal, mais tout s'affirmait faux. Tout avait Fair

HDU 2594 Simpsons’ Hidden Talents KMP题解

KMP的应用.直接使用s1产生next 数组,然后在s2中搜索s1,那么记录最后一个搜索到的数值,就是s1的前缀在s2中的最长后缀了. 本题应该不能直接调用strstr了吧. #include <stdio.h> #include <vector> #include <string.h> #include <algorithm> #include <iostream> #include <string> #include <li

hdu 1686 Oulipo (kmp)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1686 题目大意:寻找子链在母链中出现的次数. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 int next[10010],sum,lens,lenc; 6 char str[10010],ch[1000010]; 7 8 int get_nex