Oulipo

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 8732    Accepted Submission(s): 3526

Problem Description
The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter 'e'. He was a member of the Oulipo group. A quote from the book:

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.

 
Input
The first line of the input file contains a single number: the number of test cases to follow. Each test case has the following format:

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.

 
Output
For every test case in the input file, the output should contain a single number, on a single line: the number of occurrences of the word W in the text T.

 
Sample Input
3
BAPC
BAPC
AZA
AZAZAZA
VERDI
AVERDXIVYERDIAN
 
Sample Output
1
3
0
#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入门题)的更多相关文章

  1. zstu.4194: 字符串匹配(kmp入门题&& 心得)

    4194: 字符串匹配 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 206  Solved: 78 Description 给你两个字符串A,B,请 ...

  2. hdu 1358 Period(KMP入门题)

    Period Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  3. HDU2203(KMP入门题)

    亲和串 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  4. HDU2087(KMP入门题)

    剪花布条 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  5. HDU1711(KMP入门题)

    Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  6. 题解报告:hdu 2087 剪花布条(KMP入门)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2087 Problem Description 一块花布条,里面有些图案,另有一块直接可用的小饰条,里面 ...

  7. HDU 2222 Keywords Search AC自己主动机入门题

    单词统计的题目,给出一些单词,统计有多少单词在一个文本中出现,最经典的入门题了. AC自己主动机的基础: 1 Trie. 以这个数据结构为基础的,只是添加一个fail指针和构造fail的函数 2 KM ...

  8. hdu 3695:Computer Virus on Planet Pandora(AC自动机,入门题)

    Computer Virus on Planet Pandora Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 256000/1280 ...

  9. poj 2524:Ubiquitous Religions(并查集,入门题)

    Ubiquitous Religions Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 23997   Accepted:  ...

随机推荐

  1. Git 常用场景操作

    git init      在本地新建一个repo,进入一个项目目录,执行git init,会初始化一个repo,并在当前文件夹下创建一个.git文件夹.   git clone      获取一个u ...

  2. 高速创建和mysql表相应的java domain实体类

    今天创建了一个表有十几个字段,创建完之后必定要写一个与之相应的java domain实体类. 这不是反复的工作吗?为什么不先把这个表的全部的字段查出来,然后放到linux环境下,用sed工具在每一行的 ...

  3. poj Kindergarten

    Kindergarten 又是一道自己没思考出来的题 !!!!! 还是老样子,题目去我拉的专题里有. 题目: 给出G给女孩,B给男孩.女孩之间是相互认识的,男孩之间也是相互认识的.如今题目中给出M对男 ...

  4. 【Sprint2 每日Scrum】 第一天(4.22)Sprint2计划会议成果

    Sprint2计划会议成果 从今天起我们就开始正式的Sprint2之旅了,经过上一次Sprint1的冲刺计划和几天的调整,我们已经大致了解了敏捷开发的流程和思想,并将我们的TD学生助手做出了大致的框架 ...

  5. codeforces 557 C

    由于期末.非常久没刷题了,CF一直掉-- 这个题事实上非常简单. .由于做法非常easy想到嘛.. 就是枚举max=x时,最大能保留多少价值.不断更新ans, 结果就是全部价值和减去ans就好 因为最 ...

  6. js json按key值排序

    最近有个需求需要把json按key值进行排序,可是js并没有直接的函数可以对json进行排序的这么办呢? 然后想到了一个间接的方法来实现: 1.将json中的key值取出,存在一个数组中,然后对这个数 ...

  7. 图像处理之增强---图像增强算法四种,图示与源码,包括retinex(ssr、msr、msrcr)和一种混合算法

    申明:本文非笔者原创,原文转载自:http://blog.csdn.net/onezeros/article/details/6342661 两组图像:左边较暗,右边较亮 第一行是原图像,他们下面是用 ...

  8. Apache/2.4.9启动错误:AH01630: client denied by server configuration

    在升级Yii框架1.11->2.0beta时,PHP升级到5.5.顺带升级Apache2.2.x到2.4.9. 把原有vhost配置移植过来,出现Apache启动错误: AH01630: cli ...

  9. 【BZOJ1975】[Sdoi2010]魔法猪学院 A*

    [BZOJ1975][Sdoi2010]魔法猪学院 Description iPig在假期来到了传说中的魔法猪学院,开始为期两个月的魔法猪训练.经过了一周理论知识和一周基本魔法的学习之后,iPig对猪 ...

  10. 开源无广告输入法Rime

    最近在看别人的博客的时候,看到别人推荐一种开源的输入法Rime.然后就下载下来一用,果然给力啊,没有广告,没有插件,很干净的输入法.比其他输入法强太多.废话少说,下面是链接https://code.g ...