Codeforces Beta Round #96 (Div. 1) C. Logo Turtle DP
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?
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 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.
FT
1
2
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.
题意:
给你一串由FT构成的串,F代表前进,T代表转向,初始方向是1,转向后F由1变为-1(或者-1变成1)
给你一个n,意思是你也可以在任意位置的字符改变任意次数但总和不要超过n次的情况下,其状态即F变T,T变F;
问你改变n次后,最后机器人能走到的最远距离
题解:
范围很小
设定DP[i][j][k][0/1]表示在走完i个字符使用j次变化,在k位置,方向0/1是否能够达成
算好复杂度,无脑暴力怼它的转移.
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
typedef long long LL;
const int N=1e6+,mod=,inf=2e9+; int dp[][][][],m,n;
char a[N];
int main() {
scanf("%s%d",a+,&m);
n = strlen(a+);
dp[][][][] = ;
for(int i = ; i < n; ++i) {
for(int j = ; j <= m; ++j) {
for(int k = ; k < ; ++k) {
for(int cnt = ; cnt <= m - j; ++cnt) {
if(a[i+] == 'T') {
if(cnt%) {
dp[i+][j+cnt][k+][] |= dp[i][j][k][];
if(k->=)dp[i+][j+cnt][k-][] |= dp[i][j][k][];
}
else {
dp[i+][j+cnt][k][] |= dp[i][j][k][];
dp[i+][j+cnt][k][] |= dp[i][j][k][];
}
}
else {
if(cnt%) {
dp[i+][j+cnt][k][] |= dp[i][j][k][];
dp[i+][j+cnt][k][] |= dp[i][j][k][];
}
else {
dp[i+][j+cnt][k+][] |= dp[i][j][k][];
if(k->=)dp[i+][j+cnt][k-][] |= dp[i][j][k][];
}
}
}
}
}
}
int ans = ;
for(int j = ; j < ; ++j) {
if(dp[n][m][j][]) ans = max(ans,abs(j-));
if(dp[n][m][j][]) ans = max(ans,abs(j-));
}
cout<<ans<<endl;
return ;
}
Codeforces Beta Round #96 (Div. 1) C. Logo Turtle DP的更多相关文章
- 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. 2) E. Logo Turtle dp
http://codeforces.com/contest/133/problem/E 题目就是给定一段序列,要求那个乌龟要走完整段序列,其中T就是掉头,F就是向前一步,然后开始在原点,起始方向随意, ...
- 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 ...
- Codeforces Beta Round #96 (Div. 2) (A-E)
写份DIV2的完整题解 A 判断下HQ9有没有出现过 #include <iostream> #include<cstdio> #include<cstring> ...
- Codeforces Beta Round #77 (Div. 2 Only)
Codeforces Beta Round #77 (Div. 2 Only) http://codeforces.com/contest/96 A #include<bits/stdc++.h ...
- Codeforces Beta Round #80 (Div. 2 Only)【ABCD】
Codeforces Beta Round #80 (Div. 2 Only) A Blackjack1 题意 一共52张扑克,A代表1或者11,2-10表示自己的数字,其他都表示10 现在你已经有一 ...
- Codeforces Beta Round #83 (Div. 1 Only)题解【ABCD】
Codeforces Beta Round #83 (Div. 1 Only) A. Dorm Water Supply 题意 给你一个n点m边的图,保证每个点的入度和出度最多为1 如果这个点入度为0 ...
- Codeforces Beta Round #79 (Div. 2 Only)
Codeforces Beta Round #79 (Div. 2 Only) http://codeforces.com/contest/102 A #include<bits/stdc++. ...
- Codeforces Beta Round #76 (Div. 2 Only)
Codeforces Beta Round #76 (Div. 2 Only) http://codeforces.com/contest/94 A #include<bits/stdc++.h ...
随机推荐
- 第3节 mapreduce高级:4、倒排索引的建立
倒排索引建立 需求分析 需求:有大量的文本(文档.网页),需要建立搜索索引 最终实现的结果就是哪个单词在哪个文章当中出现了多少次 思路分析: 首选将文档的内容全部读取出来,加上文档的名字作为key,文 ...
- (error) MISCONF Redis is configured to save RDB snapshots, but is currently not able to persist on d
出现redis错误: (error) MISCONF Redis is configured to save RDB snapshots, but is currently not able to p ...
- ubuntu Android Studio以及SDK安装
先使用迅雷下载linux版的Android Studio以及SDK,下载地址是https://dl.google.com/dl/android/studio/ide-zips/1.1.0/androi ...
- 集训第四周(高效算法设计)F题 (二分+贪心)
Description A set of n<tex2html_verbatim_mark> 1-dimensional items have to be packed in iden ...
- Python基础—线程、进程和协程
今天已是学习Python的第十一天,来干一碗鸡汤继续今天的内容,今天的鸡汤是:超越别人对你的期望.本篇博客主要介绍以下几点内容: 线程的基本使用: 线程的锁机制: 生产者消费之模型(队列): 如何自定 ...
- Jmeter关联,正则表达式提取器使用2
正则表达式的用处很多,最基础的用法 1,断言 2,传参(关联) 例子 1.http请求 2正则表达式提取,想要提取列表列中id,一遍打开列表页 如果是1,每次就会取相同的值!匹配数字的权限高于模板$0 ...
- Git push时报错 ! [remote rejected] master -> master (pre-receive hook declined) error: failed to push some refs to......
今天在使用Git回退到之前某个版本的代码时,进行push时出现如下错误: ! [remote rejected] master -> master (pre-receive hook decli ...
- 洛谷 P4057 [Code+#1]晨跑
P4057 [Code+#1]晨跑 题目描述 “无体育,不清华”.“每天锻炼一小时,健康工作五十年,幸福生活一辈子” 在清华,体育运动绝对是同学们生活中不可或缺的一部分.为了响应学校的号召,模范好学生 ...
- 苹果Macbook Air怎么安装Win7系统图解教程
下面开始我们在苹果Macbook Air上的Windows7之旅吧.
- Python进阶系列之怎么写出pythonic的代码
使用 in/not in 检查key是否存在于字典中 判断某个key是否存在于字典中时,一般的初学者想到的方法是,先以列表的形式把字典所有的key返回,在判断该key是否存在于key列表中 d = { ...