码迷,mamicode.com
首页 > 其他好文 > 详细

Match:Oulipo(POJ 3461)

时间:2016-02-04 06:40:48      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:

                技术分享

                  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 }

  技术分享

Match:Oulipo(POJ 3461)

标签:

原文地址:http://www.cnblogs.com/Philip-Tell-Truth/p/5180865.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!