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

 

题解:

记cnt[i]为节点i沿着fail一直走下去可以获得的积分,那么

f[i][j]为走了i步到节点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     }

 

转载于:https://www.cnblogs.com/Yuzao/p/7105592.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
只用一点点时间即可实现动作游戏的combo输入 Want to create an action game that allows you to perform combos? The GAC (Great Animation Combos) System is created just for that. Create 3D/2D games that take your animations and seamlessly provides cool combo sequences to your players. This asset comes packed with features to get your games up and running (and combo-ing) with a few clicks and minimal coding. Feature List: –NEW! 3rd Party Plugins Setup Menu– Add access to supported 3rd party Assets with just button clicks –NEW! Charged Activators-Hold down a specific activator input and release in time to provide a different animation attack –NEW! Debug Input Response Times-Allows the adjustment of input response times giving even more customized control. –3rd Party Activator Support– InControl, NEW! Control Freak, NEW! uGUI, and NEW! Rewired supported! (InControl, Control Freak and Rewired sold separately by their respective owners) –NEW 2D 3rd Party Support!– Tested to now support Smooth Moves asset right out of the box using Legacy Animations! –Context Variables– Use context variables to add conditions to Combos and Activators for limitless combo creations –Target Tracker Hit Detections– Use the tracker to have game objects react to hits with different animations and movements by calculating the distance and angle –Unity Physics Compatible– Use Unity’s physics system in conjunction with GAC with 3 different component modes –uScript and Playmaker Support– Use uScripts nodes and Playmaker states to call your activators! (uScript & Playmaker sold separately by their respective owners) –Mobile Support – Support for building to mobile devices using the Touch Gestures (PC/Mac, Android and iOS Supported) –Unity2D Compatible– Select a Movement Component Type to build in 3D or 2D officially supporting Unity Sprites that work with Mecanim –Sequence-Activators– Activate inputs in a sequence that trigger animations (eg. Down>Forward>P) for advanced combo creations –Sync-Activators– Combine inputs to trigger simultaneously (eg. A+S pressed together) for advanced combo creations –Multiplayer Support– Setup multiplayer gameplay by calling GAC on specific gameobjects –Delayed Animations– Create combos that link into animations when input event is delayed (eg. A, A…(wait)…A) –Mecanim Support– Add your animation states to use for combos. GAC plays well with states setup for other uses (Idle, Run etc) –Activator Setup Menu– Scripting basic events not required, do activator input setups through the inspector –Animation Linking – Combine your attack animations to create cool combos –Affect Specific Gameobject Layers – Customize what your animations can affect –Preview Animation Combo Setups with the intuitive GAC-PAC (Preview Animation Combos) Window –Full Source Code Access!
好的,这是一道经典的单调栈问题。题目描述如下: 有 $n$ 个湖,第 $i$ 个湖有一个高度 $h_i$。现在要在这些湖之间挖一些沟渠,使得相邻的湖之间的高度差不超过 $d$。请问最少需要挖多少个沟渠。 这是一道单调栈的典型应用题。我们可以从左到右遍历湖的高度,同时使用一个单调栈来维护之前所有湖的高度。具体来说,我们维护一个单调递增的栈,栈中存储的是湖的下标。假设当前遍历到第 $i$ 个湖,我们需要在之前的湖中找到一个高度最接近 $h_i$ 且高度不超过 $h_i-d$ 的湖,然后从这个湖到第 $i$ 个湖之间挖一条沟渠。具体的实现可以参考下面的代码: ```c++ #include <cstdio> #include <stack> using namespace std; const int N = 100010; int n, d; int h[N]; stack<int> stk; int main() { scanf("%d%d", &n, &d); for (int i = 1; i <= n; i++) scanf("%d", &h[i]); int ans = 0; for (int i = 1; i <= n; i++) { while (!stk.empty() && h[stk.top()] <= h[i] - d) stk.pop(); if (!stk.empty()) ans++; stk.push(i); } printf("%d\n", ans); return 0; } ``` 这里的关键在于,当我们遍历到第 $i$ 个湖时,所有比 $h_i-d$ 小的湖都可以被舍弃,因为它们不可能成为第 $i$ 个湖的前驱。因此,我们可以不断地从栈顶弹出比 $h_i-d$ 小的湖,直到栈顶的湖高度大于 $h_i-d$,然后将 $i$ 入栈。这样,栈中存储的就是当前 $h_i$ 左边所有高度不超过 $h_i-d$ 的湖,栈顶元素就是最靠近 $h_i$ 且高度不超过 $h_i-d$ 的湖。如果栈不为空,说明找到了一个前驱湖,答案加一。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值