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 <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
int b[];//之前用next[10005]CE,不明确,可能有函数吧。。。,好坑
char ch1[],ch2[];
void get_next(int len1)
{
int i=,j=-;
b[]=-;
while(i<len1)
{
if(j==-||ch1[i]==ch1[j])
{
i++;j++;
if(ch1[i]==ch1[j])
b[i]=b[j];
else
b[i]=j;
}
else
j=b[j];
}
}
int kmp(int len1,int len2)
{
int i=,j=,k=;
get_next(len1);
while(i<len2)
{
if(j==-||ch2[i]==ch1[j])
{i++;j++;}
else
{
j=b[j];
}
if(j==len1)
{
k++;
}
}
return k;
} int main()
{
int n;
cin>>n;
getchar();
while(n--)
{
cin>>ch1>>ch2;
int len1,len2;
len1=strlen(ch1);
len2=strlen(ch2);
get_next(len1);
cout<<kmp(len1,len2)<<endl;
}
return ;
}

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. centos7,yum安装的redis用systemctl无法启动

    因为之前使用显示命令启动redis的,要使redis在后台运行就需要改redis.conf中的daemonize 为yes. 这次在centos7上也顺手改了为yes,然后使用systemctl启动, ...

  2. C# 常用接口学习 IComparable 和 IComparer

    C# 常用接口学习 IComparable 和 IComparer 作者:乌龙哈里 时间:2015-11-01 平台:Window7 64bit,Visual Studio Community 201 ...

  3. ES6 之 let和const命令 Symbol Promise对象

    ECMAScript 6入门 ECMAScript 6(以下简称ES6)是JavaScript语言的下一代标准,已经在2015年6月正式发布了. (2016年6月,发布了小幅修订的<ECMASc ...

  4. KVM下windows虚拟机使用virtio驱动

    KVM下windows虚拟机默认disk使用的是Qemu IDE硬盘,网卡默认是rtl8139网卡.为了使kvm主机在相同的配置下,有更好的效率,可以将网卡和磁盘替换成virtio的驱动. windo ...

  5. 单链表,循环链表,双向链表(C++实现)

    首先是单链表(带附加表头),实现类代码如下: template<class T> struct LinkNode{//链表节点 T data; LinkNode *link; LinkNo ...

  6. Intent Flag实际项目 -- 超时跳转登录界面并清理前面所有activity

    项目中涉及到登录超时跳转登录界面的逻辑,我以前的跳转flag为Intent.FLAG_ACTIVITY_CLEAR_TOP,但是点击返回按钮还是会回到上个界面.代码如下: ActivityUtils. ...

  7. android ScrollView中嵌套listview listview可点击处理,可展开

    public class MyListView extends ListView { public MyListView(Context context, AttributeSet attrs, in ...

  8. 解决Fileupload 在IE 10 中需要双击的问题

    方法来自于:https://code.google.com/p/primefaces/issues/detail?id=5355 the oncomplete event is working for ...

  9. [妙味JS基础]第八课:return、定时器基础

    知识点总结 return 1)函数名+括号 = return 返回值 2)所有的函数默认的返回值 = 未定义 3)return后面的代码不执行 arguments  =>为实参的集合,当参数个数 ...

  10. hdu_5925_Coconuts(离散化+dfs)

    题目链接:hdu_5925_Coconuts 题意: 给你一张很大的图,和小于200个的障碍点,问这张图中的联通块有多少个 题解: 由于障碍点只有200个,所以肯定有很多的空白部分,我们将这些空白部分 ...