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. js页面文字选中后分享实现

    var $sinaMiniBlogShare = function(eleShare, eleContainer) { var eleTitle = document.getElementsByTag ...

  2. BNUOJ 26228 Juggler

    Juggler Time Limit: 3000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 42 ...

  3. codeforces 359A

    #include<stdio.h> #define N 60 int map[N][N]; int main() {  int n,m,i,j,flag;  while(scanf(&qu ...

  4. 乱记结论之OI常用四大数列

    一.斐波那契数列 $f(0)=1,f(1)=1,f(i)=f(i-1)+f(i-2) \ \ \ \ (i>=2)$ 经典的解释是兔子生小孩,第0年一对兔子,一对兔子需要一年长大,后面每年都生小 ...

  5. Linux下汇编语言学习笔记10 ---

    这是17年暑假学习Linux汇编语言的笔记记录,参考书目为清华大学出版社 Jeff Duntemann著 梁晓辉译<汇编语言基于Linux环境>的书,喜欢看原版书的同学可以看<Ass ...

  6. hdu - 1565 方格取数(1) && 1569 方格取数(2) (最大点权独立集)

    http://acm.hdu.edu.cn/showproblem.php?pid=1565 两道题只是数据范围不同,都是求的最大点权独立集. 我们可以把下标之和为奇数的分成一个集合,把下标之和为偶数 ...

  7. HDU——1498 50 years, 50 colors

    50 years, 50 colors Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  8. c内存分配(转)

    图示 C内存分配 程序代码区 存放函数体的二进制代码 全局数据区 全局变量和静态变量的存储是放在一起的.初始化的全局变量和静态变量在一块区域,未初始化的全局变量和未初始化的静态变量在相邻的另一块区域. ...

  9. wget下载网络图片

    wget http.......  --no-check-certificate

  10. 移动端 js 实现图片上传 预览

    方法一: <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv=&q ...