codeforces1409F Subsequences of Length Two

https://codeforces.com/contest/1409/problem/F

Fuck, I can't do this after more than 500 people, I wrote a 100-line n^3 greedy preprocessing and then n^3dp keeps on wa

In fact, let dp[i][j][k] be the first i position. After j modification, there are k most logarithms of t[0], so the state is very easy to transfer

If s[i+1]==t[0], then either maintain it as t[0], or let it become t[1]

If s[i+1]==t[1], either maintain it as t[1] or become t[0]

Otherwise, either remain unchanged, or change to t[0], or change to t[1]

When changing t[1], the answer+k, when changing t[0], k=k+1

#include<bits/stdc++.h>
using namespace std;

const int maxl=210;

int n,m,cnt,tot,ans;
int dp[maxl][maxl][maxl];
char s[maxl],ss[maxl],t[2];

inline void upd(int &a,int b)
{
	a=max(a,b);
}

inline void prework()
{
	scanf("%d%d",&n,&m);
	scanf("%s",s+1);
	scanf("%s",t);
}

inline void mainwork()
{
	ans=0;
	if(t[0]==t[1])
	{
		int cnta=0;
		for(int i=1;i<=n;i++)
		if(s[i]==t[0])
			cnta++;
		int mx=min(n,cnta+m);
		ans=mx*(mx-1)/2;
		return;
	}
	memset(dp,-1,sizeof(dp));
	dp[0][0][0]=0;
	for(int i=0;i<n;i++)
		for(int j=0;j<=m;j++)
			for(int k=0;k<=n;k++)
			if(dp[i][j][k]>=0)
			{
				if(s[i+1]==t[0])
				{
					upd(dp[i+1][j][k+1],dp[i][j][k]);
					upd(dp[i+1][j+1][k],dp[i][j][k]+k);
				}
				else if(s[i+1]==t[1])
				{
					upd(dp[i+1][j][k],dp[i][j][k]+k);
					upd(dp[i+1][j+1][k+1],dp[i][j][k]);
				}
				else
				{
					upd(dp[i+1][j][k],dp[i][j][k]);
					upd(dp[i+1][j+1][k],dp[i][j][k]+k);
					upd(dp[i+1][j+1][k+1],dp[i][j][k]);
				}
			} 
	for(int j=0;j<=m;j++)
		for(int k=0;k<=n;k++)
			upd(ans,dp[n][j][k]);
}

inline void print()
{
	printf("%d",ans);
}

int main()
{
	prework();
	mainwork();
	print();
	return 0;
}

 

Guess you like

Origin blog.csdn.net/liufengwei1/article/details/108414479