HDU 3341 Lost's revenge (AC自动机 + DP + 变进制/hash)题解
题意:给你些分数串,给你一个主串,主串每出现一个分数串加一分,要你重新排列主串,最多几分
思路:显然这里开$40^4$去状压内存不够。但是我们自己想想会发现根本不用开那么大,因为很多状态是废状压,不是不存在的,那么可以考虑想办法简化状态。
一个是hash,直接打表所有子情况,用ha[][][][]表示出所有情况,那么直接dp[status][size]去dp。
还有一种用变进制:
假设ACGT的总数分别为num[0],num[1],num[2],num[3]
那么对于ACGT的数量分别为ABCD的状态可以记录为:
A*(num[1]+1)*(num[2]+1)*(num[3]+1) + B*(num[2]+1)*(num[3]+1)+ C*(num[3]+1) +D
显然末尾D基数为1,次末尾C基数(num[3]+1),那么D不管怎么变(最多变num[3])永远影响不到C那个级数((num[3] + 1 )* k),那么就能区分变得是哪一位。
代码:
/*变进制*/
#include<set>
#include<map>
#include<queue>
#include<cmath>
#include<string>
#include<cstdio>
#include<vector>
#include<cstring>
#include <iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 500 + 5;
const int M = 50 + 5;
const ull seed = 131;
const int INF = 0x3f3f3f3f;
const int MOD = 20090717;
int n, m;
int dp[15000][maxn];
int getid(char s){
if(s == 'A') return 0;
if(s == 'C') return 1;
if(s == 'G') return 2;
return 3;
}
struct Aho{
struct state{
int next[4];
int fail, cnt;
}node[maxn];
int size;
queue<int> q; void init(){
size = 0;
newtrie();
while(!q.empty()) q.pop();
} int newtrie(){
memset(node[size].next, 0, sizeof(node[size].next));
node[size].cnt = node[size].fail = 0;
return size++;
} void insert(char *s){
int len = strlen(s);
int now = 0;
for(int i = 0; i < len; i++){
int c = getid(s[i]);
if(node[now].next[c] == 0){
node[now].next[c] = newtrie();
}
now = node[now].next[c];
}
node[now].cnt++;
} void build(){
node[0].fail = -1;
q.push(0); while(!q.empty()){
int u = q.front();
q.pop();
if(node[node[u].fail].cnt && u) node[u].cnt += node[node[u].fail].cnt;
for(int i = 0; i < 4; i++){
if(!node[u].next[i]){
if(u == 0)
node[u].next[i] = 0;
else
node[u].next[i] = node[node[u].fail].next[i];
}
else{
if(u == 0) node[node[u].next[i]].fail = 0;
else{
int v = node[u].fail;
while(v != -1){
if(node[v].next[i]){
node[node[u].next[i]].fail = node[v].next[i];
break;
}
v = node[v].fail;
}
if(v == -1) node[node[u].next[i]].fail = 0;
}
q.push(node[u].next[i]);
}
}
}
} void query(char *s){
int ans = 0;
int len = strlen(s);
int num[4] = {0};
for(int i = 0; i < len; i++){
num[getid(s[i])]++;
}
memset(dp, -1, sizeof(dp));
dp[0][0] = 0;
int fac[4];
fac[0] = (num[1] + 1) * (num[2] + 1) * (num[3] + 1);
fac[1] = (num[2] + 1) * (num[3] + 1);
fac[2] = (num[3] + 1);
fac[3] = 1;
for(int i = 0; i <= num[0]; i++){
for(int j = 0; j <= num[1]; j++){
for(int k = 0; k <= num[2]; k++){
for(int l = 0; l <= num[3]; l++){
int id = i * fac[0] + j * fac[1] + k * fac[2] + l;
for(int g = 0; g < size; g++){
if(dp[id][g] == -1) continue;
for(int h = 0; h < 4; h++){
int nex;
if(h == 0 && i == num[0]) continue;
if(h == 1 && j == num[1]) continue;
if(h == 2 && k == num[2]) continue;
if(h == 3 && l == num[3]) continue;
nex = id + fac[h];
int add = node[node[g].next[h]].cnt;
if(dp[nex][node[g].next[h]] < dp[id][g] + add){
dp[nex][node[g].next[h]] = dp[id][g] + add;
if(i + j + k + l + 1 == len) ans = max(ans , dp[nex][node[g].next[h]]);
}
}
}
}
}
}
}
printf("%d\n", ans);
} }ac;
char s[45];
int main(){
int ca = 1;
while(~scanf("%d", &n) && n){
ac.init();
for(int i = 0; i < n; i++){
scanf("%s", s);
ac.insert(s);
}
ac.build();
scanf("%s", s);
printf("Case %d: ", ca++);
ac.query(s);
}
return 0;
}
/*Hash*/
#include<set>
#include<map>
#include<queue>
#include<cmath>
#include<string>
#include<cstdio>
#include<vector>
#include<cstring>
#include <iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 500 + 5;
const int M = 50 + 5;
const ull seed = 131;
const int INF = 0x3f3f3f3f;
const int MOD = 20090717;
int n, m;
int dp[15000][maxn];
int ha[42][42][42][42];
int getid(char s){
if(s == 'A') return 0;
if(s == 'C') return 1;
if(s == 'G') return 2;
return 3;
}
struct Aho{
struct state{
int next[4];
int fail, cnt;
}node[maxn];
int size;
queue<int> q; void init(){
size = 0;
newtrie();
while(!q.empty()) q.pop();
} int newtrie(){
memset(node[size].next, 0, sizeof(node[size].next));
node[size].cnt = node[size].fail = 0;
return size++;
} void insert(char *s){
int len = strlen(s);
int now = 0;
for(int i = 0; i < len; i++){
int c = getid(s[i]);
if(node[now].next[c] == 0){
node[now].next[c] = newtrie();
}
now = node[now].next[c];
}
node[now].cnt++;
} void build(){
node[0].fail = -1;
q.push(0); while(!q.empty()){
int u = q.front();
q.pop();
if(node[node[u].fail].cnt && u) node[u].cnt += node[node[u].fail].cnt; //attention
for(int i = 0; i < 4; i++){
if(!node[u].next[i]){
if(u == 0)
node[u].next[i] = 0;
else
node[u].next[i] = node[node[u].fail].next[i];
}
else{
if(u == 0) node[node[u].next[i]].fail = 0;
else{
int v = node[u].fail;
while(v != -1){
if(node[v].next[i]){
node[node[u].next[i]].fail = node[v].next[i];
break;
}
v = node[v].fail;
}
if(v == -1) node[node[u].next[i]].fail = 0;
}
q.push(node[u].next[i]);
}
}
}
} void query(char *s){
int ans = 0;
int len = strlen(s);
int tot = 0;
int num[4] = {0};
for(int i = 0; i < len; i++){
num[getid(s[i])]++;
}
for(int i = 0; i <= num[0]; i++){
for(int j = 0; j <= num[1]; j++){
for(int k = 0; k <= num[2]; k++){
for(int l = 0; l <= num[3]; l++){
ha[i][j][k][l] = tot;
for(int g = 0; g < size; g++){
dp[tot][g] = -1;
}
tot++;
}
}
}
}
for(int i = 0; i <= num[0]; i++){
for(int j = 0; j <= num[1]; j++){
for(int k = 0; k <= num[2]; k++){
for(int l = 0; l <= num[3]; l++){ }
}
}
}
dp[0][0] = 0;
for(int i = 0; i <= num[0]; i++){
for(int j = 0; j <= num[1]; j++){
for(int k = 0; k <= num[2]; k++){
for(int l = 0; l <= num[3]; l++){
int id = ha[i][j][k][l];
for(int g = 0; g < size; g++){
if(dp[id][g] == -1) continue;
for(int h = 0; h < 4; h++){
int nex;
if(h == 0 && i == num[0]) continue;
if(h == 1 && j == num[1]) continue;
if(h == 2 && k == num[2]) continue;
if(h == 3 && l == num[3]) continue;
if(h == 0) nex = ha[i + 1][j][k][l];
if(h == 1) nex = ha[i][j + 1][k][l];
if(h == 2) nex = ha[i][j][k + 1][l];
if(h == 3) nex = ha[i][j][k][l + 1];
int add = node[node[g].next[h]].cnt;
if(dp[nex][node[g].next[h]] < dp[id][g] + add){
dp[nex][node[g].next[h]] = dp[id][g] + add;
if(i + j + k + l + 1 == len) ans = max(ans , dp[nex][node[g].next[h]]);
}
}
}
}
}
}
}
printf("%d\n", ans);
} }ac;
char s[45];
int main(){
int ca = 1;
while(~scanf("%d", &n) && n){
ac.init();
for(int i = 0; i < n; i++){
scanf("%s", s);
ac.insert(s);
}
ac.build();
scanf("%s", s);
printf("Case %d: ", ca++);
ac.query(s);
}
return 0;
}
HDU 3341 Lost's revenge (AC自动机 + DP + 变进制/hash)题解的更多相关文章
- 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 ...
- HDU 2457 DNA repair(AC自动机+DP)题解
题意:给你几个模式串,问你主串最少改几个字符能够使主串不包含模式串 思路:从昨天中午开始研究,研究到现在终于看懂了.既然是多模匹配,我们是要用到AC自动机的.我们把主串放到AC自动机上跑,并保证不出现 ...
- HDU 2425 DNA repair (AC自动机+DP)
DNA repair Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- HDU 4758 Walk Through Squares(AC自动机+DP)
题目链接 难得出一个AC自动机,我还没做到这个题呢...这题思路不难想,小小的状压出一维来,不过,D和R,让我wa死了,AC自动机,还得刷啊... #include<iostream> # ...
- [HDU 4787] GRE Words Revenge (AC自动机)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4787 题目大意: 给你若干个单词,查询一篇文章里出现的单词数.. 就是被我水过去的...暴力重建AC自 ...
- hdu3341Lost's revenge(ac自动机+dp)
链接 类似的dp省赛时就做过了,不过这题卡内存,需要把当前状态hash一下,可以按进制来算出当前的状态,因为所有的状态数是不会超过10*10*10*10的,所以完全可以把这些存下来. 刚开始把trie ...
- HDU 2825 Wireless Password【AC自动机+DP】
给m个单词,由这m个单词组成的一个新单词(两个单词可以重叠包含)长度为n,且新单词中包含的基本单词数目不少于k个.问这样的新单词共有多少个? m很小,用二进制表示新单词中包含基本单词的情况. 用m个单 ...
- HDU-3341-Lost's revenge(AC自动机, DP, 压缩)
链接: https://vjudge.net/problem/HDU-3341 题意: Lost and AekdyCoin are friends. They always play "n ...
- HDU3341 Lost's revenge(AC自动机&&dp)
一看到ACGT就会想起AC自动机上的dp,这种奇怪的联想可能是源于某道叫DNA什么的题的. 题意,给你很多个长度不大于10的小串,小串最多有50个,然后有一个长度<40的串,然后让你将这个这个长 ...
随机推荐
- Connections could not be acquired from the underlying database!
Connections could not be acquired from the underlying database! 报错截图: 报错内容: Exception in thread &quo ...
- DP 状态 DP 转移方程 动态规划解题思路
如何学好动态规划(2) 原创 Gene_Liu LeetCode力扣 今天 算法萌新如何学好动态规划(1) https://mp.weixin.qq.com/s/rhyUb7d8IL8UW1IosoE ...
- Why failover-based implementations are not enough Redis分布式锁实现 SET resource_name my_random_value NX PX 30000
核心 SET resource_name my_random_value NX PX 30000 Distributed locks with Redis – Redis https://redis. ...
- get uuid
https://wx2.qq.com/?&lang=zh_CN /** * 启动二维码登录 */ function doQrcodeLogin() { loginFactory.getUUID ...
- socket创建和结束
什么是进程 进程(Process)是计算机中的程序关于某数据集合上的一次运行活动,是系统进行资源分配和调度的基本单位,是操作系统结构的基础.在早期面向进程设计的计算机结构中,进程是程序的基本执行实体: ...
- pycharm设置头文件模板(for mac)
我们要达到的效果是每次新建一个.py文件都会有头文件,关于头文件的作用之前已做解释
- Typora使用与GItHhub图床配置
Typora使用 (windows) 1 快捷键 1.1 表格 快捷方式:CTRL+T ID name year 1 Oracle 10 2 Mysql 10 3 Postgresql 20 1.2 ...
- hbase笔记---新版api之对表的操作,指定region创建,普通创建,删除,修改列族信息
hbase 对于表的相关操作: 实现功能有:指定region创建,普通创建,删除,修改列族信息 package learm.forclass.testclass; import org.apache. ...
- checkbox限制选中个数
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...
- js创建javaMap
/** * Simple Map * var m = new Map(); * m.put('key','value'); * var v_otherMap = v_m.toMapString();* ...