Oulipo (poj3461
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 29759 | Accepted: 11986 |
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
1.按照递推的思想:
根据定义next[0]=-1,假设next[j]=k, 即P[0...k-1]==P[j-k,j-1]
1)若P[j]==P[k],则有P[0..k]==P[j-k,j],很显然,next[j+1]=next[j]+1=k+1;
2)若P[j]!=P[k],则可以把其看做模式匹配的问题,即匹配失败的时候,k值如何移动,显然k=next[k]。
因此可以这样去实现:
void getNext(char *p,int *next)
{
int j,k;
next[]=-;
j=;
k=-;
while(j<strlen(p)-)
{
if(k==-||p[j]==p[k]) //匹配的情况下,p[j]==p[k]
{
j++;
k++;
next[j]=k;
}
else //p[j]!=p[k]
k=next[k];
}
}
问题: next应该到数组长度……
优化一点:
void getNext(char *p,int *next)
{
int j,k;
int lastIndex=strlen(p)-;
next[]=-;
j=;
k=-;
while(j < lastIndex)
{
if(k==-||p[j]==p[k]) //匹配的情况下,p[j]==p[k]
{
++j;
++k; //若p[j]==p[k],则需要修正
if(p[j]==p[k])
next[j]=next[k];
else
next[j]=k;
}
else //p[j]!=p[k]
k=next[k];
}
}
2.直接求解方法
void getNext(char *p,int *next)
{
int i,j,temp;
for(i=;i<strlen(p);i++)
{
if(i==)
{
next[i]=-; //next[0]=-1
}
else if(i==)
{
next[i]=; //next[1]=0
}
else
{
temp=i-;
for(j=temp;j>;j--)
{
if(equals(p,i,j))
{
next[i]=j; //找到最大的k值
break;
}
}
if(j==)
next[i]=;
}
}
} bool equals(char *p,int i,int j) //判断p[0...j-1]与p[i-j...i-1]是否相等
{
int k=;
int s=i-j;
for(;k<=j-&&s<=i-;k++,s++)
{
if(p[k]!=p[s])
return false;
}
return true;
}
本题代码:
#include<iostream>
#include<cstdio>
#include<cstring> using namespace std; char w[], t[];
int next[]; void getNext()
{
int j, k;
int i = strlen(w);
j = ;
k = -;
next[] = -; while(j < i)
{
if(k == - || w[j] == w[k])
{
j++;
k++;
next[j] = k;
}
else
k = next[k];
}
} int main()
{
int c;
scanf("%d", &c);
while(c--)
{
memset(next, , sizeof(next));
scanf("%s", w);
scanf("%s", t);
getNext();
int i, j, l1, l2;
l1 = strlen(w);
l2 = strlen(t);
i = j = ;
int sum = ;
while(i < l1 && j < l2)
{
if(w[i] == t[j] || i == -)
{
i++;
j++;
}
else
i = next[i]; if(i == l1)
{
sum++;
i = next[i];
}
}
printf("%d\n", sum);
}
return ;
}
Oulipo (poj3461的更多相关文章
- 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 3461 Oulipo(乌力波)
POJ 3461 Oulipo(乌力波) Time Limit: 1000MS Memory Limit: 65536K [Description] [题目描述] The French autho ...
- POJ-3461 Oulipo(KMP,模式串在主串中出现次数)
题意:给你两个字符串p和s,求出p在s中出现的次数. 显然,我们要先把模式串放到前面,之后主串放后面,中间隔开,这样就可以根据前缀数组的性质来求了. 我先想直接把p接到s前面,之后求Next数组对st ...
- Oulipo(Hash入门第一题 Hash函数学习)
Hash,一般翻译做散列.杂凑,或音译为哈希,就是把任意长度的输入(又叫做预映射, pre-image),通过散列算法,变换成固定长度的输出,该输出就是散列值.这种转换是一种压缩映射,也就是,散列值的 ...
- HDU 1686 Oulipo , 同 POJ 3461 Oulipo (字符串匹配,KMP)
HDU题目 POJ题目 求目标串s中包含多少个模式串p KMP算法,必须好好利用next数组,, (kmp解析)——可参考 海子的博客 KMP算法 //写法一: #include<string ...
- HDU 1686 Oulipo(kmp)
Problem Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, w ...
- 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裸题)
Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, without t ...
随机推荐
- Chromely
Chromely Chromely is a lightweight alternative to Electron.NET, Electron for .NET/.NET Core develope ...
- kali安装教程
首先在vm里面新建虚拟机,直接选择典型,然后下一步. 1 2 然后到了这一步,选择中间的安装程序光盘镜像文件,然后去文件里面找你自己下载的镜像,这时候可能系统会出现无法检测此光盘镜像中的操作系 ...
- Git016--Work
GIT常用命令 git常用命令: //初始化git目录: $ git init //把当前目录变成git可以管理的仓库 //添加文件到暂存区 $ git add file //把文件添加到仓库 $ g ...
- URIs, URLs, and URNs
首先,URI,是uniform resource identifier,统一资源标识符,用来唯一的标识一个资源.而URL是uniform resource locator,统一资源定位器,它是一种具体 ...
- 手把手教你用Pytorch-Transformers——实战(二)
本文是<手把手教你用Pytorch-Transformers>的第二篇,主要讲实战 手把手教你用Pytorch-Transformers——部分源码解读及相关说明(一) 使用 PyTorc ...
- 【转载】研发应该懂的binlog知识(下)
引言 这篇是<研发应该懂的binlog知识(上)>的下半部分.在本文,我会阐述一下binlog的结构,以及如何使用java来解析binlog.不过,话说回来,其实严格意义上来说,研发应该还 ...
- 利用Redisson实现分布式锁及其底层原理解析
Redis介绍 参考地址:https://blog.csdn.net/turbo_zone/article/details/83422215 redis是一个key-value存储系统.和Memcac ...
- K-th Number Poj - 2104 主席树
K-th Number Poj - 2104 主席树 题意 给你n数字,然后有m次询问,询问一段区间内的第k小的数. 解题思路 这个题是限时训练做的题,我不会,看到这个题我开始是拒绝的,虽然题意清晰简 ...
- 持续集成工具——Jenkins
一.jenkins简介 1.持续集成工具 2.基于JAVA环境 二.环境搭建 1.安装JDK 2.安装配置git 3.安装配置tomcat Tomcat是针对Java的一个开源中间件服务器(容器),基 ...
- 1705. [Usaco2007 Nov]Telephone Wire 架设电话线
传送门 显然 $dp$,首先设 $f[i][j]$ 表示当前考虑到第 $i$ 个电线杆,高度为 $j$ 时的最小代价 那么有转移 $f[i][j]=f[i-1][k]+cost+C(j-k)$,其中 ...