HDU 2296 Ring [AC自动机 DP 打印方案]

时间:2021-03-13 02:59:42

Ring

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

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.


题意:

给出m个模式串,每个串有一定的分值,构造一个长度不超过n的串,使得分值最大,并输出该字符串
在分数同样大时,输出长度最小的
长度一样时输出字典序最小的


HDU2296是一道有Bug的题目...............题目本身有表述不清楚导致有歧义........没有说明字符串后缀重合应该怎么处理.........所以说有两种程序可以通过本题,一种是Trie树正着插入、不管后缀重合、然后在AC自动机上DP时记录整个字符串,还有一种是Trie树倒着插入、后缀重合时权值都算上、DP时只记录转移来的状态...................然后本题字典序判断用Tire树的DFS序绝对不对,这个DFS序不能表达出整个生成的字符串的字典序............并且我一开始求DFS序是在建完AC自动机后求的,我的AC自动机是用了Trie图优化的,所以我就眼睁睁的看着为什么Trie树上的dfs一直停不下来............................................不要问我为什么花了一晚上

冷静的说做法:套路DP,f[i][j]生成到i AC自动机上走到j 的最大权值,记录pa[i][j]转移来的状态 字典序最小所以倒着插入 遇到相同暴力往前比较就行了

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N=,M=,INF=1e9;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
}
int n,m,w[N];
char s[];
struct node{
int ch[],val,fail,w;
char c;
}t[N];
int sz;
void ins(char s[],int id){
int u=,n=strlen(s+);
reverse(s+,s++n);
for(int i=;i<=n;i++){
int c=s[i]-'a';
if(!t[u].ch[c]) t[u].ch[c]=++sz;
u=t[u].ch[c];
t[u].c=s[i];
}
t[u].val=id;
}
int q[N],head,tail;
void getAC(){
head=tail=;
for(int i=;i<;i++) if(t[].ch[i]) q[tail++]=t[].ch[i];
while(head!=tail){
int u=q[head++];
t[u].w=w[t[u].val];
t[u].w+=t[t[u].fail].w;
//val ???
for(int i=;i<;i++){
int &v=t[u].ch[i];
if(!v) v=t[t[u].fail].ch[i];
else{
t[v].fail=t[t[u].fail].ch[i];
q[tail++]=v;
}
}
}
}
//int dfn[N],dfc;
//void dfs(int u){
// dfn[u]=++dfc; //printf("dfs %d %d %c\n",u,dfn[u],t[u].c);
// for(int i=0;i<26;i++) if(t[u].ch[i]) dfs(t[u].ch[i]);
//}
int f[][N],pa[][N];
bool cmp(int a,int b,int i){//a<b
//printf("cmp %d %d %d\n",a,b,i);
while(t[a].c==t[b].c) a=pa[i][a],b=pa[i][b],i--;//,printf("cmp %d %d %d\n",a,b,i);
return t[a].c<t[b].c;
}
void lalala(int i,int j){
if(i==) return;
putchar(t[j].c);
lalala(i-,pa[i][j]);
}
void dp(){
memset(f,-,sizeof(f));
f[][]=;
for(int i=;i<n;i++)
for(int j=;j<=sz;j++) if(f[i][j]!=-){
// printf("use %d %d %d %d\n",i,j,f[i][j],pa[i][j]);
for(int k=;k<;k++){
int p=t[j].ch[k];
int _=f[i][j]+t[p].w;//printf("k %d %d %d %d\n",k,p,_,f[i+1][p]);
if(_>f[i+][p]) f[i+][p]=_,pa[i+][p]=j;
else if(_==f[i+][p]&&j!=pa[i+][p]&&cmp(j,pa[i+][p],i)) pa[i+][p]=j;
}
}
int ans=-,ai=,aj=;
for(int i=;i<=n;i++) for(int j=;j<=sz;j++){//printf("f %d %d %d %d\n",i,j,f[i][j],pa[i][j]);
if(f[i][j]>ans) ans=f[i][j],ai=i,aj=j;
else if(f[i][j]==ans&&i==ai&&cmp(j,aj,i)) ans=f[i][j],aj=j;//,printf("dfn %d %d %d\n",i,j,f[i][j]);
}
//printf("ans %d %d %d\n",ans,ai,aj);
if(ans!=)
lalala(ai,aj);
puts("");
}
int main(){
freopen("in","r",stdin);
int T=read();
while(T--){
memset(t,,sizeof(t));sz=;
n=read();m=read();
for(int i=;i<=m;i++) scanf("%s",s+),ins(s,i);
for(int i=;i<=m;i++) w[i]=read();
getAC(); //for(int i=1;i<=sz;i++) printf("check %d %d %c\n",i,t[i].val,t[i].c);
dp();
}
}