hdoj--1686--Oulipo
2015-10-17 21:04
411 查看
Oulipo
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 8161 Accepted Submission(s): 3290
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
思路:利用kmp进行求解,需要注意的就是数组的长度,要求是要大于1000000的,小的话会判定为超时的。
ac代码:
#include<stdio.h> #include<string.h> char a[1000010],b[1000010]; int lena,nextval[1000010],lenb,next[1000010];//坑死人,妈蛋,没看清长度,一直超时,不能说声数组开小了啊。 void get_next(){ int i=1,j=0; while(i<=lena){//next数组需要多计算一位。 if(j==0||a[i]==a[j]){ i++; j++; next[i]=j; } else j=next[j]; } } int kmp(){ int ans=0,i=1,j=1; get_next(); while(i<=lenb){ if(b[i]==a[j]||j==0){ i++; j++; } else j=next[j]; if(j>lena){ ans++; } } return ans; } int main(){ int n; scanf("%d",&n); while(n--){ scanf("%s",a+1); scanf("%s",b+1); lena=strlen(a+1); lenb=strlen(b+1); int ans=kmp(); printf("%d\n",ans); } return 0; }
相关文章推荐
- 服务器安装Macfee(麦咖啡)杀毒软件后可能出现的问题
- JDBC配置
- 类与对象动手动脑实践
- Chapter 4 Divide-and-Conquer
- BC第二场
- StoryBoard 关于Segue ----prepareForSegue:sender: 理解
- java网络编程之 UDP的使用
- 好用的编辑框布局控件TdxLayoutControl
- Linux 2.6内核中新的锁机制--RCU
- ThreadPoolExecutor运行原理
- 基于R语言的关联规则实现
- String 类
- iOS应用沙盒机制浅析
- 编写猜数字游戏
- <LeetCode><Easy> 100 Same Tree
- java线程池的简单使用
- Centos6.5 下编译64位 Hadoop 2.2.0
- 信息安全系统设计基础第六周学习总结
- Latex 表格(跨行、跨列、背景加灰)new
- spring IOC源码分析(3)