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: ...
随机推荐
- windows xp下mysql5.0安装
安装注意要点: 1.不要安装在带有中文的安装路径 2.之前若有安装过mysql,请一定要卸载干净 MySQL安装的图解5.0.28 - CSDN
- 拒绝ssh远程暴力破解
拒绝ssh远程暴力破解 简介 在网络技术日益发展的今天,网络上的安全问题日益严重.当你在公网上使用Linux服务器时,很有可能你的服务器正在遭受ssh暴力破解. 曾经有一次我的同伴将给客户提供监控服务 ...
- c#打包文件解压缩 C#中使用委托、接口、匿名方法、泛型委托实现加减乘除算法 一个简单例子理解C#的协变和逆变 对于过长字符串的大小比对
首先要引用一下类库:using Ionic.Zip;这个类库可以到网上下载. 下面对类库使用的封装方法: 得到指定的输入流的ZIP压缩流对象 /// <summary> /// 得到指定的 ...
- WWDC2014苹果的“软件”发布会
WWDC 2014 苹果的"软件"发布会 在今年的 6 月 2 日到 6 日,苹果照例举行了一年一次的全球开发者大会(World Wide Developer Conference ...
- 话说Session
Session这个概念,对于搞软件的来说,再熟悉不过了.就拿我来说,Hibernate, Shiro, Spring, JSP, Web Server等等,全都涉及到Session. 不怕笑话,一直都 ...
- linux SPI驱动——简单的gpio模拟SPI驱动测试 (二)
1: /* 2: * Add by xuyonghong for duotin car radio fm 3: * Copyright (C) 2016-5-24 xuyonghong@duotin. ...
- 深入理解spring国际化
深入理解spring国际化 转自http://blog.csdn.net/ethan_fu/article/details/45621337
- ajax跨域请求的问题
使用getJson跨域请求,需要向服务器发送一个参数callback=? $.getJSON("http://appcenter.mobitide.com/admin/appSearch.p ...
- A桶中有多少水?
如果你能算出桶中有多少水,我便许你下山去玩.有一天,老和尚让小和尚将A桶的水挑到B桶去,可是小和尚却想下山玩,不愿意挑水,老和尚便说:”如果你能够根据我的提示算出A桶中有多少升水,我便许你下山去玩.” ...
- Web UI回归测试 -- BackstopJS 入门
BackstopJS是一个测试工具,用于测试ui图和实际项目是否偏差. 话不多说,直接启动一个项目吧测试吧. 1.首先全局安装BackstopJS npm install -g backstopjs ...