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();
}
}

HDU 2296 Ring [AC自动机 DP 打印方案]的更多相关文章

  1. HDU 2296 Ring -----------AC自动机,其实我想说的是怎么快速打印字典序最小的路径

    大冥神的代码,以后能贴的机会估计就更少了....所以本着有就贴的好习惯,= =....直接贴 #include <bits/stdc++.h> using LL = long long ; ...

  2. HDU2296 Ring —— AC自动机 + DP

    题目链接:https://vjudge.net/problem/HDU-2296 Ring Time Limit: 2000/1000 MS (Java/Others)    Memory Limit ...

  3. HDU 2296 Ring ( Trie图 && DP && DP状态记录)

    题意 : 给出 m 个单词,每一个单词有一个权重,如果一个字符串包含了这些单词,那么意味着这个字符串拥有了其权重,问你构成长度为 n 且权重最大的字符串是什么 ( 若有权重相同的,则输出最短且字典序最 ...

  4. HDU-2296 Ring(AC自动机+DP)

    题目大意:给出的m个字符串都有一个权值.用小写字母构造一个长度不超过n的字符串S,如果S包含子串s,则S获取s的权值.输出具有最大权值的最小字符串S. 题目分析:先建立AC自动机.定义状态dp(ste ...

  5. HDU2296 Ring(AC自动机 DP)

    dp[i][j]表示行走i步到达j的最大值,dps[i][j]表示对应的串 状态转移方程如下: dp[i][chi[j][k]] = min(dp[i - 1][j] + sum[chi[j][k]] ...

  6. 对AC自动机+DP题的一些汇总与一丝总结 (2)

    POJ 2778 DNA Sequence (1)题意 : 给出m个病毒串,问你由ATGC构成的长度为 n 且不包含这些病毒串的个数有多少个 关键字眼:不包含,个数,长度 DP[i][j] : 表示长 ...

  7. hdu 2296 aC自动机+dp(得到价值最大的字符串)

    Ring Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  8. HDU2296 Ring(AC自动机+DP)

    题目是给几个带有价值的单词.而一个字符串的价值是 各单词在它里面出现次数*单词价值 的和,问长度不超过n的最大价值的字符串是什么? 依然是入门的AC自动机+DP题..不一样的是这题要输出具体方案,加个 ...

  9. HDU 2425 DNA repair (AC自动机+DP)

    DNA repair Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

随机推荐

  1. C/C++之循环结构

    C语言中提供四种循环,即goto循环.while循环.do…while循环和for循环.四种循环可以用来处理同一问题,一般情况下它们可以互相代替换,但一般不提倡用goto循环,因为强制改变程序的顺序经 ...

  2. win7 64位安装redis 及Redis Desktop Manager使用(转载的)

    写基于dapper的一套自动化程序,看到 mgravell的另一个项目,StackExchange.Redis,之前在.NET上用过一段时间Redis,不过一直是其它的驱动开发包,这个根据作者介绍,是 ...

  3. [国嵌攻略][070-095][Linux编程函数手册]

    第1类 时间编程类 1.1 获取日历时间 1.1.1 函数名 time 1.1.2 函数原形 time_t time(time_t *t) 1.1.3 函数功能 返回日历时间 1.1.4 所属头文件 ...

  4. 基础二 day4

    昨日回顾int bit_lenth()bool int ----> bool 非零True,0 False bool----> True 1 False 0 str ----> bo ...

  5. TypeScript笔记 5--变量声明(解构和展开)

    解构是什么 解构(destructuring assignment)是一种表达式,将数组或者对象中的数据赋给另一变量. 在开发过程中,我们经常遇到这样问题,需要将对象某个属性的值赋给其它两个变量.代码 ...

  6. parsing XML document from class path resource [config/applicationContext.xml]; nested exception is java.io.FileNotFoundException: class path resource [config/applicationContext.xml] 解决方案

    parsing XML document from class path resource [config/applicationContext.xml]; nested exception is j ...

  7. 算法-java代码实现插入排序

    插入排序  

  8. 如何解决wamp中apache外部IP访问问题

    # # Some examples: #ErrorDocument 500 "The server made a boo boo." #ErrorDocument 404 /mis ...

  9. SQL作业及调度创建

    转自:http://www.cnblogs.com/accumulater/p/6223909.html --定义创建作业 转自http://hi.baidu.com/procedure/blog/i ...

  10. util包就是用来放一些公用方法和数据结构的

    util包就是用来放一些公用方法和数据结构的