题目地址:http://acm.uestc.edu.cn/problem.php?pid=1633

 AC自动机+BFS

 AC自动机,参见:http://www.cnblogs.com/luna-lovegood/archive/2012/04/06/2434340.html

 先用给出的模式串构造AC automaton,再进行BFS即可。

代码:

//zzy2012.3.28AC#include<cstdio>#include<cstring>#include<cstdlib>#include<string>#include<cmath>#include<iostream>#include<algorithm>

using namespace std;

char chrn;#define SCRN while(scanf("%c",&chrn) && chrn!='\n');

typedef struct{int f,t,cnt;//f:最大前缀匹配的点;t:when enter the node,how many patterns are recognised;cnt 在dp时用    int e[3];}node;

typedef struct{int idx,k,n;}node2;

int N,K;int top,front,rear,queue[10000];node trie[3000];node2 Q[10000000];

void push(int i){    queue[rear++]=i;}

int pop(){    front++;return queue[front-1];}

void push2(node2 i){    Q[rear++]=i;}

node2 pop2(){    front++;return Q[front-1];}

void ini(){    top=0;    trie[0].f=-1;    trie[0].t=0;    trie[0].cnt=0;    trie[0].e[0]=-1;    trie[0].e[1]=-1;    trie[0].e[2]=-1;    top++;}

void insert(int idx,char *s,int len){if(trie[idx].e[s[0]-'A']==-1)    {        trie[idx].e[s[0]-'A']=top;        trie[top].f=0;        trie[top].cnt=-1;        trie[top].e[0]=-1;        trie[top].e[1]=-1;        trie[top].e[2]=-1;        len--;if(len==0)            trie[top++].t=1;else        {            trie[top].t=0;            top++;            insert(top-1,s+1,len);        }    }else    {        len--;if(len==0)        {            trie[trie[idx].e[s[0]-'A']].t++;return ;        }else            insert(trie[idx].e[s[0]-'A'],s+1,len);    }}

void struct_AC()//key function{int i,r,v,u;for(i=0; i<3; i++)if(trie[0].e[i]==-1)            trie[0].e[i]=0;    front=rear=0;for(i=0; i<3; i++)if(trie[0].e[i]>0)        {            trie[trie[0].e[i]].f=0;            push(trie[0].e[i]);        }while(rear>front)//struct f and t for each node    {        r=pop();for(i=0; i<3; i++)if(trie[r].e[i]>-1)            {                u=trie[r].e[i];                v=trie[r].f;while(trie[v].e[i]==-1){                    v=trie[v].f;                }                trie[u].f=trie[v].e[i];                trie[u].t+=trie[trie[v].e[i]].t;                push(u);            }    }}

int dp(){int max=0,idx,i,j,sign,t;    node2 r,u;    r.idx=0;    r.k=0;    r.n=0;    front=rear=0;    push2(r);while(rear>front)    {        r=pop2();        idx=r.idx;        t=r.n;//trie[idx].cnt;        if(t<trie[idx].cnt-1)continue;if(r.k==K-1)        {for(i=0; i<3; i++)            {if(trie[idx].e[i]>-1)                {                    j=trie[idx].e[i];if(t+trie[j].t>max)                        max=t+trie[j].t;                }else                {                    j=trie[idx].f;while(trie[j].e[i]==-1)                        j=trie[j].f;                    j=trie[j].e[i];if(t+trie[j].t>max)                        max=t+trie[j].t;                }//cout<<"__max:"<<max<<' '<<idx<<' '<<j<<' '<<r.k<<endl;            }        }else        {for(i=0; i<3; i++)if(trie[idx].e[i]>-1)                {                    j=trie[idx].e[i];if(t+trie[j].t>trie[j].cnt)                    {                        trie[j].cnt=t+trie[j].t;//cout<<"OP4---:"<<idx<<' '<<j<<' '<<r.k<<' '<<endl;                        u.idx=j;                        u.k=r.k+1;                        u.n=trie[j].cnt;//cout<<"  OP 1\n";//printf("%c\n",'A'+i);                        push2(u);                    }                }else                {                    j=trie[idx].f;while(trie[j].e[i]==-1)                        j=trie[j].f;                    j=trie[j].e[i];

if(t+trie[j].t>trie[j].cnt)                    {                        trie[j].cnt=t+trie[j].t;//cout<<"OP3: "<<idx<<" "<<j<<' '<<r.k<<' '<<endl;

                        u.idx=j;                        u.k=r.k+1;                        u.n=trie[j].cnt;//cout<<"  OP2++++++++++++++++ \n";//printf("%c\n",'A'+i);                        push2(u);                    }                }        }    }return max;}

int main(){int i,num;char s[25];//freopen("1633in.txt","r",stdin);    cin>>N>>K;    SCRN    ini();for(i=0; i<N; i++)    {        gets(s);        insert(0,s,strlen(s));    }    struct_AC();    num=dp();    cout<<num<<endl;return 0;}

转载于:https://www.cnblogs.com/Lattexiaoyu/archive/2012/04/06/2434351.html

CDOJ1633 Video Game Combos [AC自动机+dp]相关推荐

  1. [USACO12Jan][luogu3041] Video Game Combos [AC自动机+dp]

    题面 传送门 思路 首先,有一个非常显然的思路就是dp: 设$dp[i][j]$表示前i个字符,最后一个为j 然后发现这个东西有后效性 改!设$dp[i][j]$代表前i个字符,最后15个的状态为j( ...

  2. 洛谷 P3041 视频游戏的连击Video Game Combos(AC自动机+拓扑排序+数位DP)

    洛谷 P3041 视频游戏的连击Video Game Combos 难度一般,不过这个数位DP其实应该叫做记忆化搜索 题意:玩游戏时可以通过按键组合打出combo技能:然后是已知N个combo的按键方 ...

  3. uvalive4842(AC自动机+DP)

    题意: 给出猴子打字时打某个字母的概率,猴子最多可以敲键盘m次,问得到的长度是m的单词包含模式串的概率. 思路: AC自动机+dp. 首先,我们用模式串构造一个AC自动机,用dp[i]][j]表示当前 ...

  4. HDU 2296 Ring AC自动机 + DP

    题意:给你n个模式串,每个模式串有一个得分,让你构造出一个长度为N之内且分数最高的文本串;输出字典序列最小的. 解题思路:  AC自动机 + DP , 不过要输出字典序列最小,多开一个 一个三维字符串 ...

  5. bzoj 1030: [JSOI2007]文本生成器(AC自动机+DP)

    1030: [JSOI2007]文本生成器 Time Limit: 1 Sec  Memory Limit: 162 MB Submit: 5187  Solved: 2136 [Submit][St ...

  6. 【BZOJ】4861: [Beijing2017]魔法咒语 AC自动机+DP+矩阵快速幂

    [题意]给定n个原串和m个禁忌串,要求用原串集合能拼出的不含禁忌串且长度为L的串的数量.(60%)n,m<=50,L<=100.(40%)原串长度为1或2,L<=10^18. [算法 ...

  7. P3041 [USACO12JAN]Video Game Combos【AC自动机+DP】

    时空限制 1000ms / 128MB 题目描述 Bessie is playing a video game! In the game, the three letters 'A', 'B', an ...

  8. 【AC自动机+DP】[USACO JAN2012 GOLD Problem 1: Video Game Combos]

    题目大意 题目大意:给定有ABC组成的串n个,然后请你生成一个长度为K的串求给定的串在生成串中最多被匹配时的次数 分析 AC自动机模板题. 构建trie,然后DP. p是当前节点,l是已经构造的串的长 ...

  9. POJ 3691 DNA repair AC自动机 + DP

    题意:给你只包含'A','G','T','C'四个字母的n个模板串和1个文本串,问你文本串改变多少个字符就可以使得文本串中没有一个模板串 解题思路: 我们可以知道  dp[i][j] 为文本串到 第i ...

最新文章

  1. NVIDIA GPU自动调度神经网络
  2. 将solr安装到tomcat里
  3. mysqlperformanceblog
  4. js img图片加载失败,重新加载+断网检查
  5. cstring判断是否包含子串_最长子串-滑动窗口
  6. 警惕!不要让页面响应时间成为应用性能指标上的杠精
  7. CentOS_5.5_安装GCC编译LiME
  8. ARM汇编,MCR和MRC
  9. mbr硬盘锁c语言源码,今日头一贴!修改MBR引导区,让你的电脑嗝屁!嗝屁!
  10. 你真的理解什么是“财富自由”吗?
  11. python 卡方分布函数_推断统计分析(二):python验证三大抽样分布
  12. 武汉大花岭科目二考试说明
  13. 《强化学习与最优控制》学习笔记(三):强化学习中值空间近似和策略空间近似概述
  14. 互联网+人社峰会召开腾讯开放三大能力建“人社”生态
  15. 超全Redis命令总结(备忘)(建议赶紧收藏)
  16. linux下 kafka的安装
  17. 互联网日报 | 3月9日 星期二 | 我国将修改反垄断法;BOSS直聘职场性别差异报告,城镇就业女性的平均薪酬为6847元...
  18. vue3+ts中使用腾讯地图
  19. F - Queen Collision (模拟题,找规律)
  20. Python核心:5. 容器类型和6.函数function

热门文章

  1. 数据结构与算法(C++版)
  2. gin使用BasicAuth()(验证)中间件
  3. 网址http格式的拆分
  4. shell命令的文本计算
  5. lua源代码分析02:内存管理
  6. MySQL笔记4:desc命令的两个用法
  7. 什么是逻辑分析仪?逻辑分析仪的参数、使用步骤和优势
  8. 玩转以太坊(Ethereum)的测试网络
  9. html5--6-59 其他常用CSS属性
  10. 【转载】PHP 常用的header头部定义汇总