题目链接:https://ac.nowcoder.com/acm/contest/70/E
题目大意:
略
分析:
DP或记忆化搜索,个人觉得记忆化搜索比较好做,逻辑清晰,代码量少
代码如下:
#include <bits/stdc++.h>
using namespace std; #define rep(i,n) for (int i = 0; i < (n); ++i)
#define For(i,s,t) for (int i = (s); i <= (t); ++i)
#define rFor(i,t,s) for (int i = (t); i >= (s); --i)
#define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)
#define rforeach(i,c) for (__typeof(c.rbegin()) i = c.rbegin(); i != c.rend(); ++i) #define pr(x) cout << #x << " = " << x << " "
#define prln(x) cout << #x << " = " << x << endl #define LOWBIT(x) ((x)&(-x)) #define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin()) #define ms0(a) memset(a,0,sizeof(a))
#define msI(a) memset(a,inf,sizeof(a))
#define msM(a) memset(a,-1,sizeof(a)) inline int gc(){
static const int BUF = 1e7;
static char buf[BUF], *bg = buf + BUF, *ed = bg; if(bg == ed) fread(bg = buf, , BUF, stdin);
return *bg++;
} inline int ri(){
int x = , f = , c = gc();
for(; c<||c>; f = c=='-'?-:f, c=gc());
for(; c>&&c<; x = x* + c - , c=gc());
return x*f;
} typedef long long LL;
typedef unsigned long long uLL;
const double EPS = 1e-;
const int inf = 1e9 + ;
const LL mod = 1e9 + ;
const int maxN = 1e5 + ;
const LL ONE = ; int n, m, ans;
string c;
// dp[i][j][k][0/1]表示已进行了i次指令,已修改了j次指令,当前走到k位置,朝向为前(后)这种情况能否到达
// dp[i][j][k][0/1] = 1表示能到达,dp[i][j][k][0/1] = 0表示不能
// 为避免负数问题,k初始值为100
int dp[][][][]; inline void dfs(int x, int y, int z, int d) {
if(x > m || y > n || dp[x][y][z][d]) return;
dp[x][y][z][d] = ; if(c[x] == 'F') {
// 修改
dfs(x + , y + , z, !d);
// 不修改
dfs(x + , y, z + (d ? - : ), d);
}
if(c[x] == 'T') {
// 修改
dfs(x + , y + , z + (d ? - : ), d);
// 不修改
dfs(x + , y, z, !d);
}
} int main(){
cin >> c >> n;
m = c.size(); dfs(, , , ); rep(k, ) {
if(dp[m][n][k][] || dp[m][n][k][]) ans = max(ans, abs(k - ));
} cout << ans << endl;
return ;
}