HDU2296 Ring

Ring

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



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的字符串,并且价值最大,然后输出这个字符串。。。
分析:先建立一个AC自动机,插入的时候把每个单词的结尾附上一个价值,然后建立自动机的时候这个节点指向的fail节点上的值要加到这个节点来,因为fail指针是从根节点出发的最长后缀,建好之后,进行一个dp就可以了,i代表长度,j代表节点,不过记录字符串很麻烦。。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<string>
#include<iostream>
using namespace std;
const int MAXN=1010;
string path[MAXN<<1][MAXN];
int dp[MAXN<<1][MAXN];
char s[1100][26];
struct node
{
    int fail;
    int next[26];
    int cost;
    void init()
    {
        fail=0;
        memset(next,0,sizeof(next));
        cost=0;
    }
}tree[MAXN];
int size;
void insert(char *str,int val)
{
    int i=0,index;
    int p=0;
    while(str[i])
    {
        index=str[i]-'a';
        if(tree[p].next[index]==0)
        {
            tree[++size].init();
            tree[p].next[index]=size;
        }
        p=tree[p].next[index];
        i++;
    }
    tree[p].cost+=val;
}
void build_ac_automation()
{
    int i;
    queue<int> q;
    q.push(0);
    while(!q.empty())
    {
        int temp=q.front();
        q.pop();
        for(i=0;i<26;i++)
        {
            if(tree[temp].next[i]!=0)
            {
                int p=tree[temp].next[i];
                if(temp!=0)
                    tree[p].fail=tree[tree[temp].fail].next[i];
                tree[p].cost+=tree[tree[p].fail].cost;
                q.push(p);
            }
            else
                tree[temp].next[i]=tree[tree[temp].fail].next[i];
        }
    }
}
void solve(int len)
{
    string temp;
    int i,j,k,p,ans;
    for (i=0;i<=len;i++)
    {
        for(j=0;j<=size;j++)
            path[i][j].clear();
    }
    memset(dp,-1,sizeof(dp));
    dp[0][0]=ans= 0;
    for(i=1;i<=len;i++)
    {   for(j=0;j<=size;j++)
        {
            if(dp[i-1][j]==-1)
                continue;
            for(k=0;k<26;k++)
            {
                p = tree[j].next[k];
                if (dp[i][p] < dp[i - 1][j] + tree[p].cost)
                {
                    dp[i][p] = dp[i - 1][j] + tree[p].cost;
                    temp.clear();
                    temp.push_back('a' + k);
                    path[i][p] = path[i - 1][j] + temp; //长度为i,节点为p的状态,j代表是前一个单词节点
                }
                else if(dp[i][p]==dp[i-1][j]+tree[p].cost)
                {
                    temp.clear();
                    temp.push_back('a' + k);
                    temp = path[i - 1][j] + temp;
                    if (temp < path[i][p])
                        path[i][p] = temp;
                }
                ans=max(ans, dp[i][p]);
            }
        }
    }
    if(ans==0)
        putchar('\n');
    else
    {
        for(i=1;i<=len;i++)
        {
            for(j=0;j<=size;j++)
            {
                if(dp[i][j]==ans)
                {
                    temp=path[i][j];
                    break;
                }
            }
            if (j <= size)
                break;
        }
        for(j=0;j<=size;j++)
        {
            if(dp[i][j]==ans&&path[i][j]<temp)
                temp=path[i][j];
        }
        cout<<temp<<endl;
    }
}
int main()
{
    int t,n,m,i,x;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        tree[0].init();
        size=0;
        for(i=1;i<=m;i++)
            scanf("%s",s[i]);
        for(i=1;i<=m;i++)
        {
            scanf("%d",&x);
            insert(s[i],x);
        }
        build_ac_automation();
        solve(n);
    }
    return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值