HDU2296 Ring(AC自动机+DP)

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

 给你M个单词构成一个词典,每个单词有一个权值(单词出现多次算多个权值),现在要你构造一个不超过长度N的字符串,使得该字符串权值最大。如果出现多个答案,输出最短的,如果依然有多解,输出字典序最小的。

注意这题的AC自动机要用match, match[i]表示i节点的后缀单词权值总和.后缀单词指的是:所有可以做i节点表示的串的后缀的单词.

 令dp[i][j]=x表示当前在i点走过了长j的路所生产的最大权值为x. 再用string  path[i][j]来保存那个具有最大权值的最优字符串即可.不过要注意如果最后算的的最大权值是0,那么要输出空串.因为空串最短.

dp[i][j] = max(dp[k][j-1]+match[i]) 若dp[i][j]更新的时候,path[i][j]也要看看是否需要更新成:

if(path[i][j]!=””)path[i][j] =  better(path[i][j] , path[k][j-1]+”x”)x字符表示从k走到i的那条边的字符是x.

初值path=空串,dp[0][0]=0.

AC代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<stdlib.h>
#include<queue>
#include<map>
#include<iomanip>
#include<math.h>
#include<sstream>
using namespace std;
typedef long long ll;
typedef double ld;
int N,M;
const int maxnode=1000+100;
const int sigma_size=26;
int trip[maxnode][sigma_size];
int match[maxnode];
int f[maxnode];
int sz;
int dp[maxnode][50+10];
string path[maxnode][50+10];
int ans;
string res;
inline string better(string a,string b)
{
    if(a=="")
        return b;
    if(a.size()!=b.size())
        return a.size()<b.size() ? a:b;
    return a<b?a:b;
}
void init()
{
    sz=1;
    memset(trip[0],0,sizeof(trip[0]));
    match[0]=f[0]=0;
}
void add(char *s,int v)
{
    int n=strlen(s),u=0;
    for(int i=0; i<n; i++)
    {
        int id=s[i]-'a';
        if(trip[u][id]==0)
        {
            trip[u][id]=sz;
            memset(trip[sz],0,sizeof(trip[sz]));
            match[sz++]=0;
        }
        u=trip[u][id];
    }
    match[u]=v;
}
void getFail()
{
    queue<int> q;
    for(int i=0; i<sigma_size; i++)
    {
        int u=trip[0][i];
        if(u)
        {
            f[u]=0;
            q.push(u);
        }
    }
    while(!q.empty())
    {
        int r=q.front();
        q.pop();
        for(int i=0; i<sigma_size; i++)
        {
            int u=trip[r][i];
            if(!u)
            {
                trip[r][i]=trip[f[r]][i];
                continue;
            }
            q.push(u);
            int v=f[r];
            while(v && trip[v][i]==0)
                v=f[v];
            f[u]=trip[v][i];
            match[u] += match[f[u]];
        }
    }
}
void solve()
{
    ans=0;
    res="";
    for(int i=0; i<sz; i++)
        for(int j=0; j<=N; j++)
        {
            dp[i][j]=-1;
            path[i][j]="";
        }
    dp[0][0]=0;
    for(int len=0; len<N; len++) //当前走的长度
        for(int k=0; k<sz; k++)
            if(dp[k][len]!=-1)//当前所在的节点
            {
                for(int j=0; j<sigma_size; j++) //下一步走的方向
                    if(dp[trip[k][j]][len+1] <= dp[k][len]+match[trip[k][j]])
                    {
                        char temp='a'+j;
                        if(dp[trip[k][j]][len+1] < dp[k][len]+match[trip[k][j]])
                        {
                            dp[trip[k][j]][len+1] = dp[k][len]+match[trip[k][j]];
                            path[trip[k][j]][len+1] = path[k][len]+temp;
                        }
                        else
                        {
                            path[trip[k][j]][len+1] = better(path[trip[k][j]][len+1],path[k][len]+temp);
                        }

                        if(ans < dp[trip[k][j]][len+1])
                        {
                            ans = dp[trip[k][j]][len+1];
                            res = path[trip[k][j]][len+1];
                        }
                        else if(ans == dp[trip[k][j]][len+1])
                            res = better(res,path[trip[k][j]][len+1]);
                    }
            }
}
char str[100+10][10+5];
int val[100+10];
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        init();
        scanf("%d%d",&N,&M);
        for(int i=0; i<M; i++)
            scanf("%s",str[i]);
        for(int i=0; i<M; i++)
        {
            scanf("%d",&val[i]);
            add(str[i],val[i]);
        }
        getFail();
        solve();
        if(ans==0)
            printf("\n");
        else
            cout<<res<<endl;
    }
    return 0;
}


 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值