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

KMP第二弹~

#include<iostream>
#include<string.h>
#include<stdio.h>
#include<ctype.h>
#include<algorithm>
#include<stack>
#include<queue>
#include<set>
#include<math.h>
#include<vector>
#include<map>
#include<deque>
#include<list>
using namespace std;
char a[1000007],b[10007];
int next[10007];
int c;
int m,n;
void getnext()
{
int i=0;
int j=-1;
next[0]=-1;
while(i<n)
{
if(j==-1||b[j]==b[i])
{
i++;
j++;
if(b[j]==b[i])
next[i]=next[j];
else
next[i]=j;
}
else
j=next[j];
}
} int kmp()
{
int i=0,j=0;
while(i<m&&j<n)
{
if(j==-1||a[i]==b[j])
{
i++;
j++;
}
else
j=next[j];
if(j==n)
{
j=next[j];
c++;
}
}
return c;
}
int main()
{
int test;
scanf("%d",&test);
getchar();
while(test--)
{
memset(next,0,sizeof(next));
c=0;
gets(b);
gets(a);
m=strlen(a);
n=strlen(b);
getnext();
printf("%d\n",kmp());
}
return 0;
}

HDU1686——Oulipo的更多相关文章

  1. hdu----1686 Oulipo (ac自动机)

    Oulipo Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  2. HDU-1686 Oulipo

    学习:重点理解这句话的意思: next[j]会告诉我们从哪里开始匹配     模板题. Oulipo Time Limit: 3000/1000 MS (Java/Others)    Memory ...

  3. HDU1686:Oulipo

    Problem Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, w ...

  4. hdu1686 Oulipo KMP/AC自动机

    The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter 'e ...

  5. kuangbin专题十六 KMP&&扩展KMP HDU1686 Oulipo

    The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter 'e ...

  6. hdu1686 Oulipo kmp

    题目传送门 思路:kmp模板,稍微修改下 #include<bits/stdc++.h> #define clr(a,b) memset(a,b,sizeof(a)) using name ...

  7. [hdu1686] Oulipo【KMP】

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1686 保存KMP模版,代码里P是模版串,next[]就是为它建立的.T是文本串,就是一般比较长的.nex ...

  8. HDU1686 Oulipo 题解 KMP算法

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1686 题目大意:给你一个子串t和一个母串s,求s中有多少个子串t. 题目分析:KMP模板题. cal_ ...

  9. AC自动机基础知识讲解

    AC自动机 转载自:小白 还可参考:飘过的小牛 1.KMP算法: a. 传统字符串的匹配和KMP: 对于字符串S = ”abcabcabdabba”,T = ”abcabd”,如果用T去匹配S下划线部 ...

随机推荐

  1. Gsoap 使用心得 2

                                         Gsoap 返回图片byte的困惑 前些日子刚使用gsoap将二进制文件上传(服务期端使用c# wcf 编写),上传功能实现没 ...

  2. 转: seajs手册与文档之 -- 快速参考 ( ~~useful )

    目录 快速参考 seajs.use seajs.config define require require.async exports module.exports 快速参考 该页面列举了 SeaJS ...

  3. DIOR HOMME_百度百科

    DIOR HOMME_百度百科     DIOR HOMME    编辑    Dior Homme 男装品牌,中文名迪奥·桀傲,由迪奥 (Dior) 在2001年更名更来,品牌来源地法国.迪奥·桀傲 ...

  4. FMDB的基本应用

    FMDB简介 iOS中原生的SQLite API在进行数据存储的时候,需要使用C语言中的函数,操作比较频繁.于是,就出现了一系列将AQLite API进行封装的库,例如FMDB.PlausibleDa ...

  5. Linux 静态库与共享库的使用

    申明: 正如题如示,本篇讲的是Linux下是静态库与共享库,而Window下的动态链接库详细情况可见这篇文章:windows动态链接库 DLL 浅析.虽然原理,思想差不多,但是细节却各有不同. 一.静 ...

  6. Android 一个绚丽的loading动效分析与实现!

    http://blog.csdn.net/tianjian4592/article/details/44538605 前两天我们这边的头儿给我说,有个 gif 动效很不错,可以考虑用来做项目里的loa ...

  7. SEOR要懂得如何建立完善的seo运营团队

    网站运营要想能做大做全面,完善的seo运营团队是不可缺少的,跟企业管理一样的道理,seo运营的成功也在于对团队的合理利用,发挥团队中每个成员的优势才能运营好网站.网站的运营CEO要懂得如何建立完善的s ...

  8. C#学习之-----再论委托

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...

  9. Firemonkey ListBoxItem自绘

    ListBoxItem1的事件ListBoxItem1Paint procedure TForm1.ListBoxItem1Paint(Sender: TObject; Canvas: TCanvas ...

  10. 基于visual Studio2013解决算法导论之022队列实现(基于链表)

     题目 基于链表的队列实现 解决代码及点评 #include <stdio.h> #include <stdlib.h> #include <time.h> ...