hdu1686 KMP 求在字符串A中字符串B出现的次数
Oulipo
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 24592 Accepted Submission(s): 9516
Tout avait Pair normal, mais tout s’affirmait faux. Tout avait Fair normal, d’abord, puis surgissait l’inhumain, l’affolant. Il aurait voulu savoir où s’articulait l’association qui l’unissait au roman : stir son tapis, assaillant à tout instant son imagination, l’intuition d’un tabou, la vision d’un mal obscur, d’un quoi vacant, d’un non-dit : la vision, l’avision d’un oubli commandant tout, où s’abolissait la raison : tout avait l’air normal mais…
Perec would probably have scored high (or rather, low) in the following contest. People are asked to write a perhaps even meaningful text on some subject with as few occurrences of a given “word” as possible. Our task is to provide the jury with a program that counts these occurrences, in order to obtain a ranking of the competitors. These competitors often write very long texts with nonsense meaning; a sequence of 500,000 consecutive 'T's is not unusual. And they never use spaces.
So we want to quickly find out how often a word, i.e., a given string, occurs in a text. More formally: given the alphabet {'A', 'B', 'C', …, 'Z'} and two finite strings over that alphabet, a word W and a text T, count the number of occurrences of W in T. All the consecutive characters of W must exactly match consecutive characters of T. Occurrences may overlap.
One line with the word W, a string over {'A', 'B', 'C', …, 'Z'}, with 1 ≤ |W| ≤ 10,000 (here |W| denotes the length of the string W).
One line with the text T, a string over {'A', 'B', 'C', …, 'Z'}, with |W| ≤ |T| ≤ 1,000,000.
//HD1686
#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
using namespace std;
const int N = ;
int i, n, m, t, sum;
char T[N], P[N];//P[]是需要比较的字符串,T[]是被比较的字符串
int next1[N]; void getnext(char *P, int *next1)
{
int j = ;
int k = -;
next1[] = -;
while (j<m)
{
if (k == - || P[j] == P[k])
{
next1[++j] = ++k;
}
else
{
k = next1[k];
}
}
}
int KMP(char *T, char *P)
{
sum = ;
int i = ;
int j = ;
getnext(P, next1);
while (i<n&&j<m)//注意这里的等号要不要取
{
if (j == - || T[i] == P[j])//匹配的情况,坐标后移
{
i++;
j++;
}
else//如果不匹配
{
j = next1[j];
}
if (j == m)//判断是否匹配结束
{
sum++;
}
}
return sum;
}
int main()
{
scanf("%d", &t);
for (int i = ; i<t; i++)
{
scanf("%s%s", T, P);
m = strlen(P);
n = strlen(T);
printf("%d\n", KMP(T, P));
}
//system("pause");
return ;
}
hdu1686 KMP 求在字符串A中字符串B出现的次数的更多相关文章
- Python3求英文文档中每个单词出现的次数并排序
[本文出自天外归云的博客园] 题目要求: 1.统计英文文档中每个单词出现的次数. 2.统计结果先按次数降序排序,再按单词首字母降序排序. 3.需要考虑大文件的读取. 我的解法如下: import ch ...
- C#中如何排除/过滤/清空/删除掉字符串数组中的空字符串
C#中要如何才能删除一个字符串数组中的空字符串呢?随着微软对C#不断发展和更新,C#中对于数组操作的方式也变得越来越多样化.以往要实现过滤数组中的空字符串,都是需要实行循环的方式来排除和过滤.C#3. ...
- python中字符串的操作方法
python中字符串的操作方法大全 更新时间:2018年06月03日 10:08:51 作者:骏马金龙 我要评论这篇文章主要给大家介绍了关于python中字符串操作方法的相关资料,文中通过示例代码详细 ...
- 使用C#删除一个字符串数组中的空字符串
C#中要如何才能删除一个字符串数组中的空字符串呢?随着微软对C#不断发展和更新,C#中对于数组操作的方式也变得越来越多样化.以往要实现过滤数组中的空字符串,都是需要实行循环的方式来排除和过滤.C#3. ...
- hdu 4333"Revolving Digits"(KMP求字符串最小循环节+拓展KMP)
传送门 题意: 此题意很好理解,便不在此赘述: 题解: 解题思路:KMP求字符串最小循环节+拓展KMP ①首先,根据KMP求字符串最小循环节的算法求出字符串s的最小循环节的长度,记为 k: ②根据拓展 ...
- hdu6153 扩展kmp求一个字符串的后缀在另一个字符串出现的次数。
/** 题目:hdu6153 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6153 题意:给定两个串,求其中一个串t的每个后缀在另一个串s中出现的次数乘以 ...
- KMP求字符串最小循环节
证明1: 对于一个字符串S,长度为L,如果由长度为len的字符串s(字符串s的最小循环节是其本身)循环k次构成,那么字符串s就是字符串S的最小循环节 那么字符串有个很重要的性质和KMP挂钩,即 i ...
- hdu 3374 String Problem (字符串最小最大表示 + KMP求循环节)
Problem - 3374 KMP求循环节. http://www.cnblogs.com/wuyiqi/archive/2012/01/06/2314078.html 循环节推导的证明相当 ...
- KMP算法(研究总结,字符串)
KMP算法(研究总结,字符串) 前段时间学习KMP算法,感觉有些复杂,不过好歹是弄懂啦,简单地记录一下,方便以后自己回忆. 引入 首先我们来看一个例子,现在有两个字符串A和B,问你在A中是否有B,有几 ...
随机推荐
- sublime3 There are no packages available for installation
我的是网上下载的绿色版 1.找到sublime\Data\Packages 删除Packages control相关的文件夹和文件 下载https://packagecontrol.io/Packa ...
- oracle --(一)数据块(data Block)
基本关系:数据库---表空间---数据段---分区---数据块 数据块(data Block)一.数据块Block是Oracle存储数据信息的最小单位.这里说的是Oracle环境下的最小单位.Orac ...
- day17-jdbc 7.Statement介绍
SQL语句:DML.DQL.DCL.DDL.DML和DQL是用的最多的.DCL和DDL用的很少. 程序员一般是操作记录,创建一表很少. package cn.itcast.jdbc; import c ...
- SQL serve 数据库--视图、事物、分离附加、备份还原
视图是数据库中的一种虚拟表,与真实的表一样,视图包含一系列带有名称的行和列数据.行和列数据用来自定义视图的查询所引用的表,并且在引用视图时动态生成. 视图只能用来查询,不能增删改:不允许出现重复列 ...
- MSScriptControl详解(可实现在C#等语言中调用JAVASCRIPT代码)
ScriptControl接口 属性名称 类型 备注 AllowUI BOOL 检测是否允许运行用户的接口元素.如果为False,则诸如消息框之类的界面元素不可见. CodeObject Object ...
- Configuration File (php.ini) Path Loaded Configuration File 都有加载php.ini文件,有什么不同的地方?
Configuration File (php.ini) Path /usr/local/php7/etc 这个目录下面也有php.ini文件(如果在编译./configure -with- ...
- (华为机试大备战)java。多了解了解最常用的那个类库的方法对处理字符串的方法
1.常考字符串处理:对处理字符串的方法. (a)统计字符串中特定字符的个数. 2.郭靖考了一道二维数组?? 3.多了解了解最常用的那个类库的方法.
- Python程序设计5——函数
本章介绍Python中模块和函数的概念. Python程序是由包.模块和函数三者组成,包是由一系列模块组成的集合,模块是处理某一类问题的函数和类的集合.Python提供的许多工具包和模块安装在Pyth ...
- Entity Framework Tutorial Basics(4):Setup Entity Framework Environment
Setup Entity Framework Environment: Entity Framework 5.0 API was distributed in two places, in NuGet ...
- Java Collection.RP
在 Java2中,有一套设计优良的接口和类组成了Java集合框架Collection,使程序员操作成批的数据或对象元素极为方便.这些接口和类有很多对抽象数据类型操作的API,而这是我们常用的且在数据结 ...