POJ 3461:Oulipo
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 28155 | Accepted: 11251 |
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 textT,
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模板题,没有比这个更模板的了。。。
唯一要提醒自己的是最后模式串和原串走的时候j是一直往前走的,不回头!怎么就记不住,每次都是搞了两层循环,然后TLE,你这么搞之前搞的那个next数组有什么用,动脑袋想一想啊光速小子。
代码:
#include <iostream>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstring>
using namespace std; char moshi[10005];
char pipei[1000005]; int result;
int next_moshi[10005]; void get_next()
{
int i,j=-1;
int len=strlen(moshi);
next_moshi[0]=-1; for(i=0;i<len;)
{
if(j==-1||moshi[i]==moshi[j])
{
i++;
j++; if(moshi[i]==moshi[j])
{
next_moshi[i]=next_moshi[j];
}
else
{
next_moshi[i]=j;
}
}
else
{
j=next_moshi[j];
}
}
} void get_result()
{
int i=0,j=0; int len_pipei=strlen(pipei);
int len_moshi=strlen(moshi); while(j<len_pipei)
{
if(moshi[i]==pipei[j])
{
i++;
j++;
}
else
{
if(next_moshi[i]==-1)
{
i=0;
j++;
}
else
{
i=next_moshi[i];
}
}
if(i>=len_moshi)
{
result++;
i=next_moshi[len_moshi];
}
}
} int main()
{
int Test;
scanf("%d",&Test); while(Test--)
{
result=0; scanf("%s%s",moshi,pipei); get_next();
get_result(); cout<<result<<endl;
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
POJ 3461:Oulipo的更多相关文章
- 【poj 3461】Oulipo(字符串--KMP)
题意:求子串在文本串中出现了多少次. 解法:使用KMP的next[ ]和tend[ ]数组计数. #include<cstdio> #include<cstdlib> #inc ...
- 【POJ 3461】 Oulipo
[题目链接] 点击打开链接 [算法] KMP [代码] #include <algorithm> #include <bitset> #include <cctype&g ...
- POJ 3461 Oulipo
E - Oulipo Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit ...
- POJ 3461 Oulipo(乌力波)
POJ 3461 Oulipo(乌力波) Time Limit: 1000MS Memory Limit: 65536K [Description] [题目描述] The French autho ...
- POJ 3461 Oulipo[附KMP算法详细流程讲解]
E - Oulipo Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit ...
- 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 ...
- POJ 3321:Apple Tree + HDU 3887:Counting Offspring(DFS序+树状数组)
http://poj.org/problem?id=3321 http://acm.hdu.edu.cn/showproblem.php?pid=3887 POJ 3321: 题意:给出一棵根节点为1 ...
- POJ 3252:Round Numbers
POJ 3252:Round Numbers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10099 Accepted: 36 ...
- POJ 3461 Oulipo 【KMP统计子串数】
传送门:http://poj.org/problem?id=3461 Oulipo Time Limit: 1000MS Memory Limit: 65536K Total Submission ...
随机推荐
- list中会直接绑定HashMap中的数据
import java.util.ArrayList;import java.util.HashMap;import java.util.List; public class HashMapSync ...
- 统计学习方法——KD树最近邻搜索
李航老师书上的的算法说明没怎么看懂,看了网上的博客,悟出一套循环(建立好KD树以后的最近邻搜索),我想应该是这样的(例子是李航<统计学习算法>第三章56页:例3.3): 步骤 结点查询标记 ...
- Unity3D渲染优化技巧
优化图形性能 良好的性能对大部分游戏的成功具有决定作用.下面是一些简单的指导,用来最大限度地提高游戏的图形渲染. 图形需要哪些开销 游戏的图形部分主要开销来自电脑的两个系统: GPU 或 CPU.优化 ...
- Lesson 1 Finding fossil man
Why are legends handed down by storytellers useful? We can read of things that happend 5000 years ag ...
- 发送短信-----用restful 校验规则
我们的需求如下 1. 获取手机号 . 手机格式校验 . 生成随机验证码 . 验证码发送到手机上 . 把验证码+手机号保留(30s过期) 复习 我们在有表的情况下的序列化 class MessageSe ...
- NO25 三剑客之SED行天下
功能说明 Sed是Stream Editor(流编辑器)缩写,是操作.过滤和转换文本内容的强大工具.常用功能有增删改查,过滤,取行. [root@oldboy ~]# sed --version #→ ...
- flask-Bootstrap Jinja2 原生 模板 和 jumpserver 模板
#模板 {% block doc -%} <!DOCTYPE html> <html{% block html_attribs %}{% endblock html_attribs ...
- JavaScript深入理解对象方法——Object.entries()
Object.entries()方法返回一个给定对象自身可枚举属性的键值对数组,其排列与使用 for...in 循环遍历该对象时返回的顺序一致(区别在于 for-in 循环也枚举原型链中的属性) 示例 ...
- 洛谷 P1247 取火柴游戏
题目传送门 暴力 \((\)由于我这样的初中蒟蒻不\((bu)\)喜\((hui)\)欢\((xie)\)数学证明,所以题解中的证明全是其他大佬的题解已经多次证明过的,这里就不再啰嗦了.\()\) - ...
- PowerShell中执行.net类库
Powershell脚本一个比较强大的功能是可以直接调用.net类库(ps core能调用.net core类库),除了调用系统类库外,也可以调用自己编写的类库,从而扩充我们脚本的功能.本文这里简单的 ...