题目链接: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. Linux Perf Probes for Oracle Tracing

    Luca Canali on 21 Jan 2016 Topic: this post is about Linux perf and uprobes for tracing and profilin ...

  2. bash帮助文档简单学习;bash手册翻译

    关于bash的四种工作方式的不同,可以参考:http://feihu.me/blog/2014/env-problem-when-ssh-executing-command-on-remote/,但是 ...

  3. 理解Neural Style

    paperA Neural Algorithm of Artistic Style 在艺术领域,尤其是绘画,艺术家们通过创造不同的内容与风格,并相互交融影响来创立独立的视觉体验.如果给定两张图像,现在 ...

  4. POJ 1017 Packets(积累)

    [题意简述]:这个是别人的博客,有清晰的题意描写叙述.和解题思路,借助他的想法,能够非常好的解决问题! [分析]:贪心?模拟?见代码 //216K 16Ms #include<iostream& ...

  5. poj 2932 Coneology (扫描线)

    题意 平面上有N个两两不相交的圆,求全部最外层的,即不被其它圆包括的圆的个数并输出 思路 挑战程序竞赛P259页 代码 /* ************************************* ...

  6. eclipse中文凝视字体太小解决方法

    新安装的eclipse中文凝视字体太小.解决方法例如以下: 打开Elcipse-->点击菜单条上的"Windows"-->点击"Preferences&quo ...

  7. python_获得列表中重复的项的索引

    a = ['b','a', 'b', 'c', 'a', 'c','d'] b=[] f=[] for i in a: c=[] for item in enumerate(a): if item[1 ...

  8. CCNP路由实验之十二 MPLS

     个.第3个数据包„„同样的操作.包含查询路由表.重写MAC地址,CRC校验等. 系列路由器.或者12000系列路由器. Netflow switching 通过一种标准的交换机制,处理了流的第一 ...

  9. .net 4.0 网站发布(转)

    http://www.cnblogs.com/daomul/archive/2013/05/23/3095232.html 1. 进入解决方案的web项目下,右击项目选择 "发布(B)&qu ...

  10. android页面间传递对象

    android传递对象有两种方式: 一种是Serializable和Parcelable 对于第一种方式: import java.io.Serializable; public class Shop ...