Description
A lot of people associate Logo programming language with turtle graphics. In this case the turtle moves along the straight line and accepts commands "T" ("turn around") and "F" ("move 1 unit forward").
You are given a list of commands that will be given to the turtle. You have to change exactlyn commands from the list (one command can be changed several times). How far from the starting point can the turtle move after it followsall the commands of the modified list?
Input
The first line of input contains a string commands — the original list of commands. The stringcommands contains between 1 and 100 characters, inclusive, and contains only characters "T" and "F".
The second line contains an integer n (1 ≤ n ≤ 50) — the number of commands you have to change in the list.
Output
Output the maximum distance from the starting point to the ending point of the turtle's path. The ending point of the turtle's path is turtle's coordinate after it followsall the commands of the modified list.
Sample Input
FT 1
2
题解:
岐哥的代码。。
假设一开始的起点在n位置,然后最左就移动到1,最右就移动到2n
一个一个命令考虑过去,然后对于每个命令,可以选择改变或者不改变,然后当前的决策对于以后的决策产生的影响就是当前弄完后,停在了哪里,正朝着哪个方向。
/***********************************************
* Author: fisty
* Created Time: 2015/2/4 19:21:43
* File Name : F.cpp
*********************************************** */
#include<cstdio>
#include<cstring>
#include<cstdlib>
bool dp[110][55][210][2];
char s[110];
inline void Max(int &a, int b) {
if (b > a)
a = b;
}
int Gao(char s, int k, bool flag, int dir) {
if (flag)
s = 'F' + 'T' - s;
if (s == 'F') {
if (dir == 1)
return k + 1;
else
return k - 1;
} else {
return k;
}
}
int main() {
int m;
scanf("%s%d", s + 1, &m);
int n = strlen(s + 1);
// printf("n=%d\n", n);
dp[0][0][n][1] = true;
//dp[0][0][n][0] = true;
for (int i = 0; i < n; i++) {
for (int j = 0; j <= m; j++) {
for (int k = 0; k <= 2 * n; k++) {
for (int dir = 0; dir < 2; dir++) {
if (!dp[i][j][k][dir])
continue;
int d1=dir,d2=dir;
if(s[i+1] == 'T') d1=!dir;
dp[i + 1][j][Gao(s[i + 1], k, 0, dir)][d1] = true;
if (j < m)
{
if(s[i+1] == 'F') d2 = !dir;
dp[i + 1][j + 1][Gao(s[i + 1], k, 1, dir)][d2] = true;
}
}
}
}
}
/* for(int i=1;i<=n;i++) {
for(int j=0;j<=i;j++) {
for(int k=0;k<=2*n;k++) {
for(int dir=0;dir<2;dir++){
if(dp[i][j][k][dir]) {
printf("%d %d %d %d\n",i,j,k,dir);
}
}
}
}
}*/
int ans = 0;
for (int i = m; i >= 0; i -= 2) {
for (int j = 0; j <= 2 * n; j++) {
for (int dir = 0; dir < 2; dir++) {
if (dp[n][i][j][dir]) {
Max(ans, abs(j - n));
}
}
}
}
printf("%d\n", ans);
return 0;
}
/*
1
Time: 15 ms, memory: 2460 KB
Verdict: OK
Input
FT
1
Output
2
Answer
2
Checker comment
ok 1 number(s): "2"
2
Time: 15 ms, memory: 2460 KB
Verdict: OK
Input
FFFTFFF
2
Output
6
Answer
6
Checker comment
ok 1 number(s): "6"
3
Time: 15 ms, memory: 2460 KB
Verdict: OK
Input
F
1
Output
0
Answer
0
Checker comment
ok 1 number(s): "0"
4
Time: 15 ms, memory: 2460 KB
Verdict: OK
Input
FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
50
Output
100
Answer
100
Checker comment
ok 1 number(s): "100"
*/