There is a machine turtle on a number axis, which can follow the instructions people give it to perform a step forward (F) and backward (t) operation. Given the initial operation, you have the right to modify the n-step command (each command can be modified many times). Ask the maximum distance from the source after you modify the n-step command.
Practice: a very intuitive idea. It is also a greedy practice to convert t to F as much as possible.
We use DFS (I, j, T, cur), where I represents the serial number of the current traversal command, and j Represents from 0 ~ How many t I converted during the I process, t indicates from 0 ~ The total number of times t occurs in the I sequence number. cur indicates the distance (positive and negative) from the current origin, DP [I] [de] [J] [cur], among them, I indicates the serial number of the current traversal command, and De indicates whether the tortoise is in the current state to the left or to the right (initially to the right). J indicates the number of operations remaining in the current state, cur indicates the distance between the current status and the origin. In the DP array,
If the four values are determined, we will continue to search for the results.So you can use this to perform a memory-based search.
In fact, there is still a problem left over. We are greedy to convert t to F as much as possible, so the number of T will certainly be less than the number of operands. In this case, we can calculate the number of operations that are not transferred. Because each step can be modified many times, if the remaining number is an even number, the result will not be affected. If the remaining number is an odd number, then we have to change the leftmost or rightmost F to T (the last calculated distance is reduced by one ).
Dynamic Programming is generally written in two ways. One is direct push (for loop), the other is write recursive function (DFS), and uses the DP array for memory-based search. When I first got this question, I first thought about direct push. Later I wrote something wrong. I switched to the DFS Writing Method and used a four-dimensional array for memory-based search.
Code:
# Include <iostream> # include <cstdio> # include <cstring> # include <cmath> # include <algorithm> # define n 51 using namespace STD; int DP [N * 2] [2] [N] [N * 4]; char a [n * 2]; int N, Len, ans; int DFS (INT idx, int J, int T, int cur) {If (j> N) return 0; If (idx = Len) {ans = max (ANS, ABS (cur) -(n-j) & 1); // if the remainder of the (n-j) operand is an odd number, subtract one return ans ;} int & res = DP [idx] [(t-j) & 1] [n-J] [cur + 100]; If (res! =-1) return res; // The Memory search if (a [idx] = 'F') return res = DFS (idx + 1, J, T, cur + (t-j) & 1 )? -); // If F is used, if (a [idx] = 'T') {return res = max (DFS (idx + 1, J, t + 1, cur), DFS (idx + 1, J + 1, t + 1, cur + (t-j) & 1 )? -); // Conversion and non-conversion} int main () {memset (DP,-1, sizeof DP); scanf ("% s ", a); Len = strlen (a); scanf ("% d", & N); DFS (0, 0, 0); cout <ans <Endl; return 0 ;}
This is a longer code .. Later, in order to climb the first page of solution size, I dropped the code to 15 lines... Paste it to commemorate the ghost ( ̄) ghost
Code:
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;int n,len,ans,dp[102][2][51][202];char a[102];int dfs(int idx,int j,int t,int cur){ if(j>n) return 0;if(!a[idx]) return ans=max(ans,(cur>=0?cur:-cur)-((n-j)&1)); if(dp[idx][(t-j)&1][n-j][cur+100]!=-1) return dp[idx][(t-j)&1][n-j][cur+100]; if(a[idx]=='F') return dp[idx][(t-j)&1][n-j][cur+100]=dfs(idx+1,j,t,cur+(((t-j)&1)?-1:1)); if(a[idx]=='T') return dp[idx][(t-j)&1][n-j][cur+100]=max(dfs(idx+1,j,t+1,cur),dfs(idx+1,j+1,t+1,cur+(((t-j)&1)?-1:1)));}int main(){ memset(dp,-1,sizeof dp);scanf("%s%d",a,&n);dfs(0,0,0,0),printf("%d\n",ans); return 0;}
Codeforces 132c. Vasya and beautiful arrays [DP, DFS]