HDU 2296 Ring AC自动机上的DP

传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2296

Ring

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


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

题意:让你构造一个长度不超过n的串,再给出m个单词(各不相同)。每个单词有一个价值,这个串的价值是每一个单词出现的次数乘上这个单词的价值。求价值最大的串。优先长度最短,次优先字典序最小。没有答案输出空串。

思路:简单的AC自动机上的DP。用dp[i][j]表示当前串长为i,在自动机的节点j上的获得的最大价值,再用string path[i][j]记录当前构造得到的串。DP转移时,枚举下个构造的字母。在自动机上进行跳转。

代码:

#include<cstdio>
#include<cstring>
#define SIGMA_SIZE 26
#define maxn 1200
#include<queue>
#include<string>
#include<iostream>
using namespace std;
int ch[maxn][SIGMA_SIZE];
int val[maxn];
int last[maxn], f[maxn];
int cnt;
inline int idx(char c)
{
	return c - 97;
}
int dp[55][maxn];
string path[55][maxn];
bool cmp(string a,string b)
{
    if(a.size()!=b.size()) return a.size()<b.size();
    else return a<b;
}
void insert(char s[],int v)
{
	int len = strlen(s);
	int u = 0;
	for (int i = 0; i<len; i++)
	{
		int v = idx(s[i]);
		if (!ch[u][v]) ch[u][v] = ++cnt;
		u = ch[u][v];
	}
	val[u] = v;
}
void slove(int m)
{
    memset(dp,-1,sizeof(dp));
    dp[0][0]=0;
    string ans_str="";
    int ans=0;
    for(int i=0;i<55;i++)
    for(int j=0;j<maxn;j++) path[i][j]="";
    for(int i=0;i<=m;i++)
    for(int j=0;j<=cnt;j++)
    {
        if(dp[i][j]==-1) continue;
        for(int k=0;k<26;k++)
        {
            int p=j;
            while(p&&!ch[p][k]) p=f[p];
            p=ch[p][k];
            int d=0;
            if(val[p]) d+=val[p];
            int tmp=last[p];
            while(tmp)
            {
                d+=val[tmp];
                tmp=last[tmp];
            }
            if(dp[i][j]+d>dp[i+1][p])
            {
                dp[i+1][p]=dp[i][j]+d;
                path[i+1][p]=path[i][j]+char(k+97);
            }
            else if(dp[i][j]+d==dp[i+1][p])
            {
                string tmp=path[i][j]+char(k+97);
                if(cmp(tmp,path[i+1][p])) path[i+1][p]=tmp;
            }
            if(dp[i][j]>=ans)
            {
                if(dp[i][j]>ans)
                {
                    ans=dp[i][j];
                    ans_str=path[i][j];
                }
                else
                {
                    if(cmp(path[i][j],ans_str)) ans_str=path[i][j];
                }
            }
        }
    }
    printf("%s\n",ans_str.c_str());
}
void getFail()
{
	queue<int>q;
	f[0] = 0;
	for (int c = 0; c<SIGMA_SIZE; c++)
	{
		int u = ch[0][c];
		if (u)
		{
			f[u] = 0;
			q.push(u);
			last[u] = 0;
		}
	}
	while (!q.empty())
	{
		int r = q.front();
		q.pop();
		for (int c = 0; c<SIGMA_SIZE; c++)
		{
			int u = ch[r][c];
			if (!u)
			{
				//ch[r][c] = ch[f[r]][c];
				continue;
			}
			q.push(u);
			int v = f[r];
			while (v&&!ch[v][c]) v = f[v];
			f[u] = ch[v][c];
			last[u] = val[f[u]] ? f[u] : last[f[u]];
		}
	}
}
char str[105][15];
int main()
{
	int n,m,t;
	scanf("%d",&t);
	while (t--)
	{
	    scanf("%d %d",&m,&n);
		memset(ch, 0, sizeof(ch));
		memset(val, 0, sizeof(val));
		cnt = 0;
		for (int i = 1; i <= n; i++)
		{
			scanf("%s", str[i]);
		}
		for(int i=1;i<=n;i++)
        {
            int v;
            scanf("%d",&v);
            insert(str[i],v);
        }
		getFail();
        slove(m);
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值