链接:

https://vjudge.net/problem/HDU-3341

题意:

Lost and AekdyCoin are friends. They always play "number game"(A boring game based on number theory) together. We all know that AekdyCoin is the man called "nuclear weapon of FZU,descendant of Jingrun", because of his talent in the field of number theory. So Lost had never won the game. He was so ashamed and angry, but he didn't know how to improve his level of number theory.

One noon, when Lost was lying on the bed, the Spring Brother poster on the wall(Lost is a believer of Spring Brother) said hello to him! Spring Brother said, "I'm Spring Brother, and I saw AekdyCoin shames you again and again. I can't bear my believers were being bullied. Now, I give you a chance to rearrange your gene sequences to defeat AekdyCoin!".

It's soooo crazy and unbelievable to rearrange the gene sequences, but Lost has no choice. He knows some genes called "number theory gene" will affect one "level of number theory". And two of the same kind of gene in different position in the gene sequences will affect two "level of number theory", even though they overlap each other. There is nothing but revenge in his mind. So he needs you help to calculate the most "level of number theory" after rearrangement.

思路:

看得出DP, 但是不会记录状态.

考虑a,t,g,c的个数, 令每个的进制为个数加1.则0-x, 用x+1进制存储.

枚举使用状态..

会存在重复

代码:

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <vector>
  5. //#include <memory.h>
  6. #include <queue>
  7. #include <set>
  8. #include <map>
  9. #include <algorithm>
  10. #include <math.h>
  11. #include <stack>
  12. #include <string>
  13. #include <assert.h>
  14. #include <iomanip>
  15. #include <iostream>
  16. #include <sstream>
  17. #define MINF 0x3f3f3f3f
  18. using namespace std;
  19. typedef long long LL;
  20. const LL MOD = 20090717;
  21. const LL MAXN = 2e6+10;
  22. const int MAXASCII = 4;
  23. struct TrieTree
  24. {
  25. int Next[MAXASCII];
  26. int end;
  27. int fail;
  28. void Clear()
  29. {
  30. memset(Next, 0, sizeof(Next));
  31. end = 0;
  32. fail = 0;
  33. }
  34. }tree[MAXN];
  35. char s[MAXN];
  36. int Dp[1010][11*11*11*11+10];
  37. map<char, int> Mp;
  38. int n, m, k, cnt;
  39. void Insert(char *s)
  40. {
  41. int len = strlen(s);
  42. int p = 0;
  43. for (int i = 0;i < len;i++)
  44. {
  45. if (tree[p].Next[Mp[s[i]]] == 0)
  46. tree[p].Next[Mp[s[i]]] = ++cnt, tree[cnt].Clear();
  47. p = tree[p].Next[Mp[s[i]]];
  48. }
  49. tree[p].end++;
  50. }
  51. void BuildAC()
  52. {
  53. queue<int> que;
  54. for (int i = 0;i < MAXASCII;i++)
  55. {
  56. if (tree[0].Next[i] != 0)
  57. {
  58. tree[tree[0].Next[i]].fail = 0;
  59. que.push(tree[0].Next[i]);
  60. }
  61. }
  62. while (!que.empty())
  63. {
  64. int u = que.front();
  65. que.pop();
  66. tree[u].end += tree[tree[u].fail].end;
  67. for (int i = 0;i < MAXASCII;i++)
  68. {
  69. if (tree[u].Next[i] != 0)
  70. {
  71. tree[tree[u].Next[i]].fail = tree[tree[u].fail].Next[i];
  72. que.push(tree[u].Next[i]);
  73. }
  74. else
  75. tree[u].Next[i] = tree[tree[u].fail].Next[i];
  76. }
  77. }
  78. }
  79. int Solve()
  80. {
  81. int Num[4] = {0}, Bit[4];
  82. int len = strlen(s+1);
  83. for (int i = 1;i <= len;i++)
  84. Num[Mp[s[i]]]++;
  85. Bit[0] = (Num[3]+1)*(Num[2]+1)*(Num[1]+1);
  86. Bit[1] = (Num[3]+1)*(Num[2]+1);
  87. Bit[2] = (Num[3]+1);
  88. Bit[3] = 1;
  89. memset(Dp, -1, sizeof(Dp));
  90. Dp[0][0] = 0;
  91. for (int A = 0;A <= Num[0];A++)
  92. {
  93. for (int B = 0; B <= Num[1]; B++)
  94. for (int C = 0; C <= Num[2]; C++)
  95. for (int D = 0; D <= Num[3]; D++)
  96. {
  97. int state = A*Bit[0]+B*Bit[1]+C*Bit[2]+D;
  98. for (int j = 0;j <= cnt;j++)
  99. {
  100. if (Dp[j][state] == -1)
  101. continue;
  102. for (int t = 0;t < 4;t++)
  103. {
  104. if (t == 0 && A == Num[0])
  105. continue;
  106. if (t == 1 && B == Num[1])
  107. continue;
  108. if (t == 2 && C == Num[2])
  109. continue;
  110. if (t == 3 && D == Num[3])
  111. continue;
  112. int node = tree[j].Next[t];
  113. Dp[node][state+Bit[t]] = max(Dp[node][state+Bit[t]], Dp[j][state]+tree[node].end);
  114. }
  115. }
  116. }
  117. }
  118. int res = 0;
  119. int sta = Num[0]*Bit[0]+Num[1]*Bit[1]+Num[2]*Bit[2]+Num[3]*Bit[3];
  120. for (int i = 0;i <= cnt;i++)
  121. res = max(res, Dp[i][sta]);
  122. return res;
  123. }
  124. int main()
  125. {
  126. Mp['A'] = 0;
  127. Mp['T'] = 1;
  128. Mp['G'] = 2;
  129. Mp['C'] = 3;
  130. int testcnt = 0;
  131. while (~scanf("%d", &n) && n)
  132. {
  133. cnt = 0;
  134. tree[cnt].Clear();
  135. for (int i = 1;i <= n;i++)
  136. {
  137. scanf("%s", s);
  138. Insert(s);
  139. }
  140. scanf("%s", s+1);
  141. BuildAC();
  142. printf("Case %d: %d\n", ++testcnt, Solve());
  143. }
  144. return 0;
  145. }

