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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【USACO12JAN】视频游戏的连击Video Game Combos

發(fā)布時間:2023/12/29 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【USACO12JAN】视频游戏的连击Video Game Combos 小編覺得挺不錯的,現(xià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?

貝西在玩一款游戲,該游戲只有三個技能鍵 “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.

?

題解:

記cnt[i]為節(jié)點i沿著fail一直走下去可以獲得的積分,那么

f[i][j]為走了i步到節(jié)點j的最大積分 注意初始化...

f[i][a[j].next[k]]=max(f[i][a[j].next[k]],f[i-1][j]+a[a[j].next[k]].cnt);

1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 #include<algorithm> 5 #include<queue> 6 using namespace std; 7 const int INF=-2e8; 8 struct node 9 { 10 int next[3]; 11 int cnt; 12 }a[505]; 13 int f[1005][505]; 14 int root=0,num=0,fail[505]; 15 char s[1005]; 16 void Clear() 17 { 18 a[num].cnt=0; 19 for(int i=0;i<3;i++)a[num].next[i]=0; 20 } 21 void add() 22 { 23 scanf("%s",s); 24 int p=root; 25 for(int i=0,ls=strlen(s);i<ls;i++) 26 { 27 if(a[p].next[s[i]-'A'])p=a[p].next[s[i]-'A']; 28 else 29 { 30 a[p].next[s[i]-'A']=++num; 31 Clear(); 32 p=num; 33 } 34 } 35 a[p].cnt++; 36 } 37 void getfail() 38 { 39 queue<int>q; 40 q.push(root); 41 int u,p,v; 42 while(!q.empty()) 43 { 44 u=q.front();q.pop(); 45 for(int i=0;i<3;i++) 46 { 47 if(!a[u].next[i]) 48 { 49 if(a[fail[u]].next[i])a[u].next[i]=a[fail[u]].next[i]; 50 continue; 51 } 52 p=fail[u]; 53 while(p) 54 { 55 if(a[p].next[i])break; 56 p=fail[p]; 57 } 58 if(a[p].next[i] && a[p].next[i]!=a[u].next[i])fail[a[u].next[i]]=a[p].next[i]; 59 v=a[u].next[i]; 60 a[v].cnt+=a[fail[v]].cnt; 61 q.push(a[u].next[i]); 62 } 63 } 64 } 65 int main() 66 { 67 //freopen("pp.in","r",stdin); 68 int n,k,ans=0; 69 scanf("%d%d",&n,&k); 70 for(int i=1;i<=n;i++) 71 add(); 72 getfail(); 73 for(int i=0;i<=k;i++) 74 for(int j=0;j<=num;j++) 75 f[i][j]=INF; 76 f[0][0]=0; 77 for(int i=1;i<=k;i++) 78 { 79 for(int j=0;j<=num;j++) 80 { 81 for(int k=0;k<3;k++) 82 { 83 f[i][a[j].next[k]]=max(f[i][a[j].next[k]],f[i-1][j]+a[a[j].next[k]].cnt); 84 } 85 } 86 } 87 for(int i=1;i<=num;i++)if(f[k][i]>ans)ans=f[k][i]; 88 printf("%d\n",ans); 89 return 0; 90 }

?

轉(zhuǎn)載于:https://www.cnblogs.com/Yuzao/p/7105592.html

總結(jié)

以上是生活随笔為你收集整理的【USACO12JAN】视频游戏的连击Video Game Combos的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

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