[洛谷3041]视频游戏的连击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?
貝西在玩一款游戲,該游戲只有三個(gè)技能鍵 “A”“B”“C”可用,但這些鍵可用形成N種(1 <= N<= 20)特定的組合技。第i個(gè)組合技用一個(gè)長(zhǎng)度為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.
輸入輸出樣例
輸入樣例#1: 復(fù)制 3 7 ABA CB ABACB 輸出樣例#1: 復(fù)制 4說(shuō)明
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自動(dòng)機(jī)+dp,不過(guò)dp那塊還是調(diào)了半天。。
代碼:
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<queue> 5 using namespace std; 6 int n,cnt,ans,k; 7 int fail[3010]; 8 int end[3010]; 9 int ch[3010][27]; 10 int dp[3010][1001]; 11 bool vis[3010][1001]; 12 char s[100]; 13 void build(string s) 14 { 15 int len=s.length(); 16 int now=0; 17 for(int i=0;i<len;i++) 18 { 19 if(!ch[now][s[i]-'A']) ch[now][s[i]-'A']=++cnt; 20 now=ch[now][s[i]-'A']; 21 } 22 end[now]+=1; 23 } 24 void build_fail() 25 { 26 queue<int>q; 27 for(int i=0;i<3;i++) 28 if(ch[0][i]) 29 q.push(ch[0][i]); 30 while(!q.empty()) 31 { 32 int u=q.front(); q.pop(); 33 for(int i=0;i<3;i++) 34 { 35 if(ch[u][i]) 36 { 37 fail[ch[u][i]]=ch[fail[u]][i]; 38 q.push(ch[u][i]); 39 } 40 else ch[u][i]=ch[fail[u]][i]; 41 } 42 } 43 } 44 int get(int now,int val) 45 { 46 while(now) val+=end[now],now=fail[now]; 47 return val; 48 } 49 int main() 50 { 51 scanf("%d%d",&n,&k); 52 for(int i=1;i<=n;i++) 53 { 54 scanf("%s",s); 55 build(s); 56 } 57 build_fail(); 58 vis[0][0]=1; 59 for(int i=0;i<k;i++) 60 for(int j=0;j<=cnt;j++) 61 { 62 if(!vis[i][j]) continue; 63 for(int l=0;l<3;l++) 64 { 65 int now=ch[j][l]; 66 dp[i+1][now]=max(dp[i+1][now],get(now,dp[i][j])); 67 vis[i+1][now]=true; 68 } 69 } 70 for(int i=0;i<=cnt;i++) ans=max(ans,dp[k][i]); 71 printf("%d",ans); 72 return 0; 73 }?
轉(zhuǎn)載于:https://www.cnblogs.com/Slrslr/p/9496647.html
總結(jié)
以上是生活随笔為你收集整理的[洛谷3041]视频游戏的连击Video Game Combos的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: JQuery追加一列tr到table中
- 下一篇: 关于HTTP协议经典面试题 及答案