您的位置:首页 > 其它

【NOI2015】品酒大会

2015-09-07 19:32 218 查看
题目链接,同步赛的时候不会 SA , 所以当时不会做这个题2333

如果我们想要求出长度为 rr 的相同子串的对数

将满足 height[i]≥rheight[i] \geq r 的 ii 的集合与 i−1i - 1 的集合合并

那么每个集合内的子串都是 “rr 相似” 的

cnt[r]cnt[r] 表示 height[i]=rheight[i] = r 时的方案数

max[r]max[r] 表示 height[i]=rheight[i] = r 时的最大值

那么,按 heightheight 值从大到小将集合合并,

合并时, 记 xx ,yy 为要合并的两个集合,那么

cnt[r] += size[x]*size[y]

ans[r] = max{max[x]*max[y] , min[x]*min[y]}

最后从大到小计算答案

cnt′'[r] += cnt′'[r + 1] + cnt[r]

ans′'[r] = max{ans′'[r + 1],ans[r]}


时间复杂度: O(N∗logN)O(N*log N)

// savour uoj#131

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <vector>
#include <utility>
#include <stack>
#include <queue>
#include <iostream>
#include <algorithm>

template<class Num>void read(Num &x)
{
    char c; int flag = 1;
    while((c = getchar()) < '0' || c > '9')
        if(c == '-') flag *= -1;
    x = c - '0';
    while((c = getchar()) >= '0' && c <= '9')
        x = (x<<3) + (x<<1) + (c-'0');
    x *= flag;
    return;
}
template<class Num>void write(Num x)
{
    if(x < 0) putchar('-'), x = -x;
    static char s[20];int sl = 0;
    while(x) s[sl++] = x%10 + '0',x /= 10;
    if(!sl) {putchar('0');return;}
    while(sl) putchar(s[--sl]);
}

const int maxn = 3e5 + 20;

char s[maxn];

int a[maxn];

int n, sa[maxn], rank[maxn];
int c[maxn], height[maxn];

int fa[maxn], size[maxn];
long long max[maxn], min[maxn];

long long cnt, ans;
long long outc[maxn], outa[maxn];

struct Edge
{
    int v, next;

    Edge(int v = 0,int next = 0):v(v), next(next){}

}edge[maxn];
int head[maxn], el;

int find(int x)
{
    return x == fa[x] ? x : (fa[x] = find(fa[x]));
}
void newedge(int u,int v)
{
    edge[++el] = Edge(v, head[u]), head[u] = el;
}
void build_sa(int m)
{
    static int t0[maxn], t1[maxn];
    int *x = t0, *y = t1;

    for(int i = 1; i <= m; i++) c[i] = 0;
    for(int i = 1; i <= n; i++) c[x[i] = s[i]]++;
    for(int i = 1; i <= m; i++) c[i] += c[i - 1];
    for(int i = n; i >= 1; i--) sa[c[x[i]]--] = i;

    for(int k = 1; k <= n; k <<= 1)
    {
        int p = 0;
        for(int i = 0; i < k; i++) y[++p] = n - i;
        for(int i = 1; i <= n; i++)
            if(sa[i] > k) y[++p] = sa[i] - k;

        for(int i = 1; i <= m; i++) c[i] = 0;
        for(int i = 1; i <= n; i++) c[x[y[i]]]++;
        for(int i = 1; i <= m; i++) c[i] += c[i - 1];
        for(int i = n; i >= 1; i--) sa[c[x[y[i]]]--] = y[i];

        std::swap(x, y);

        x[sa[p = 1]] = 1;

        for(int i = 2; i <= n; i++)
            x[sa[i]] = y[sa[i]] == y[sa[i - 1]] && sa[i] + k <= n && sa[i - 1] + k <= n && y[sa[i] + k] == y[sa[i - 1] + k] ? p : ++p;

        if(p == n) break;
        m = p;
    }
}
void build_height()
{
    int k = 0;

    for(int i = 1; i <= n; i++) rank[sa[i]] = i;
    for(int i = 1; i <= n; i++)
    {
        if(k != 0) k--;

        if(rank[i] == 1) continue;

        int j = sa[rank[i] - 1];
        while(s[j + k] == s[i + k]) k++;
        height[rank[i]] = k;
    }
}
void gather(int x,int y)
{
    x = find(x), y = find(y);

    if(x == y) return;

    if(x > y) std::swap(x, y);

    long long calc = std::max(max[x] * max[y], min[x] * min[y]);

    if(!cnt || calc > ans) ans = calc;

    cnt += (long long)size[x] * size[y];

    fa[y] = x, size[x] += size[y];
    max[x] = std::max(max[y], max[x]);
    min[x] = std::min(min[y], min[x]);
}
void prework()
{
    build_sa(256), build_height();

    for(int i = 2; i <= n; i++)
        newedge(height[i], i);

    for(int i = 1; i <= n; i++)
    {
        fa[i] = i, size[i] = 1;
        min[i] = max[i] = a[sa[i]];
    }
}
void solve()
{
    for(int i = n - 1; i >= 0; i--)
    {
        for(int j = head[i]; j ; j = edge[j].next)
            gather(edge[j].v, edge[j].v - 1);

        outc[i] = cnt, outa[i] = ans;
    }
    for(int i = 0; i < n; i++)
    {
        write(outc[i]);
        putchar(' ');
        write(outa[i]), puts("");
    }
}
void init()
{
    read(n);

    scanf("%s", s + 1);
    for(int i = 1; i <= n; i++) read(a[i]);
}
int main()
{
#ifndef ONLINE_JUDGE
    freopen("savour.in","r",stdin);
    freopen("savour.out","w",stdout);
#endif

    init();

    prework();

    solve();

#ifndef ONLINE_JUDGE
    fclose(stdin);
    fclose(stdout);
#endif
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: