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 ...
随机推荐
- Object not locked by thread before notify() in onPostExecute
Ask Question Asked 5 years, 4 months ago Active 3 years, 9 months ago Viewed 56k time 41 2 I try to ...
- 16/7/8_PHP-字符串介绍
不知道为什么慕课网还这个都要介绍.不过还是一个新的知识点. PHP开发中,我们遇到最多的可能就是字符串. 字符串变量用于包含字符串的值. 一个字符串 通过下面的3种方法来定义: 1.单引号2.双引号3 ...
- GMS测试常用命令CTS>S&VTS
本文档介绍一下cts,gts,sts,vts,cts-on-gsi等测试的常用命令,基于Android9. [附件]Google官网的命令网页. 常用通用命令参数: 列出历史测试结果:l r 指定设备 ...
- React-onsenui之RouterNavigator组件解读
var index = 1;// index的最外层初始值,亦是全局 var MyPage = React.createClass({ //构成工具栏组件,根据hasBackButton的值为back ...
- 浅谈数学上的矩阵——矩阵的乘法运算的概念及C++上的实现模板
首先让我们来谈一谈数学意义上的矩阵(在座各位也可以简单地将它理解为一个二维数组) 这样可以帮助我们理解矩阵加速及其运用的原理(矩阵加速是一个及其玄学的东西,所以请重点理解矩阵乘法) 这里给出一段严格 ...
- [10期]浅谈SSRF安全漏洞
引子:SSRF 服务端请求伪造攻击 很多web应用都提供从其他服务器上获取数据的功能.使用用户指定的URL,web应用可以从其他服务器获取图片,下载文件,读取文件内容等. 这个功能被恶意使用的话,可以 ...
- 前端 CSS的选择器 伪类选择器 CSS3 nth-child()
first-child 选中第一个标签 应用CSS样式 <!DOCTYPE html> <html lang="en"> <head> < ...
- Hibernate入门4
HIbernate的导航查询: 适用场景:当一张A表关联到另一张B表的多条记录,存在一对多的关系(或者多对多),那么查询A表的记录时,就可以将A表某条记录关联的B表的所有记录查询出来,这种方式,就叫做 ...
- java面向对象基础总结
本周学习了java面向对象的一些基本概念,介绍了它三个主要特性,封装性.继承性.多态性,类与对象的关系,栈堆的关系,三个特性中主要讲了封装性,其他两个后面再讲. 类实际上是表示一个客观世界某类群体的一 ...
- 使用hash表进行数组去重
哈希表(Hash table,也叫散列表),是根据关键码值(Key value)而直接进行访问的数据结构.也就是说,它通过把关键码值映射到表中一个位置来访问记录,以加快查找的速度.这个映射函数叫做散列 ...