CodeForces 132C Logo Turtle (记忆化搜索)
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 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.
Sample Input
FT 1
2
FFFTFFF 2
6
Hint
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.
题解:给一个序列,有TF,F代表机器人向前走一步,T代表机器人转身,再给你一个N问改变N个字母后机器人最多可以走多远,每个字母可以改变多次;刚开始写了下没用记忆化搜索果断超时了,而且还不知道怎么把一个字母改变多次,然后看了下大神的写法,加个数组记忆下就好了,关于字母可以改变多次,直接循环N,N-=2就好了;
dp[2][MAXN<<1][MAXN][50],MAXN<<1是因为当前位置可能为负数;
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<map>
#include<string>
using namespace std;
const int INF=0x3f3f3f3f;
#define SI(x) scanf("%d",&x)
#define PI(x) printf("%d",x)
#define P_ printf(" ")
#define mem(x,y) memset(x,y,sizeof(x))
const int MAXN=;
char s[MAXN];
int dp[][MAXN<<][MAXN][];
int dfs(int fw,int pos,int l,int n){
if(n<)return ;
if(!s[pos])
return n>?:abs(l);
if(dp[fw][l+][pos][n]!=-)return dp[fw][l+][pos][n];
return dp[fw][l+][pos][n]=max(dfs(fw,pos+,fw?l+:l-,n-(s[pos]=='T')),dfs(-fw,pos+,l,n-(s[pos]=='F')));
}
int main(){
int n;
while(~scanf("%s",s)){
SI(n);
mem(dp,-);
int ans=;
for(;n>=;n-=)ans=max(ans,dfs(,,,n));
printf("%d\n",ans);
}
return ;
}
CodeForces 132C Logo Turtle (记忆化搜索)的更多相关文章
- CodeForces 173C Spiral Maximum 记忆化搜索 滚动数组优化
Spiral Maximum 题目连接: http://codeforces.com/problemset/problem/173/C Description Let's consider a k × ...
- CodeForces 398B 概率DP 记忆化搜索
题目:http://codeforces.com/contest/398/problem/B 有点似曾相识的感觉,记忆中上次那个跟这个相似的 我是用了 暴力搜索过掉的,今天这个肯定不行了,dp方程想了 ...
- CodeForces 918D MADMAX(博弈+记忆化搜索)
time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...
- Codeforces 667C Reberland Linguistics 记忆化搜索
链接 Codeforces 667C Reberland Linguistics 题意 给你一个字符串,除去前5个字符串后,使剩下的串有长度为2或3的词根组成,相邻的词根不能重复.找到所有的词根 思路 ...
- codeforces 132C Logo Turtle(dp)
可以用三维dp来保存状态, dp[i][j][k]表示在前i个字符变换了j步之后方向为k(k = 1 or k = 0)的最优解,也就是离原点的最大距离.这里规定0方向为正方向,1位负方向,表示的是当 ...
- Codeforces #564div2 E1(记忆化搜索)
虽然不是正解但毕竟自己做出来了还是记下来吧- 对每个人分别dfs得到其期望,某两维的组合情况有限所以Hash一下避免了MLE. #include <cstdio> #include < ...
- Codeforces Gym 100231G Voracious Steve 记忆化搜索
Voracious Steve 题目连接: http://codeforces.com/gym/100231/attachments Description 有两个人在玩一个游戏 有一个盆子里面有n个 ...
- Codeforces Round #336 (Div. 2) D. Zuma 记忆化搜索
D. Zuma 题目连接: http://www.codeforces.com/contest/608/problem/D Description Genos recently installed t ...
- Educational Codeforces Round 1 E. Chocolate Bar 记忆化搜索
E. Chocolate Bar Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/598/prob ...
随机推荐
- [转]UltraISO制作U盘启动盘安装Win7/9/10系统攻略
原文地址:http://www.cnblogs.com/pchmonster/p/4716708.html U盘安装好处就是不用使用笨拙的光盘,光盘还容易出现问题,无法读取的问题.U盘体积小,携带方便 ...
- VC++6.0出现no compile tool is associated with the extension.解决方法
对于刚解除VC++6.0的小白,在编译时候经常出现下图的错误提示: 解释为:不能编译此BmpRot.h文件,没有合适的编译工具可以编译此扩展名的文件. 很明显,当然只有.cpp文件才能编译. .h头文 ...
- 怎样使用LaTeX输入葡萄牙语等语言中的特殊字符
论文中引用了大名鼎鼎ER random graph model,但是这两位的名字不太好打,发现Google Scholar中直接下载的bib文件中也是错的.找了一会,发现转义字符已经定义得很好了.只是 ...
- 异常:ERROR [org.hibernate.proxy.BasicLazyInitializer] - CGLIB Enhancement failed...
ERROR [org.hibernate.proxy.BasicLazyInitializer] - CGLIB Enhancement failed: com.movie.类 放到lib 包下 \W ...
- CDH 1、CDH简介
1.Apache Hadoop 不足之处 • 版本管理混乱 • 部署过程繁琐.升级过程复杂 • 兼容性差 • 安全性低 2.Hadoop 发行版 • Apache Hadoop • Cloudera’ ...
- Unity 读取Excel
游戏有大多数配置文件,比如玩家等级,游戏商店信息等等.通常情况下把这些放入excel中来读取 第一种解决方案: xlsx –> csv –> 改变成UTF-8 或者Unicode编码 –& ...
- 点滴记录——Centos 6.5 yum安装Ganglia
转载请说明出处:http://blog.csdn.net/cywosp/article/details/39701141 注:下面操作都仅仅是在一台机器上操作 1. 安装php支持 yum inst ...
- 【剑指offer】Q40:数组中出现一次的数
书里面关于分类的推断有些麻烦,通过某一位为0为1来对数组元素进行分类.假如第3位为1.那么也就是元素x & 8 等于或不等于0,所以不是必需非的用第几位去推断. def once(array) ...
- Unity NGUI 血条制作
NGUI 血条制作步骤 实现过程: 模拟血条的变化当点击按钮Button是血条会实时发生变化. 1.向Unity中导入NGUI2.6.3.unitypackage 点击create your ui 后 ...
- 在Linux系统上限制远程登录的IP
在Linux系统上限制远程登录的IP,使用系统自带的配置文件. /etc/hosts.allow /etc/hosts.deny 匹配原则 先allow 后deny.