(KMP裸题)hdu1686 Oulipo

标签: KMP
13人阅读 评论(0) 收藏 举报
分类:

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
 

Source
 

Recommend
lcy   |   We have carefully selected several similar problems for you:  1358 1711 3336 3746 2203 

哔哩哔哩上大佬写的代码,以后作为模板使用,,,KMP刚开始接触,理解得还不是很透彻,,,

后记?!:

如何理解KMP 链接在此,还不点?!这位大佬是我看的多个讲解博客和视频中讲得最清晰的一个(大佬的其他算法的讲解也不错哦)。

//kmp裸题
#include<bits/stdc++.h>


using namespace std;
const int maxn=1e6+10;
int nxt[maxn];  //存储真前缀和真后缀相匹配的最大长度,所谓真前缀,就是长度小于字符串的长度,例如,abcd中最长真前缀是abc,真后缀同理
char str[maxn],mo[maxn];


void getNext(){
    int i=0,j=-1,len=strlen(mo); /*nxt[0]默认为-1,nxt[0]规定为0,
                                 当然也可以理解成j为真前缀和真后缀相匹配的最大长度,
                                 而真前缀长度肯定比字符 */
    nxt[0]=-1;
    while(i<len){
        if(j==-1||mo[i]==mo[j]) nxt[++i]=++j;
        else j=nxt[j];
    }
}


int kmp(){
	getNext();
    int i=0,j=0,len1=strlen(str),len2=strlen(mo);
    int ans=0;
    while(i<len1){
        //cout<<"***j="<<j<<endl;
        if(j==-1||mo[j]==str[i]) i++,j++;
        else j=nxt[j];
        if(j==len2) ans++;
         //cout<<"j="<<j<<endl;
    }
    return ans;
}


int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        scanf("%s%s",mo,str);
        /*for(int i=0;i<=10;i++)
            cout<<nxt[i]<<endl;*/
        printf("%d\n",kmp());
    }
    return 0;
}


查看评论

ARM裸机程序开发入门

本课程主要讲解ARM汇编语言在硬件上的编程应用,相关硬件的原理图分析,LED、蜂鸣器、按键、光感设备的裸机开发。
  • 2016年01月20日 10:11

HDU 1686 Oulipo

KMP 水题。
  • sky_miange
  • sky_miange
  • 2015-04-09 20:19:29
  • 692

A - Oulipo(KMP算法经典)

#include #include #include int match (char w[],char s[],int next[])//统计w[]在s[]中出现的次数 {    int cnt=...
  • u014665013
  • u014665013
  • 2014-07-20 19:53:52
  • 972

【哈希&KMP模板题】-POJ-3461-Oulipo

题目链接:http://poj.org/problem?id=3461 题目大意:
  • u013303743
  • u013303743
  • 2014-04-26 15:54:18
  • 1305

HDU - 1686 Oulipo (KMP)

Problem Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, ...
  • u011345136
  • u011345136
  • 2014-09-12 09:27:55
  • 853

poj3461—KMP裸题

题目大意:给出两个字符串p和s,求出p在s中出现的次数 #include #include #include #include using namespace std; char p[1...
  • zz_ylolita
  • zz_ylolita
  • 2016-02-10 22:36:22
  • 665

HDU - 1686 KMP裸题

题意: 求文本串中有多少个模式串。 思路: kmp算法是ac自动机的基础,其实质就是建立一个自动机,让文本串在自动机上运行。其中最关键的就是next数组,对于这个一定要深刻理解。 next...
  • Bahuia
  • Bahuia
  • 2017-02-14 08:48:11
  • 224

hdu1686 Oulipo【kmp】

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1686 题意:给你字符串W和T,让你求W在T中出现了多少次,可重叠的计算(见样例) 解析:每次匹配完...
  • Richie_ll
  • Richie_ll
  • 2017-02-23 19:19:42
  • 223

hdu1686 KMP水题

简单KMP题
  • zxf654073270
  • zxf654073270
  • 2014-10-28 19:04:13
  • 607

hdu1686 Oulipo(KMP)

Oulipo http://acm.hdu.edu.cn/showproblem.php?pid=1686 Time Limit: 3000/1000 MS (Java/Others)    Mem...
  • u011697060
  • u011697060
  • 2014-03-19 21:29:19
  • 284
    个人资料
    持之以恒
    等级:
    访问量: 2万+
    积分: 2480
    排名: 1万+
    最新评论