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

题意:

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. zepto源码学习-03 $()

    在第一篇的时候提到过关于$()的用法,一个接口有很多重载,用法有很多种,总结了下,大概有一以下几种 1.$(selector,context?) 传入一个选择器返回一个zepto对象 2.$(func ...

  2. Design Tutorial: Inverse the Problem

    Codeforces Round #270 D:http://codeforces.com/contest/472/problem/D 题意:给以一张图,用邻接矩阵表示,现在问你这张图能不能够是一棵树 ...

  3. jsp接收相同复合参数 request.getParameterValues()用法

    在网站中往往需要用户选择复选框,此时需要创建多个复选框让用户进行选择: <head> <meta http-equiv="Content-Type" conten ...

  4. glibc, eglibc和 glib的区别

    http://blog.csdn.net/wind19/article/details/6082874

  5. Document字段发生变化后,报的错

    2016-10-11 15:27:47,828 [ERROR] [main] SpringApplication:838 - Application startup failedorg.springf ...

  6. 捉虫记:SHGetSpecialFolderPath返回错误码为2

    通常我们想获得系统的一些路径时,都会使用一些Shell函数.比如SHGetSpecialFolderPath,SHGetFolderPath,SHGetKnownFolderPath等,传入我们想要的 ...

  7. Android开发UI之ViewPager及PagerAdapter

    ViewPager,官网链接--http://developer.android.com/reference/android/support/v4/view/ViewPager.html ViewPa ...

  8. Awesomplete 屌爆了

    自动完成,功能强大! 具体请参考 http://leaverou.github.io/awesomplete

  9. 【C++】命令行Hangman #2015年12月15日 00:20:27

    增加了可以在构造Hangman对象时通过传入参数设定“最大猜测次数”的功能.少量修改.# 2015年12月15日 00:20:22 https://github.com/shalliestera/ha ...

  10. C#使用SQLite出错:无法加载 DLL“SQLite.Interop.dll”,找不到指定的模块

    在SQLite官方下载了System.Data.SQLite,编写如下测试代码: 复制内容到剪贴板 程序代码 using (SQLiteConnection conn = new SQLiteConn ...