题目链接:http://codeforces.com/contest/132/problem/C

C. Logo Turtle
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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
input
FFFTFFF
2
output
6
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.

题解:

方法一(四维dp):

1.dp[i][j][dir][dis]表示:执行到第i个指令,修改了j个指令,前进方向为dir,且到达了dis的地方的情况是否存在。其值为0或1。

2.枚举已有状态,推出下一步状态。(与常见的dp不同,常见的dp为枚举可能的状态,然后看他能从那些状态转移过来)。

3.由于结束点可能在左边,即距离为负数,为了防止溢出,将起始点往右移100。

类似的做法的题:http://blog.csdn.net/dolfamingo/article/details/73903530

易错点:

写成dp[i][j][dir][dis] 或者dp[i][j][dis][dir]都可以,但是写成dp[dir][i][j][dis] 或者dp[dis][i][j][dir]等等就不行了,因为枚举的顺序不对。递推必须自底向上,如果“底”都没推出来,那“上”自然也推不出来了。

学习之处:

一:目前了解到的递推式DP有两种:

1.当前状态(可能不存在)能从哪些状态转移过来。 被赋值的状态是当前状态。

2.当前状态(已存在)能推出哪些状态。 被赋值的状态是被推出来的状态。

二:

当设定的k维dp不好递推时,如果再加多一维(这一维是数值,且范围很小)仍不会超时,那么就可以改写成k+1维的dp,这样所有可能的状态都通过dp数组的下标体现出来了,所以要做的就是:递推出存在的状态,然后在这些状态中找出最优结果。

四维DP:

 #include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const double eps = 1e-;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+;
const int maxn = +; int n, m, dp[][][][];
char s[maxn]; void init()
{
scanf("%s%d",s+, &m);
n = strlen(s+); dp[][][][] = ; //起始点平移到100,防止下标溢出
for(int i = ; i<n; i++)
for(int j = ; j<=m; j++) //这样枚举,一个指令最多只能修改一次
for(int dir = ; dir<; dir++)
for(int dis = ; dis<=; dis++)
{
if(!dp[i][j][dir][dis]) continue; dp[i+][j+(s[i+]!='F')][dir][dis+(dir?-:)] = ;
dp[i+][j+(s[i+]!='T')][!dir][dis] = ;
}
} void solve()
{
int ans = -INF;
for(int j = m; j>=; j -= ) //一个指令可以修改多次
for(int dir = ; dir<; dir++)
for(int dis = ; dis<=; dis++)
if(dp[n][j][dir][dis])
ans = max(ans, abs(-dis)); //与100的距离,即为实际距离
cout<<ans<<endl;
} int main()
{
init();
solve();
}

三维DP:

 #include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const double eps = 1e-;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+;
const int maxn = +; char s[];
int n,m, dp[][][]; int main()
{
scanf("%s%d",s+,&m);
n = strlen(s+);
for(int i = ; i<=n; i++)
for(int j = ; j<=m; j++)
for(int k = ; k<; k++)
dp[i][j][k] = -INF; dp[][][] = dp[][][] = ;
for(int i = ; i<n; i++)
for(int j = ; j<=m; j++)
for(int k = ; k<; k++)
{
dp[i+][j+(s[i+]!='F')][k] = max(dp[i+][j+(s[i+]!='F')][k],dp[i][j][k]+(k?:-));
dp[i+][j+(s[i+]!='T')][!k] = max(dp[i+][j+(s[i+]!='T')][!k],dp[i][j][k]);
} int ans = -INF;
for (int j = m; j>=; j -= )
ans = max(ans, max(dp[n][j][], dp[n][j][])); printf("%d\n",ans);
}

Codeforces Beta Round #96 (Div. 1) C. Logo Turtle —— DP的更多相关文章

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

  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. spring boot原理分析

    1.分析spring-boot-starter-parent <parent> <groupId>org.springframework.boot</groupId> ...

  2. 贪心—— P1809 过河问题_NOI导刊2011提高(01)

    洛谷——P1809 过河问题_NOI导刊2011提高(01) 题目描述 有一个大晴天,Oliver与同学们一共N人出游,他们走到一条河的东岸边,想要过河到西岸.而东岸边有一条小船. 船太小了,一次只能 ...

  3. django使用logging记录日志

    django使用logging记录日志,我没有用这方式去记录日志,主要还是项目小的原因吧, 有机会遇见大项目的话可以回头研究. 配置setting.py配置文件 import logging impo ...

  4. luogu P1140 相似基因

    题目背景 大家都知道,基因可以看作一个碱基对序列.它包含了4种核苷酸,简记作A,C,G,T.生物学家正致力于寻找人类基因的功能,以利用于诊断疾病和发明药物. 在一个人类基因工作组的任务中,生物学家研究 ...

  5. 想用idea的update classes and resources找不到了,热部署无法使用

    发现为了方便调试页面,想用idea的update classes and resources找不到了,发现需要把deployment选择exploded 的那个war包,才能在service--> ...

  6. 详解JavaScript变量提升

    变量在程序中随处可见.它们是一些始终在相互影响,相互作用的的数据和逻辑.正是这些互动使应用程序活了起来. 在JavaScript中使用变量很重要的一方面就是变量的提升 —— 它决定了一个变量何时可以被 ...

  7. android状态栏总结

    针对状态栏的操作,只针对4.4kitKat(含)以上的机型,部分国产rom会失效,目前发现的有华为的EMUI Activity必须是noActionbar主题 本文基于StatusBarUtils略作 ...

  8. linux查看 cpu及内存和硬盘使用情况的命令top

    使用时输入 top,退出时输入q http://www.cnblogs.com/ggjucheng/archive/2012/01/08/2316399.html 简介 top命令是Linux下常用的 ...

  9. linux系统中mysql自动备份脚本

    mysql数据库中存储着网站最核心最宝贵的数据,如果因为不可预测的原因导致数据损坏或丢失,对一个网站的打击是毁灭性的,一次又一次的教训提醒着我们一定要做好备份,但是手工备份确实比较麻烦,每天都要手工操 ...

  10. 程序猿的量化交易之路(30)--Cointrader之ConfigUtil(17)

    转载须注明出处:viewmode=contents">http://blog.csdn.net/minimicall?viewmode=contents.http://cloudtra ...