Skip to content

Commit

Permalink
Add: Problem 1015
Browse files Browse the repository at this point in the history
  • Loading branch information
MyYaYa committed Apr 3, 2017
1 parent c3fdfde commit b0c3228
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions src/1015/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
using namespace std;

vector<int> compute(string p);
int kmp(string, string);

int main() {
int num;
cin >> num;
for (int i = 0; i < num; i++) {
string a;
string p;
cin >> p;
cin >> a;
cout << kmp(a,p) << endl;
}
return 0;
}

vector<int> compute(string p) {
int pl = p.length();
vector<int> next(pl+1, 0);
for (int i = 1; i < pl; i++) {
int j = i;
while (j > 0) {
j = next[j];
if (p[j] == p[i]) {
next[i+1] = j + 1;
break;
}
}
}
return next;
}

int kmp(string a, string p) {
int ans = 0;
int al = a.length();
int pl = p.length();
vector<int> next;
next = compute(p);

for (int i = 0, j = 0; i < al; i++) {
if (j < pl && a[i] == p[j]) j++;
else {
while (j > 0) {
j = next[j];
if (a[i] == p[j]) {
j++;
break;
}
}
}
if (j == pl) ans++;
}
return ans;
}

0 comments on commit b0c3228

Please sign in to comment.