zoukankan      html  css  js  c++  java
  • HDU3518Boring counting(后缀自动机)

    Problem Description
    035 now faced a tough problem,his english teacher gives him a string,which consists with n lower case letter,he must figure out how many substrings appear at least twice,moreover,such apearances can not overlap each other.
    Take aaaa as an example.”a” apears four times,”aa” apears two times without overlaping.however,aaa can’t apear more than one time without overlaping.since we can get “aaa” from [0-2](The position of string begins with 0) and [1-3]. But the interval [0-2] and [1-3] overlaps each other.So “aaa” can not take into account.Therefore,the answer is 2(“a”,and “aa”).
     
    Input
    The input data consist with several test cases.The input ends with a line “#”.each test case contain a string consists with lower letter,the length n won’t exceed 1000(n <= 1000).
     
    Output
    For each test case output an integer ans,which represent the answer for the test case.you’d better use int64 to avoid unnecessary trouble.
     
    Sample Input
    aaaa
    ababcabb
    aaaaaa
    #
     
    Sample Output
    2
    3
    3

    emmmmmmm,WA了,因为L和R错了;

    找L和R需要全部字母插入后拓扑排序求出。

    如果不,maxlen(v)+1 < maxlen(x)的时候会拆点,导致x点和拆出的y点L,R出错。

    。。。好像是个很弱智的问题,勿喷,我知道错啦。

    和HDU4416差不多,写法上一个从0开始,一个从1开始。

    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<string>
    using namespace std;
    const int maxn=1000100;
    int tot,slink[2*maxn],trans[2*maxn][26],maxlen[2*maxn];
    char str[2*maxn];
    int R[maxn],L[maxn],last;
    long long ans;
    void init()
    {
        ans=tot=0;
        last=0;
        memset(trans[0],-1,sizeof(trans[0]));
        memset(L,0,sizeof(L));
        memset(R,0,sizeof(R));
        slink[0]=-1; maxlen[0]=0;
    }
    void add_char(int pos,char chr) 
    {
        bool ww=false;
        int c=chr-'a',nq=0;
        int p=last,np=++tot;
        maxlen[np]=maxlen[p]+1;
        memset(trans[np],-1,sizeof(trans[np])); 
        while(p!=-1&&trans[p][c]==-1)  trans[p][c]=np,p=slink[p];
        if(p==-1) slink[np]=0;
        else
        {
            int q=trans[p][c];
            if(maxlen[q]!=maxlen[p]+1)
            {
                nq=++tot;
                memcpy(trans[nq],trans[q],sizeof(trans[q]));
                maxlen[nq]=maxlen[p]+1;
                slink[nq]=slink[q];
                slink[np]=slink[q]=nq;
                while(p!=-1&&trans[p][c]==q) trans[p][c]=nq,p=slink[p];
                L[nq]=R[nq]=pos;
                ww=true;
            }
            else slink[np]=q;
        }
        last=np;
    for(;np>0;np=slink[np]){ if(!L[np]) L[np]=pos; R[np]=pos; } } int main() { while(~scanf("%s",str)){ if(str[0]=='#') return 0; init(); int N=strlen(str); for(int i=0; i<N; i++) add_char(i+1,str[i]); for(int i=1;i<=tot;i++) { if(R[i]-L[i]>=maxlen[i]) ans+=maxlen[i]-maxlen[slink[i]]; else ans+=max(0,R[i]-L[i]-maxlen[slink[i]]+1); } printf("%lld ",ans); } }

    别人的AC代码:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #define lng long long
    using namespace std;
    
    const int maxn = 5000 + 10;
    char s[maxn];
    int str[maxn], len;
    struct suffixautomaton
    {
        int ch[maxn][30], pre[maxn], val[maxn], top[maxn];
        int c[maxn];
        int l[maxn], r[maxn];
        int sz, last;
    
        void init() { pre[0] = -1; last = 0; sz = 1; memset(ch[0], 0, sizeof(ch[0])); }
    
        void insert(int x)
        {
            int p = last, np = sz++; last = np;
            memset(ch[np], 0, sizeof(ch[np]));
            val[np] = val[p] + 1;
            while(p != -1 && ch[p][x] == 0)
            {
                ch[p][x] = np;
                p = pre[p];
            }
            if(p == -1) pre[np] = 0;
            else
            {
                int q = ch[p][x];
                if(val[q] == val[p] + 1)
                    pre[np] = q;
                else
                {
                    int nq = sz++;
                    memcpy(ch[nq], ch[q], sizeof(ch[q]));
                    val[nq] = val[p] + 1;
                    pre[nq] = pre[q];
                    pre[q] = pre[np] = nq;
                    while(p != -1 && ch[p][x] == q) { ch[p][x] = nq; p = pre[p]; }
                }
            }
        }
    
        void solve()
        {
            memset(c, 0, sizeof(c));
            for(int i = 0; i < sz; ++i) c[val[i]] += 1;
            for(int i = 1; i <= len; ++i) c[i] += c[i - 1];
            for(int i = 0; i < sz; ++i) top[--c[val[i]]] = i;
            for(int i = 0; i < sz; ++i) { l[i] = len + 1; r[i] = -1; }
            for(int i = 0; ; i = ch[i][str[val[i]]])
            {
                l[i] = r[i] = val[i];
                if(val[i] == len) break;
            }
            for(int i = sz - 1; i > 0; --i)
            {
                int u = top[i];
                l[pre[u]] = min(l[pre[u]], l[u]);
                r[pre[u]] = max(r[pre[u]], r[u]);
            }
            lng ans = 0;
            for(int i = 0; i < sz; ++i)
            {
                if(r[i] - l[i] > val[pre[i]])
                {
                    lng tmp = min(val[i], r[i] - l[i]);
                    ans += (tmp - val[pre[i]]);
                }
            }
            printf("%I64d
    ", ans);
        }
    }sam;
    
    int main()
    {
        while(~scanf("%s", s) && s[0] != '#')
        {
            len = strlen(s);
            sam.init();
            for(int i = 0; s[i]; ++i) 
            { 
                str[i] = s[i] - 'a'; 
                sam.insert(str[i]); 
            }
            sam.solve();
        }
        return 0;
    }

     地址:http://blog.csdn.net/cool_Fires/article/details/9732475

  • 相关阅读:
    【重构学习】12 重构学习感想
    【重构学习】11 大型重构
    【重构学习】10 继承关系的重构
    【重构学习】09 函数调用的重构
    【重构学习】08 条件表达式的重构
    嵊州D4T1 翻车 rollover 真的翻车了
    计算圆内格点数
    嵊州D3T2 福尔贝斯太太的快乐夏日 summer
    嵊州D3T3 light
    嵊州D3T1 山魔 烙饼问题
  • 原文地址:https://www.cnblogs.com/hua-dong/p/7895713.html
Copyright © 2011-2022 走看看