Oulipo HDU - 1686--strlen()耗时啊啊啊--KMP
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<iostream>
#include<algorithm>
#include<string.h>using namespace std;
int nxt[1000001];
char s[1000001];
char p[1000001];
int ans;
int l1,l2;
void getnext()
{nxt[0]=-1;int i=0,j=-1;while(i<l1){if(j==-1||p[i]==p[j]){i++;j++;nxt[i]=j;}elsej=nxt[j];}
}
int kmp()
{int i=0;int j=0;getnext();if(l2==1&&l1==1){if(s[0]==p[0])return 1;return 0;}while(i<l2&&j<l1){if(j==-1||s[i]==p[j]){i++;j++;}elsej=nxt[j];if(j==l1){ans++;j=nxt[j];}}return ans;
}
int main()
{ios::sync_with_stdio(false);cin.tie(0);int t;cin>>t;while(t--){ans=0;cin>>p;cin>>s;l1=strlen(p);l2=strlen(s);cout<<kmp()<<endl;}return 0;
}
Oulipo HDU - 1686--strlen()耗时啊啊啊--KMP相关推荐
- Oulipo HDU - 1686(哈希或KMP)匹配字符串
题意:字符串匹配:寻找字符串S中,字符串T出现的次数 思路:KMP或哈希 The French author Georges Perec (1936–1982) once wrote a book, ...
- KMP - Oulipo - HDU - 1686
HDU - 1686 The French author Georges Perec (1936–1982) once wrote a book, La disparition, without th ...
- Oulipo HDU - 1686 (kmp初见讨伐!)
题目链接 题意:给你两个字符串s,t.让你求在s中t出现了多少次. 解题思路: 先写前缀函数(前缀函数的写法点这里了解) 然后就是常规KMP模板了.统计答案时重置pos_t的位置. 错误原因:对重置p ...
- Oulipo HDU - 1686 (使用扩展kmp进行讨伐!)
题目链接 题意:给你两个字符串s,t.让你求在s中t出现了多少次. 解题思路: kmp做法点这里 使用扩展kmp.构建一个新字符串k=t+▲+s ▲为分隔符,保证不会影响s求z函数. 对k使用扩展km ...
- HDU - 1686 Oulipo
https://vjudge.net/problem/HDU-1686 HDU - 1686 Oulipo 题目 分析 AC代码 题目 The French author Georges Perec ...
- HDU - 1686 Oulipo KMP
Oulipo Problem Description The French author Georges Perec (1936–1982) once wrote a book, La dispari ...
- HDU 1686 Oulipo【kmp求子串出现的次数】
The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter 'e ...
- hdu 1686 Oulipo(kmp)
Problem Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, w ...
- HDU 1686 [KMP]
题目链接 思路:本题需要求出模式串在主串里的出现次数,利用KMP求出即可 /* --------------KMP算法-------------- 用于在字符串S中匹配字符串T本题利用KMP算法进行匹 ...
最新文章
- ruby语法_Ruby函数(方法)语法
- java tar 安装程序_linux下jdk的安装(tar包)
- 《南方都市报》:三鹿集团300万摆平搜索引擎?
- Python——有关transpose的多维转置的难点
- linux周期执行某任务方法
- ES6——扩展运算符/三点运算符(...)
- 使用qt多线程的一个简单方法
- CICD详解(六)——SVN+Jenkins项目控制实战
- Ukey双因素身份认证步骤 安当加密
- matlab ignoreanalyticconstraints,MATLAB函数随笔之计算篇
- 计算机组成原理运算器实验报告及数据分析,《计算机组成原理》运算器实验报告...
- linux下使用iso文件安装yum
- 散列表--双散列、再散列与可扩散列
- 一个SAP程序员的2020年度总结:未知生,焉知死
- 数据库知识点整理(一)
- 交易履约之订单中心实践
- Vue CLI 官方文档(一)@vue/cli、@vue/cli-service、插件和 Preset
- linux脚本一致性判断,生产环境之文件一致性检测脚本
- TypeScript常用基础语法学习
- java rtp 分片_RTP 协议解包为 H264 裸流