Match:Oulipo(POJ 3461)

                

                  Oulipo

  题目大意:给你一个字符串,要你找到字符串包含指定子串的个数

  只要你知道了KMP,这一题简直不要太简单,注意STL的string是会超时的,还是乖乖用char吧

  

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <functional>
 4 #include <string.h>
 5
 6 using namespace std;
 7
 8 typedef int Position;
 9 static char Text[1000002], Word[10002];
10 static int Next[10003];
11
12 void KmpSearch(const int, const int);
13 void GetNext(const int);
14
15 int main(void)
16 {
17     int case_sum;
18     scanf("%d", &case_sum);
19     getchar();
20
21     while (case_sum--)
22     {
23         scanf("%s", Word);
24         scanf("%s", Text);
25         KmpSearch(strlen(Word), strlen(Text));
26     }
27     return EXIT_SUCCESS;
28 }
29
30 void KmpSearch(const int w_len, const int t_len)
31 {
32     Position i = 0, j = 0;
33     int k_count = 0;
34     GetNext(w_len);
35
36     while (i < t_len)
37     {
38         if (j == -1 || Text[i] == Word[j])
39         {
40             i++;
41             j++;
42             if (j == w_len)
43             {
44                 k_count++;
45                 j = Next[j];
46             }
47         }
48         else j = Next[j];
49     }
50     printf("%d\n", k_count);
51 }
52
53 void GetNext(const int w_len)
54 {
55     Position i = 0, k = -1;
56     Next[0] = -1;
57
58     while (i < w_len)
59     {
60         if (k == -1 || Word[i] == Word[k])
61         {
62             i++;
63             k++;
64             Next[i] = Word[i] != Word[k] ? k : Next[k];
65         }
66         else k = Next[k];
67     }
68 }

  

时间: 2024-07-29 18:22:21

Match:Oulipo(POJ 3461)的相关文章

AC日记——Oulipo poj 3461

Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 37958   Accepted: 15282 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

Oulipo POJ - 3461(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

Oulipo POJ - 3461 KMP

//以前没分类过 现在该分类一下了#include <iostream> #include <algorithm> #include <stdio.h> #include <set> #include <queue> #include <cstring> #include <string> #include <limits.h> #include <string.h> #include <ve

POJ 3461 Oulipo

E - Oulipo Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 3461 Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter 'e'. He was a member

POJ 3461 Oulipo (求模式串在文本串中出现的次数)

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

[POJ] 3461 Oulipo [KMP算法]

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

poj 3461 Oulipo(KMP模板题)

题目链接:http://poj.org/problem?id=3461 Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 23559   Accepted: 9437 Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter 'e'. He was a

POJ 3461 Oulipo(乌力波)

POJ 3461 Oulipo(乌力波) Time Limit: 1000MS   Memory Limit: 65536K [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: Tou

POJ 3461 Oulipo KMP算法题解

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