hrbust/哈理工oj 2060 截取方案数【KMP匹配】
2016-03-10 17:17
295 查看
截取方案数 | ||||||
| ||||||
Description | ||||||
给定一个模式串T,主串S,问:从S中截取T有多少种方案? | ||||||
Input | ||||||
有多组测试数据,对于每组测试数据,第一行是模式串T,第二行是主串S,数据中仅包含大小写字母和数字,模式串T长度不超过10^4, 主串S长度不超过10^5。 注意:数据是随机的。 | ||||||
Output | ||||||
对于每组测试数据,输出一行,为截取方案数。 | ||||||
Sample Input | ||||||
abc abcdaabcab abcd abcdaabcab aba abababa | ||||||
Sample Output | ||||||
2 1 3 | ||||||
Source | ||||||
HCPC2014校赛训练赛 3 |
#include<stdio.h>
#include<string.h>
using namespace std;
int next[200005];
char a[2000005];
char b[2000005];
int lena;
int lenb;
int output;
void set_naxt()//子串的next数组
{
int i=0,j=-1;
next[0]=-1;
while(i<lenb)
{
if(j==-1||b[i]==b[j])
{
i++; j++;
next[i]=j;
}
else
j=next[j];
}
}
int kmp()
{
int i=0,j=0;
set_naxt();
while(i<lena)
{
if(j==-1||a[i]==b[j])
{
i++;j++;
}
else
j=next[j];
if(j==lenb)
{
output++;
}
}
}
int main()
{
while(~scanf("%s",b))
{
output=0;
scanf("%s",a);
lena=strlen(a);
lenb=strlen(b);
kmp();
printf("%d\n",output);
}
}
相关文章推荐
- Hadoop 解除 "Name node is in safe mode"
- HDU 1.1.2 A+B for Input-Output Practice (II)
- http上传文件和进度监控以及构造multipart/form-data请求
- iOS----- Crash 分析(文三)- 符号化崩溃日志
- iOS----- Crash 分析(文二)-崩溃日志组成
- datagrid后台给每列添加js方法
- [Leetcode]@python 109. Convert Sorted List to Binary Search Tree
- Redis常用命令
- JAVA改错学习法
- MongoDB学习—(5)修改器$inc,$unset,$push,$pushAll,$allToSet,$pop,$pull,$pullAll
- 3722: PA2014 Final Budowa|树形DP
- 梯度下降,随机梯度下降,批量梯度下降,mini-batch 梯度下降
- centos7 安装redis及遇到的问题
- ios 'Can't add self as subview'崩溃日志详解
- ios的toolbar的item的几种简单布局
- Android系统启动分析
- static c++
- matlab s变换
- checkbox判断选中的三种方法
- 原生态JAVAEE酒店管理系统系列六