题目链接: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)的更多相关文章

  1. 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 ...

  2. 【codeforces 761C】Dasha and Password(动态规划做法)

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  3. 【codeforces 761C】Dasha and Password(贪心+枚举做法)

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  4. 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序列的 ...

  5. Codeforces 999F Cards and Joy(二维DP)

    题目链接:http://codeforces.com/problemset/problem/999/F 题目大意:有n个人,n*k张卡牌,每个人会发到k张卡牌,每个人都有一种喜欢的卡牌f[i],当一个 ...

  6. Codeforces Round #394 (Div. 2) C. Dasha and Password 暴力

    C. Dasha and Password 题目连接: http://codeforces.com/contest/761/problem/C Description After overcoming ...

  7. 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 ...

  8. 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 ...

  9. Codeforces 761C Dasha and Password(枚举+贪心)

    题目链接 Dasha and Password 题目保证一定有解. 考虑到最多只有两行的指针需要移动,那么直接预处理出该行移动到字母数字或特殊符号的最小花费. 然后O(N^3)枚举求最小值即可. 时间 ...

随机推荐

  1. js页面3秒自动跳转

    如何让当前页面3秒以后自动跳转到其他页面?JS页面自动跳转 想实现登陆后3秒自动跳转到某页的功能,在网上搜了一下,供以后使用 1.<script   language= "javasc ...

  2. unimrcp-voice-activity语音检测

    研究 unimrcp有一段时间了,其中unimrcp voice acitve的算法,是遭到大家频繁吐槽.今天我们简单的介绍一下unimrcp voice activity 的这个简单粗暴的算法: u ...

  3. bucket list 函数解析

    cls_bucket_list 函数 librados::IoCtx index_ctx; // key   - oid (for different shards if there is any) ...

  4. 在docker中开启新的container

    当你在启动某个容器类型的时候  如果产生了类似的错误: docker: Error: No such image: b27f5dfcfc70c16d7b135889460def6b3f831bcc72 ...

  5. 【0807 | Day 10】字符编码以及Python2/3编码的区别

    一.计算机基础 计算机组成:CPU.内存.硬盘 CPU:控制程序运行(从内存中取出文本编辑器的数据存入内存) 内存:运行程序 硬件:存储数据 二.文本编辑器存取文件的原理 比如计算机只能识别0和1,文 ...

  6. Java源码之ConcurrentHashMap

    ⑴背景 ConcurrentHashMap是线程安全高效的HashMap.而HashMap在多线程情况下强行使用HashMap的put方法可能会导致程序死循环,使CPU使用率达到100%.(http: ...

  7. threejs 学习之

    主要内容: 使用 threejs 创建 20x20 的网格,鼠标移动时,方块跟随移动,点击时在网格任意位置放置方块,按 shift 时,删除当前位置方块. 流程如下: 创建网格 创建一个与网格同样尺寸 ...

  8. mybatis 源码分析(二)mapper 初始化

    mybatis 的初始化还是相对比较复杂,但是作者在初始化过程中使用了多种设计模式,包括建造者.动态代理.策略.外观等,使得代码的逻辑仍然非常清晰,这一点非常值得我们学习: 一.mapper 初始化主 ...

  9. CEF避坑指南(一)——下载并编译第一个示例

    CEF即Chromium Embedded Framework,Chrome浏览器嵌入式框架.它提供了接口供程序员们把Chrome放到自己的程序中.许多大型公司,如网易.腾讯都开始使用CEF进行前端开 ...

  10. 熔断器Hystrix

    什么是服务雪崩? 单个服务发生故障,占用过多的系统资源,从而导致级联故障的情况称为服务雪崩. 什么是Hystrix? 在分布式环境中,许多服务依赖项中的一些必然会失败.(服务挂了) Hystrix是一 ...