HDU-3341-Lost's revenge(AC自动机, DP, 压缩)的更多相关文章

  1. HDU 3341 Lost's revenge AC自动机+dp

    Lost's revenge Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)T ...

  2. HDU 2457 DNA repair(AC自动机+DP)题解

    题意:给你几个模式串,问你主串最少改几个字符能够使主串不包含模式串 思路:从昨天中午开始研究,研究到现在终于看懂了.既然是多模匹配,我们是要用到AC自动机的.我们把主串放到AC自动机上跑,并保证不出现 ...

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

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

  4. HDU 4758 Walk Through Squares(AC自动机+DP)

    题目链接 难得出一个AC自动机,我还没做到这个题呢...这题思路不难想,小小的状压出一维来,不过,D和R,让我wa死了,AC自动机,还得刷啊... #include<iostream> # ...

  5. [HDU 4787] GRE Words Revenge (AC自动机)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4787 题目大意: 给你若干个单词,查询一篇文章里出现的单词数.. 就是被我水过去的...暴力重建AC自 ...

  6. HDU 4758 Walk Through Squares( AC自动机 + 状态压缩DP )

    题意:给你两个串A,B, 问一个串长为M+N且包含A和B且恰好包含M个R的字符串有多少种组合方式,所有字符串中均只含有字符L和R. dp[i][j][k][S]表示串长为i,有j个R,在自动机中的状态 ...

  7. HDU 4057 Rescue the Rabbit ( AC自动机 + 状态压缩DP )

    模板来自notonlysuccess. 模式串只有10个,并且重复出现的分值不累加,因此很容易想到状态压缩. 将模式串加入AC自动机,最多有10*100个状态. dp[i][j][k]:串长为i,在T ...

  8. HDU 2825 Wireless Password【AC自动机+DP】

    给m个单词,由这m个单词组成的一个新单词(两个单词可以重叠包含)长度为n,且新单词中包含的基本单词数目不少于k个.问这样的新单词共有多少个? m很小,用二进制表示新单词中包含基本单词的情况. 用m个单 ...

  9. HDU3341 Lost's revenge(AC自动机&&dp)

    一看到ACGT就会想起AC自动机上的dp,这种奇怪的联想可能是源于某道叫DNA什么的题的. 题意,给你很多个长度不大于10的小串,小串最多有50个,然后有一个长度<40的串,然后让你将这个这个长 ...

随机推荐

  1. Scala调用Kafka的生产者和消费者Demo,以及一些配置参数整理

    kafka简介 Kafka是apache开源的一款用Scala编写的消息队列中间件,具有高吞吐量,低延时等特性. Kafka对消息保存时根据Topic进行归类,发送消息者称为Producer,消息接受 ...

  2. vmware 安装XP 32位Professional版本

    VMware14 安装XP 专业版,总部报错Dicrectory Boot not found   下载了N多个XP的版本发现,原来是XP版本的问题,现将正确版本写在这里,以备后用 百度网盘共享位置 ...

  3. GstStaticCaps的初始化

    struct _GstStaticCaps { /*< public >*/ GstCaps *caps; const char *string; /*< private >* ...

  4. 【转】Visual Studio Code必备插件

    先ctrl+shift+p,弹出命令面板-选中Extensions:Install Extensions 或者直接点击左侧栏这个扩展按钮(Ctrl+Shift+X) 然后左侧栏就会显示出很多插件,如图 ...

  5. [Es6]原生Promise的使用方法

    参考:https://www.cnblogs.com/imwtr/p/5916793.html 1.new Promise(func) 通过实例化构造函数成一个promise对象,构造函数中有个函数参 ...

  6. (三)使用Intent在活动中穿梭:显式和隐式Intent

    一.显式Intent @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstan ...

  7. angular 源码 <一> rotuerLinkActive

    这几篇,查看angular 源码. rotuerLinkActive 是路由的样式设置. 它的值是 css 的一个类.或者几个类. 主要代码如下 @Input() set routerLinkActi ...

  8. 函数实现计算等差数列的第n项

    等差数列的第n项 描述 等差数列是指从第二项起,每一项与它的前一项的差等于同一个常数的一种数列,这个常数叫做等差数列的公差.‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬ ...

  9. Android高版本收不到静态注册的广播

    Beginning with Android 8.0 (API level 26), the system imposes additional restrictions on manifest-de ...

  10. C# 交换排序

    用交换排序的方式实现对int类型的数组arrays从小到大排序 思路: 第一步:一个长度为n的数组,把最小的放第一行,第二小的数字放第二行,,,, 0(0为数组的第一项) 第一小的数字 1      ...