题目在这里:点击打开链接

题意:

F表示前进一步,T表示变成反方向

给一串FT字符,和一个n,表示可以改变多少次,求可以走到的离原点最远的距离

改变就是F变成T、T变成F

关键:

dfs(int d,int pos,int i,int cnt)

dp[][][][] 依次表示,方向、最长距离、到字符串的哪一个点了、还剩多少改变次

因为你每到一步,下一步只有两种情况:

一种是方向改变,pos不变

一种个是方向不变,pos朝当前+1

两种情况的cnt 根据当前值是F还是T -0或者-1

哎╮(╯▽╰)╭我还是想不到这样定状态

感觉这样dfs里面dp的写法好奇怪。。但是自己不会写。。

参考别人那样写的。。好省代码

PS:

严重不爽!!

因为memset(dp,0,sizeof dp)一直超时!

memset(dp,-1,sizeof dp)就可以!

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<string>
using namespace std; int dp[5][205][105][55],n;
char s[105]; int dfs(int d,int pos,int i,int cnt)
{
if(cnt<0) return 0;
if(i>=strlen(s)) return cnt>0?0:abs(pos);
int &p=dp[d+1][pos+100][i][cnt];
if(p!=-1) return p;
p=max(dfs(d,pos+d,i+1,cnt-(s[i]!='F')),dfs((-1)*d,pos,i+1,cnt-(s[i]!='T')));
return p;
} int main()
{
while(~scanf("%s%d",s,&n))
{
memset(dp,-1,sizeof dp);
int ans=0;
while(n>=0)//n剩偶数个的时候 可以在一位上改变都抵消掉
{
ans=max(ans,dfs(1,0,0,n));
n-=2;
}
printf("%d\n",ans);
}
return 0;
}

codeforces 132C Logo Turtle--- dp dfs的更多相关文章

  1. codeforces 132C Logo Turtle(dp)

    可以用三维dp来保存状态, dp[i][j][k]表示在前i个字符变换了j步之后方向为k(k = 1 or k = 0)的最优解,也就是离原点的最大距离.这里规定0方向为正方向,1位负方向,表示的是当 ...

  2. CodeForces 132C Logo Turtle (记忆化搜索)

    Description A lot of people associate Logo programming language with turtle graphics. In this case t ...

  3. 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 ...

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

    C. Logo Turtle   A lot of people associate Logo programming language with turtle graphics. In this c ...

  5. CF#132 C. Logo Turtle DP

    C. Logo Turtle 题意 有一个海龟在一个x轴的0点,给出一个由'F','T'组成的字符序列. 海龟要按照这个序列进行行动,如果第i个字符为'F',表示沿当前方向走,'T'表示转身. 现在你 ...

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

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

  7. Codeforces Round #277 (Div. 2) D. Valid Sets (DP DFS 思维)

    D. Valid Sets time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  8. [CF132C] Logo Turtle

    [CF132C] Logo Turtle , Luogu A turtle moves following by logos.(length is \(N\)) \(F\) means "m ...

  9. BZOJ-4424 &&CodeForces-19E Fairy DP+dfs (Link-Cut-Tree可A)

    Va爷的胡策题T2 E. Fairy time limit per test1.5 seconds memory limit per test256 megabytes inputstandard i ...

随机推荐

  1. MongoDB实战指南(六):MongoDB复制集之复制集概述

    1. 复制集概述 数据库总是会遇到各种失败的场景,如网络连接断开.断电等,尽管journaling日志功能也提供了数据恢复的功能,但journaling通常是针对单个节点来说的,只能保证单节点数据的一 ...

  2. [codility]Falling-discs

    http://codility.com/demo/take-sample-test/omega2013 这题有点意思.首先经过思考,想到从底部往上扫,去迎接掉下来的disc.但这样仍然是不行的.后来看 ...

  3. ANDROID_MARS学习笔记_S01原始版_020_Mp3player001_歌曲列表

    一.项目设计 二.歌曲列表简介 1.利用java.net.HttpURLConnection以流的形式下载xml文件为String 2.自定义ContentHandler-->Mp3ListCo ...

  4. 总结一下SQL语句中引号(')、quotedstr()、('')、format()在SQL语句中的用法

    总结一下SQL语句中引号(').quotedstr().('').format()在SQL语句中的用法 日期:2005年6月1日 作者:seasky212 总结一下SQL语句中引号(').quoted ...

  5. 170. Two Sum III - Data structure design

    题目: Design and implement a TwoSum class. It should support the following operations: add and find. a ...

  6. APP-PER-50022: Oracle Human Resources could not retrieve a value for the User Type profile option.

    Symptoms ----------------------- AP > Setup > Organizations Show Error tips: APP-PER-50022: Or ...

  7. 用Remastersys定制自己的Ubuntu安装光盘

    这两天因为要做Ubuntu的平台移植,一直在给自己电脑上的Ubuntu系统装各种软件,其间几次将内核破坏,导致不得不重装系统,经过几次痛苦的等待,痛定思痛,决定试一试能不能将自己的Ubuntu系统定制 ...

  8. ArchLinux安装开源VMware Tools

    首先按照传统的Linux下安装VMware Tools的方法[1]]出现了很多的错误,安装过程完全没有办法进行下去.我在ArchLinux Wiki中看到这样一句说:VMware Tools for ...

  9. 在 ASP.NET MVC 中创建自定义 HtmlHelper

    在ASP.NET MVC应用程序的开发中,我们常碰到类似Html.Label或Html.TextBox这样的代码,它将在网页上产生一个label或input标记.这些HtmlHelper的扩展方法有些 ...

  10. GIS数据格式:Shapefile

    转自:http://lab.osgeo.cn/2449.html Shapefile是ESRI提出的数据格式,随着ArcView GIS 3.x发布,属于简单要素类.Shapefile由于其数据结构简 ...