Oulipo

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7141    Accepted Submission(s): 2835

Problem 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
#include<stdio.h>
#include<string.h>
#define MAX 1000100
char p[10010],str[MAX];
int f[10010];
void getfail()
{
int i,j,len;
len=strlen(p);
f[0]=f[1]=0;
for(i=1;i<len;i++)
{
j=f[i];
while(j &&p [i]!=p[j])
j=f[j];
f[i+1]=p[i]==p[j]?j+1:0;
}
}
int main()
{
int n,m,j,i,t;
int l1,l2;
scanf("%d",&t);
while(t--)
{
scanf("%s%s",p,str);
getfail();
l1=strlen(p);
l2=strlen(str);
int s=0,j=0;
for(i=0;i<l2;i++)
{
while(j&&str[i]!=p[j])
j=f[j];
if(str[i]==p[j])
j++;
if(j>=l1)
{
s++;
j=f[j];//注意此处
}
}
printf("%d\n",s);
}
return 0;
}

失配函数优化写法:

#include<stdio.h>
#include<string.h>
#define MAX 10010
int ans,len1,len2;
char s[MAX],str[MAX*100];
int f[MAX];
void huang()
{
int i=0,j = -1;
f[i]=j;
while(i < len1)
{
if(j == -1||s[i]==s[j])
{
i++;
j++;
f[i]=j;
}
else
j=f[j];
}
}
void kmp()
{
huang();
int i=0,j=0;
for(i=0;i<len2;i++)
{
while(j && str[i] != s[j])
j = f[j];
if(str[i]==s[j])
j++;
if(j >= len1)
{
ans++;
j = f[j];
}
}
}
int main()
{
int i,t;
scanf("%d",&t);
while(t--)
{
scanf("%s%s",s,str);
len1=strlen(s);
len2=strlen(str);
huang();
ans=0;
kmp();
printf("%d\n",ans);
}
return 0;
}

  

 

 借着此题,写下求失配函数的模板:

/*
* f[]为失配函数数组
*
*/
getfail()
{
int i,j;
int len = strlen(p);
f[0]=f[1]=1;
for(i = 1; i < len; i++)
{
j = f[i];
while(j && p[i] != p[j])
j = f[j];
f[i+1]= p[i] ==p[j]?j+1:0;
}
}

  

