HDU 1686 Oulippo

http://acm.hdu.edu.cn/showproblem.php?pid=1686

题意:给定一个文本串和给定一个模式串,求文本串中有几个模式串。

思路:直接套用KMP模板。

 1 #include<iostream>
 2 #include<cstring>
 3 #include<string>
 4 using namespace std;
 5
 6 const int maxn = 1000000 + 5;
 7
 8 int next[10002], n, m;
 9 char text[maxn], pattern[10002];
10
11 void get_next()
12 {
13     int i, j;
14     i = -1;
15     j = 0;
16     ::next[0] = -1;
17     while (j<m)
18     {
19         if (i == -1 || pattern[i] == pattern[j])
20         {
21             ::next[++j] = ++i;
22         }
23         else
24             i = ::next[i];
25     }
26 }
27
28 int KMP()
29 {
30     int i, j, count = 0;
31     i = j = 0;
32     get_next();
33     while (i<n)
34     {
35         if (j == -1 || text[i] == pattern[j])
36         {
37             i++;
38             j++;
39             if (j == m)  count++;
40         }
41         else
42             j = ::next[j];
43     }
44     return count;
45 }
46
47 int main()
48 {
49     //freopen("D:\\txt.txt", "r", stdin);
50     int t, ans;
51     cin >> t;
52     getchar();
53     while (t--)
54     {
55         scanf("%s", pattern);
56         scanf("%s", text);
57         n = strlen(text);
58         m = strlen(pattern);
59         ans = KMP();
60         cout << ans << endl;
61     }
62     return 0;
63 }
时间: 2024-08-06 13:16:27

HDU 1686 Oulippo的相关文章

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匹配运用

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 题目大意:寻找子链在母链中出现的次数. 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

hdu 1686 KMP模板

1 // hdu 1686 KMP模板 2 3 // 没啥好说的,KMP裸题,这里是MP模板 4 5 #include <cstdio> 6 #include <iostream> 7 #include <cstring> 8 #include <algorithm> 9 10 using namespace std; 11 12 const int MAX_N = 1000008; 13 const int MAX_M = 10008; 14 char T

[kuangbin带你飞]专题十六 KMP &amp; 扩展KMP &amp; Manacher B - Oulipo HDU - 1686(kmp)

B - Oulipo HDU - 1686 题目链接:https://vjudge.net/contest/70325#problem/B 题目: 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 Pa

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

Oulipo http://acm.hdu.edu.cn/showproblem.php?pid=1686 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the l

HDU 1686 Oulipo 求大串中最多可匹配多少个小串(kmp)

http://acm.hdu.edu.cn/showproblem.php?pid=1686 Oulipo Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 6098    Accepted Submission(s): 2448 Problem Description The French author Georges Perec (19

hdu 1686 &amp; poj 2406 &amp; poj 2752 (KMP入门三弹连发)

首先第一题 戳我穿越;http://acm.hdu.edu.cn/showproblem.php?pid=1686 题目大意好理解,每组输入一个子串和一个母串,问在母串中有多少个子串? 文明人不要暴力,因为宽度会超时,除去暴力后这就是赤果果的KMP KMP的重点在于在子串中建立一个匹配表,记录 到每一位的 前缀后缀 中的相同的子子串的最大长度 然后在比较子母串的时候当遇到不同时 后移的位数就是前面相同的个数减去对应的匹配表例的数 额 讲的不清不楚 那推荐戳这里:http://kb.cnblogs