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

题意:

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. 如何使用 Java8 实现观察者模式?(下)

    [编者按]本文作者是 BAE 系统公司的软件工程师 Justin Albano.在本篇文章中,作者通过在 Java8 环境下实现观察者模式的实例,进一步介绍了什么是观察者模式.专业化及其命名规则,供大 ...

  2. 1088-Gnome Sequencing

    描述 In the book All Creatures of Mythology, gnomes are kind, bearded creatures, while goblins tend to ...

  3. 中国海洋大学第四届朗讯杯高级组 A Rocky

    http://acm.sdut.edu.cn/sdutoj/showproblem.php?pid=2718&cid=1203 题意:给你一个m乘n的格子阵,从一边进去,直线往前走,如果前边有 ...

  4. javaweb学习总结(三十七)——获得MySQL数据库自动生成的主键

    测试脚本如下: 1 create table test1 2 ( 3 id int primary key auto_increment, 4 name varchar(20) 5 ); 测试代码: ...

  5. ANDROID_MARS学习笔记_S01原始版_008_Looper\Bundle异步消息处理

    一.流程 1.自定义Handler,重写handleMessage(Message msg),用msg得到bundle,从而得到传递过来的数据 2.开启android.os.HandlerThread ...

  6. 从 C++ 到 Qt(命令行编译)good

    从 C++ 到 Qt 转载自:http://hi.baidu.com/cyclone/blog/item/8f8f08fa52d22f8758ee9006.html Qt 是 C++ 的库,Qt在an ...

  7. How to: Host and Run a Basic Windows Communication Foundation Service

    This is the third of six tasks required to create a Windows Communication Foundation (WCF) applicati ...

  8. hadoop2.2编程:MRUnit

    examples: Overview This document explains how to write unit tests for your map reduce code, and test ...

  9. bzoj1821

    题目要求最近的两个部落间距尽可能最远 不难想到一种贪心的方法,对每两个点之间距离从小到大排序, 把每个点看成一个部落 然后不断将距离近的两个部落合并成一个部落,直到剩下了k个部落,那么下一条不同部落之 ...

  10. 采用软件nginx实现web服务器集群

    nginx:软件负载均衡器  是高并发量http/反向代理服务器.实现windows下IIS的负载均衡 条件:2台服务器 1.cpu:Inter(R) 酷睿 i5 cpu 2.26GHz 内存:2G ...