D一道很典型的区间DP
//区间DP典题
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 520;
ll n, L, R;
string s;
ll sum0[N], sum1[N];
ll f[N][N];
void solve()
{
cin >> n >> L >> R >> s;
s = ' ' + s;
for (int i = 1; i <= n; i++)
{
sum0[i] = sum0[i - 1] + (s[i] == '0');
sum1[i] = sum1[i - 1] + (s[i] == '1');
}
for (int len = 1; len <= n; len++)//枚举长度
{
for (int l = 1; l + len - 1 <= n; l++)//枚举左端点
{
if (len == 1)continue;
int r = l + len - 1;
for (int k = l; k < r; k++)
{
int x = abs(sum0[k] - sum0[l - 1] - sum1[r] + sum1[k]);
if (L <= x && x <= R)f[l][r] = max(f[l][r], f[l][k] + f[k+1][r] + 1);
}
}
}
cout << f[1][n] << endl;
}
int main()
{
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
int t=1;
//cin >> t;
while (t--)
{
solve();
}
return 0;
}
标签:98,int,ll,long,牛客,solve,补题,DP
From: https://blog.csdn.net/m0_74237834/article/details/140492152