日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時(shí)間:2023/12/29 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 洛谷 P3041 视频游戏的连击Video Game Combos(AC自动机+拓扑排序+数位DP) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

洛谷 P3041 視頻游戲的連擊Video Game Combos

難度一般,不過這個(gè)數(shù)位DP其實(shí)應(yīng)該叫做記憶化搜索

題意:玩游戲時(shí)可以通過按鍵組合打出combo技能;然后是已知N個(gè)combo的按鍵方式,然后求K次按鍵最多可以放出的combo技能(combo技能之間可以重疊)。

思路:

  • 在AC自動(dòng)機(jī)上如果一個(gè)點(diǎn)表示一個(gè)字符串以及它的后綴,則一個(gè)點(diǎn)可以對(duì)應(yīng)多個(gè)combo技能(這是關(guān)鍵點(diǎn))
  • 可以先用拓?fù)渑判蛱幚沓雒總€(gè)點(diǎn)對(duì)應(yīng)了多少個(gè)combo技能,處理時(shí)要用fail的反向邊即refail計(jì)算入度(這里應(yīng)該不用這么麻煩,build的時(shí)候就可以直接加上去了)
  • 然后就是數(shù)位DP啦,這題沒啥坑點(diǎn),思路簡潔
  • 題目描述

    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?

    貝西在玩一款游戲,該游戲只有三個(gè)技能鍵 “A”“B”“C”可用,但這些鍵可用形成N種(1 <= N<= 20)特定的組合技。第i個(gè)組合技用一個(gè)長度為1到15的字符串S_i表示。

    當(dāng)貝西輸入的一個(gè)字符序列和一個(gè)組合技匹配的時(shí)候,他將獲得1分。特殊的,他輸入的一個(gè)字符序列有可能同時(shí)和若干個(gè)組合技匹配,比如N=3時(shí),3種組合技分別為"ABA", “CB”, 和"ABACB",若貝西輸入"ABACB",他將獲得3分。

    若貝西輸入恰好K (1 <= K <= 1,000)個(gè)字符,他最多能獲得多少分?

    輸入格式

    • 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.

    輸入輸出樣例

    輸入
    3 7
    ABA
    CB
    ABACB
    輸出
    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.

    //#pragma comment(linker, "/STACK:102400000,102400000") #include "bits/stdc++.h" #define pb push_back #define ls l,m,now<<1 #define rs m+1,r,now<<1|1 #define hhh printf("hhh\n") #define see(x) (cerr<<(#x)<<'='<<(x)<<endl) using namespace std; typedef long long ll; typedef pair<int,int> pr; inline int read() {int x=0;char c=getchar();while(c<'0'||c>'9')c=getchar();while(c>='0'&&c<='9')x=x*10+c-'0',c=getchar();return x;}const int maxn = 4e2+10; const int mod = 1e4+7; const double eps = 1e-9;char s[maxn]; int N, K; int trie[maxn][26], fail[maxn], cnt; vector<int> refail[maxn]; int in[maxn], num[maxn]; int dp[1010][maxn];void insert() {int len=strlen(s), p=0;for(int i=0; i<len; ++i) {int k=s[i]-'A';if(!trie[p][k]) trie[p][k]=++cnt;p=trie[p][k];}num[p]++; }void build() {queue<int> q;for(int i=0; i<26; ++i) if(trie[0][i]) q.push(trie[0][i]);while(q.size()) {int now=q.front(); q.pop();for(int i=0; i<26; ++i) {if(trie[now][i]) {fail[trie[now][i]]=trie[fail[now]][i];refail[trie[fail[now]][i]].pb(trie[now][i]);in[trie[now][i]]++;q.push(trie[now][i]);}else trie[now][i]=trie[fail[now]][i];}} }void Toposort() {queue<int> q;for(int i=0; i<=cnt; ++i) if(!in[i]) q.push(i);while(q.size()) {int now=q.front(); q.pop();for(int i=0; i<refail[now].size(); ++i) {int v=refail[now][i];num[v]+=num[now];if(--in[v]==0) q.push(v);}} }int dfs(int pos, int now) {if(pos>K) return 0;if(~dp[pos][now]) return dp[pos][now];int ans=0;for(int i=0; i<26; ++i)ans=max(ans,num[trie[now][i]]+dfs(pos+1,trie[now][i]));return dp[pos][now]=ans; }int main() {//ios::sync_with_stdio(false);N=read(), K=read();for(int i=0; i<N; ++i) scanf("%s", s), insert();build();Toposort();memset(dp,-1,sizeof(dp));printf("%d\n", dfs(1,0)); }

    總結(jié)

    以上是生活随笔為你收集整理的洛谷 P3041 视频游戏的连击Video Game Combos(AC自动机+拓扑排序+数位DP)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。