hdu 1686 KMP算法
题意:
求子串w在T中出现的次数。
kmp算法详解:http://www.cnblogs.com/XDJjy/p/3871045.html
#include <iostream>
#include <cstring>
#include <string>
#include <cstdio> using namespace std;
int lenp;
int lens;
void getnext(int *next, char *p)
{
int j = , k = -;
next[] = -;
while(j < lenp)
{ if(k == - || p[j] == p[k])
{
j++;
k++;
next[j] = k;
}
else
k = next[k];
}
} char s[], p[];
int next[];
int main(void)
{
int N;
scanf("%d", &N);
while( N-- )
{ scanf("%s", p);
scanf("%s", s);
lens=(int)strlen(s);
lenp=(int)strlen(p); getnext(next, p);
int i = ;
int j = ;
int sum = ; while(i < lens&&j<lenp)
{
//printf("%d %d %c %c\n",i,j,s[i],p[j]);
if( j == - || s[i] == p[j] )
{ i++;
j++;
}
else
{
j = next[j];
}
//printf("#%d %d %c %c\n",i,j,s[i],p[j]);
if( j >=lenp)
{
sum++;
j = next[j];/////////!!!!!!!!!!
//printf("%d %c %c\n",sum,s[i],p[j]);
}
}
printf("%d\n", sum);
}
return ;
}
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <algorithm>
using namespace std;
#define L1 1000005
#define L2 10005 int next[L2], len1, len2, res;
char s[L1], p[L2]; void get_next ()
{
int j = , k = -;
next[] = -;
while (j < len2)
{
if (k == - || p[j] == p[k])
{
j++, k++;
next[j] = k;
}
else k = next[k];
}
}
void kmp (int pos)
{
int i = pos, j = ;
while (i < len1 && j < len2)
{
//printf("%d %d %c %c\n",i,j,s[i],p[j]);
if (j == - || s[i] == p[j])
{
i++, j++;
}
else j = next[j];
//printf("#%d %d %c %c\n",i,j,s[i],p[j]);
if (j >= len2)
res++, j = next[j]; //神奇之处,效率大增
}
}
int main()
{
int t;
scanf ("%d", &t);
while (t--)
{
res = ;
scanf ("%s%s", p, s);
len1 = strlen (s);
len2 = strlen (p);
get_next ();
kmp ();
printf ("%d\n", res);
}
return ;
}
hdu 1686 KMP算法的更多相关文章
- hdu 1686 KMP模板
// hdu 1686 KMP模板 // 没啥好说的,KMP裸题,这里是MP模板 #include <cstdio> #include <iostream> #include ...
- hdu 1711 KMP算法模板题
题意:给你两个串,问你第二个串是从第一个串的什么位置開始全然匹配的? kmp裸题,复杂度O(n+m). 当一个字符串以0为起始下标时.next[i]能够描写叙述为"不为自身的最大首尾反复子串 ...
- hdu 4300 kmp算法扩展
Clairewd’s message Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- HDU 1686 & KMP
题意: 求模板在匹配串所有子串中出现次数. SOL: 本题与普通kmp有一点不同,因为待匹配串中的模板串可能相互包含. 我们考虑正常的kmp是在怎么做的 i = 1 2 3 4 5 6 7 8 9 … ...
- HDU 1686 (KMP模式串出现的次数) Oulipo
题意: 求模式串W在母串T中出现的次数,各个匹配串中允许有重叠的部分. 分析: 一开始想不清楚当一次匹配完成时该怎么办,我还SB地让i回溯到某个位置上去. 后来仔细想想,完全不用,直接让模式串向前滑动 ...
- Oulipo HDU 1686 KMP模板
题目大意:求模式串在主串中的出现次数. 题目思路:KMP模板题 #include<iostream> #include<algorithm> #include<cstri ...
- hdu 3613 KMP算法扩展
Best Reward Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total ...
- HDU 2594 kmp算法变形
Simpsons’ Hidden Talents Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java ...
- hdu 1686 Oulipo KMP匹配次数统计
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1686 分析:典型的KMP算法,统计字符串匹配的次数. 用Next数组压缩时间复杂度,要做一些修改. / ...
随机推荐
- TransportClient操作详解
Elasticsearch JAVA操作有三种客户端: 1.TransportClient 2.JestClient 3.RestClient 还有种是2.3中有的NodeClient,在5.5.1中 ...
- C#制作自定义安装程序
(一),安装程序 以前用vs制作过安装程序,现在把步骤写出来,有帮助的大家一定要顶哦 第一步:建立工程 1.打开vs,新建项目->其他项目类型->安装和部署(這個子项下面有安装项目和Web ...
- MySQL Join算法与调优白皮书(三)
Batched Key Access Join Index Nested-Loop Join虽好,但是通过辅助索引进行链接后需要回表,这里需要大量的随机I/O操作.若能优化随机I/O,那么就能极大的提 ...
- 后台判断ajax请求的请求后字段
headers设置:X-Requested-With:XMLHttpRequest 后台我就是根据这个来判断的
- nginx基于TCP的反向代理
一.4层的负载均衡 Nginx Plus的商业授权版开始具有TCP负载均衡的功能.从Nginx 1.7.7版本开始加入的,现在变成了一个商业收费版本,想要试用,需要在官网申请.也就是说,Nginx除了 ...
- windows下mysql定时备份
场景:一套B/S小系统,租用了一个虚拟服务器windows 2003,数据库是mysql,做每天的数据库备份 1.关于windows下的定时任务执行 * 命令说明 - /sc 指定计划类型,取 ...
- Windows Server 2012十大实用快捷键组合
在本文中,我们将一起体验快捷键如何在微软最新服务器操作系统中帮助用户提升工作效率. 微软推出的最新服务器操作系统比我印象中任何一款前代Windows Server产品都依赖于键盘操作——当然,这些产品 ...
- 转 - 使用from __future__ import unicode_literals时要注意的问题
原文链接: http://www.cnblogs.com/ajianbeyourself/p/4471035.html 使用from __future__ import unicode_literal ...
- 导出ExcelDemo
public String exportExcel(){ String message=null; SimpleDateFormat df =new SimpleDateFormat("yy ...
- yum ftp本地源
一. 准备工作1. 安装系统centos7.32. 环境 10.10.10.14 controller-1 10.10.10.15 computer-1 3. 在14主机上安装FTP服务yum ins ...