HDU 1686 Oulipo

 1 #include <iostream>
 2 #include <cstring>
 3 #include <string>
 4 #include <algorithm>
 5 using namespace std;
 6 
 7 char a[1000100];
 8 char b[10100];
 9 int Len_a, Len_b, sum;
10 int Next[10100];
11 
12 void GetNext()
13 {
14     int i = 0, j = Next[0] = -1;
15     while (i < Len_b)
16     {
17         if (j == -1 || b[i] == b[j])
18         {
19             i++;
20             j++;
21             Next[i] = (b[i] == b[j] ? Next[j] : j);
22         }
23         else
24             j = Next[j];
25     }
26 }
27 
28 void KMP()
29 {
30     int i = 0, j = 0;
31     while (i < Len_a)
32     {
33         if (j == -1 || a[i] == b[j])
34         {
35             i++;
36             j++;
37             if (j == Len_b)
38             {
39                 sum++;
40                 j = Next[j];
41             }
42         }
43         else
44             j = Next[j];
45     }
46 }
47 
48 int main(void)
49 {
50     ios::sync_with_stdio(false);
51     
52     int cas;
53     cin >> cas;
54     while (cas--)
55     {
56         cin >> b >> a;
57         Len_a = strlen(a);
58         Len_b = strlen(b);
59         GetNext();
60         sum = 0;
61         KMP();
62         cout << sum << endl;
63     }
64     
65     return 0;
66 }

 

posted @ 2018-04-23 21:19  duck_lu  阅读(140)  评论(0编辑  收藏  举报