HDU - 2087 剪花布条——kmp
2017-08-21 13:40
441 查看
模板题
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 1000 + 10;
char s[maxn], p[maxn];
int lens, lenp, Next[maxn];
void makeNext() {
int pos = 0, len = 0;
Next[pos] = len;
for (pos = 1; pos < lenp; pos++) {
while (len && p[pos] != p[len]) len = Next[len - 1];
if (p[pos] == p[len]) len++;
Next[pos] = len;
}
}
int kmp() {
int ans = 0;
makeNext();
for (int i = 0, j = 0; i < lens; i++) {
while (j && s[i] != p[j]) j = Next[j - 1];
if (s[i] == p[j]) j++;
if (j == lenp) {
ans++;
j = 0;
}
}
return ans;
}
int main() {
while (~scanf("%s", s) && s[0] != '#') {
scanf("%s", p);
lens = strlen(s), lenp = strlen(p);
printf("%d\n", kmp());
}
return 0;
}
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 1000 + 10;
char s[maxn], p[maxn];
int lens, lenp, Next[maxn];
void makeNext() {
int pos = 0, len = 0;
Next[pos] = len;
for (pos = 1; pos < lenp; pos++) {
while (len && p[pos] != p[len]) len = Next[len - 1];
if (p[pos] == p[len]) len++;
Next[pos] = len;
}
}
int kmp() {
int ans = 0;
makeNext();
for (int i = 0, j = 0; i < lens; i++) {
while (j && s[i] != p[j]) j = Next[j - 1];
if (s[i] == p[j]) j++;
if (j == lenp) {
ans++;
j = 0;
}
}
return ans;
}
int main() {
while (~scanf("%s", s) && s[0] != '#') {
scanf("%s", p);
lens = strlen(s), lenp = strlen(p);
printf("%d\n", kmp());
}
return 0;
}
相关文章推荐
- hdu 2087 剪花布条 (kmp入门)
- hdu 2087 剪花布条 --(KMP,匹配串在主串中不可以重叠)
- (KMP 1.3)hdu 2087 剪花布条(求文本串中有几个模式串)
- 剪花布条 (HDU_2087) KMP
- Hdu-2087-剪花布条-【KMP】
- KMP 入门水题 hdu 2087 剪花布条
- HDU - 2087 剪花布条 (KMP)
- HDU 2087 剪花布条 KMP入门
- HDU 2087 剪花布条 KMP极其初级之入门题(KMP模板在这里)
- hdu 2087 剪花布条 KMP
- hdu 2087剪花布条 (KMP入门 子串出现的次数和子串个数)
- HDU-2087 剪花布条 【KMP简单变型】
- HDU- KMP模板题 - 1686 Oulipo - 2087 剪花布条 - 3746 Cyclic Nacklace
- HDU 2087 剪花布条 KMP
- hdu——2087剪花布条 ( kmp 和 find用法 )
- hdu 2087 剪花布条 kmp
- HDU 2087 剪花布条 kmp
- HDU-2087 剪花布条 ( kmp )
- HDU 2087 (KMP不可重叠的匹配) 花布条
- HDU 2087 剪花布条 kmp