HDU 2087 剪花布条

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

题意:给定一个文本串和给定一个模式串,求文本串中有几个模式串。匹配成功的串不可以再使用。

思路:题目不难,还是可以直接套用KMP模板。需要注意的就是如果在主串中如果成功匹配了一次,那么模式串应该回到开头,而不是j=next[j]。

         例如:aaaaaa

         答案要求是输出3,如果j=next[j]的话,会输出5,。这样就不对了,因为重复使用了a。这点需要注意。

 1 #include<iostream> 
 2 #include<cstring>
 3 #include<string>
 4 using namespace std;
 5 
 6 const int maxn = 1000+5;
 7 
 8 int next[maxn], n, m;
 9 char text[maxn], pattern[maxn];
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)  
40             {
41                 count++; 
42                 j = 0;  //如果匹配成功一个,则j返回模式串开头,而不是j=next[j]
43             }
44         }
45         else
46             j = ::next[j];
47     }
48     return count;
49 }
50 
51 int main()
52 {
53     //freopen("D:\\txt.txt", "r", stdin);
54     int t, ans;
55     while (scanf("%s", text)&& text[0]!='#')
56     {
57         scanf("%s", pattern);
58         //cout << pattern << endl << text << endl;
59         n = strlen(text);
60         m = strlen(pattern);
61         ans = KMP();
62         cout << ans << endl;
63     }
64     return 0;
65 }

 

posted @ 2017-01-30 12:56  Kayden_Cheung  阅读(149)  评论(0编辑  收藏  举报
//目录