poj3461 Oulipo【KMP】

题目链接:

http://poj.org/problem?id=3461

题目大意:

给一个字符串T,表示文章,再给一个字符串W,表示单词。T和W都只包含26个大写英文字母。

现在计算单词W在文章T中出现的次数。W在T中出现的次数必须连续完全匹配,没两次匹配可能

有重叠的部分。

思路:

先求出字符串W的Next[]指针,然后进行匹配,当一次匹配成功后,继续回退到Next[j]向后进行

匹配,直到字符串T的末尾。此时,得到的匹配成功次数为所求,即W在T中出现的次数。

AC代码:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
const int MAXN = 1000010;

char T[MAXN],W[MAXN];
int Next[MAXN],len1,len2;

void GetNext()
{
    int i = 0,j = -1;
    Next[0] = -1;
    while(i < len2)
    {
        if(j == -1 || W[i] == W[j])
        {
            i++;
            j++;
            Next[i] = j;
        }
        else
            j = Next[j];
    }
}

int KMP()
{
    len1 = strlen(T);
    len2 = strlen(W);
    GetNext();
    int i = 0,j = 0;
    int Ans = 0;
    while(i < len1)
    {
        if(j == -1 || T[i] == W[j])
            i++,j++;
        else
            j = Next[j];
        if(j == len2)
        {
            Ans++;
            j = Next[j];
        }
    }
    return Ans;
}

int main()
{
    int N;
    cin >> N;
    while(N--)
    {
        cin >> W >> T;
        int ans = KMP();
        cout << ans << endl;
    }

    return 0;
}
时间: 2024-10-07 06:10:08

poj3461 Oulipo【KMP】的相关文章

POJ3461 Oulipo 【KMP】

Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22295   Accepted: 8905 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

hdoj 1686 Oulipo【kmp】

Oulipo Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submission(s) : 80   Accepted Submission(s) : 51 Problem Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, without

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 1686:Oulipo 【KMP】

Oulipo Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submission(s) : 45   Accepted Submission(s) : 29 Problem Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, without

POJ2406 Power Strings 【KMP】

Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 31388   Accepted: 13074 Description Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def" then a*b = "

【KMP】【最小表示法】NCPC 2014 H clock pictures

题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1794 题目大意: 两个无刻度的钟面,每个上面有N根针(N<=200000),每个针都是相同的,分别指向Ai,Bi(360°被分成360000小份),问能否将其中一个旋转和另一个重合. 题目思路: [KMP][最小表示法] 循环同构问题.可以写KMP,我懒得写KMP了就写了循环同构的最小表示法. 首先将Ai排序,然后求差(记得取模360000,WA了一次),接下来复制一遍开始匹配. A

【动态规划】【KMP】HDU 5763 Another Meaning

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5763 题目大意: T组数据,给两个字符串s1,s2(len<=100000),s2可以被解读成2种意思,问s1可以解读成几种意思(mod 1000000007). 题目思路: [动态规划][KMP] 题目有点绕,看看样例就懂了.其实不用KMP直接用substr就能做. 首先不解读成另一个意思的话,f[i]=f[i-1],接着如果当前位置能够与s2匹配,那么f[i]+=f[i-strlen(s2)]

HDU3746 Cyclic Nacklace 【KMP】

Cyclic Nacklace Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2538    Accepted Submission(s): 1154 Problem Description CC always becomes very depressed at the end of this month, he has checke

NYOJ327 亲和串 【KMP】

亲和串 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描述 最近zyc遇到了一个很棘手的问题:判断亲和串,以前判断亲和串的时候直接可以看出来,但现在不同了,现在给出的两字符串都非常的大,看的zyc头都晕了.于是zyc希望大家能帮他想一个办法来快速判断亲和串.亲和串定义:给定两个字符串s1和s2,如果能通过s1循环移动,使s2包含在s1中,那么我们就说s2是s1的亲和串. 输入 本题有多组测试数据,每组数据的第一行包含输入字符串s1,第二行包含输入字符串s2,s1与s2的