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 ...
随机推荐
- 类File
* File类常用的构造方法: * (1)File(String s);//由s确定File对象的文件名 * (2)File(String directory,String s);//由directo ...
- 【mysql连接与mysqldump】INFORMATION_SESSION_VARIABLES feature is disabled
摘要: mysql5.7 客户端连接出现:INFORMATION_SESSION_VARIABLES feature is disabled问题: mysqldump: Couldn't execut ...
- 关于mysql的使用命令(持续更新中...)
特别提示 本说明中的mysql 是基于windwos平台下的5.5 版本 安装完成后 请到mysql中设置配置文件 链接分享:链接:https://pan.baidu.com/s/1tv4ulZ ...
- new String创建了几个对象
String str = new String(“abc”) 到底创建了几个对象? 首先String str是定义了一个字符串变量,并未产生对象,=不产生对象,那么只有后面的new String(& ...
- 厉害了,Apache架构师们遵循的 30 条设计原则
作者:Srinath 翻译:贺卓凡,来源:公众号ImportSource Srinath通过不懈的努力最终总结出了30条架构原则,他主张架构师的角色应该由开发团队本身去扮演,而不是专门有个架构师团队或 ...
- [2019杭电多校第三场][hdu6608]Fansblog
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6608 大致题意是比p小的最大素数q,求q!%p的值. 由威尔逊定理开始推: $(p-1)!\equiv ...
- Neo4j 修改关系类型 type
没有直接修改的函数,也不需要,下面代码就可以: MATCH (n:User {name:"foo"})-[r:REL]->(m:User {name:"bar&qu ...
- spring-security问题记录
一.错误信息 Could not decode JSON for additional information: BaseClientDetails 2019-12-03 22:18:37.239 W ...
- Hystrix (容错,回退,降级,缓存)
Hystrix熔断机制就像家里的保险丝一样,若同时使用高功率的电器,就会烧坏电路,这时候保险丝自动断开就有效的保护了电路.而我们程序中也同样是这样.例如若此时数据库压力太大速度很慢,此时还有不断的请求 ...
- ElasticSearch 7.x 默认不在支持指定索引类型
原文:ElasticSearch 7.x 默认不在支持指定索引类型 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://bl ...