链接:

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进制存储.

枚举使用状态..

会存在重复

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
//#include <memory.h>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <math.h>
#include <stack>
#include <string>
#include <assert.h>
#include <iomanip>
#include <iostream>
#include <sstream>
#define MINF 0x3f3f3f3f
using namespace std;
typedef long long LL;
const LL MOD = 20090717;
const LL MAXN = 2e6+10;
const int MAXASCII = 4; struct TrieTree
{
int Next[MAXASCII];
int end;
int fail;
void Clear()
{
memset(Next, 0, sizeof(Next));
end = 0;
fail = 0;
}
}tree[MAXN]; char s[MAXN];
int Dp[1010][11*11*11*11+10];
map<char, int> Mp;
int n, m, k, cnt; void Insert(char *s)
{
int len = strlen(s);
int p = 0;
for (int i = 0;i < len;i++)
{
if (tree[p].Next[Mp[s[i]]] == 0)
tree[p].Next[Mp[s[i]]] = ++cnt, tree[cnt].Clear();
p = tree[p].Next[Mp[s[i]]];
}
tree[p].end++;
} void BuildAC()
{
queue<int> que;
for (int i = 0;i < MAXASCII;i++)
{
if (tree[0].Next[i] != 0)
{
tree[tree[0].Next[i]].fail = 0;
que.push(tree[0].Next[i]);
}
}
while (!que.empty())
{
int u = que.front();
que.pop();
tree[u].end += tree[tree[u].fail].end;
for (int i = 0;i < MAXASCII;i++)
{
if (tree[u].Next[i] != 0)
{
tree[tree[u].Next[i]].fail = tree[tree[u].fail].Next[i];
que.push(tree[u].Next[i]);
}
else
tree[u].Next[i] = tree[tree[u].fail].Next[i];
}
}
} int Solve()
{
int Num[4] = {0}, Bit[4];
int len = strlen(s+1);
for (int i = 1;i <= len;i++)
Num[Mp[s[i]]]++;
Bit[0] = (Num[3]+1)*(Num[2]+1)*(Num[1]+1);
Bit[1] = (Num[3]+1)*(Num[2]+1);
Bit[2] = (Num[3]+1);
Bit[3] = 1;
memset(Dp, -1, sizeof(Dp));
Dp[0][0] = 0;
for (int A = 0;A <= Num[0];A++)
{
for (int B = 0; B <= Num[1]; B++)
for (int C = 0; C <= Num[2]; C++)
for (int D = 0; D <= Num[3]; D++)
{
int state = A*Bit[0]+B*Bit[1]+C*Bit[2]+D;
for (int j = 0;j <= cnt;j++)
{
if (Dp[j][state] == -1)
continue;
for (int t = 0;t < 4;t++)
{
if (t == 0 && A == Num[0])
continue;
if (t == 1 && B == Num[1])
continue;
if (t == 2 && C == Num[2])
continue;
if (t == 3 && D == Num[3])
continue;
int node = tree[j].Next[t];
Dp[node][state+Bit[t]] = max(Dp[node][state+Bit[t]], Dp[j][state]+tree[node].end);
}
}
}
}
int res = 0;
int sta = Num[0]*Bit[0]+Num[1]*Bit[1]+Num[2]*Bit[2]+Num[3]*Bit[3];
for (int i = 0;i <= cnt;i++)
res = max(res, Dp[i][sta]);
return res;
} int main()
{
Mp['A'] = 0;
Mp['T'] = 1;
Mp['G'] = 2;
Mp['C'] = 3;
int testcnt = 0;
while (~scanf("%d", &n) && n)
{
cnt = 0;
tree[cnt].Clear();
for (int i = 1;i <= n;i++)
{
scanf("%s", s);
Insert(s);
}
scanf("%s", s+1);
BuildAC();
printf("Case %d: %d\n", ++testcnt, Solve());
} return 0;
}

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. 服务提供者框架讲解 之 myJDBC

    利用一个简单的myJDBC,讲一下'服务提供者框架'的思想.主要是思想 目录 什么是 服务提供者框架 代码讲解 服务接口 服务提供者接口 服务注册API.服务访问API 静态工厂方法 服务实现类 – ...

  2. 排查java 内存CPU报警

    #!/bin/bash source /etc/profile #接收外部传入PID,任选一种 #servicePid=$1 headPid=`ps auxw|sort -rn -k3|head -4 ...

  3. Kafka集群安装及prometheus监控

    前提 zookeeper安装参考:https://www.cnblogs.com/JustinLau/p/11372782.html 其他安装参考:https://www.cnblogs.com/lu ...

  4. 在docker下SQL Server attach mdf和ldf文件

    (DB:MyPost) USE masterGO-- Create database via attachCREATE DATABASE [MyPost]    ON ( FILENAME = N'C ...

  5. 【vue】过滤器的使用

    一.在methods中使用过滤器------全局定义的过滤器 //main.js中 import Vue from 'vue' Vue.filter('testFilter1',function(va ...

  6. was控制台无法停止应用

    问题描述: was控制台无法停止应用,只能通过停止server的方式停止: 代码实现: import org.slf4j.Logger; import org.slf4j.LoggerFactory; ...

  7. JavaScript--常用对象的属性及方法(1)

    1.Number对象(基本数据类型) Number对象的方法大多是一些强制转换方法,如果转换失败返回NaN,以下举例中用number来代替具体数字: *console.log在控制台输出(键盘F12可 ...

  8. javamail "535 5.7.3 Authentication unsuccessful" 问题排查

    有一家odm的服务器用Javamail发邮件的时候报错  Authentication unsuccessful  其他的有些又是正常的 网上查了一下解决方法如下 JavaMailSenderImpl ...

  9. Go 缓冲信道

    缓冲信道 语法结构:cap为容量 ch := make(chan type, cap) 缓冲信道支持len()和cap(). 只能向缓冲信道发送容量以内的数据. 只能接收缓冲信道长度以内的数据. 缓冲 ...

  10. 从linux和ucos的比较中来看进程这个概念

    这种问题就要和ucos结合起来嘛. 程序和进程: 程序:存放在磁盘上的一些列代码和数据的可执行映像,是一个静止的实体. 进程:是一个执行中的程序,它是动态的实体. linux进程的四要素: 1. 有一 ...