zoukankan      html  css  js  c++  java
  • hdu1686字符串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. 

    InputThe 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. 
    OutputFor 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
    题意:还是找最大匹配,但是这次可以重复
    题解:刚开始用的上一题的第一个方法,直接暴力遍历字符串结果tle,发现可以利用字符串kmp本身的特点处理
    #include<map>
    #include<set>
    #include<cmath>
    #include<queue>
    #include<stack>
    #include<vector>
    #include<cstdio>
    #include<iomanip>
    #include<cstdlib>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #define pi acos(-1)
    #define ll long long
    #define mod 1000000007
    #define ls l,m,rt<<1
    #define rs m+1,r,rt<<1|1
    
    using namespace std;
    
    const double g=10.0,eps=1e-9;
    const int N=10000+5,maxn=1000000+5,inf=0x3f3f3f3f;
    
    int Next[N],slen,plen,res;
    string str,ptr;
    
    void getnext()
    {
        int k=-1;
        Next[0]=-1;
        for(int i=1;i<=slen-1;i++)
        {
            while(k>-1&&str[k+1]!=str[i])k=Next[k];
            if(str[k+1]==str[i])k++;
            Next[i]=k;
        }
    }
    void kmp()
    {
        int k=-1;
        for(int i=0;i<plen;i++)
        {
            while(k>-1&&str[k+1]!=ptr[i])k=Next[k];
            if(str[k+1]==ptr[i])k++;
            if(k==slen-1)
            {
                res++;
            }
        }
    }
    int main()
    {
        ios::sync_with_stdio(false);
        cin.tie(0);
     //   cout<<setiosflags(ios::fixed)<<setprecision(2);
        int t;
        cin>>t;
        while(t--){
            cin>>str>>ptr;
            slen=str.size();
            plen=ptr.size();
            getnext();
            res=0;
            kmp();
            cout<<res<<endl;
        }
        return 0;
    }
    View Code
  • 相关阅读:
    MySql查看时间
    我的名人名言
    记录:今天架构让隔壁项目组使用MapDB,就好奇的百度了一下
    Hbase安装
    Hbase安装的坑: org.apache.hadoop.hbase.PleaseHoldException: Master is initializing(2)
    Hbase安装的坑:java.lang.ClassNotFoundException: org.apache.htrace.SamplerBuilder(1)
    Kafka安装
    linux下zookeeper安装
    启动kafka生产者报错: WARN [Producer clientId=console-producer] Connection to node -1 could not be established. Broker may not be available. (org.apache.kafka.clients.NetworkClient)
    org.postgresql.util.PSQLException: ������������: ��������� "runoobdba" ���������
  • 原文地址:https://www.cnblogs.com/acjiumeng/p/6807935.html
Copyright © 2011-2022 走看看