zoukankan      html  css  js  c++  java
  • string matching

    string matching

    exkmp

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn=1000005;
    int Nex[maxn],extend[maxn];
    void getNex(char str[])
    {
        int i=0,j,po,len=strlen(str);
        Nex[0]=len;
        while(str[i]==str[i+1]&&i+1<len)
        {
            i++;
        }
        Nex[1]=i;
        po=1;
        for(i=2; i<len; i++)
        {
            if(Nex[i-po]+i<Nex[po]+po)
            {
                Nex[i]=Nex[i-po];
            }
            else
            {
                j=Nex[po]+po-i;
                if(j<0)j=0;
                while(i+j<len&&str[j]==str[j+i])
                {
                    j++;
                }
                Nex[i]=j;
                po=i;
            }
        }
    }
    void Extend(char s1[],char s2[])
    {
        int i=0,j,po,len=strlen(s1),l2=strlen(s2);
        getNex(s2);
        while(s1[i]==s2[i]&&i<l2&&i<len)
        {
            i++;
        }
        extend[0]=i;
        po=0;
        for(i=1; i<len; i++)
        {
            if(Nex[i-po]+i<extend[po]+po)
            {
                extend[i]=Nex[i-po];
            }
            else
            {
                j=extend[po]+po-i;
                if(j<0)j=0;
                while(i+j<len&&j<l2&&s1[j+i]==s2[j])j++;
                extend[i]=j;
                po=i;
            }
        }
    }
    char s[1000005];
    char t[1000005];
    int main()
    {
       freopen("1.in","r",stdin);
        int T;
        scanf("%d",&T);
        while(T--)
        {
            memset(extend,0,sizeof extend);
            memset(Nex,0,sizeof Nex);
            scanf("%s",s);
            strcpy(t,s);
            Extend(s,t);
            int n=strlen(s);
            long long ans=0;
            //cout<<s<<t<<endl;
            for(int i=1; i<=n-2; i++)
            {
                if(i+extend[i]==n)
                    ans+=extend[i];
                else
                ans+=extend[i]+1;
                //cout<<extend[i]<<" "<<i<<" "<<n-1<<'
    ';
            }
            if(n>1)
            ans+=1;
            cout<<ans<<'
    ';
        }
    }
  • 相关阅读:
    17多校6 HDU
    E. Present for Vitalik the Philatelist 反演+容斥
    HDU
    F. Cowmpany Cowmpensation dp+拉格朗日插值
    hdu6088 组合数+反演+拆系数fft
    任意模数fft
    Codeforces Round #258 (Div. 2)E
    bzoj3670: [Noi2014]动物园
    HDU
    IO-InputStreamReader
  • 原文地址:https://www.cnblogs.com/liulex/p/11312295.html
Copyright © 2011-2022 走看看