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 ...
随机推荐
- IOS 3D UI --- CALayer的transform扩展
http://www.cocoachina.com/bbs/read.php?tid=117061 例子代码可以在 http://download.csdn.net/detail/worldmatri ...
- 启发式合并CSU - 1811
F - Tree Intersection CSU - 1811 Bobo has a tree with n vertices numbered by 1,2,…,n and (n-1) edges ...
- action类中属性驱动和模型驱动的区别
1.Struts2的属性驱动 在Action类中,属性××通过get××()和set××()方法,把参数在整个生命周期内进行传递,这就是属性驱动 代码如下: package org.abu.csdn. ...
- *** 红包书用法 及 ubuntu全局配置
使用教程 http://go.wasai.org/sswiki https://home.maysoul.com/wiki/doku.php?id=shadowsocks ubuntu使用教程 htt ...
- python链家网高并发异步爬虫and异步存入数据
python链家网二手房异步IO爬虫,使用asyncio.aiohttp和aiomysql 很多小伙伴初学python时都会学习到爬虫,刚入门时会使用requests.urllib这些同步的库进行单线 ...
- python_字符串常用方法
1.切片就是通过索引(索引:索引:步长)截取字符串的一段,形成新的字符串(原则就是顾头不顾腚). a = 'ABCDEFGHIJK' print(a[0:3]) # print(a[:3]) 从开头开 ...
- Python数据类型之数字类型
整数 在Python中,整数可以执行 加(+)减(-)乘(*)除(/) 运算. 1 + 2 3 - 2 2 * 3 3 / 2 # 1.5 在控制台,Python直接返回运算结果. Python中也可 ...
- Eclipse Myeclipse 设定文件的默认打开方式
Eclipse Myeclipse 设定文件的默认打开方式 菜单:Window -> Preferences -> General -> Editors -> File A ...
- centos相关
查看虚拟机里的Centos7的IP:ip addr或者ifconfig ---https://blog.csdn.net/dancheren/article/details/73611878 Cen ...
- AndroidSweetSheet:从底部弹出面板(1)
AndroidSweetSheet:从底部弹出面板(1) AndroidSweetSheet又是一个从底部弹出面板的开源项目.我在以前写的文章中介绍了不少这些项目,见附录文章5,6,7,8.现在 ...