洛谷 P3041 [USACO12JAN] Video Game Combos

题目描述

Bessie is playing a video game! In the game, the three letters ‘A‘, ‘B‘, and ‘C‘ are the only valid buttons. Bessie may press the buttons in any order she likes; however, there are only N distinct combos possible (1 <= N <= 20). Combo i is represented as a string S_i which has a length between 1 and 15 and contains only the letters ‘A‘, ‘B‘, and ‘C‘.

Whenever Bessie presses a combination of letters that matches with a combo, she gets one point for the combo. Combos may overlap with each other or even finish at the same time! For example if N = 3 and the three possible combos are "ABA", "CB", and "ABACB", and Bessie presses "ABACB", she will end with 3 points. Bessie may score points for a single combo more than once.

Bessie of course wants to earn points as quickly as possible. If she presses exactly K buttons (1 <= K <= 1,000), what is the maximum number of points she can earn?

贝西在玩一款游戏,该游戏只有三个技能键 “A”“B”“C”可用,但这些键可用形成N种(1 <= N<= 20)特定的组合技。第i个组合技用一个长度为1到15的字符串S_i表示。

当贝西输入的一个字符序列和一个组合技匹配的时候,他将获得1分。特殊的,他输入的一个字符序列有可能同时和若干个组合技匹配,比如N=3时,3种组合技分别为"ABA", "CB", 和"ABACB",若贝西输入"ABACB",他将获得3分。

若贝西输入恰好K (1 <= K <= 1,000)个字符,他最多能获得多少分?

输入输出格式

输入格式:

  • Line 1: Two space-separated integers: N and K.
  • Lines 2..N+1: Line i+1 contains only the string S_i, representing combo i.

输出格式:

  • Line 1: A single integer, the maximum number of points Bessie can obtain.

输入输出样例

输入样例#1: 
3 7
ABA
CB
ABACB

输出样例#1: 
4

说明

The optimal sequence of buttons in this case is ABACBCB, which gives 4 points--1 from ABA, 1 from ABACB, and 2 from CB.

最简单的AC自动机+DP了。。。。再不会的话AC自动机白学了。

#include<bits/stdc++.h>
#define ll long long
#define maxn 1005
using namespace std;
int ch[maxn][3],n,m,k,ans=0;
int root=0,tot=0,val[maxn];
int f[maxn],g[maxn][maxn];
char s[maxn];

inline int id(char c){
    return c-‘A‘;
}

inline void ins(){
    int len=strlen(s),now=root;
    for(int i=0;i<len;i++){
        int c=id(s[i]);
        if(!ch[now][c]) ch[now][c]=++tot;
        now=ch[now][c];
    }
    val[now]=1;
}

inline void get_fail(){
    queue<int> q;
    for(int i=0;i<3;i++) if(ch[0][i]){
        q.push(ch[0][i]);
    }

    int r,v,x;
    while(!q.empty()){
        x=q.front(),q.pop();
        for(int i=0;i<3;i++){
            r=ch[x][i];
            if(!r){
                ch[x][i]=ch[f[x]][i];
                continue;
            }

            q.push(r);
            f[r]=ch[f[x]][i];
            val[r]+=val[f[r]];
        }
    }
}

inline void dp(){
    memset(g,-0x3f,sizeof(g));
    g[0][0]=0;
    int to;
    for(int i=0;i<k;i++)
        for(int j=0;j<=tot;j++)
            for(int u=0;u<3;u++){
                to=ch[j][u];
                g[i+1][to]=max(g[i+1][to],g[i][j]+val[to]);
            }

    for(int i=0;i<=tot;i++) ans=max(ans,g[k][i]);
}

int main(){
    scanf("%d%d",&n,&k);
    for(int i=1;i<=n;i++){
        scanf("%s",s);
        ins();
    }

    get_fail();
    dp();

    printf("%d\n",ans);
    return 0;
}

原文地址:https://www.cnblogs.com/JYYHH/p/8280569.html

时间: 2024-08-02 02:02:24

洛谷 P3041 [USACO12JAN] Video Game Combos的相关文章

洛谷P3043 [USACO12JAN]牛联盟Bovine Alliance

P3043 [USACO12JAN]牛联盟Bovine Alliance 题目描述 Bessie and her bovine pals from nearby farms have finally decided that they are going to start connecting their farms together by trails in an effort to form an alliance against the farmers. The cows in each

