题目链接: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. Django学习笔记(12)——分页功能

    这一篇博客记录一下自己学习Django中分页功能的笔记.分页功能在每个网站都是必要的,当页面因需要展示的数据条目过多,导致无法全部显示,这时候就需要采用分页的形式进行展示. 分页在网站随处可见,下面展 ...

  2. SQLite的sqlite_master表

    SQLite的sqlite_master表   sqlite_master表是SQLite的系统表.该表记录该数据库中保存的表.索引.视图.和触发器信息.每一行记录一个项目.在创建一个SQLIte数据 ...

  3. [LibreOJ β Round #4] 子集

    显然是个二分图,直接求最大独立就行了. #include<bits/stdc++.h> #define ll long long #define pb push_back using na ...

  4. Ubuntu 16.04中的Dock的应用顺序调整

    操作步骤: 参考: https://askubuntu.com/questions/39805/is-there-an-easy-way-to-rearrange-or-move-the-icons- ...

  5. 【linux】CentOS编译程序报错 修复 ./Modules/_ssl.c:64:25: 致命错误:openssl/rsa.h:没有那个文件或目录

    如果你在编译时遇到这个错误,这可能是下面的原因:你尝试编译的程序使用OpenSSL,但是需要和OpenSSL链接的文件(库和头文件)在你Linux平台上缺少. 所以在CentOS下, 退到根路径,[需 ...

  6. Go语言_RPC_Go语言的RPC

    一 标准库的RPC RPC(Remote Procedure Call,远程过程调用)是一种通过网络从远程计算机程序上请求服务,而不需要了解底层网络细节的应用程序通信协议.简单的说就是要像调用本地函数 ...

  7. iOS开发之分段控制器(UISegmentedControl)

    今天我们来说下iOS中的分段选择控制器UISegmentedControl,这一控件有什么作用呢 每个segment都能被点击,相当于集成了多个button 通常我们会点击不同的segment来切换不 ...

  8. 微信小程序制作商 业务流程

  9. Cent OS下发送邮件

    首先安装发送邮件的服务: yum install -y sendmail 安装完成之后在安装mutt yum install -y mutt 安装完成之后我们就可以发送邮件了 mutt     tes ...

  10. python(15)- 装饰器及装饰器的使用

    装饰器 1.无参数 2.函数有参数 3.函数动态参数 4.装饰器参数 装饰器的应用 下面题目同http://www.cnblogs.com/xuyaping/p/6679305.html,只不过加了装 ...