zoukankan      html  css  js  c++  java
  • [SDOI2016] 生成魔咒

    Description

    按顺序在一个序列的末尾插入数字,求每次插入后的本质不同子串个数

    Solution

    后缀自动机增量构造时,已经有的结点的 len 是不会再变化的,而 nq 结点不会产生贡献,于是每次加上 maxlen[newnode]-maxlen[link[newnode]] 即可

    由于插入的是数字,所以需要用 map 存储

    #include <bits/stdc++.h>
    using namespace std;
    const int Maxn = 200005;
    struct Suffix_Automata {
        int maxlen[Maxn], link[Maxn], Size, Last;
        map<int,int> trans[Maxn];
        int t[Maxn], a[Maxn], cnt[Maxn], f[Maxn];
        long long ans;
        Suffix_Automata() { Size = Last = 1; }
        inline void Extend(int id) {
            int cur = (++ Size), p;
            maxlen[cur] = maxlen[Last] + 1;
            cnt[cur] = 1;
            for (p = Last; p && !trans[p][id]; p = link[p]) trans[p][id] = cur;
            if (!p) link[cur] = 1;
            else {
                int q = trans[p][id];
                if (maxlen[q] == maxlen[p] + 1) link[cur] = q;
                else {
                    int clone = (++ Size);
                    maxlen[clone] = maxlen[p] + 1;
                    trans[clone] = trans[q];
                    link[clone] = link[q];
                    for (; p && trans[p][id] == q; p = link[p]) trans[p][id] = clone;
                    link[cur] = link[q] = clone;
                }
            }
            Last = cur;
            ans += maxlen[cur] - maxlen[link[cur]];
        }
    } sam;
    
    int main() {
        ios::sync_with_stdio(false);
        int n;
        cin>>n;
        for(int i=0;i<n;i++) {
            int x;
            cin>>x;
            sam.Extend(x), cout<<sam.ans<<endl;
        }
    }
    
    
    
  • 相关阅读:
    线程
    unix架构
    Unix命令
    可重入函数reentrant function
    Eclipse 中 program arguments 与 VM arguments 的区别
    Java中Generics的使用
    Java的Reflection机制
    Java按值传递、按引用传递
    Java label
    LeetCode Merge Intervals
  • 原文地址:https://www.cnblogs.com/mollnn/p/13179313.html
Copyright © 2011-2022 走看看