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. 从头写一个Cucumber测试(一) Selenium Test

    转载:https://yaowenjie.github.io/%E7%BC%96%E7%A8%8B%E7%9B%B8%E5%85%B3/cucumber-test, 背景(废话不读系列)   前段时间 ...

  2. wince开发_摩托罗拉MC3100_打开条码设置

    呵呵不多说,直接上图 1.打开控制面板 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0 ...

  3. substr使用注意

    substr使用时要判断起点和长度是否超过了串本身的长度,否则会抛异常

  4. MGTemplateEngine 模版发动机简单使用

    https://github.com/nxtbgthng/MGTemplateEngine MGTemplateEngine 模版引擎 MGTemplateEngine比較象 PHP 中的 Smart ...

  5. 横向卷轴(side-scrolling)地图的canvas实现

    标题有点小题大作了,实际上是实现一张看起来连续的运动背景图片. 效果如下:   // 实现原理: 图片1与图片2是两张首尾衔接的图片,图片1以一定速度移出canvas,图片2慢慢移进canvas,当图 ...

  6. Markdown 语法的超快速上手

    本文支持WTFPL协议,因此你想往哪转就往哪转. Why markdown? Markdown是一种可以使用普通文本编辑器编写的标记语言,通过简单的标记语法,它可以使普通文本内容具有一定的格式. Ma ...

  7. 清除inline-block元素默认间距

    1. font-size:0; 2.letter-spaceing:-0.5em;

  8. 牛客练习赛14 D 比较月亮大小 【水】

    链接:https://www.nowcoder.com/acm/contest/82/D 来源:牛客网 比较月亮大小 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其 ...

  9. php设计模式之单例模式实例(设计mysqli连接数据的数据处理类)

    一直在研究php的设计模式,但是没有亲历使用过,所以还是一知半解,通过几天的学习终于对php的单例设计模式稍稍的有些了解,特此写出一个数据库处理类(只涉及到简单的原理),以便自己以后方便查阅,至于其他 ...

  10. 找到bashrc

    (1)直接sudo gedit ~/.bashrc就可以了,编辑完后关闭就行 (2)主文件夹下ctrl+h就能找到.bashrc文件 之所以要找到bashrc文件,是为了把命令 source /opt ...