HDU2296(AC自动机+DP)

Ring

Problem Description

For the hope of a forever love, Steven is planning to send a ring to Jane with a romantic string engraved on. The string’s length should not exceed N. The careful Steven knows Jane so deeply that he knows her favorite words, such as “love”, “forever”. Also, he knows the value of each word. The higher value a word has the more joy Jane will get when see it.
The weight of a word is defined as its appeared times in the romantic string multiply by its value, while the weight of the romantic string is defined as the sum of all words’ weight. You should output the string making its weight maximal.

Input

The input consists of several test cases. The first line of input consists of an integer T, indicating the number of test cases. Each test case starts with a line consisting of two integers: N, M, indicating the string’s length and the number of Jane’s favorite words. Each of the following M lines consists of a favorite word Si. The last line of each test case consists of M integers, while the i-th number indicates the value of Si.
Technical Specification

  1. T ≤ 15
  2. 0 < N ≤ 50, 0 < M ≤ 100.
  3. The length of each word is less than 11 and bigger than 0.
  4. 1 ≤ Hi ≤ 100.
  5. All the words in the input are different.
  6. All the words just consist of ‘a’ - ‘z’.

Output

For each test case, output the string to engrave on a single line.
If there’s more than one possible answer, first output the shortest one. If there are still multiple solutions, output the smallest in lexicographically order.

The answer may be an empty string.

Sample Input

2
7 2
love
ever
5 5
5 1
ab
5

Sample Output

lovever
abab

Hint

Sample 1: weight(love) = 5, weight(ever) = 5, so weight(lovever) = 5 + 5 = 10
Sample 2: weight(ab) = 2 * 5 = 10, so weight(abab) = 10

题目大意:

构造一个长度不超过n的字符串,使得

i=1mhisi
最大
如果有多个串都满足,要求长度最小,如果还有长度相同的,要求字典序最小

solution:

有多个模式串,而且长度都不是太大,很容易想到构建AC自动机。然后做法就很显然了——在trie图上跑DP

f[i][j]表示长度为i,在j号节点上的最大价值
pre[i][j]表示f[i][j]的最优转移的来源

转移非常简单,没什么好说的,但要求出字符串,就要在转移的时候注意一下,细节有点多

code:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<cstring>
using namespace std;
int sl,best,T,Max,n,len,son[70000][26],w[70000],num,hz[70000],que[70000],head,tail,f[55][70000],val[70000];
struct czy{
    int a,b,c;
}pre[55][70000],id;
char ch[150],ans[1000],now[1000];
bool ok(int a,int b){
    id=pre[a][b];
    while(1){
        now[id.a]=id.c+'a';
        if(!id.a)
            break;
        id=pre[id.a][id.b];
    }
    for(int i=0;i<sl;i++)
        if(ans[i]<now[i])
            return false;
        else
        if(ans[i]>now[i])
            return true;
    return false;
}
bool check1(int a,int b,int c,char d){
    id=pre[a-1][b];
    while(1){
        ch[id.a]=id.c+'a';
        if(!id.a)
            break;
        id=pre[id.a][id.b];
    }
    ch[a-1]=d;
    id=pre[a][c];
    while(1){
        now[id.a]=id.c+'a';
        if(!id.a)
            break;
        id=pre[id.a][id.b];
    }
    for(int i=0;i<a;i++)
        if(now[i]<ch[i])
            return false;
        else
        if(now[i]>ch[i])
            return true;    
    return false;
}
int main(){
    scanf("%d",&T);
    while(T--){
        scanf("%d%d",&Max,&n);
        num=1;
        memset(son,0,sizeof(son));
        memset(hz,0,sizeof(hz));
        memset(f,0,sizeof(f));
        memset(w,0,sizeof(w));
        for(int i=1;i<=n;i++){
            scanf("%s",ch);
            len=strlen(ch);
            int x=1;
            for(int j=0;j<len;j++)
                if(son[x][ch[j]-'a'])
                    x=son[x][ch[j]-'a'];
                else
                    x=son[x][ch[j]-'a']=++num;
            w[x]=i;
        }
        for(int i=1;i<=n;i++)
            scanf("%d",&val[i]);
        head=1;
        tail=0;
        for(int i=0;i<26;i++)
            if(son[1][i]){
                hz[son[1][i]]=1;
                que[++tail]=son[1][i];
            }
            else
                son[1][i]=1;
        while(head<=tail){
            int u=que[head++];
            for(int i=0;i<26;i++)
                if(son[u][i]){
                    hz[son[u][i]]=son[hz[u]][i];
                    que[++tail]=son[u][i];
                }
                else
                    son[u][i]=son[hz[u]][i];
        }
        for(int i=0;i<=Max;i++)
            for(int j=1;j<=num;j++){
                pre[i][j]=(czy){0,0,0};
                f[i][j]=-1;
            }
        f[0][1]=0;
        best=0;
        sl=0;
        for(int i=1;i<=Max;i++)
            for(int j=1;j<=num;j++)
                if(f[i-1][j]!=-1){
                    for(int k=0;k<26;k++){
                        int v=son[j][k];
                        if((f[i][v]==-1)||(f[i][v]<f[i-1][j]+val[w[v]])||(f[i][v]==f[i-1][j]+val[w[v]])&&check1(i,j,v,k+'a')){
                            pre[i][v]=(czy){i-1,j,k};
                            f[i][v]=f[i-1][j]+val[w[v]];
                        }
                    }
                }
        memset(ans,0,sizeof(ans));
        for(int i=1;i<=Max;i++)
            for(int j=1;j<=num;j++){
                if(f[i][j]>best||f[i][j]==best&&i==sl&&ok(i,j)){
                    best=f[i][j];
                    sl=i;
                    id=pre[i][j];
                    while(1){
                        now[id.a]=id.c+'a';
                        if(!id.a)
                            break;
                        id=pre[id.a][id.b];
                    }
                    for(int k=0;k<sl;k++)
                        ans[k]=now[k];
                }
            }
        for(int i=0;i<sl;i++)
            putchar(ans[i]);
        puts("");
    }
    return 0;
}
阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_38601996/article/details/78219635
文章标签: AC自动机 DP
个人分类: AC自动机 DP
上一篇bzoj1009 GT考试(AC自动机+DP+矩阵快速幂)
下一篇51nod1681公共祖先(dfs序+树状数组)
想对作者说点什么? 我来说一句
相关热词

没有更多推荐了,返回首页

关闭
关闭
关闭