题目链接:Click here~~
题意:
给 m 个带权值的串,构造一个长度为 n 的串,使总串的权值和最大,并输出字典序最小的方案。
解题思路:
令 dp[i][j] 表示长度为 i ,位于自动机节点 j 所能构成的最大权值。
状态和写法和上道题目类似,然后暴力记录方案。
#include <queue>
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define CLR(a,v) memset(a,v,sizeof(a))
namespace Trie
{
const int N = 1111;
const int Size = 26;
int top;
struct Node{
Node *next[Size], *f;
int ended_val;
}node[N], *root;
inline Node* new_node()
{
node[top].ended_val = 0;
CLR(node[top].next,false);
return &node[top++];
}
void init()
{
top = 0;
root = new_node();
}
void insert(char *s,int val)
{
Node *u = root;
for(int i=0;s[i];i++)
{
int id = s[i] - 'a';
if(u->next[id] == NULL)
u->next[id] = new_node();
u = u->next[id];
}
u->ended_val = val;
}
}
namespace ACam
{
using namespace Trie;
void get_fail()
{
queue<Node*> Q;
for(int i=0;i<Size;i++)
{
Node *&ch = root->next[i];
if(!ch)
ch = root;
else
{
ch->f = root;
Q.push(ch);
}
}
while(!Q.empty())
{
Node *cur = Q.front();Q.pop();
for(int i=0;i<Size;i++)
{
Node *&ch = cur->next[i];
if(!ch)
ch = cur->f->next[i];
else
{
ch->f = cur->f->next[i];
ch->ended_val += ch->f->ended_val;
Q.push(ch);
}
}
}
}
int dp[52][1111];
char sol[52][1111][55],tmp[55],add[2];
bool less(char *a,char *b)
{
int la = strlen(a);
int lb = strlen(b);
return la < lb || la == lb && strcmp(a,b) < 0;
}
void solve(int n)
{
memset(dp,-1,sizeof(dp));
dp[0][0] = 0;
sol[0][0][0] = add[1] = '\0';
for(int i=0;i<n;i++)
{
for(int j=0;j<top;j++)
{
if(dp[i][j] == -1)
continue;
for(int k=0;k<Size;k++)
{
add[0] = 'a' + k;
strcpy(tmp,sol[i][j]);
strcat(tmp,add);
int jj = node[j].next[k] - node;
int val = dp[i][j] + node[jj].ended_val;
if(dp[i+1][jj] < val || dp[i+1][jj] == val && less(tmp,sol[i+1][jj]))
{
dp[i+1][jj] = val;
strcpy(sol[i+1][jj],tmp);
}
}
}
}
int mmax = 0;
char ans[52];
ans[0] = '\0';
for(int i=1;i<=n;i++)
for(int j=0;j<top;j++)
{
if(dp[i][j] > mmax || dp[i][j] == mmax && less(sol[i][j],ans))
{
mmax = dp[i][j];
strcpy(ans,sol[i][j]);
}
}
puts(ans);
}
}
const int N = 52;
char str[102][13];
int main()
{
//freopen("out.ads","w",stdout);
int T,n,m;
scanf("%d",&T);
while(T--)
{
ACam::init();
scanf("%d%d",&n,&m);
for(int i=0;i<m;i++)
scanf("%s",str[i]);
for(int i=0;i<m;i++)
{
int val;
scanf("%d",&val);
ACam::insert(str[i],val);
}
ACam::get_fail();
ACam::solve(n);
}
return 0;
}