poj 3461 Oulipo,裸kmp
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 32373 | Accepted: 13093 |
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
Source
#include <iostream>
#include <cstdio>
#include <map>
#include <cstring>
#include <algorithm> using namespace std; #define N 1000005
#define ll long long int TT;
char s[N];
char t[N];
int next[N];
int ans;
int l;
int lt; void get_next()
{
int i,j;
i = ;
j = -;
next[] = -;
while(i<l)
{
if(j == - || s[i] == s[j]){
i++;j++;next[i] = j;
}
else{
j = next[j];
}
}
} void kmp()
{
int i,j;
i = ;
j = ;
while(i < lt)
{
if(j == - || t[i] == s[j]){
if(j == l - ){
ans ++;
j = next[j];
}
else{
i++;j++;
}
}
else{
j = next[j];
}
}
} int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
scanf("%d",&TT);
while(TT--){
//while(scanf("%I64d%I64d",&n,&k)!=EOF){
ans = ;
scanf("%s",s);
l = strlen(s);
scanf("%s",t);
lt = strlen(t);
get_next();
kmp();
printf("%d\n",ans);
}
return ;
}
poj 3461 Oulipo,裸kmp的更多相关文章
- POJ 3461 Oulipo[附KMP算法详细流程讲解]
E - Oulipo Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit ...
- POJ 3461 Oulipo 【KMP统计子串数】
传送门:http://poj.org/problem?id=3461 Oulipo Time Limit: 1000MS Memory Limit: 65536K Total Submission ...
- POJ 3461 Oulipo(KMP裸题)
Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, without t ...
- poj 3461 Oulipo(KMP模板题)
Oulipo Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 36903 Accepted: 14898 Descript ...
- POJ 3461 Oulipo(——KMP算法)
Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, without t ...
- poj 3461 - Oulipo 经典kmp算法问题
2017-08-13 19:31:47 writer:pprp 对kmp算法有了大概的了解以后,虽然还不够深入,但是已经可以写出来代码,(可以说是背会了) 所以这道题就作为一个模板,为大家使用吧. 题 ...
- poj 3461 Oulipo(kmp统计子串出现次数)
题意:统计子串出现在主串中的次数 思路:典型kmp #include<iostream> #include<stdio.h> #include<string.h> ...
- poj 3461 Oulipo(KMP)
Oulipo Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 49378 Accepted: 19617 Descript ...
- 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 ...
随机推荐
- iOS perform action after period of inactivity (no user interaction)
代码看完后感觉非常优秀 http://stackoverflow.com/questions/8085188/ios-perform-action-after-period-of-inactivity ...
- 插入函数概念 接收一个callback,然后外面函数,接一个对象,和一个next,外面都执行完了,继续执行下一个函数,相当于一个链状体,插入了一个函数的概念 | log4js 输出 等于 console.info
插入函数概念 接收一个callback,然后外面函数,接一个对象,和一个next,外面都执行完了,继续执行下一个函数,相当于一个链状体,插入了一个函数的概念 app.use(function (ctx ...
- js获取当前时间的前一天/后一天
Date curDate = new Date();var preDate = new Date(curDate.getTime() - 24*60*60*1000); //前一天var nextDa ...
- Springboot邮箱接口(使用个人邮箱发送邮件)
近期项目使用邮件验证,这里使用个人邮箱进行测试,记录开发笔记 SpringBoot自带短信接口 maven pom.xml 引入: <dependency> <groupId> ...
- Bootstrap历练实例:块级按钮
<!DOCTYPE html><html><head> <meta http-equiv="Content-Type" content=& ...
- mac系统快捷键大全详细介绍(全部)
对于使用苹果电脑的操作系统的新人来说,快捷键是个很麻烦的问题,要一个个的找到快捷键也不是很容易的问题,今天这篇文章就解决了到处找快捷键的麻烦. 第一种分类:启用快捷键 按下按键或组合键,直到所需的功能 ...
- mongodb详细教程
转自:https://www.cnblogs.com/liruihuan/tag/MongoDB/
- Django中重定向页面的时候使用命名空间
urls.py from django.urls import path from . import views app_name='front' urlpatterns = [ path('',vi ...
- CodeForces 699C - Vacations
题目链接:http://codeforces.com/problemset/problem/699/C C. Vacations time limit per test1 second memory ...
- final,buaa_oo conclusion
UML系列作业设计架构 第13次作业 本单元的第一次作业中,涉及到了类图的解析.在着手做这单元作业的时候,需要将每一种 UmlElement 再封装,并在解析时,用 helper 单例来进行查询处理( ...