HUD1686(KMP入门题)
Oulipo
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 8732 Accepted Submission(s): 3526
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.
#include<cstdio>
#include<cstring>
using namespace std;
char W[],T[];
int lenW,lenT;
int next[];
void getnext()
{
int i=,k=-;
next[]=-;
while(i<lenW)
{
if(k==-||W[i]==W[k])
{
i++;
k++;
next[i]=k;
}
else k=next[k];
}
}
int KMP()
{
getnext();
int i=,j=;
int cnt=;
while(i<lenT)
{
if(j==-||W[j]==T[i])
{
i++;
j++;
}
else j=next[j];
if(j==lenW)
{
cnt++;
j=next[j];//若两个不同的匹配中有交集则j=next[j],若没有交集j=0;
}
}
return cnt;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
memset(next,,sizeof(next));
scanf("%s%s",W,T);
lenW=strlen(W);
lenT=strlen(T);
printf("%d\n",KMP());
} return ;
}
字符串Hash
#include<cstdio>
#include<cstring>
using namespace std;
typedef unsigned long long ull;
const ull B=;
const int MAXN=;
char a[MAXN],b[MAXN];
int lena,lenb;
ull t;
ull ah;
ull bh[MAXN];
void getah()
{
for(int i=;i<lena;i++)
t*=B;
for(int i=;i<lena;i++)
ah=ah*B+a[i];
}
void getbh()
{
ull h=;
for(int i=;i<lena;i++)
h=h*B+b[i];
bh[]=h;
for(int i=lena;i<lenb;i++)
bh[i-lena+]=bh[i-lena]*B+b[i]-b[i-lena]*t;
}
int KMP()
{
int cnt=;
memset(bh,,sizeof(bh));
getah();
getbh();
for(int i=;i<lenb-lena+;i++)
if(ah==bh[i]) cnt++;
return cnt;
}
int n;
int main(){ while(scanf("%d",&n)!=EOF)
{
while(n--)
{
ah=;
t=;
scanf("%s",a);
scanf("%s",b);
lena=strlen(a);
lenb=strlen(b);
printf("%d\n",KMP());
}
} return ;
}
HUD1686(KMP入门题)的更多相关文章
- zstu.4194: 字符串匹配(kmp入门题&& 心得)
4194: 字符串匹配 Time Limit: 1 Sec Memory Limit: 128 MB Submit: 206 Solved: 78 Description 给你两个字符串A,B,请 ...
- hdu 1358 Period(KMP入门题)
Period Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- HDU2203(KMP入门题)
亲和串 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- HDU2087(KMP入门题)
剪花布条 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- HDU1711(KMP入门题)
Number Sequence Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- 题解报告:hdu 2087 剪花布条(KMP入门)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2087 Problem Description 一块花布条,里面有些图案,另有一块直接可用的小饰条,里面 ...
- HDU 2222 Keywords Search AC自己主动机入门题
单词统计的题目,给出一些单词,统计有多少单词在一个文本中出现,最经典的入门题了. AC自己主动机的基础: 1 Trie. 以这个数据结构为基础的,只是添加一个fail指针和构造fail的函数 2 KM ...
- hdu 3695:Computer Virus on Planet Pandora(AC自动机,入门题)
Computer Virus on Planet Pandora Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 256000/1280 ...
- poj 2524:Ubiquitous Religions(并查集,入门题)
Ubiquitous Religions Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 23997 Accepted: ...
随机推荐
- Django开发微信公众平台
处理微信发来的信息,实际上就是处理xml的过程.先写xml工具类 # -*- coding:utf-8 -*- from xml.dom import minidom from Web.model.W ...
- 《UNIX-Shell编程24学时教程》读书笔记Chap1,2 Shell基础,脚本基础
Chap1 Shell基础 知道该使用哪种命令是依赖于经验的.----惟手熟尔. 1.1 什么是命令 其实知道这些名词好像也没什么帮助,嘻嘻 1.2 什么是Shell 不同用户不同的提示符:不同的环境 ...
- kubernetes对象之deployment
系列目录 简述 Deployment为Pod和ReplicaSet提供了一个声明式定义(declarative)方法,用来替代以前的ReplicationController来方便的管理应用.典型的应 ...
- 换站点Logo图片---轻开电子商务系统(企业入门级B2C站点)
一共2个文件: 显示及上传文件:site/links/img_logo.html 保存图片文件:site/links/img_logo_up1.chtml 在轻开电子商务系统(企业入门级B2C站点)的 ...
- Scrapy爬虫入门系列1 安装
安装python2.7 参见CentOS升级python 2.6到2.7 安装pip 参见CentOS安装python setuptools and pip 依赖 https://docs.scra ...
- jQuery功能强大的图片查看器插件
简要教程 viewer是一款功能强大的图片查看器jQuery插件.它可以实现ACDsee等看图软件的部分功能.它可以对图片进行移动,缩放,旋转,翻转,可以前后浏览一组图片.该图片查看器还支持移动设备, ...
- iOS8中提示框的使用UIAlertController(UIAlertView和UIActionSheet二合一)
本文转载至 http://blog.csdn.net/liuwuguigui/article/details/39494597 IOS8UIAlertViewUIActionSheet ...
- java四种线程池简介,使用
为什么使用线程池 1.减少了创建和销毁线程的次数,每个工作线程都可以被重复利用,可执行多个任务. 2.可以根据系统的承受能力,调整线程池中工作线线程的数目,防止消耗过多的内存 3.web项目应该创建统 ...
- SEO大师分析的八条
- hsv hsb rgb lab
lab 欧式距离 反映 人类所能感受到的这两种颜色的差异