• hihocoder 1457 后缀自动机四·重复旋律7 ( 多串连接处理技巧 )


    题目链接

    分析 :

    这道题对于单个串的用 SAM 然后想想怎么维护就行了

    但是多个串下、可以先将所有的串用一个不在字符集( 这道题的字符集是 '0' ~ '9' )

    链接起来、建立后缀自动机之后

    在统计答案的时候直接忽略掉不合法的边集的状态转移即可

    #include<bits/stdc++.h>
    #define LL long long
    #define ULL unsigned long long
    
    #define scl(i) scanf("%lld", &i)
    #define scll(i, j) scanf("%lld %lld", &i, &j)
    #define sclll(i, j, k) scanf("%lld %lld %lld", &i, &j, &k)
    #define scllll(i, j, k, l) scanf("%lld %lld %lld %lld", &i, &j, &k, &l)
    
    #define scs(i) scanf("%s", i)
    #define sci(i) scanf("%d", &i)
    #define scd(i) scanf("%lf", &i)
    #define scIl(i) scanf("%I64d", &i)
    #define scii(i, j) scanf("%d %d", &i, &j)
    #define scdd(i, j) scanf("%lf %lf", &i, &j)
    #define scIll(i, j) scanf("%I64d %I64d", &i, &j)
    #define sciii(i, j, k) scanf("%d %d %d", &i, &j, &k)
    #define scddd(i, j, k) scanf("%lf %lf %lf", &i, &j, &k)
    #define scIlll(i, j, k) scanf("%I64d %I64d %I64d", &i, &j, &k)
    #define sciiii(i, j, k, l) scanf("%d %d %d %d", &i, &j, &k, &l)
    #define scdddd(i, j, k, l) scanf("%lf %lf %lf %lf", &i, &j, &k, &l)
    #define scIllll(i, j, k, l) scanf("%I64d %I64d %I64d %I64d", &i, &j, &k, &l)
    
    #define lson l, m, rt<<1
    #define rson m+1, r, rt<<1|1
    #define lowbit(i) (i & (-i))
    #define mem(i, j) memset(i, j, sizeof(i))
    
    #define fir first
    #define sec second
    #define VI vector<int>
    #define ins(i) insert(i)
    #define pb(i) push_back(i)
    #define pii pair<int, int>
    #define VL vector<long long>
    #define mk(i, j) make_pair(i, j)
    #define all(i) i.begin(), i.end()
    #define pll pair<long long, long long>
    
    #define _TIME 0
    #define _INPUT 0
    #define _OUTPUT 0
    clock_t START, END;
    void __stTIME();
    void __enTIME();
    void __IOPUT();
    using namespace std;
    
    const LL mod = (LL)1e9 + 7;
    
    struct SAM
    {
        static const int MAXN = ((int)1e6 + 10)<<1;//大小为字符串长度两倍
        static const int LetterSize = 26;
    
        int tot, last, ch[MAXN][LetterSize], fa[MAXN], len[MAXN];
        int tpSum[MAXN], tp[MAXN], cnt[MAXN]; //tpSum,tp用于拓扑排序,tp为排序后的数组
    
        void init(void){
            last = tot = 1;
            len[1] = 0;
            memset( ch[1], 0, sizeof ch[1]);
        }
    
        void add( int x){
            int p = last, np = last = ++tot;
            len[np] = len[p] + 1, cnt[last] = 1;
            memset( ch[np], 0, sizeof ch[np]);
            while( p && !ch[p][x]) ch[p][x] = np, p = fa[p];
            if( p == 0)
                fa[np] = 1;
            else
            {
                int q = ch[p][x];
                if( len[q] == len[p] + 1)
                    fa[np] = q;
                else
                {
                    int nq = ++tot;
                    memcpy( ch[nq], ch[q], sizeof ch[q]);
                    len[nq] = len[p] + 1, fa[nq] = fa[q], fa[q] = fa[np] = nq;
                    while( p && ch[p][x] == q)  ch[p][x] = nq, p = fa[p];
                }
            }
        }
    
        void toposort(void){
            for(int i = 1; i <= len[last]; i++)   tpSum[i] = 0;
            for(int i = 1; i <= tot; i++)   tpSum[len[i]]++;
            for(int i = 1; i <= len[last]; i++)   tpSum[i] += tpSum[i-1];
            for(int i = 1; i <= tot; i++)   tp[tpSum[len[i]]--] = i;
            //for(int i = tot; i; i--)   cnt[fa[tp[i]]] += cnt[tp[i]];
        }
    
        LL sum[MAXN];
        LL GetAns(){
    
            mem(cnt, 0);
    
            cnt[1] = 1;
    
            for(int i=1; i<=tot; i++){
                int cur = tp[i];
    
                if(!cnt[cur]) continue;
    
                for(int c=0; c<10; c++){
                    int nxt = ch[cur][c];
                    if(nxt){
                        cnt[nxt] = (cnt[nxt] + cnt[cur]) % mod;
                        sum[nxt] = (sum[nxt] + (sum[cur] * 10 % mod + 1LL * c * cnt[cur] % mod) % mod) %mod;
                    }
                }
            }
    
            LL ret = 0;
            for(int i=1; i<=tot; i++) ret = (ret + sum[i]) % mod;
    
            return ret;
        }
    
    } sam;
    
    int n;
    char str[(int)1e6 + 10];
    
    int main(void){__stTIME();__IOPUT();
    
        sci(n);
    
        sam.init();
    
        for(int i=0; i<n; i++){
            scs(str);
    
            int len = strlen(str);
    
            for(int j=0; j<len; j++)
                sam.add(str[j] - '0');
    
            if(i != n-1)
                sam.add(10);
        }
    
        sam.toposort();
    
        printf("%lld
    ", sam.GetAns());
    
    
    __enTIME();return 0;}
    
    
    void __stTIME()
    {
        #if _TIME
            START = clock();
        #endif
    }
    
    void __enTIME()
    {
        #if _TIME
            END = clock();
            cerr<<"execute time = "<<(double)(END-START)/CLOCKS_PER_SEC<<endl;
        #endif
    }
    
    void __IOPUT()
    {
        #if _INPUT
            freopen("in.txt", "r", stdin);
        #endif
        #if _OUTPUT
            freopen("out.txt", "w", stdout);
        #endif
    }
    View Code
  • 相关阅读:
    Ajax学习感悟
    C#自定义控件designmodel的判断
    Asp.net+Flash多文件上传
    .net下帮助文件(sandcastle)
    水晶报表小例用于学习
    WinAPI转C#利器
    利用HttpHandler和Cache统计点击量
    关于C#调用API的理解(汇多考勤机HD4K)
    ubuntu 12.04 配置PHP開發環境遇到的問題
    windows 7 系統在VMWear workstation 9上安裝Mac OS X 10.7
  • 原文地址:https://www.cnblogs.com/qwertiLH/p/9712648.html
Copyright © 2020-2023  润新知