码迷,mamicode.com
首页 > 其他好文 > 详细

Educational Codeforces Round 5F. Expensive Strings

时间:2019-04-19 11:56:24      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:code   comment   ace   back   出现   link   air   +=   double   

题意:给n个串ti,ps,i是s在ti中出现的次数,要求找到s,使得\(\sum_{i=1}^nc_i*p_{s,i}*|s|\)最大
题解:sam裸题,每次插入时相当于在fail链上到1的位置加ci,最后统一乘该节点状态的长度,我居然写了个lct维护!= =还wa了....后来发现打个标记topo一下即可

//#pragma GCC optimize(2)
//#pragma GCC optimize(3)
//#pragma GCC optimize(4)
//#pragma GCC optimize("unroll-loops")
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include<bits/stdc++.h>
#define fi first
#define se second
#define db double
#define mp make_pair
#define pb push_back
#define pi acos(-1.0)
#define ll long long
#define vi vector<int>
#define mod 1000000009
#define ld long double
//#define C 0.5772156649
//#define ls l,m,rt<<1
//#define rs m+1,r,rt<<1|1
#define pll pair<ll,ll>
#define pil pair<int,ll>
#define pli pair<ll,int>
#define pii pair<int,int>
#define ull unsigned long long
//#define base 1000000000000000000
#define fin freopen("a.txt","r",stdin)
#define fout freopen("a.txt","w",stdout)
#define fio ios::sync_with_stdio(false);cin.tie(0)
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline void sub(ll &a,ll b){a-=b;if(a<0)a+=mod;}
inline void add(ll &a,ll b){a+=b;if(a>=mod)a-=mod;}
template<typename T>inline T const& MAX(T const &a,T const &b){return a>b?a:b;}
template<typename T>inline T const& MIN(T const &a,T const &b){return a<b?a:b;}
inline ll qp(ll a,ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;a=a*a%mod,b>>=1;}return ans;}
inline ll qp(ll a,ll b,ll c){ll ans=1;while(b){if(b&1)ans=ans*a%c;a=a*a%c,b>>=1;}return ans;}

using namespace std;

const ull ba=233;
const db eps=1e-5;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int N=600000+10,maxn=1000000+10,inf=0x3f3f3f3f;

char s[N];
vector<char>v[100010];
struct SAM{
    int last,cnt;
    int ch[N<<1][26],fa[N<<1],l[N<<1];
    int a[N<<1],c[N<<1];
    ll sz[N<<1];
    SAM(){cnt=1;}
    void ins(int c,int x){
        if(ch[last][c])
        {
            int p=last,q=ch[last][c];
            if(l[q]==l[p]+1)last=q;
            else
            {
                int nq=++cnt;l[nq]=l[p]+1;
                memcpy(ch[nq],ch[q],sizeof ch[q]);
                fa[nq]=fa[q];fa[q]=last=nq;
                for(;ch[p][c]==q;p=fa[p])ch[p][c]=nq;
            }
            sz[last]+=x;
            return ;
        }
        int p=last,np=++cnt;last=np;l[np]=l[p]+1;
        for(;p&&!ch[p][c];p=fa[p])ch[p][c]=np;
        if(!p)fa[np]=1;
        else
        {
            int q=ch[p][c];
            if(l[p]+1==l[q])fa[np]=q;
            else
            {
                int nq=++cnt;l[nq]=l[p]+1;
                memcpy(ch[nq],ch[q],sizeof(ch[q]));
                fa[nq]=fa[q];fa[q]=fa[np]=nq;
                for(;ch[p][c]==q;p=fa[p])ch[p][c]=nq;
            }
        }
        sz[np]+=x;
    }
    void build(int id,int x)
    {
        last=1;
        for(int i=0;i<v[id].size();i++)ins(v[id][i]-'a',x);
    }
    void topo()
    {
        for(int i=1;i<=cnt;i++)c[l[i]]++;
        for(int i=1;i<=cnt;i++)c[i]+=c[i-1];
        for(int i=1;i<=cnt;i++)a[c[l[i]]--]=i;
    }
    void cal()
    {
        topo();
        for(int i=cnt;i;i--)sz[fa[a[i]]]+=sz[a[i]];
        ll ans=0;
        for(int i=2;i<=cnt;i++)ans=max(ans,sz[i]*l[i]);
        printf("%lld\n",ans);
    }
}sam;
int main()
{
    int n;scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%s",s);
        for(int j=0;s[j];j++)v[i].pb(s[j]);
    }
    for(int i=1;i<=n;i++)
    {
        int x;scanf("%d",&x);
        sam.build(i,x);
    }
    sam.cal();
    return 0;
}
/********************

********************/

Educational Codeforces Round 5F. Expensive Strings

标签:code   comment   ace   back   出现   link   air   +=   double   

原文地址:https://www.cnblogs.com/acjiumeng/p/10734886.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!