CF132C 题解
思路
看见
因为可以反复修改,所以在统计答案时不必修改次数
代码
# include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair <int, int> pii;
int n, k, maxx;
string a;
bitset <2> vis[105][205][55];
void dfs (int step, int now, int use, int dir) {
if (use > k || vis[step][now + 100][use][dir])
return ;
vis[step][now + 100][use][dir] = 1;
if (step >= n) {
if ((use ^ k ^ 1) & 1)
maxx = max (maxx, abs (now));
return ;
}
if (a[step] == 'F')
dfs (step + 1, now + dir, use, dir), dfs (step + 1, now, use + 1, -dir);
else
dfs (step + 1, now, use, -dir), dfs (step + 1, now + dir, use + 1, dir);
return ;
}
int main () {
ios::sync_with_stdio (0);
cin.tie (0);
cout.tie (0);
cin >> a >> k;
n = a.size ();
dfs (0, 0, 0, 1);
cout << maxx;
return 0;
}