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 ...
随机推荐
- beyond compare 4.2.9桌面右键集成的问题修复
beyond compare 4.2.9桌面右键集成的问题修复 安装后,发现在WIN64时,注册表中注册的DLL库有问题 出错处: [HKEY_CLASSES_ROOT\CLSID\{57FA2D12 ...
- uni-app-在开启小程序在微信开发工具打开时,打开失败,解决方法:手动打开
这是我自学ui-app的第一张记录内容,问题都是我在实际开发中遇到的问题 1.微信开发工具打开失败,提示是: 接着我的流程就是打开上面提到的链接,进入页面:https://developers.wei ...
- C++边双缩点,Redundant Paths 分离的路径
一道比较简单的 关于边双的题,个人感觉难度不大. 求出整个图的边双,根据边双的定义我们可以延伸出 边双的任两个点都有至少两种路径来互相抵达(因为其不存在割边) .不妨将每个边双缩成一个点,样例中的图便 ...
- PL/SQL Developer 报错Dynamic Performance Tables not accessible, Automatic Statistics disabled for this session You can disable statistics in the preference menu, or obtain select priviliges on the V$ses
可以从以下几个方面考虑: 1)单独给用户授动态性能视图的权限: SQL> grant select on V_session to user; SQL> grant select on ...
- 剑指offer--day01
1.1题目:二维数组中的查找:在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断 ...
- gradle阿里云镜像配置
Maven镜像的配置参考: http://blog.java1234.com/blog/articles/252.html buildscript { repositories { mavenLoca ...
- js 相关好文章推荐
1.关于xmlhttprequest https://segmentfault.com/a/1190000004322487 2.XMLHttpRequest2 新技巧 https://www.htm ...
- react-redux --》react中 最好用的状态管理方式
一.Redux与组件 react-redux是一个第三方插件使我们在react上更方便的来使用redux这个数据架构 React-Redux提供connect方法,用于从UI组件生成容器组件,conn ...
- python字典-基础
一.解释 像列表一样,“字典”是许多值的集合.但不像列表的下标,字典的索引可以 使用许多不同数据类型,不只是整数.字典的索引被称为“键”,键及其关联的值 称为“键-值”对. 二.列表创建方式 1. I ...
- linux command --- terminal common commands
switch to root : sudo su.su root.sudo -s switch to users : su god(user name) set root password : sud ...