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.
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
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.
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
要求构造一个字符串使其包含的给定子串权值最大。
dp维护最大价值在AC自动机里跑就好啦
#include<iostream>
#include<queue>
#include<string>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; const int LO=,NU=;
inline int f(char u){
return u-'a';
}
struct tree{
int f;
int q;
int t[LO];
int v[LO];
}t[NU];
int n,m,p,num,w;
bool us[NU];
char s[][];
queue <int> q;
int dp[][NU];
string ss[][NU],str;
inline int dfs(int x){
if (us[x]) return t[x].q;
us[x]=;
if (!x) return ;
return t[x].q+=dfs(t[x].f);
}
inline void in(int x){
int p=,l,m=strlen(s[x]);
for (register int i=;i<m;i++){
l=f(s[x][i]);
if (!t[p].t[l]) t[p].t[l]=++num;
p=t[p].t[l];
}
t[p].q=w;
}
inline void mafa(){
register int i;int k,p;
q.push();t[].f=;
while(!q.empty()){
k=q.front();q.pop();
for (i=;i<LO;i++)
if (t[k].t[i]){
p=t[k].f;
while((!t[p].t[i])&&p) p=t[p].f;
t[t[k].t[i]].f=(k==p)?:t[p].t[i];
q.push(t[k].t[i]);
}
}
}
int main(){
int tt;
scanf("%d",&tt);
register int i,j,k,l;int u;int ans;
while(tt--){
scanf("%d%d",&n,&m);
num=u=ans=;
for (i=;i<m;i++) scanf("%s",s[i]);
for (i=;i<m;i++){
scanf("%d",&w);
in(i);
}
mafa();
for (i=;i<=num;i++) us[i]=;
for (i=;i<=num;i++) if (!us[i]) dfs(i);
for (i=;i<=num;i++)
for (j=;j<LO;j++){
if (!t[i].t[j]){
u=t[i].f;
while(!t[u].t[j]&&u) u=t[u].f;
u=t[u].t[j];
}else u=t[i].t[j];
t[i].v[j]=u;
}
for (i=;i<=n;i++)
for (j=;j<=num;j++) dp[i][j]=-;
dp[][]=;ss[][]="";
for (i=;i<n;i++)
for (j=;j<=num;j++)
if (dp[i][j]!=-)
for (l=;l<LO;l++)
if ((dp[i+][t[j].v[l]]<t[t[j].v[l]].q+dp[i][j])||(dp[i+][t[j].v[l]]==t[t[j].v[l]].q+dp[i][j]&&ss[i+][t[j].v[l]]>ss[i][j]+(char)(l+'a')))
dp[i+][t[j].v[l]]=dp[i][j]+t[t[j].v[l]].q,ss[i+][t[j].v[l]]=ss[i][j]+(char)(l+'a');
ans=;str="";
for (i=;i<=n;i++)
for (j=;j<=num;j++) if (dp[i][j]>ans||(dp[i][j]==ans&&(str.size()>ss[i][j].size()||(str.size()==ss[i][j].size()&&str>ss[i][j])))) ans=dp[i][j],str=ss[i][j];
cout<<str<<endl;
for (i=;i<=num;i++)
for (j=;j<LO;j++) t[i].t[j]=t[i].v[j]=;
for (i=;i<=num;i++) t[i].q=t[i].f=;
}
}