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 ;
}

Codeforces Beta Round #96 (Div. 1) C. Logo Turtle DP的更多相关文章

  1. Codeforces Beta Round #96 (Div. 1) C. Logo Turtle —— DP

    题目链接:http://codeforces.com/contest/132/problem/C C. Logo Turtle time limit per test 2 seconds memory ...

  2. Codeforces Beta Round #96 (Div. 2) E. Logo Turtle dp

    http://codeforces.com/contest/133/problem/E 题目就是给定一段序列,要求那个乌龟要走完整段序列,其中T就是掉头,F就是向前一步,然后开始在原点,起始方向随意, ...

  3. Codeforces Beta Round #96 (Div. 1) D. Constants in the language of Shakespeare 贪心

    D. Constants in the language of Shakespeare Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codef ...

  4. Codeforces Beta Round #96 (Div. 2) (A-E)

    写份DIV2的完整题解 A 判断下HQ9有没有出现过 #include <iostream> #include<cstdio> #include<cstring> ...

  5. Codeforces Beta Round #77 (Div. 2 Only)

    Codeforces Beta Round #77 (Div. 2 Only) http://codeforces.com/contest/96 A #include<bits/stdc++.h ...

  6. Codeforces Beta Round #80 (Div. 2 Only)【ABCD】

    Codeforces Beta Round #80 (Div. 2 Only) A Blackjack1 题意 一共52张扑克,A代表1或者11,2-10表示自己的数字,其他都表示10 现在你已经有一 ...

  7. Codeforces Beta Round #83 (Div. 1 Only)题解【ABCD】

    Codeforces Beta Round #83 (Div. 1 Only) A. Dorm Water Supply 题意 给你一个n点m边的图,保证每个点的入度和出度最多为1 如果这个点入度为0 ...

  8. Codeforces Beta Round #79 (Div. 2 Only)

    Codeforces Beta Round #79 (Div. 2 Only) http://codeforces.com/contest/102 A #include<bits/stdc++. ...

  9. Codeforces Beta Round #76 (Div. 2 Only)

    Codeforces Beta Round #76 (Div. 2 Only) http://codeforces.com/contest/94 A #include<bits/stdc++.h ...

随机推荐

  1. Python 1-3区分Python文件的两种用途和模块的搜索路径

    区分Python文件的两种用途 run.py文件: import m1 m1.py文件: def f1(): print('f1') def f2(): print('f2') #当文件被执行时__n ...

  2. 开发基本的php框架

    github路径:https://github.com/zhengchuzhou/easyPhpFramework 一.目录结构及用途 二.相关代码: 1.入口文件(index.php): <? ...

  3. win10永久激活

    现在我们可以看下当前系统的激活状态,查看方法"WIN+R"打开运行对话框,输入命令slmgr.vbs -xpr,点击确定,这样可以查看到当前系统的激活信息.大家可以发现,虽然小编系 ...

  4. Tensor数据类型

    目录 Tensor数据类型 属性 数据类型判断 数据类型转换  tensor转numpy Tensor数据类型 list: [1,1.2,'hello'] ,存储图片占用内存非常大 np.array, ...

  5. 去面试Python工程师,这几个基础问题一定要能回答,Python面试题No4

    今天的面试题以基础为主,去面试Python工程师,这几个基础问题不能答错 第1题:列表和元组有什么不同? 列表和元组是Python中最常用的两种数据结构,字典是第三种. 相同点: 都是序列 都可以存储 ...

  6. noi.ac NOIP2018 全国热身赛 第二场 T3 color

    [题解] 我们可以发现每次修改之后叶子结点到根的路径最多分为两段:一段白色或者黑色,上面接另一段灰色的.二分+倍增找到分界点,然后更新答案即可. check的时候只需要判断当前节点对应的叶子结点的区间 ...

  7. python之字典 2014-4-5

    #字典:当索引不好用时1.字典 类似于php的关联数组 列表类似于索引数组 2.创建字典 phonebook={'alice':'2100','tom':'1900'} 键值之间用: 项之间用, 空字 ...

  8. git-svn操作

    1.git svn clone --username=chenzheng   http://10.0.0.178/repos/trunk/hxqcgf/auto_accessories.admin.h ...

  9. B. Mr. Kitayuta's Colorful Graph,二维并查集,一个简单变形就可以水过了~~

    B. Mr. Kitayuta's Colorful Graph ->  Link  <- 题目链接在上面,题目比较长,就不贴出来了,不过这是道很好的题,很多方法都可以做,真心邀请去A了这 ...

  10. [ C++ 快速高精度模板 ] [ BigN类 ] 大整数类 高精度 模板 BigInt FFT 快速傅里叶变换

    [原创 转载请注明]瞎写的,如果代码有错,或者各位大佬有什么意见建议,望不吝赐教 更新日志: 对于规模较小的整数乘法使用$$O(n^2)$$方法,提高速度 modify()和operator[]的bu ...