HDU 2296-Ring(AC自动机+DP)

Ring

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


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
 

Source

题意:给你m个字符串,每个字符串有一个权值,让你造一个长度不超过n的字符串,使得这个字符串价值最大,假如价值相同输出字典序最小的字符串(可以使空串)。

题解:dp[i][j]:表示匹配到自动机的i节点的前j位的最大价值。具体转移见代码。因为要输出使得价值最大的的字符串,故需要开一个path数组记录当前状态的字符串,这里采用string类,当然也可以用char字符串,string方便些而已。。。

#include<map>        
#include<stack>        
#include<queue>        
#include<vector>  
#include<string>
#include<math.h>        
#include<stdio.h>        
#include<iostream>        
#include<string.h>        
#include<stdlib.h>        
#include<algorithm>        
using namespace std;        
typedef long long ll;        
#define inf 1000000000     
#define mod 1000000007         
#define  maxn  2005     
#define  lowbit(x) (x&-x)        
#define  eps 1e-10   
int pre[maxn],a[maxn][60],flag[maxn],size,n,m,k,val[120],dp[maxn][60];
char s[105][15];
string path[maxn][60];
queue<int>q;
string change(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 insert(int num,int w)  
{  
    int i,len=strlen(s[num]),now=0,cnt=0;  
    for(i=0;i<len;i++)  
    {  
        int v=s[num][i]-'a';  
        if(!a[now][v])  
        {  
            flag[size]=0;  
            memset(a[size],0,sizeof(a[size]));  
            a[now][v]=size++;  
        }  
        now=a[now][v];  
    }  
    flag[now]=w;  
}  
void build_fail()  
{  
    int now,i;  
    for(i=0;i<26;i++)  
    {  
        int tmp=a[0][i];  
        if(tmp)  
            pre[tmp]=0,q.push(tmp);  
    }  
    while(q.empty()==0)  
    {  
        now=q.front();  
        q.pop();  
        for(i=0;i<26;i++)  
        {  
            if(a[now][i]==0)  
            {  
                a[now][i]=a[pre[now]][i];  
                continue;  
            }  
			int v=pre[now];
			while(v && a[v][i]==0)
				v=pre[v];
            pre[a[now][i]]=a[v][i];  
            q.push(a[now][i]);  
			flag[a[now][i]]+=flag[pre[now]];
        }     
    }  
}
void work()
{
	int i,j,k,ans=0;
	string res="";
	for(i=0;i<size;i++)
		for(j=0;j<=n;j++)
			dp[i][j]=-1,path[i][j]="";
	dp[0][0]=0;
	for(i=0;i<n;i++)
		for(j=0;j<size;j++)
		{
			if(dp[j][i]==-1)
				continue;
			for(k=0;k<26;k++)
			{
				int tmp=a[j][k];
				if(dp[tmp][i+1]<=dp[j][i]+flag[tmp])
				{
					char c='a'+k;
					if(dp[tmp][i+1]<dp[j][i]+flag[tmp])
					{
						dp[tmp][i+1]=dp[j][i]+flag[tmp];
						path[tmp][i+1]=path[j][i]+c;
					}
					else
						path[tmp][i+1]=change(path[tmp][i+1],path[j][i]+c);
					if(ans<dp[tmp][i+1])
					{
						ans=dp[tmp][i+1];
						res=path[tmp][i+1];
					}
					else if(ans==dp[tmp][i+1])
						res=change(res,path[tmp][i+1]);
				}
			}
		}
	 if(ans==0)
		 printf("\n");
	 else
		 cout << res << endl;
}
int  main(void)  
{  
    int i,T;  
	scanf("%d",&T);
    while(T--)  
    {  
		scanf("%d%d",&n,&m);
        size=1;flag[0]=0;  
        memset(pre,0,sizeof(pre));  
        memset(a[0],0,sizeof(a[0]));  
		for(i=1;i<=m;i++)
			scanf("%s",s[i]);
		for(i=1;i<=m;i++)
		{
			scanf("%d",&val[i]);
			insert(i,val[i]);
		}
        build_fail();
		work();
	} 
    return 0;  
}  


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值