[hdu2087]kmp水题

题意:求模板串在文本串中出现的次数(位置无交叉)。只需在找到的时候把模板串指针归0即可。

  1 #pragma comment(linker, "/STACK:10240000,10240000")
  2 
  3 #include <iostream>
  4 #include <cstdio>
  5 #include <algorithm>
  6 #include <cstdlib>
  7 #include <cstring>
  8 #include <map>
  9 #include <queue>
 10 #include <deque>
 11 #include <cmath>
 12 #include <vector>
 13 #include <ctime>
 14 #include <cctype>
 15 #include <set>
 16 #include <bitset>
 17 #include <functional>
 18 #include <numeric>
 19 #include <stdexcept>
 20 #include <utility>
 21 
 22 using namespace std;
 23 
 24 #define mem0(a) memset(a, 0, sizeof(a))
 25 #define mem_1(a) memset(a, -1, sizeof(a))
 26 #define lson l, m, rt << 1
 27 #define rson m + 1, r, rt << 1 | 1
 28 #define define_m int m = (l + r) >> 1
 29 #define rep_up0(a, b) for (int a = 0; a < (b); a++)
 30 #define rep_up1(a, b) for (int a = 1; a <= (b); a++)
 31 #define rep_down0(a, b) for (int a = b - 1; a >= 0; a--)
 32 #define rep_down1(a, b) for (int a = b; a > 0; a--)
 33 #define all(a) (a).begin(), (a).end()
 34 #define lowbit(x) ((x) & (-(x)))
 35 #define constructInt4(name, a, b, c, d) name(int a = 0, int b = 0, int c = 0, int d = 0): a(a), b(b), c(c), d(d) {}
 36 #define constructInt3(name, a, b, c) name(int a = 0, int b = 0, int c = 0): a(a), b(b), c(c) {}
 37 #define constructInt2(name, a, b) name(int a = 0, int b = 0): a(a), b(b) {}
 38 #define pchr(a) putchar(a)
 39 #define pstr(a) printf("%s", a)
 40 #define sstr(a) scanf("%s", a)
 41 #define sint(a) scanf("%d", &a)
 42 #define sint2(a, b) scanf("%d%d", &a, &b)
 43 #define sint3(a, b, c) scanf("%d%d%d", &a, &b, &c)
 44 #define pint(a) printf("%d
", a)
 45 #define test_print1(a) cout << "var1 = " << a << endl
 46 #define test_print2(a, b) cout << "var1 = " << a << ", var2 = " << b << endl
 47 #define test_print3(a, b, c) cout << "var1 = " << a << ", var2 = " << b << ", var3 = " << c << endl
 48 
 49 typedef double db;
 50 typedef long long LL;
 51 typedef pair<int, int> pii;
 52 typedef multiset<int> msi;
 53 typedef set<int> si;
 54 typedef vector<int> vi;
 55 typedef map<int, int> mii;
 56 
 57 const int dx[8] = {0, 0, -1, 1, 1, 1, -1, -1};
 58 const int dy[8] = {-1, 1, 0, 0, 1, -1, 1, -1 };
 59 const int maxn = 2e5 + 7;
 60 const int md = 10007;
 61 const int inf = 1e9 + 7;
 62 const LL inf_L = 1e18 + 7;
 63 const double pi = acos(-1.0);
 64 const double eps = 1e-6;
 65 
 66 template<class T>T gcd(T a, T b){return b==0?a:gcd(b,a%b);}
 67 template<class T>bool max_update(T &a,const T &b){if(b>a){a = b; return true;}return false;}
 68 template<class T>bool min_update(T &a,const T &b){if(b<a){a = b; return true;}return false;}
 69 template<class T>T condition(bool f, T a, T b){return f?a:b;}
 70 template<class T>void copy_arr(T a[], T b[], int n){rep_up0(i,n)a[i]=b[i];}
 71 int make_id(int x, int y, int n) { return x * n + y; }
 72 
 73 struct KMP {
 74     int next[1000010];
 75     void GetNext(char s[]) {
 76         mem0(next);
 77         next[0] = next[1] = 0;
 78         for(int i = 1; s[i]; i++) {
 79             int j = next[i];
 80             while(j && s[i] != s[j]) j = next[j];
 81             next[i + 1] = s[j] == s[i]? j + 1 : 0;
 82         }
 83     }
 84     int find(char s[], char s2[]) {
 85         int j = 0, len = strlen(s2), ans = 0;
 86         for (int i = 0; s[i]; i++) {
 87             while (j && s[i] != s2[j]) j = next[j];
 88             if (s[i] == s2[j]) j++;
 89             if (j == len) {
 90                 ans++;
 91                 j = 0;
 92             }
 93         }
 94         return ans;
 95     }
 96 };
 97 
 98 KMP kmp;
 99 char s[maxn], s2[maxn];
100 
101 int main() {
102     //freopen("in.txt", "r", stdin);
103     while (scanf("%s", s)) {
104         if (s[0] == '#') break;
105         scanf("%s", s2);
106         kmp.GetNext(s2);
107         cout << kmp.find(s, s2) << endl;
108     }
109     return 0;
110 }
View Code
原文地址:https://www.cnblogs.com/jklongint/p/4449246.html