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. 解决在SecurecCRT登录后,发现方向键、backspace(退格键)、delete(删除键)为乱码的问题

    问题:使用securecrt ssh到linux之后,backspace(退格键),delete(删除键),以及4个方向键都为乱码,不能正常使用.按tab键也没有自动补全文件名. 即: 按Backsp ...

  2. flume1.8 基础架构介绍(一)

    1. 系统要求 1. Java运行环境 -- Java 1.8及以上 2. 内存 -- 足够的内存供配置的sources,channels 或者sinks使用 3. 硬盘空间 -- 足够的硬盘空间供配 ...

  3. Spider_Man_4 の BeautifulSoup

    一 介绍 Beautiful Soup 是一个可以从HTML或XML文件中提取数据的Python库.它能够通过你喜欢的转换器实现惯用的文档导航,查找,修改文档的方式.Beautiful Soup会帮你 ...

  4. GO开发[五]:golang结构体struct

    Go结构体struct Go语言的结构体(struct)和其他语言的类(class)有同等的地位,但Go语言放弃了包括继承在内的大量面向对象特性,只保留了组合(composition)这个最基础的特性 ...

  5. 在js中怎样获得checkbox里选中的多个值?

    https://zhidao.baidu.com/question/203897221.html 思路:利用name属性值获取checkbox对象,然后循环判断checked属性(true表示被选中, ...

  6. DEDECMS 留言薄模块的使用方法

    一.留言薄的安装 留言薄的安装过程和其他插件一样,首先我们进入后台模块管理列表,点击其对应的"安装": 以上步骤,我们完成了留言薄插件的安装. 二.留言薄的卸载 留言薄的卸载,同样 ...

  7. NSDateFormatter相关整理

    //实例化一个NSDateFormatter对象NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init]; //设定时间格式,这里可 ...

  8. 深入浅出docker

    笔者在海外工作多年,所以文中多用英文单词,有些时候是为了更精准的描述,请见谅.希望这篇随笔能帮大家入门docker.由于在海外连博客园有些慢,所以我图片用的比较少,以后再考虑一下如何更好的解决图片上传 ...

  9. scrapy_图片下载

    需要安装第三方库: 安装 pillow库 pip install -i https://pypi.doubanio.com/simple pillow 如何对图片进行自动下载? 首先明白,图片去哪下? ...

  10. Java进阶篇(六)——Swing程序设计(下)

    三.布局管理器 Swing中,每个组件在容器中都有一个具体的位置和大小,在容器中摆放各自组件时很难判断其具体位置和大小,这里我们就要引入布局管理器了,它提供了基本的布局功能,可以有效的处理整个窗体的布 ...