C. Logo Turtle
 

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 exactly n commands from the list (one command can be changed several times). How far from the starting point can the turtle move after it follows all the commands of the modified list?

Input

The first line of input contains a string commands — the original list of commands. The string commands 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 follows all the commands of the modified list.

Examples
input
FT
1
output
2
Note

In the first example the best option is to change the second command ("T") to "F" — this way the turtle will cover a distance of 2 units.

In the second example you have to change two commands. One of the ways to cover maximal distance of 6 units is to change the fourth command and first or last one.

题意:

  给你一串由FT构成的串,F代表前进,T代表转向,初始方向是1,转向后F由1变为-1(或者-1变成1)

  给你一个n,意思是你也可以在任意位置的字符改变任意次数但总和不要超过n次的情况下,其状态即F变T,T变F;

  问你改变n次后,最后机器人能走到的最远距离

题解:

  范围很小

  设定DP[i][j][k][0/1]表示在走完i个字符使用j次变化,在k位置,方向0/1是否能够达成

  算好复杂度,无脑暴力怼它的转移.

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
typedef long long LL;
const int N=1e6+,mod=,inf=2e9+; int dp[][][][],m,n;
char a[N];
int main() {
scanf("%s%d",a+,&m);
n = strlen(a+);
dp[][][][] = ;
for(int i = ; i < n; ++i) {
for(int j = ; j <= m; ++j) {
for(int k = ; k < ; ++k) {
for(int cnt = ; cnt <= m - j; ++cnt) {
if(a[i+] == 'T') {
if(cnt%) {
dp[i+][j+cnt][k+][] |= dp[i][j][k][];
if(k->=)dp[i+][j+cnt][k-][] |= dp[i][j][k][];
}
else {
dp[i+][j+cnt][k][] |= dp[i][j][k][];
dp[i+][j+cnt][k][] |= dp[i][j][k][];
}
}
else {
if(cnt%) {
dp[i+][j+cnt][k][] |= dp[i][j][k][];
dp[i+][j+cnt][k][] |= dp[i][j][k][];
}
else {
dp[i+][j+cnt][k+][] |= dp[i][j][k][];
if(k->=)dp[i+][j+cnt][k-][] |= dp[i][j][k][];
}
}
}
}
}
}
int ans = ;
for(int j = ; j < ; ++j) {
if(dp[n][m][j][]) ans = max(ans,abs(j-));
if(dp[n][m][j][]) ans = max(ans,abs(j-));
}
cout<<ans<<endl;
return ;
}

最新文章

  1. Thymeleaf 集成spring
  2. SQLite数据库与Contentprovider(1)
  3. C++ CheckBox_Porerty
  4. Win7/Win8.1预订升级Win10失败临时解决方案
  5. 定位 - CoreLocation - 区域报警
  6. 【POJ2912】【并查集】Rochambeau
  7. 《University Calculus》-chape4-导数的应用-洛必达法则
  8. jquery 做出专业的界面,SHOW 一下最近的成果~~~
  9. iOS8指纹识别TouchID
  10. 移动端解决input focus后键盘弹出,高度被挤压的问题
  11. 两层嵌套的JSON包的解法
  12. Java对象的创建 —— new之后JVM都做了什么?
  13. Spring MVC 使用介绍(三)—— Controller接口控制器
  14. SQL 入门了解
  15. Python单元测试unittest【转自https://www.cnblogs.com/feng0815/p/8045850.html】
  16. 阅读笔记4 我是一只IT小小鸟
  17. 采用镜像的方法安装python第三方库
  18. bzoj1047&amp;bzoj1012
  19. SSH协议
  20. 深度学习开源库tiny-dnn的使用(MNIST)

热门文章

  1. ffmpeg处理网络流
  2. 15Spring AOP基础
  3. 查看FPM在你的机子上的平均内存占用情况
  4. Win2008 Server MySql安装包详细安装教程
  5. Python解释器的种类以及特点
  6. Discuz论坛广告横幅大图在百度app内无法显示,百度app默认开启了广告屏蔽
  7. AutoMapper 使用总结1
  8. Leetcode 142.环形链表II
  9. 九度oj 题目1182:统计单词
  10. 测试各种低价VPS