[CF132C] Logo Turtle
A turtle moves following by logos.(length is \(N\)) \(F\) means "move 1 unit forward", \(T\) means turned around(180°). You must change the order \(M\) times.(\(F\) -> \(T\) , \(T\) -> \(F\))
\(N \le 50, M \le 100\).
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define debug(...) fprintf(stderr,__VA_ARGS__)
#define Debug(x) cout<<#x<<"="<<x<<endl
using namespace std;
typedef long long LL;
const int INF=1e9+7;
inline LL read(){
register LL x=0,f=1;register char c=getchar();
while(c<48||c>57){if(c=='-')f=-1;c=getchar();}
while(c>=48&&c<=57)x=(x<<3)+(x<<1)+(c&15),c=getchar();
return f*x;
}
const int N = 1005;
int dp[N][N][2];
char s[N];
int n, m;
inline void chkmax(int& a, int b){ a = a > b ? a : b; }
int main(){
// freopen("a.in","r",stdin);
scanf("%s", (s + 1));
n = strlen(s + 1);
m = read();
memset(dp, 0xcf, sizeof dp);
dp[0][0][0] = dp[0][0][1] = 0;
for(int i = 1; i <= n; ++i){
for(int j = 0; j <= m; ++j){ // turned j times in total.
for(int k = 0; k <= j; ++k){ // turn k times at i.
if(s[i] == 'F'){
if(k & 1){
chkmax(dp[i][j][0], dp[i - 1][j - k][1]);
chkmax(dp[i][j][1], dp[i - 1][j - k][0]);
}
else{
chkmax(dp[i][j][0], dp[i - 1][j - k][0] + 1);
chkmax(dp[i][j][1], dp[i - 1][j - k][1] - 1);
}
}
if(s[i] == 'T'){
if(k & 1){
chkmax(dp[i][j][0], dp[i - 1][j - k][0] + 1);
chkmax(dp[i][j][1], dp[i - 1][j - k][1] - 1);
}
else{
chkmax(dp[i][j][0], dp[i - 1][j - k][1]);
chkmax(dp[i][j][1], dp[i - 1][j - k][0]);
}
}
}
// printf("dp[%d][%d] = %d, %d\n", i, j, dp[i][j][0], dp[i][j][1]);
}
}
printf("%d\n", max(dp[n][m][0], dp[n][m][1]));
}
[CF132C] Logo Turtle的更多相关文章
- 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 ...
- 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 ...
- CF#132 C. Logo Turtle DP
C. Logo Turtle 题意 有一个海龟在一个x轴的0点,给出一个由'F','T'组成的字符序列. 海龟要按照这个序列进行行动,如果第i个字符为'F',表示沿当前方向走,'T'表示转身. 现在你 ...
- CodeForces 132C Logo Turtle (记忆化搜索)
Description A lot of people associate Logo programming language with turtle graphics. In this case t ...
- codeforces 132C Logo Turtle(dp)
可以用三维dp来保存状态, dp[i][j][k]表示在前i个字符变换了j步之后方向为k(k = 1 or k = 0)的最优解,也就是离原点的最大距离.这里规定0方向为正方向,1位负方向,表示的是当 ...
- Codeforces Beta Round #96 (Div. 2) E. Logo Turtle dp
http://codeforces.com/contest/133/problem/E 题目就是给定一段序列,要求那个乌龟要走完整段序列,其中T就是掉头,F就是向前一步,然后开始在原点,起始方向随意, ...
- CF习题集二
CF习题集二 一.CF507E Breaking Good 题目描述 \(Breaking Good\)这个游戏对于有经验的玩家来说也有一定的难度. 游戏的主角小明希望加入一个叫斧头帮的犯罪团伙.这个 ...
- Overview over available Turtle and Screen methods
24.5.2.1. Turtle methods Turtle motion Move and draw forward() | fd() backward() | bk() | back() rig ...
- turtle文库 ——python
本文将会为您介绍关于python--turtle库函数,学会这个库函数,会有很多让你意想不到的事情发生哦! 我也也会为你们,简单的编写几个代码,让你们看一下turtle函数的魅力 Turtle库是Py ...
随机推荐
- Linux 安装python3.7
首先需要安装依赖 yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-dev ...
- python第三方库的更新和安装指定版本
安装指定版本: pip install openpyxl==2.3.4 更新到最新版本: pip install --upgrade openpyxl
- jieba中文处理
一:前言 和拉丁语系不同,亚洲语言是不用空格分开每个有意义的词的.而当我们进行自然语言处理的时候,大部分情况下,词汇是我们对句子和文章理解的基础,因此需要一个工具去把完整的文本中分解成粒度更细的词. ...
- Density of Power Network(ZOJ 3708)
Problem The vast power system is the most complicated man-made system and the greatest engineering i ...
- ios高版本中select的option选项内容不显示问题
<select class="form-control" @change="inputChange(item.id,postObj[item.id])" ...
- python ros 设置机器人的位置
#!/usr/bin/env python import rospy import math from tf import transformations from geometry_msgs.msg ...
- js中判断对象类型的几种方法
我们知道,JavaScript中检测对象类型的运算符有:typeof.instanceof,还有对象的constructor属性: 1) typeof 运算符 typeof 是一元运算符,返回结果是一 ...
- js中那些方法不改变原来的数组对象
一.map方法 function fuzzyPlural(single) { var result = single.replace(/o/g, 'e'); //replace也不会修改原来数组的值, ...
- LightGBM两种使用方式
原生形式使用lightgbm(import lightgbm as lgb) import lightgbm as lgb from sklearn.metrics import mean_squar ...
- Splinter自动登录
默认用foxfire浏览器,如果用chrome请到官网下载 chromedriver驱动,解压后放到python目录scripts下 然后添加环境变量,在Path下添加chromedrvier的路径. ...