hdoj 1686 Oulipo【求一个字符串在另一个字符串中出现次数】的更多相关文章

  1. HDU 1686 - Oulipo - [KMP模板题]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1686 Time Limit: 3000/1000 MS (Java/Others) Memory Li ...

  2. javascript 写一段代码,判断一个字符串中出现次数最多的字符串,并统计出现的次数

    javascript 写一段代码,判断一个字符串中出现次数最多的字符串,并统计出现的次数 function test(){ var bt = document.getElementById(" ...

  3. Java实现统计某字符串在另一个字符串中出现的次数

    面试时会经常考这样的题目,估计也不让使用正则表达式.还好这个算法还算简单,不过在草稿纸上写难免会出现运行异常,好吧,面试官赢了,乃们屌丝就实实在在的把代码码出来吧. 谢谢“心扉”对我代码bug的纠正, ...

  4. HDU - 1686 Oulipo KMP匹配运用

    id=25191" target="_blank" style="color:blue; text-decoration:none">HDU - ...

  5. 我的Java开发学习之旅------>求字符串中出现次数最多的字符串以及出现的次数

    金山公司面试题:一个字符串中可能包含a~z中的多个字符,如有重复,如String data="aavzcadfdsfsdhshgWasdfasdf",求出现次数最多的那个字母及次数 ...

  6. Java求字符串中出现次数最多的字符

    Java求字符串中出现次数最多的字符  [尊重原创,转载请注明出处]http://blog.csdn.net/guyuealian/article/details/51933611      Java ...

  7. 字符串A转换到字符串B,只能一次一次转换,每次转换只能把字符串A中的一个字符全部转换成另一个字符,是否能够转换成功

    public class DemoTest { public static void main(String[] args) { System.)); } /** * 有一个字符串A 有一个字符串B ...

  8. C#算法之判断一个字符串是否是对称字符串

    记得曾经一次面试时,面试官给我电脑,让我现场写个算法,判断一个字符串是不是对称字符串.我当时用了几分钟写了一个很简单的代码. 这里说的对称字符串是指字符串的左边和右边字符顺序相反,如"abb ...

  9. Java-map-第一题 (Map)利用Map,完成下面的功能: 从命令行读入一个字符串,表示一个年份,输出该年的世界杯冠军是哪支球队。如果该 年没有举办世界杯,则输出:没有举办世界杯。 附:世界杯冠军以及对应的夺冠年份,请参考本章附录。 附录

    第一题 (Map)利用Map,完成下面的功能: 从命令行读入一个字符串,表示一个年份,输出该年的世界杯冠军是哪支球队.如果该 年没有举办世界杯,则输出:没有举办世界杯. 附:世界杯冠军以及对应的夺冠年 ...

随机推荐

  1. iOS pop使用通知传值

    iOS pop回父级页面,使用通知传值 输入所要发送的信息 ,同时将label的值通过button方法调用传递, - (IBAction)buttonClick:(id)sender { //添加 字 ...

  2. 怎样在win7上远程连接linux系统

    window操作系统的电脑 一台安装了linux系统的服务器 putty.exe小软件 方法/步骤   在前面的环境和软件都有的情况下,双击putty.exe软件,如下图:   在软件界面中的:Hos ...

  3. windows上SVN服务器以及客户端TortoiseSVN的安装配置

    (1)svn的安装 1. 下载软件:Setup-Subversion-1.7.5.msi,安装就很容易了,一路NEXT 2. 把SVN的bin文件夹路径添加到环境变量中 把svn安装目录下的bin路径 ...

  4. gooflow0.6的流程设计

    为何使用gooflow:1.兼容性好 2.扩展点很多可以个性化设计 3.配有api文档 4.json格式的数据传输 gooflow0.8版 由于最近项目需要,急需设计一个流程,考虑到时间问题,和用户个 ...

  5. Android艺术开发探索第四章——View的工作原理(上)

    这章就比较好玩了,主要介绍一下View的工作原理,还有自定义View的实现方法,在Android中,View是一个很重要的角色,简单来说,View是Android中视觉的呈现,在界面上Android提 ...

  6. python 元组问题解决

    a = (1,2,{'k1':'b2'}) a[2]['k1'] = 5 print(a) (1, 2, {'k1': 5}) 为什么元素'b2' = 5 应该是元素'k1' = 5 求解 a[2][ ...

  7. python基础之元组(Tuple)、字典(Dictionary)详解

    元组定义 元组是另一个数据类型,类似于List(列表). 元组用”()”标识.内部元素用逗号隔开.但是元素不能二次赋值,相当于只读列表. 举例: tuple = ( ‘abcd’, 786 , 2.2 ...

  8. 关于scroll无法绑定的问题

    关于jQuery的scroll([[data],fn])事件, 概述是:当用户滚动指定的元素(元素包括:所有可滚动的元素和 window 对象)时,会触发该事件. 如绑定窗口对象: $(window) ...

  9. Netty版本升级血泪史之线程篇

    1. 背景 1.1. Netty 3.X系列版本现状 根据对Netty社区部分用户的调查,结合Netty在其它开源项目中的使用情况,我们可以看出目前Netty商用的主流版本集中在3.X和4.X上,其中 ...

  10. 关于用xercesImpl包解析xml文件遇中文报错问题

    今天在运行项目时报错: Caused by: org.apache.xerces.impl.io.MalformedByteSequenceException: 3 字节 UTF-8 序列的字节 3 ...