hdu2296 Ring (AC自动机+dp)

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3056    Accepted Submission(s): 977


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
 
题意:给你m个模板串,每个模板串都有自己的价值,让你构造长度为n的字符串,这个字符串中每有一个模板串,整个字符串的总价值就加上那个模板串的价值,一种模板可以重复加,问能构造出来的最大的价值。
思路:构造trie图,然后用dp[i][j]表示长度为i,当前节点为j的最大价值,然后再用char s[i][j]表示当dp[i][j]最大时所对应的字符串。

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
typedef long long ll;
#define inf 99999999
#define pi acos(-1.0)
#define maxnode 1200
int dp[55][maxnode];
char s1[55][55];
char s[55][maxnode][55];

struct trie{
    int sz,root,val[maxnode],next[maxnode][30],fail[maxnode];
    int q[1111111];
    void init(){
        int i;
        sz=root=0;
        val[0]=0;
        for(i=0;i<26;i++){
            next[root][i]=-1;
        }
    }
    int idx(char c){
        return c-'a';
    }
    void charu(char *s,int value){
        int i,j,u=0;
        int len=strlen(s);
        for(i=0;i<len;i++){
            int c=idx(s[i]);
            if(next[u][c]==-1){
                sz++;
                val[sz]=0;
                next[u][c]=sz;
                u=next[u][c];
                for(j=0;j<26;j++){
                    next[u][j]=-1;
                }
            }
            else{
                u=next[u][c];
            }
        }
        val[u]=value;  //这里要注意
    }
    void build(){
        int i,j;
        int front,rear;
        front=1;rear=0;
        for(i=0;i<26;i++){
            if(next[root][i]==-1 ){
                next[root][i]=root;
            }
            else{
                fail[next[root][i] ]=root;
                rear++;
                q[rear]=next[root][i];
            }
        }
        while(front<=rear){
            int x=q[front];
            val[x]+=val[fail[x] ]; //!!这里是关键点
            front++;
            for(i=0;i<26;i++){
                if(next[x][i]==-1){
                    next[x][i]=next[fail[x] ][i];

                }
                else{
                    fail[next[x][i] ]=next[fail[x] ][i];
                    rear++;
                    q[rear]=next[x][i];
                }

            }
        }
    }
    void solve(int n){
        int i,j,t,sum,len;
        char str[60];
        dp[0][0]=0;
        for(i=0;i<n;i++){
            for(j=0;j<=sz;j++){
                if(dp[i][j]==-1)continue;
                for(t=0;t<26;t++){
                    sum=dp[i][j]+val[next[j][t] ];
                    if(sum>dp[i+1][next[j][t] ] ){
                        dp[i+1][next[j][t] ]=sum;
                        strcpy(s[i+1][next[j][t] ],s[i][j] );
                        len=strlen(s[i][j]);
                        s[i+1][next[j][t] ][len]='a'+t;
                        len++;
                        s[i+1][next[j][t] ][len]='\0';
                    }
                    else if(sum==dp[i+1][next[j][t] ] ){
                        strcpy(str,s[i][j]  );
                        len=strlen(str);
                        str[len]='a'+t;
                        len++;
                        str[len]='\0';
                        if(strcmp(str,s[i+1][next[j][t] ])<0  ){
                            strcpy(s[i+1][next[j][t] ],str );
                        }
                    }
                }
            }
        }
    }
}ac;

int main()
{
    int n,m,i,j,T,k,c;
    scanf("%d",&T);
    while(T--)
    {
        ac.init();
        scanf("%d%d",&n,&m);
        for(i=1;i<=m;i++){
            scanf("%s",s1[i]);
        }
        for(i=1;i<=m;i++){
            scanf("%d",&c);
            ac.charu(s1[i],c);
        }
        ac.build();
        for(i=0;i<=n;i++){
            for(j=0;j<=ac.sz;j++){
                s[i][j][0]='\0';
                dp[i][j]=-1;
            }
        }
        ac.solve(n);
        int maxx=0;
        int ii=0,jj=0,len1,len2;
        for(i=1;i<=n;i++){
            for(j=0;j<=ac.sz;j++){
                if(dp[i][j]>maxx){
                    maxx=dp[i][j];
                    ii=i;jj=j;
                }
                else if(dp[i][j]==maxx){
                    len1=strlen(s[i][j]);
                    len2=strlen(s[ii][jj]);
                    if(len1>len2)continue;
                    else if(len1<len2){
                        ii=i;jj=j;
                    }
                    else if(len1==len2){
                        if(strcmp(s[i][j],s[ii][jj] )<0 ){
                         ii=i;jj=j;
                       }
                    }
                }
            }
        }
        if(maxx==0)printf("\n");
        else{
            printf("%s\n",s[ii][jj]);
        }
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值