HDU2296(AC自动机+DP)
2017-10-12 20:49
447 查看
Ring
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
T ≤ 15
0 < N ≤ 50, 0 < M ≤ 100.
The length of each word is less than 11 and bigger than 0.
1 ≤ Hi ≤ 100.
All the words in the input are different.
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
27 2
love
ever
5 5
5 1
ab
5
Sample Output
loveverabab
Hint
Sample 1: weight(love) = 5, weight(ever) = 5, so weight(lovever) = 5 + 5 = 10Sample 2: weight(ab) = 2 * 5 = 10, so weight(abab) = 10
题目大意:
构造一个长度不超过n的字符串,使得∑i=1mhi∗si在字符串中出现的次数最大如果有多个串都满足,要求长度最小,如果还有长度相同的,要求字典序最小
solution:
有多个模式串,而且长度都不是太大,很容易想到构建AC自动机。然后做法就很显然了——在trie图上跑DPf[i][j]表示长度为i,在j号节点上的最大价值
pre[i][j]表示f[i][j]的最优转移的来源
转移非常简单,没什么好说的,但要求出字符串,就要在转移的时候注意一下,细节有点多
code:
#include<iostream> #include<cstdio> #include<cmath> #include<algorithm> #include<cstdlib> #include<cstring> using namespace std; int sl,best,T,Max,n,len,son[70000][26],w[70000],num,hz[70000],que[70000],head,tail,f[55][70000],val[70000]; struct czy{ int a,b,c; }pre[55][70000],id; char ch[150],ans[1000],now[1000]; bool ok(int a,int b){ id=pre[a][b]; while(1){ now[id.a]=id.c+'a'; if(!id.a) break; id=pre[id.a][id.b]; } for(int i=0;i<sl;i++) if(ans[i]<now[i]) return false; else if(ans[i]>now[i]) return true; return false; } bool check1(int a,int b,int c,char d){ id=pre[a-1][b]; while(1){ ch[id.a]=id.c+'a'; if(!id.a) break; id=pre[id.a][id.b]; } ch[a-1]=d; id=pre[a][c]; while(1){ now[id.a]=id.c+'a'; if(!id.a) break; id=pre[id.a][id.b]; } for(int i=0;i<a;i++) if(now[i]<ch[i]) return false; else if(now[i]>ch[i]) return true; return false; } int main(){ scanf("%d",&T); while(T--){ scanf("%d%d",&Max,&n); num=1; memset(son,0,sizeof(son)); memset(hz,0,sizeof(hz)); memset(f,0,sizeof(f)); memset(w,0,sizeof(w)); for(int i=1;i<=n;i++){ scanf("%s",ch); len=strlen(ch); int x=1; for(int j=0;j<len;j++) if(son[x][ch[j]-'a']) x=son[x][ch[j]-'a']; else x=son[x][ch[j]-'a']=++num; w[x]=i; } for(int i=1;i<=n;i++) scanf("%d",&val[i]); head=1; tail=0; for(int i=0;i<26;i++) if(son[1][i]){ hz[son[1][i]]=1; que[++tail]=son[1][i]; } else son[1][i]=1; while(head<=tail){ int u=que[head++]; for(int i=0;i<26;i++) if(son[u][i]){ hz[son[u][i]]=son[hz[u]][i]; que[++tail]=son[u][i]; } else son[u][i]=son[hz[u]][i]; } for(int i=0;i<=Max;i++) for(int j=1;j<=num;j++){ pre[i][j]=(czy){0,0,0}; f[i][j]=-1; } f[0][1]=0; best=0; sl=0; for(int i=1;i<=Max;i++) for(int j=1;j<=num;j++) if(f[i-1][j]!=-1){ for(int k=0;k<26;k++){ int v=son[j][k]; if((f[i][v]==-1)||(f[i][v]<f[i-1][j]+val[w[v]])||(f[i][v]==f[i-1][j]+val[w[v]])&&check1(i,j,v,k+'a')){ pre[i][v]=(czy){i-1,j,k}; f[i][v]=f[i-1][j]+val[w[v]]; } } } memset(ans,0,sizeof(ans)); for(int i=1;i<=Max;i++) for(int j=1;j<=num;j++){ if(f[i][j]>best||f[i][j]==best&&i==sl&&ok(i,j)){ best=f[i][j]; sl=i; id=pre[i][j]; while(1){ now[id.a]=id.c+'a'; if(!id.a) break; id=pre[id.a][id.b]; } for(int k=0;k<sl;k++) ans[k]=now[k]; } } for(int i=0;i<sl;i++) putchar(ans[i]); puts(""); } return 0; }
相关文章推荐
- hdu2296 Ring (AC自动机+dp)
- hdu2296(AC自动机+DP)
- HDU2296 Ring(AC自动机+DP)
- hdu2296-(AC自动机+DP)
- [hdu2296]Ring(AC自动机+dp)
- HDU3341 Lost's revenge(AC自动机&&dp)
- UVALive 3490 Generator(AC自动机+dp+高斯消元)
- HDU2296——Ring(AC自动机+DP)
- 【bzoj 1030】 [JSOI2007]文本生成器(AC自动机+dp)
- [AC自动机+状压dp] hdu 4534 郑厂长系列故事——新闻净化
- BZOJ 2553: [BeiJing2011]禁忌(AC自动机+期望DP+矩阵快速幂)
- HDU 4758 Walk Through Squares(AC自动机+DP)
- [BZOJ 1030][JSOI2007]文本生成器(AC自动机+DP)
- NOJ1222-English Game(AC自动机+dp)
- HDU 4057 Rescue the Rabbit (AC自动机+DP)
- ZOJ 3494 BCD Code (AC自动机+数位DP)
- HDU - 2457 DNA repair(AC自动机+DP)
- hdu 4758 Walk Through Squares(AC自动机+状态压缩DP)
- ZOJ 3494 BCD Code(AC自动机+数位DP)
- HDU 2825 Wireless Password(AC自动机+状压DP)