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)枚举求最小值即可. 时间 ...
随机推荐
- JDK的可视化工具系列 (四) JConsole、VisualVM
JConsole: Java监视与管理控制台 代码清单1: import java.util.*; public class JConsoleDemo { static class OOMObject ...
- WPF界面的异步后台加载
private void Init() { BackgroundWorker worker = new BackgroundWorker(); ...
- MySQL操作命令梳理(1)
一.索引 1.创建索引 索引的创建可以在CREATE TABLE语句中进行,也可以单独用CREATE INDEX或ALTER TABLE来给表增加索引.以下命令语句分别展示了如何创建主键索引(PRIM ...
- 【JDK】JDK源码分析-ReentrantLock
概述 在 JDK 1.5 以前,锁的实现只能用 synchronized 关键字:1.5 开始提供了 ReentrantLock,它是 API 层面的锁.先看下 ReentrantLock 的类签名以 ...
- RocketMQ中Broker的刷盘源码分析
上一篇博客的最后简单提了下CommitLog的刷盘 [RocketMQ中Broker的消息存储源码分析] (这篇博客和上一篇有很大的联系) Broker的CommitLog刷盘会启动一个线程,不停地 ...
- Kafka消息队列初识
一.Kafka简介 1.1 什么是kafka kafka是一个分布式.高吞吐量.高扩展性的消息队列系统.kafka最初是由Linkedin公司开发的,后来在2010年贡献给了Apache基金会,成为了 ...
- JS构建多端应用
JS构建多端应用 一,需求与介绍 1.1,介绍 1,Taro 是一套遵循 React语法规范的 多端开发 解决方案.现如今市面上端的形态多种多样,Web.React-Native.微信小程序等各种端大 ...
- SQL Labs刷题补坑记录(less1-less30)
补坑加1,这几天快速刷一下sqllabs 来巩固下sql注入基础吧,也算是把很久以前没刷的过一遍,do it! 第一部分: LESS1: 直接报错,有回显的注入, http://localhost/s ...
- 全屏滚动插件pagePiling.js
全屏滚动效果是最近非常流行的网页设计形式,带给用户良好的视觉和交互体验.pagePiling.js 这款 jQuery 插件可以帮助前端开发人员轻松实现这种效果.支持所有的主流浏览器,包括IE8+,支 ...
- 谨慎 mongodb 关于数字操作可能导致类型及精度变化
1.问题描述 最近有一个需求,更新Mongo数据库中 原料 集合的某字段价格,更新后,程序报错了,说长度过长了,需要Truncation. 主要错误信息如下: FormatException: An ...