codeforces 761 C. Dasha and Password(多维dp)
题目链接:http://codeforces.com/contest/761/problem/C
题意:给出n行的字符串每一列都从第一个元素开始可以左右移动每一行字符串都是首位相连的。
最后问最少移动几次能够构成一个合理字符串,构成方法每一行取当前位置那个数。只要这个字符串
中有数字,有小写字母,有特殊符号就算合理。
先预处理一下每行到达各种符号所要移动的最小距离,最后设p[i][j][k][l]
i=1表示有数字,0表示没有,j,k分别表示字母和特殊符号,l表示取到第l行。
最后p值表示最小要几步。
#include <iostream>
#include <cstring>
#define inf 0X3f3f3f3f
using namespace std;
int dp[60][4] , p[2][2][2][60];
char s[60][60];
int main() {
int n , m;
cin >> n >> m;
for(int i = 0 ; i < n ; i++) {
cin >> s[i];
}
memset(dp , inf , sizeof(dp));
memset(p , inf , sizeof(p));
for(int i = 0 ; i < n ; i++) {
for(int j = 0 ; j < m ; j++) {
if(s[i][j] >= '0' && s[i][j] <= '9') {
dp[i][1] = min(min(dp[i][1] , j) , m - j);
}
else if(s[i][j] >= 'a' && s[i][j] <= 'z') {
dp[i][2] = min(min(dp[i][2] , j) , m - j);
}
else {
dp[i][3] = min(min(dp[i][3] , j) , m - j);
}
}
}
for(int i = 0 ; i < n ; i++) {
if(i == 0) {
p[1][0][0][i] = dp[i][1];
p[0][1][0][i] = dp[i][2];
p[0][0][1][i] = dp[i][3];
}
else {
p[1][0][0][i] = min(p[1][0][0][i - 1] , dp[i][1]);
p[1][1][0][i] = min(p[1][1][0][i - 1] , min(dp[i][1] + p[0][1][0][i - 1] , dp[i][2] + p[1][0][0][i - 1]));
p[1][0][1][i] = min(p[1][0][1][i - 1] , min(dp[i][1] + p[0][0][1][i - 1] , dp[i][3] + p[1][0][0][i - 1]));
p[1][1][1][i] = min(p[1][1][1][i - 1] , min(min(dp[i][1] + p[0][1][1][i - 1] , dp[i][2] + p[1][0][1][i - 1]) , dp[i][3] + p[1][1][0][i - 1]));
p[0][1][0][i] = min(p[0][1][0][i - 1] , dp[i][2]);
p[0][1][1][i] = min(p[0][1][1][i - 1] , min(dp[i][2] + p[0][0][1][i - 1] , dp[i][3] + p[0][1][0][i - 1]));
p[0][0][1][i] = min(p[0][0][1][i - 1] , dp[i][3]);
}
}
cout << p[1][1][1][n - 1] << endl;
return 0;
}
codeforces 761 C. Dasha and Password(多维dp)的更多相关文章
- Codeforces Round #394 (Div. 2) C. Dasha and Password(简单DP)
C. Dasha and Password time limit per test 2 seconds memory limit per test 256 megabytes input standa ...
- 【codeforces 761C】Dasha and Password(动态规划做法)
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【codeforces 761C】Dasha and Password(贪心+枚举做法)
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- codeforces 761 D. Dasha and Very Difficult Problem(二分+贪心)
题目链接:http://codeforces.com/contest/761/problem/D 题意:给出一个长度为n的a序列和p序列,求任意一个b序列使得c[i]=b[i]-a[i],使得c序列的 ...
- Codeforces 999F Cards and Joy(二维DP)
题目链接:http://codeforces.com/problemset/problem/999/F 题目大意:有n个人,n*k张卡牌,每个人会发到k张卡牌,每个人都有一种喜欢的卡牌f[i],当一个 ...
- Codeforces Round #394 (Div. 2) C. Dasha and Password 暴力
C. Dasha and Password 题目连接: http://codeforces.com/contest/761/problem/C Description After overcoming ...
- Codeforces Round #394 (Div. 2) C. Dasha and Password —— 枚举
题目链接:http://codeforces.com/problemset/problem/761/C C. Dasha and Password time limit per test 2 seco ...
- Codeforces Round #394 (Div. 2) C. Dasha and Password
C. Dasha and Password time limit per test 2 seconds memory limit per test 256 megabytes input standa ...
- Codeforces 761C Dasha and Password(枚举+贪心)
题目链接 Dasha and Password 题目保证一定有解. 考虑到最多只有两行的指针需要移动,那么直接预处理出该行移动到字母数字或特殊符号的最小花费. 然后O(N^3)枚举求最小值即可. 时间 ...
随机推荐
- input属性设置type="number"之后, 仍可输入e, E, -, + 的解决办法
<el-input v-model="scope.row.variables.leaderbuweiscores.score" @keyup.native="cha ...
- 【Python】Django 的邮件引擎用法详解!!(调用163邮箱为例)
1. send_mall()方法介绍 位置: 在django.core.mail模块提供了send_mail()来发送邮件. 方法参数: send_mail(subject, message, fro ...
- 一文带你彻底理解 JavaScript 原型对象
一.什么是原型 原型是Javascript中的继承的基础,JavaScript的继承就是基于原型的继承. 1.1 函数的原型对象 在JavaScript中,我们创建一个函数A(就是声明一个函数), 那 ...
- 自己学习并保存的一些shell命令
摘要: 在学习过程中,不免会遇到有些命令,那种需要的,但是你没有掌握的命令.为了节省时间,担心忘记这些,特开辟这个随笔,随时记录用到的一些命令.那么常用的不提了,自己去收集吧~ 1.文件按日期排序 应 ...
- Re-Architecting the Video Gatekeeper(一)
原文 https://medium.com/netflix-techblog/re-architecting-the-video-gatekeeper-f7b0ac2f6b00 本文介绍了了内容配置工 ...
- 对平底锅和垃圾的O奖论文的整理和学习[1](2018-02-08发布于知乎)
今天和杉杉同志在Pacific Coffee坐了0.4天,目前两人都处于放空状态. 这种天气有暖气真的太棒了. 我今天看的论文是这两篇: MCM2013B题O奖论文MCM2016B题O奖论文 先说第一 ...
- python3虚拟环境中解决 ModuleNotFoundError: No module named '_ssl'
前提是已经安装了openssl 问题 当我在python3虚拟环境中导入ssl模块时报错,报错如下: (py3) [root@localhost Python-3.6.3]# python3 Pyth ...
- sql server中的cte
从SQL Server 2005开始,提供了CTE(Common Table Expression,公用表表达式)的语法支持. CTE是定义在SELECT.INSERT.UPDATE或DELETE语句 ...
- mybatis多表查询之多对多关系查询的实现-xml方式
Mybatis对于多对多关系下的查询提供了集合(collection)的概念来解决,collection属性是resultMap高级结果映射的子集,首先,在本例中我们使用的是集合元素来解决多对多的查询 ...
- SpringIoC和SpringMVC的快速入门
更多内容,欢迎关注微信公众号:全菜工程师小辉~ Spring的优势? 降低了组件之间的耦合性 ,实现了软件各层之间的解耦 可以使用容易提供的众多服务,如事务管理,消息服务等 容器提供单例模式支持 容器 ...