A string composed of FT is given. f Represents Forward, t represents turn, and the initial direction is 1. After turning, F changes from 1 to-1 (or-1 to 1)
For example, the value of ftfffffff is 3.
Given N, you must perform n operations (Change F to T or change t to F) to find how far forward from the original place (absolute value)
You can search to solve this problem.
For each trip, we can choose
Continue in the current direction
Or change the direction.
In a rough estimation, the maximum length of a string is 100, which is 2 ^ 100,High complexity
But for the four variables
In the current direction, how far is the current process? The number of remaining operations in the first few itineraries
The maximum distance that can be taken is determined.
Then you can perform a memory-based search.
# Include <cstdio> # include <iostream> # include <algorithm> # include <cstring> # include <cmath> using namespace STD; char STR [111]; int DP [2] [222] [111] [55]; int m; int DFS (INT Dir, int POs, int I, int left) {If (! STR [I] & left) return 0; // return 0if (DP [dir] [POS + 100] [I] [left]! =-1) return DP [dir] [POS + 100] [I] [left]; If (! STR [I] &! Left) return DP [dir] [POS + 100] [I] [left] = ABS (POS ); // return posint mv = (DIR = 1 )? 1:-1; if (Left = 0) DP [dir] [POS + 100] [I] [left] = DFS (Dir, POS + mV, I + 1, left-(STR [I]! = 'F'); // otherwise, return DP [dir] [POS + 100] [I] [left] = max (DFS (Dir, pos + mV, I + 1, left-(STR [I]! = 'F'), DFS (! Dir, POs, I + 1, left-(STR [I]! = 'T');} int main () {# ifndef online_judgefreopen ("/home/raw.96/in.txt", "r", stdin); # endifmemset (DP, -1, sizeof (DP); CIN> STR> m; int ans = 0; For (INT Mm = m; MM> = 0; Mm-= 2) {ans = max (ANS, DFS (1,0, 0, MM); // cout <"A:" <ans <Endl; /// cout <mm <Endl;} cout <ans <Endl; return 0 ;}
Cf 132c logo turtle [DP + Memory search]