CF696D Place ...

Lay...

The meaning of problems

Translations:

Solution

Of course, is the AC unit above DP

Shaped turn persimmon is obvious: \ (DP [K] [I] = max (DP [K] [I], DP [K-. 1] [J] + Val [I] [J]) \) , wherein i and j are nodes inside the AC automaton, \ (Val [i] [j] \) represents from node i to node j may increase the contribution of

Then you can consider transferring a manner similar to the matrix multiplication:

A matrix \ (A \) , where i represents the maximum row and column j the answer began to shift from a node i, and then transferred to node j

Then each transfer is \ (now_A [i] [j ] = max (now_A [i] [j], last_A [i] [k] + val [k] [j]) \)

Then you can use a method similar to the matrix of the rapid power transfer

The total time complexity is \ (O (log_n (m \ times l) ^ 3) \)

l is the average length, m is the number of strings

Since \ (m \ times l \) maximum of only 200, so this problem can be

code:

#include<bits/stdc++.h>
using namespace std;
#define int long long
int tot;
struct node{
    int a[210][210];
    node(){memset(a,-0x3f,sizeof(a));}
    node operator * (node b){
        node ret;
        for(int i=0;i<=tot;++i){
            for(int j=0;j<=tot;++j){
                for(int k=0;k<=tot;++k){
                    ret.a[i][j]=max(ret.a[i][j],a[i][k]+b.a[k][j]);
                }
            }
        }
        return ret;
    }
};
node fastpow(node a,int k){
    node ret;
    for(int i=0;i<=tot;++i){
        ret.a[i][i]=0;
    }
    while(k){
        if(k&1)ret=ret*a;
        a=a*a;
        k>>=1;
    }
    return ret;
}
int ch[210][26];
int val[6010];
char s[210];
int rt;
void insert(int w){
    int len=strlen(s+1);
    int now=rt;
    for(int i=1;i<=len;++i){
        int son=s[i]-'a';
        if(!ch[now][son]){
            ch[now][son]=++tot;
        }
        now=ch[now][son];
    }
    val[now]+=w;
}
int fail[6010];
void getfail(){
    queue<int> q;
    for(int i=0;i<26;++i){
        if(ch[rt][i])q.push(ch[rt][i]);
    }
    while(!q.empty()){
        int u=q.front();
        q.pop();
        val[u]+=val[fail[u]];
        for(int i=0;i<26;++i){
            if(ch[u][i]){
                fail[ch[u][i]]=ch[fail[u]][i];
                q.push(ch[u][i]);
            }
            else ch[u][i]=ch[fail[u]][i];
        }
    }
}
node start;
void init(){
    for(int i=0;i<=tot;++i){
        for(int j=0;j<26;++j){
            int x=ch[i][j];
            start.a[i][x]=val[x];
        }
    }
}
int aaaa[210];
signed main(){
    int n,m;
    scanf("%I64d%I64d",&m,&n);
    for(int i=1;i<=m;++i){
        scanf("%I64d",&aaaa[i]);
    }
    for(int i=1;i<=m;++i){
        memset(s,0,sizeof(s));
        scanf("%s",s+1);
        insert(aaaa[i]);
    }
    getfail();
    init();
    start=fastpow(start,n);
    int final=0;
    for(int i=1;i<=tot;++i){
        final=max(final,start.a[0][i]);
    }
    printf("%I64d\n",final);
}

(CF is a question had I64d)

Guess you like

Origin www.cnblogs.com/youddjxd/p/11772189.html