Oulipo

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 22465    Accepted Submission(s): 8674

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

题意

给出子串和母串,计算子串在母串中出现的次数

AC代码

#include <bits/stdc++.h>
#define ll long long
#define ms(a) memset(a,0,sizeof(a))
using namespace std;
const int maxn=1e6+10;
char T[maxn],S[maxn];
int Next[maxn];
int slen,tlen;
// 字符串是从0开始的
// Next数组是从1开始的
void getNext()
{
int j, k;
j = 0; k = -1; Next[0] = -1;
while(j < tlen)
if(k == -1 || T[j] == T[k])
Next[++j] = ++k;
else
k = Next[k]; }
/*
返回模式串在主串S中出现的次数
*/
int KMP_Count()
{
int ans = 0;
int i, j = 0;
if(slen == 1 && tlen == 1)
{
if(S[0] == T[0])
return 1;
else
return 0;
}
getNext();
for(i = 0; i < slen; i++)
{
while(j > 0 && S[i] != T[j])
j = Next[j];
if(S[i] == T[j])
j++;
if(j == tlen)
{
ans++;
j = Next[j];
}
}
return ans;
} int main(int argc, char const *argv[])
{
int t;
scanf("%d",&t);
getchar();
while(t--)
{
ms(S);
ms(T);
ms(Next);
gets(T);
gets(S);
slen=strlen(S);
tlen=strlen(T);
printf("%d\n",KMP_Count());
}
return 0;
}

这个题用字符串哈希也能写,速度也是差不多的

HDU 1686:Oulipo(KMP模板,子串出现次数)的更多相关文章

  1. HDU 1686 - Oulipo - [KMP模板题]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1686 Time Limit: 3000/1000 MS (Java/Others) Memory Li ...

  2. HDU - 1686 Oulipo KMP匹配运用

    id=25191" target="_blank" style="color:blue; text-decoration:none">HDU - ...

  3. hdu 1686 Oulipo KMP匹配次数统计

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1686 分析:典型的KMP算法,统计字符串匹配的次数. 用Next数组压缩时间复杂度,要做一些修改. / ...

  4. 洛谷 P3375 【模板】KMP字符串匹配 || HDU 1686 Oulipo || kmp

    HDU-1686 P3375 kmp介绍: http://www.matrix67.com/blog/archives/115 http://www.cnblogs.com/SYCstudio/p/7 ...

  5. Oulipo - HDU 1686 (KMP模板题)

    题目大意:题目叙述很多,其实只看输入输出也能明白什么意思,给两个串W,T, 判断T串中包含几个串W.   分析:还是基础的KMP应用....................... 直接上代码. === ...

  6. hdu 1686 Oulipo kmp算法

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1686 题目: Problem Description The French author George ...

  7. HDU 1686 Oulipo (KMP 可重叠)

    题目链接 Problem Description The French author Georges Perec (1936–1982) once wrote a book, La dispariti ...

  8. hdu 1686 Oulipo (kmp)

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

  9. HDU 1686 Oulipo kmp裸题

    kmp算法可参考 kmp算法 汇总 #include <bits/stdc++.h> using namespace std; const int maxn=1000000+5; cons ...

  10. HDU 1686 Oulipo / POJ 3461 Oulipo / SCU 2652 Oulipo (字符串匹配,KMP)

    HDU 1686 Oulipo / POJ 3461 Oulipo / SCU 2652 Oulipo (字符串匹配,KMP) Description The French author George ...

随机推荐

  1. CentOS下安装Hbase

    1.安装JDK.https://www.cnblogs.com/zhi-leaf/p/10315125.html 2.下载Hbase.下载地址:https://hbase.apache.org/dow ...

  2. sql根据年月日查询注册数或者和值

    //公司需要我做一个根据每天用户注册数量生成一个折现图,sql如下,//亲测好用,只是如果某一天没有注册的话,就不会显示日期 SELECT DATE_FORMAT(f.registDate, '%Y- ...

  3. ubuntu 双硬盘挂载 windows分区自动挂载

    sudo fdisk -l 查看硬盘情况 1:新建一个目录,例:old 2:mount  /dev/sdb1  old 3:cd old 4:ls  (就可以看到新硬盘的内容了) 取消挂载:umoun ...

  4. java ArrayList 迭代器快速失败源码分析

    先来看一个例子: @Test void test2() { ArrayList<String> list = new ArrayList<String>(); list.add ...

  5. js 日期格式化函数(可自定义)

    js 日期格式化函数 DateFormat var DateFormat = function (datetime, formatStr) { var dat = datetime; var str ...

  6. day05 可变不可变类型

    #可变类型: 值变了,但是id没有变,证明没有生成新的值而是在改变原值,原值是可变类型#不可变类型:值变了,id也跟着变,证明是生成了新的值而不是在改变原值,原值是不可变 # x=10# print( ...

  7. Windows Server 2016与旧版本系统比较

    一.性能和可扩性 特征描述 Windows Server 2012/2012 R2 标准版和数据中心 Windows Server 2016 标准版和数据中心 物理内存(主机)支持 每个物理服务器至多 ...

  8. C++11新特性,bind,基于对象

    body, table{font-family: 微软雅黑; font-size: 10pt} table{border-collapse: collapse; border: solid gray; ...

  9. ios 设置本地化显示的app名称

    内容的本地化这里不做介绍! 名称的本地化: 1.新建一个 Strings File文件,命名为InfoPlist,注意这里一定要命名为InfoPlist! 2.设置本地化信息:选择需要的语言! 3.填 ...

  10. Scrapy结构

    http://scrapy-chs.readthedocs.io/zh_CN/1.0/intro/overview.html scrapy 使用Twisted 这个异步网络库来处理网络通信,使用pyt ...