http://codeforces.com/contest/133/problem/E

题目就是给定一段序列,要求那个乌龟要走完整段序列,其中T就是掉头,F就是向前一步,然后开始在原点,起始方向随意,要求输出能走到最远是哪里。

首先可以保证的是,他最远走的可以默认是向右走,因为,如果你说是向左走的话,我可以设置相反的开始face,就是开始的时候面向那里,从而得到相反的结论。所以就能得到向左走,是-1,向右走,是+1

那么从最终状态考虑,

最后肯定是走完了整段序列,然后改变了n次,face是那里还不清楚的,所以就是dp[i][j][face]能表达完状态。

face : 0 or 1

dp[i][j][face]表示走完前i个,改变了j次,最后face向哪里的时候的最优解

那么转移过来的时候:

因为一个点可以改变很多次,所以。

for (int i = 1; i <= lenstr; ++i) //枚举整段序列(这是必须的)

  for (int j = 0; j <= n; ++j) //枚举前i个位置一共改变了多少次

    for (int h = 0; h <= j; ++h) //枚举第i个位置改变了多少次(因为可以重复改变)

如果同一个点改变了奇数次,相当于没变。以此类推

所以就能从dp[i - 1][j - h][face]这个转移到下一个

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
const int maxn = + ;
int dp[maxn][maxn][];
char str[maxn];
void work() {
int n;
for (int i = ; i <= maxn - ; ++i) {
for (int j = ; j <= maxn - ; ++j) {
for (int k = ; k < ; ++k) {
dp[i][j][k] = -inf;
}
}
}
dp[][][] = ;
dp[][][] = ;
scanf("%s%d", str + , &n);
for (int i = ; str[i]; ++i) {
for (int j = ; j <= n; ++j) {
for (int h = ; h <= j; ++h) {
if (str[i] == 'T') {
if (h & ) {
dp[i][j][] = max(dp[i][j][], dp[i - ][j - h][] + );
dp[i][j][] = max(dp[i][j][], dp[i - ][j - h][] - );
} else {
dp[i][j][] = max(dp[i][j][], dp[i - ][j - h][]);
dp[i][j][] = max(dp[i][j][], dp[i - ][j - h][]);
}
} else { // 'F'
if (h & ) { //to 'T'
dp[i][j][] = max(dp[i][j][], dp[i - ][j - h][]);
dp[i][j][] = max(dp[i][j][], dp[i - ][j - h][]);
} else {
dp[i][j][] = max(dp[i][j][], dp[i - ][j - h][] + );
dp[i][j][] = max(dp[i][j][], dp[i - ][j - h][] - );
}
}
}
}
}
int lenstr = strlen(str + );
int ans = max(dp[lenstr][n][], dp[lenstr][n][]);
cout << ans << endl;
} int main() {
#ifdef local
freopen("data.txt","r",stdin);
#endif
work();
return ;
}

Codeforces Beta Round #96 (Div. 2) E. Logo Turtle dp的更多相关文章

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

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

  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. Win 10 无法打开内核设备“\\.\Global\vmx86”

    Win 10操作系统, VMWareWorkstation10 无法打开内核设备“\\.\Global\vmx86”: 系统找不到指定的文件.你想要在安装 VMware Workstation 前重启 ...

  2. 1026 Table Tennis (30)(30 分)

    A table tennis club has N tables available to the public. The tables are numbered from 1 to N. For a ...

  3. MySQL上周新增激活用户在上周下单情况_20161107周一

    上周新增激活用户在上周下单情况 1.上周激活用户明细 #上周激活用户明细 SELECT a.城市,a.用户ID,a.用户名称,b.用户地址,b.联系电话,a.订单日期,c.年周,c.上周一,a.订单I ...

  4. docker安装与操作

    准备和安装 1.到这个路径下下载docker engine: https://get.docker.com/rpm/1.7.1/centos-7/RPMS/x86_64/docker-engine-1 ...

  5. bzoj 3714 [PA2014]Kuglarz——思路+最小生成树

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3714 如果用s[ i ]表示前 i 个的奇偶性,那么c(i_j)表示s[ i-1 ]^s[ ...

  6. PC lint -sem 用法示例

    -sem(std::auto_ptr::auto_ptr,custodial(1)) // the auto_ptr class type // handles custody automagical ...

  7. 资源:template

    ylbtech-资源: 1.返回顶部   2.返回顶部   3.返回顶部   4.返回顶部   5.返回顶部     6.返回顶部   作者:ylbtech出处:http://ylbtech.cnbl ...

  8. bzoj5093

    NTT+组合数学 $把每个点分别按度数考虑,由于有标号,可以得出$ $ans=n*2^{(n-1)*(n-2)}*\sum_{i=1}^{n-1}{C(n-1,i)*i^{k}}$ $本质上是求\su ...

  9. Python中正则匹配使用findall时的注意事项

    在使用正则搜索内容时遇到一个小坑,百度搜了一下,遇到这个坑的还不少,特此记录一下. 比如说有一个字符串  "123@qq.comaaa@163.combbb@126.comasdf111@a ...

  10. java 发邮件 代码

    /** * java发送带附件的邮件 * 周枫 * 2013.8.10 */ package com.dsideal.Util; import javax.mail.*; import javax.m ...