您的位置:首页 > 其它

hdu 2296 Ring(AC自动机+DP)

2016-08-06 16:31 323 查看


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和M,表示要求一个长度不大于N的字符串,现在有M个得分串,给定M个得分串,以及每个串的得分值。要求分值尽量大,字符串尽量小,字典序尽量小。

思路:

        AC自动机+DP。设dp[i][j]=x表示串长度为j时,tire树上的节点为i时,得分的最大值,则dp[u][i+1]=max(dp[u][i+1],dp[j][i]+t),其中u=ch[j][k],t='a'+k。

#include <iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<queue>
#include<algorithm>
using namespace std;
const int kind=26;
const int maxn=1050;
int N,M;
string cmp(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;
}
struct node
{
int ch[maxn][kind],sz;
int val[maxn],fail[maxn],last[maxn];
int dp[maxn][60],ans;
string path[maxn][60],res;
void init()
{
val[0]=0,sz=1;
memset(ch[0],0,sizeof(ch[0]));
}
void insert(char *s,int v)
{
int len=strlen(s),u=0;
for(int i=0; i<len; i++)
{
int id=s[i]-'a';
if(!ch[u][id])
{
memset(ch[sz],0,sizeof(ch[sz]));
val[sz]=0;
ch[u][id]=sz++;
}
u=ch[u][id];
}
val[u]=v;
}
void getFail()
{
queue<int>q;
fail[0]=0;
for(int i=0; i<kind; i++)
{
int u=ch[0][i];
if(u)
{
fail[u]=last[u]=0;
q.push(u);
}
}
while(!q.empty())
{
int r=q.front();
q.pop();
for(int i=0; i<kind; i++)
{
int u=ch[r][i];
if(!u)
{
ch[r][i]=ch[fail[r]][i];
continue;
}
q.push(u);
int v=fail[r];
while(v&&!ch[v][i]) v=fail[v];
fail[u]=ch[v][i];
val[u]+=val[fail[u]];
last[u]=val[fail[u]]?fail[u]:last[fail[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 i=0; i<N; i++)
{
for(int j=0; j<sz; j++)
{
if(dp[j][i]==-1) continue;
for(int k=0; k<kind; k++)
{
int u=ch[j][k]; char t='a'+k;
if(dp[u][i+1]<dp[j][i]+val[u])
{
dp[u][i+1]=dp[j][i]+val[u];
path[u][i+1]=path[j][i]+t;
}
if(dp[u][i+1]==dp[j][i]+val[u])
path[u][i+1]=cmp(path[u][i+1],path[j][i]+t);
if(ans<dp[u][i+1])
{
ans=dp[u][i+1];
res=path[u][i+1];
}
else if(ans==dp[u][i+1])
res=cmp(res,path[u][i+1]);
}
}
}
}
};
node ac;
char s[105][15];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
ac.init();
scanf("%d%d",&N,&M);
for(int i=0; i<M; i++)
scanf("%s",s[i]);
int x;
for(int i=0; i<M; i++)
{
scanf("%d",&x);
ac.insert(s[i],x);
}
ac.getFail();
ac.solve();
// printf("%d\n",ac.ans);
if(ac.ans==0)
printf("\n");
else cout<<ac.res<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: