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]转移来的状态 字典序最小所以倒着插入 遇到相同暴力往前比较就行了

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <algorithm>
  5. using namespace std;
  6. const int N=,M=,INF=1e9;
  7. inline int read(){
  8. char c=getchar();int x=,f=;
  9. while(c<''||c>''){if(c=='-')f=-;c=getchar();}
  10. while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
  11. return x*f;
  12. }
  13. int n,m,w[N];
  14. char s[];
  15. struct node{
  16. int ch[],val,fail,w;
  17. char c;
  18. }t[N];
  19. int sz;
  20. void ins(char s[],int id){
  21. int u=,n=strlen(s+);
  22. reverse(s+,s++n);
  23. for(int i=;i<=n;i++){
  24. int c=s[i]-'a';
  25. if(!t[u].ch[c]) t[u].ch[c]=++sz;
  26. u=t[u].ch[c];
  27. t[u].c=s[i];
  28. }
  29. t[u].val=id;
  30. }
  31. int q[N],head,tail;
  32. void getAC(){
  33. head=tail=;
  34. for(int i=;i<;i++) if(t[].ch[i]) q[tail++]=t[].ch[i];
  35. while(head!=tail){
  36. int u=q[head++];
  37. t[u].w=w[t[u].val];
  38. t[u].w+=t[t[u].fail].w;
  39. //val ???
  40. for(int i=;i<;i++){
  41. int &v=t[u].ch[i];
  42. if(!v) v=t[t[u].fail].ch[i];
  43. else{
  44. t[v].fail=t[t[u].fail].ch[i];
  45. q[tail++]=v;
  46. }
  47. }
  48. }
  49. }
  50. //int dfn[N],dfc;
  51. //void dfs(int u){
  52. // dfn[u]=++dfc; //printf("dfs %d %d %c\n",u,dfn[u],t[u].c);
  53. // for(int i=0;i<26;i++) if(t[u].ch[i]) dfs(t[u].ch[i]);
  54. //}
  55. int f[][N],pa[][N];
  56. bool cmp(int a,int b,int i){//a<b
  57. //printf("cmp %d %d %d\n",a,b,i);
  58. 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);
  59. return t[a].c<t[b].c;
  60. }
  61. void lalala(int i,int j){
  62. if(i==) return;
  63. putchar(t[j].c);
  64. lalala(i-,pa[i][j]);
  65. }
  66. void dp(){
  67. memset(f,-,sizeof(f));
  68. f[][]=;
  69. for(int i=;i<n;i++)
  70. for(int j=;j<=sz;j++) if(f[i][j]!=-){
  71. // printf("use %d %d %d %d\n",i,j,f[i][j],pa[i][j]);
  72. for(int k=;k<;k++){
  73. int p=t[j].ch[k];
  74. int _=f[i][j]+t[p].w;//printf("k %d %d %d %d\n",k,p,_,f[i+1][p]);
  75. if(_>f[i+][p]) f[i+][p]=_,pa[i+][p]=j;
  76. else if(_==f[i+][p]&&j!=pa[i+][p]&&cmp(j,pa[i+][p],i)) pa[i+][p]=j;
  77. }
  78. }
  79. int ans=-,ai=,aj=;
  80. 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]);
  81. if(f[i][j]>ans) ans=f[i][j],ai=i,aj=j;
  82. 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]);
  83. }
  84. //printf("ans %d %d %d\n",ans,ai,aj);
  85. if(ans!=)
  86. lalala(ai,aj);
  87. puts("");
  88. }
  89. int main(){
  90. freopen("in","r",stdin);
  91. int T=read();
  92. while(T--){
  93. memset(t,,sizeof(t));sz=;
  94. n=read();m=read();
  95. for(int i=;i<=m;i++) scanf("%s",s+),ins(s,i);
  96. for(int i=;i<=m;i++) w[i]=read();
  97. getAC();
  98.  
  99. //for(int i=1;i<=sz;i++) printf("check %d %d %c\n",i,t[i].val,t[i].c);
  100. dp();
  101. }
  102. }

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. 【笔记】nodejs读取JSON,数组转树

    const fs = require('fs'); // --------------- 读取源文件 --------------- const originData = require('./vux ...

  2. 为什么ios手机安装好fiddler证书/charles证书还是抓不到https请求?

    为什么ios手机安装好fiddler证书/charles证书还是抓不到https请求? 最近有不少人有此困惑, 因为你的ios系统应该是10.0以上的系统, 在手机系统设置---关于手机----证书信 ...

  3. 【centos6.5 hadoop2.7 _64位一键安装脚本】有问题加我Q直接问

    #!/bin/bash#@author:feiyuanxing [既然笨到家,就要努力到家]#@date:2017-01-05#@E-Mail:feiyuanxing@gmail.com#@TARGE ...

  4. LNMP状态管理命令

    https://lnmp.org/faq/lnmp-status-manager.html LNMP状态管理命令: LNMP 1.2+状态管理: lnmp {start|stop|reload|res ...

  5. linux下卸载apache方法小结

    方法一 代码如下: 1. root@server ~]# rpm -qa|grep httpd  httpd-2.2.3-11.el5_2.centos.4  httpd-manual-2.2.3-1 ...

  6. 为什么选择.NETCore?

    为什么.NETCore? 学习新的开发框架是一项巨大的投资.您需要学习如何在新框架中编写,构建,测试,部署和维护应用程序.作为开发人员,有许多框架可供选择,很难知道什么是最适合的工作.即使您正在使用. ...

  7. “Project 'MyFunProject' is not a J2SE 5.0 compliant project.”

  8. python_如何使用临时文件

    案例: 某项目中,从传感器中获得采集数据,每收集到1G的数据后做是数据分析,最终只保留数据分析的结果,收集到的数据放在内存中,将会消耗大量内存,我们希望把这些数据放到一个临时的文件中 临时文件不能命名 ...

  9. asp.net core 部署到服务器之后外网访问不了

    部署发现问题 今天在部署.net core的时候,发现访问http://localhost:xxxx可以,但是用外网访问并不行! 开始尝试解决问题 一开始以为是nginx的问题.各种折腾,各种改配置文 ...

  10. JDBC (五)

    1 使用dbutils进行一对多.多对多的开发 1.1 准备 mysql驱动的pom.xml <!-- https://mvnrepository.com/artifact/mysql/mysq ...