洛谷 P3040 [USACO12JAN]贝尔分享Bale Share

P3040 [USACO12JAN]贝尔分享Bale Share 题目描述 Farmer John has just received a new shipment of N (1 <= N <= 20) bales of hay, where bale i has size S_i (1 <= S_i <= 100). He wants to divide the bales between his three barns as fairly as possible. After

洛谷 P1561 [USACO12JAN]爬山Mountain Climbing

传送门 题目大意: n头牛,上山时间为u(i),下山为d(i). 要求每一时刻最多只有一头牛上山,一头牛下山. 问每头牛都上下山后花费最少时间. 题解:贪心 推了推样例,发现上山时间一定,那找个下山最快 的当最后一头山上的牛. #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #define LL long long #define N 25009 using

洛谷—— P1561 [USACO12JAN]爬山Mountain Climbing

https://daniu.luogu.org/problemnew/show/P1561 题目描述 Farmer John has discovered that his cows produce higher quality milk when they are subject to strenuous exercise. He therefore decides to send his N cows (1 <= N <= 25,000) to climb up and then back

P3041 [USACO12JAN]视频游戏的连击Video Game Combos

P3041 [USACO12JAN]视频游戏的连击Video Game Combos https://www.luogu.org/problemnew/show/P3041 分析: AC自动机. 建立AC自动机,然后dp[i][j]表示经过了i个字符,到达自动机上j这个位置,的得分. 那么dp[i-1][j] + val[ch[j][k]] -> dp[i][ch[j][k]]. 表示从j点,往前走一步,加上新加一个字符产生的贡献. 代码: 1 #include<bits/stdc++.h&g

洛谷 P2614 计算器弹琴

P2614 计算器弹琴 题目描述 总所周知,计算器可以拿来干很多它本不应该干的事情,比如写作文.(参看洛谷P2549) 小A发现了一个计算器的另一个隐藏功能——弹琴. http://www.bilibili.com/video/av2205500/ 如果按上一个键,比如说1,就会发出中音“Do”. 这边给出按键音高表 + 低音Fa < 低音So * 低音La / 低音Xi 1 中音Do 2 中音Re 3 中音Mi 4 中音Fa 5 中音So 6 中音La 7 高音Xi 8 高音Do 9 高音Re

洛谷 P2709 BZOJ 3781 小B的询问

题目描述 小B有一个序列,包含N个1~K之间的整数.他一共有M个询问,每个询问给定一个区间[L..R],求Sigma(c(i)^2)的值,其中i的值从1到K,其中c(i)表示数字i在[L..R]中的重复次数.小B请你帮助他回答询问. 输入输出格式 输入格式: 第一行,三个整数N.M.K. 第二行,N个整数,表示小B的序列. 接下来的M行,每行两个整数L.R. 输出格式: M行,每行一个整数,其中第i行的整数表示第i个询问的答案. 输入输出样例 输入样例#1: 6 4 3 1 3 2 1 1 3

洛谷1231 教辅的组成

洛谷1231 教辅的组成 https://www.luogu.org/problem/show?pid=1231 题目背景 滚粗了的HansBug在收拾旧语文书,然而他发现了什么奇妙的东西. 题目描述 蒟蒻HansBug在一本语文书里面发现了一本答案,然而他却明明记得这书应该还包含一份练习题.然而出现在他眼前的书多得数不胜数,其中有书,有答案,有练习册.已知一个完整的书册均应该包含且仅包含一本书.一本练习册和一份答案,然而现在全都乱做了一团.许多书上面的字迹都已经模糊了,然而HansBug还是可

洛谷教主花园dp

洛谷-教主的花园-动态规划 题目描述 教主有着一个环形的花园,他想在花园周围均匀地种上n棵树,但是教主花园的土壤很特别,每个位置适合种的树都不一样,一些树可能会因为不适合这个位置的土壤而损失观赏价值. 教主最喜欢3种树,这3种树的高度分别为10,20,30.教主希望这一圈树种得有层次感,所以任何一个位置的树要比它相邻的两棵树的高度都高或者都低,并且在此条件下,教主想要你设计出一套方案,使得观赏价值之和最高. 输入输出格式 输入格式: 输入文件garden.in的第1行为一个正整数n,表示需要种的