HDU 1686 Oulipo kmp模板
2016-07-23 16:24
288 查看
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1686
题意:给一个模式串,一个目标串,问目标串中有几个模式串
思路:kmp模板题,留个模板。。。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 10010;
char s1
, s2[N*100];
int nt
;
void getnt()
{
int i = 0, j = -1;
nt[0] = -1;
while(s1[i])
{
if(j == -1 || s1[i] == s1[j])
{
i++, j++;
if(s1[i] != s1[j]) nt[i] = j;
else nt[i] = nt[j];
}
else j = nt[j];
}
}
int kmp()
{
int i = 0, j = 0, cnt = 0;
getnt();
while(s2[i])
{
if(j == -1 || s2[i] == s1[j])
i++, j++;
else j = nt[j];
if(j != -1 && ! s1[j])
cnt++, j = nt[j];
}
return cnt;
}
int main()
{
int t;
scanf("%d", &t);
while(t--)
{
scanf(" %s %s", s1, s2);
printf("%d\n", kmp());
}
return 0;
}
题意:给一个模式串,一个目标串,问目标串中有几个模式串
思路:kmp模板题,留个模板。。。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 10010;
char s1
, s2[N*100];
int nt
;
void getnt()
{
int i = 0, j = -1;
nt[0] = -1;
while(s1[i])
{
if(j == -1 || s1[i] == s1[j])
{
i++, j++;
if(s1[i] != s1[j]) nt[i] = j;
else nt[i] = nt[j];
}
else j = nt[j];
}
}
int kmp()
{
int i = 0, j = 0, cnt = 0;
getnt();
while(s2[i])
{
if(j == -1 || s2[i] == s1[j])
i++, j++;
else j = nt[j];
if(j != -1 && ! s1[j])
cnt++, j = nt[j];
}
return cnt;
}
int main()
{
int t;
scanf("%d", &t);
while(t--)
{
scanf(" %s %s", s1, s2);
printf("%d\n", kmp());
}
return 0;
}
相关文章推荐
- android 主题背景的切换
- angularjs自定义指令
- Ubuntu下matplotlib中文无法显示的解决方法
- OpenCV | grabcut
- C#中的垃圾回收
- Java中的hashcode方法
- POJ 1016 不断压缩字符串判断三种结果
- Gradle学习
- hdoj2035 人见人爱A^B(快速幂+同余定理)
- 通过异步任务将一张由网络得来图片显示出来
- Where Are You Standing?
- 【HDU】3501 - Calculation 2(欧拉函数,互质数之和公式)
- OpenCV | 滤波
- iOS编码规范
- 如何使用mybatis《一》
- BUAA & ICT 夏令营之旅
- Valid Anagram
- 自己的简单HEARTBEAT笔记
- Directx中的深度缓冲 前缓冲 后缓冲
- android中的反射机制