BZOJ2580-[Usaco2012 Jan]Video Game

题目描述

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?

给你个模式串(每个长度≤15,1≤N≤20),串中只含有“ABC”三种字母。求一长度为K(1≤K≤1000)的字符串,使得匹配数最大(重复匹配计多次),输出最大值。

输入

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

dp思想非常明显吧。看到多模式串匹配,AC自动机非常明显吧。结合一下,就做好了!

dp[i][j]表示长度为i时末节点编号为j的最大匹配数。

状态转移方程也比较好想。f[i+1][AC[j].son[t]]=max(f[i+1][AC[j].son[t]],f[i][j]+AC[AC[j].son[t]].val);

ans=max(dp[k][j])。

代码中的AC[i].val表示加入这个节点会增加多少个匹配数。

Code:

#include<bits/stdc++.h>
#define N 1005
using namespace std;
struct node
{
	int fail,son[5],val;
}AC[N];
int n,k,num,ans,Q[N],f[N][N];
char s[N];
void insert(char *s)
{
	int len=strlen(s);
	int now=0;
	for(int i=0;i<len;i++)
	{
		if(!AC[now].son[s[i]-'A'])AC[now].son[s[i]-'A']=++num;
		now=AC[now].son[s[i]-'A'];
	}
	AC[now].val++;
}
void getfail()
{
	int head=1,tail=0;
	AC[0].fail=0;
	for(int i=0;i<3;i++)
	{
		int u=AC[0].son[i];
		if(u!=0)
		{
			Q[++tail]=u;
			AC[u].fail=0;
		}
	} 
	while(head<=tail)
	{
		int now=Q[head];
		AC[now].val+=AC[AC[now].fail].val;
		for(int i=0;i<3;i++)
		{
			int u=AC[now].son[i];
			if(u!=0)
			{
				AC[u].fail=AC[AC[now].fail].son[i];
				Q[++tail]=u;
			}else AC[now].son[i]=AC[AC[now].fail].son[i];
		}
		head++;
	}
}
int main()
{
	scanf("%d%d",&n,&k); 
	for(int i=1;i<=n;i++) 
	{
		scanf("%s",s);
		insert(s);
	}
	getfail();
	memset(f,-0x3f,sizeof(f));
	f[0][0]=0;
	ans=0;
	for(int i=0;i<k;i++)
		for(int j=0;j<=num;j++)
			for(int t=0;t<3;t++)
				f[i+1][AC[j].son[t]]=max(f[i+1][AC[j].son[t]],f[i][j]+AC[AC[j].son[t]].val);
	for(int i=0;i<=num;i++)
		ans=max(ans,f[k][i]);
	printf("%d\n",ans);
	return 0;
}
好的,这是一道经典的单调栈问题。题目描述如下: 有 $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
发出的红包

打赏作者

JackflyDC

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值