Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdio>
- #include <cstdlib>
- #include <cmath>
- #include <climits>
- #include <cstring>
- #include <string>
- #include <vector>
- #include <list>
- #include <queue>
- #include <stack>
- #include <map>
- #include <set>
- #include <bitset>
- #include <algorithm>
- #include <functional>
- #include <iomanip>
- using namespace std;
- #define LL long long
- const int maxn = 1000000 + 100;
- int T;
- int Next[maxn];
- char str1[maxn], str2[maxn];
- void get_next(char *str) {
- int j = 0;
- for(int i = 2; str[i]; ++i) {
- while(j != 0 && str[i] != str[j + 1]) {
- j = Next[j];
- }
- if(str[i] == str[j + 1]) {
- ++j;
- }
- Next[i] = j;
- }
- }
- int kmp(char *str1, char *str2) {
- int j = 0;
- int ret = 0;
- for(int i = 1; str1[i]; ++i) {
- while(j != 0 && str1[i] != str2[j + 1]) {
- j = Next[j];
- }
- if(str1[i] == str2[j + 1]) {
- ++j;
- }
- if(str2[j + 1] == '\0') {
- j = Next[j];
- ++ret;
- }
- }
- return ret;
- }
- int main() {
- #ifdef LOCAL
- freopen("test.txt", "r", stdin);
- // freopen("out.txt", "w", stdout);
- #endif // LOCAL
- ios::sync_with_stdio(false);
- scanf("%d", &T);
- while(T--) {
- scanf("%s%s", str1 + 1, str2 + 1);
- get_next(str1);
- printf("%d\n", kmp(str2, str1));
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement