BZOJ4199 NOI2015 品酒大會 題解&代碼

並查集維護…着急回宿舍(浪)明天再寫詳細題解

——————————分割線——————————————
題解來啦!
題意:給出一個長度爲n的串s,如果s中的第p位開始的後綴和第q位開始的後綴的公共前綴長度爲r,那麼稱p和q是r相似的。將p和q混合在一起時,p和q混合物的權值是a[p]*a[q]。輸出x∈[0,n-1]的x相似方案數和x相似時的最大權值
題解:
看到後綴的公共前綴第一反應應該是後綴數組吧
這道題題意比較複雜…不過仔細看題還是看得懂的
我們很容易可以得到r-1相似中一定包含了所有r相似的情況,那麼考慮由r相似向下計算【用數組(vector)進行一次height[]相關的重新排序即可【以代碼中排序法爲主
r相似中的各相鄰rank進行一次並查集合並,合併的時候維護max,min,cnt即可(max是集合最大值,min是集合最小值,cnt是集合size),順便累計全局變量…嘛,沒了

TLE了一個小時23333333最後發現我的後綴數組沒用倍增

/**************************************************************
    Problem: 4199
    User: Rainbow6174
    Language: C++
    Result: Accepted
    Time:5992 ms
    Memory:29628 kb
****************************************************************/

#include <iostream>
#include <cstdio>
#include <vector>
using namespace std;
const int maxn = 300005;
vector <int> edge[maxn];
int n, s[maxn], a[maxn];
int fa[maxn], mi[maxn], ma[maxn], cn[maxn];
long long tot, del, ansc[maxn], ansd[maxn];
char str[maxn];
int wa[maxn], wb[maxn], wv[maxn], cnt[maxn];
int sa[maxn], height[maxn], Rank[maxn];
inline void DA(int *r,int n,int m)
{
    int *x = wa, *y = wb, p;
    for(int i = 0; i < m; i++) cnt[i] = 0;
    for(int i = 0; i < n; i++) cnt[ x[i] = r[i] ]++;
    for(int i = 1; i < m; i++) cnt[i] += cnt[i-1];
    for(int i = n-1; i >= 0; i--) sa[--cnt[x[i]]] = i;
    for(int j = 1; j < n ; j=j<<1)
    {
        p = 0;
        for(int i = n-j; i < n; i++) y[p++] = i;
        for(int i = 0; i < n; i++) if(sa[i] >= j) y[p++] = sa[i]-j;
        for(int i = 0; i < n; i++) wv[i] = x[y[i]];
        for(int i = 0; i < m; i++) cnt[i] = 0;
        for(int i = 0; i < n; i++) cnt[wv[i]]++;
        for(int i = 1; i < m; i++) cnt[i] += cnt[i-1];
        for(int i = n-1; i >= 0; i--) sa[--cnt[wv[i]]] = y[i];
        swap(x,y);
        p=1;
        x[sa[0]] = 0;
        for(int i = 1; i < n; i++)
            x[sa[i]] = (y[sa[i]] == y[sa[i-1]]) && (y[sa[i]+j] == y[sa[i-1]+j]) ? p-1 : p++;
        if(p >= n) break;
        m = p;
    }
}
void calheight(int *r,int n)
{
    for(int i = 1; i <= n; i++)Rank[sa[i]] = i;
    int j = 0, k = 0;
    for(int i = 0; i < n; height[Rank[i]] = k, i++)
        for(k?k--:k=0, j = sa[Rank[i]-1]; r[i+k] == r[j+k]; k++);
}
void init(void)
{
    for(int i = 1; i <= n; i++)
    {
        fa[i] = i; cn[i] = 1;
        mi[i] = ma[i] = a[sa[i]];
    }
}
int Find(int x)
{
    if(x == fa[x])return x;
    return fa[x] = Find(fa[x]);
}
void unite(int x,int y)
{
    x = Find(x);
    y = Find(y);
    if(x == y) return;
    if(x > y) swap(x,y);
    long long cal = max((long long)ma[x] * (long long)ma[y], (long long)mi[x] * (long long)mi[y]);
    if(!tot || cal > del) del = cal;
    tot += (long long)cn[x] * (long long)cn[y];
    fa[y] = x;
    ma[x] = max(ma[y], ma[x]);
    mi[x] = min(mi[y], mi[x]);
    cn[x] += cn[y];
}
int main(void)
{
    scanf("%d%s", &n, str);
    for(int i = 0; i < n; i++)
    {
        scanf("%d", &a[i]);
        s[i] = str[i]-'a'+1;
    }
    DA(s,n+1,27);
    calheight(s,n);
    for(int i = 2; i <= n; i++)
        edge[height[i]].push_back(i);
    init();
    for(int i = n-1; i >= 0; i--)
    {
        for(int j = 0; j < edge[i].size(); j++)
            unite(edge[i][j],edge[i][j]-1);
        ansc[i] = tot;
        ansd[i] = del;
    }
    for(int i = 0; i < n; i++)
        printf("%lld %lld\n", ansc[i], ansd[i]);
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章