居然没有我这个神秘做法吗?
我们把最终串 F 设为 ,T 设为 ,那么错误数量就是 , 是最大子段和。
然后你考虑枚举这个最大子段和,设 表示考虑到第 个位置,以 结尾的最大子段和长度为 。
如果 能填 F,就有转移 。如果 能填 T,就有转移 ,记得取 ,转移的时候要保证 。
答案为 ,复杂度 。
code
#include<bits/stdc++.h>
#define qwe(i,l,r) for(int i=l;i<=r;++i)
#define ewq(i,l,r) for(int i=l;i>=r;--i)
#define qwex(i,x) for(int i=(x);i;i=ne[i])
#define mkp make_pair
#define You_have_no_eggs 0
#define fir first
#define sec second
//#define int ll
#define ll long long
#define pii pair<int,int>
using namespace std;
inline int read(){
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){
f=ch=='-'?-f:f;
ch=getchar();
}
while(ch>='0'&&ch<='9'){
x=(x<<3)+(x<<1)+(ch^48);
ch=getchar();
}
return f*x;
}
const int N=505+5,mod=998244353;
char s[N];
int n,f[N][N],b[N][N];
void Solve(){
int ans=0;
n=read();
scanf("%s",s+1);
qwe(m,0,n){
qwe(i,0,n+1)qwe(j,0,n+1)f[i][j]=-1e9;
f[0][0]=0;
qwe(i,1,n){
qwe(j,0,m){
if(s[i]!='T'&&j<m){
f[i][j+1]=max(f[i][j+1],f[i-1][j]+1);
}
if(s[i]!='F'){
int jj=max(0,j-1);
f[i][jj]=max(f[i][jj],f[i-1][j]);
}
}
}
qwe(i,0,m)ans=max(ans,f[n][i]-m);
}
cout<<ans<<'\n';
}
signed main(){
int T=read();
while(T--)Solve();
} :::