F-Logo Turtle codeforces

F - Logo Turtle
Time Limit:2000MS    Memory Limit:262144KB    64bit IO Format:%I64d & %I64u

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

Input
FT
1
Output
2

题解:
岐哥的代码。。
假设一开始的起点在n位置,然后最左就移动到1,最右就移动到2n
dp[i][j][k][l]表示前i个命令,用了j次机会,当前停留在k,往l方向走。
一个一个命令考虑过去,然后对于每个命令,可以选择改变或者不改变,然后当前的决策对于以后的决策产生的影响就是当前弄完后,停在了哪里,正朝着哪个方向。
这个dp就是把 位置,朝向,使用了几次改变机会 这些信息整合起来,以供下一个阶段的决策。
/***********************************************
 * 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"
*/


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值