zoukankan      html  css  js  c++  java
  • POJ3461题解——KMP入门

    题目链接:http://poj.org/problem?id=3461

    Oulipo

    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
    题目意思:就是先给你一个整数t表示有多少组样例,每组样例由两个字符串组成,找出第一个字符串在第二个字符串中出现的次数。
    题目思路:这不就是kmp算法的模板题嘛。只不过我们按照kmp的规则去匹配当匹配完成以后(j==plen时)我们要做的不是返回模式串在文本串的位置,而是count++计数,并且继续往后找有没有再次和模式串匹配相同的,这里我们让j=next[j],而这也恰好是失配时的做法。
    ac代码如下:
    #include<iostream>
    #include<stdio.h>
    #include<string.h>
    using namespace std;
    const int maxn=1e6+10;
    int n,count,next[maxn];
    char s[maxn],p[maxn];
    void GetNext(char *p)
    {
        int j=-1;
        int i=0;
        next[0]=-1;
        int plen=strlen(p);
        while(i<plen)
        {
            if(j==-1||p[i]==p[j])
            {
                ++i;++j;
                if(p[i]==p[j])next[i]=next[j];
                else next[i]=j;
            }
            else j=next[j];
        }
    }
    void kmp(char *p,char *s)
    {
        int plen=strlen(p);
        int slen=strlen(s);
        
        int i=0;
        int j=0;
        
        while(i<slen&&j<plen)
        {
            if(j==-1||s[i]==p[j])
            {
                ++i;++j;
            }
            else j=next[j];
            if(j==plen)
            {
                count++;
                j=next[j];
            }
            //printf("i=%d j=%d
    ",i,j);
        }
        return;
    }
    int main()
    {
        scanf("%d",&n);
        while(n--)
        {
            scanf("%s%s",p,s);
            count=0;
            GetNext(p);
            /*
            for(int i=0;i<strlen(p);i++)
            printf("%d ",next[i]);printf("
    ");
            */
            kmp(p,s);//printf("666
    ");
            printf("%d
    ",count);
        }
        return 0;
    }
    View Code
  • 相关阅读:
    软件开发流程
    开心一笑 项目的开发和升级(转载知乎)
    转载编程之美
    etc/hosts文件详解
    自己centos7成功的修改了主机名(记录了该改哪些文件)
    CentOS7修改主机名的三种方法
    /etc/hosts文件修改后如何生效
    如何在Linux中显示和设置主机名(适用ubantu、centos等版本)
    Linux centos7 shell 介绍、 命令历史、命令补全和别名、通配符、输入输出重定向
    hadoop学习笔记(四):HDFS文件权限,安全模式,以及整体注意点总结
  • 原文地址:https://www.cnblogs.com/Mingusu/p/11796551.html
Copyright © 2011-2022 走看看