题目链接: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. 多线程编程(Linux C)

    多线程编程可以说每个程序员的基本功,同时也是开发中的难点之一,本文以Linux C为例,讲述了线程的创建及常用的几种线程同步的方式,最后对多线程编程进行了总结与思考并给出代码示例. 一.创建线程 多线 ...

  2. Thymeleaf 模板 springboot集成使用

    一.Thymeleaf是什么? 简单说, Thymeleaf 是一款用于渲染XML/XHTML/HTML5内容的模板引擎,类似我之前用过的FreeMarker .由于它支持 html 原型,然后在 h ...

  3. Mac 10.14.4 编译openjdk1.9源码 及集成clion动态调试

    警告⚠️:本文耗时很长,先做好心理准备:编译openjdk源码需要很大的耐心,因为要踩很多坑,解决很多问题,本人从编译开始到结束用了两天时间,按照本篇教程踩坑会少许:谢谢观看 一.获取openjdk源 ...

  4. AndroidSDK的目录详解

    Tools 目录工具(必须的工具) Android SDK Tools(必须,只需下载一个版本,一般选最新版本):基础工具包,版本号带rc字样的是预览版. Android SDK Platform-t ...

  5. HTML/CSS:div居中和div内部元素垂直居中(1)

    div居中 div水平和垂直居中,text-align和vertical-align不起作用,因为标签div没有这两个属性,所以再css中设置这两个值不能居中的效果 1. div水平居中:设置marg ...

  6. Promise对象的resolve回调函数和reject回调函数使用

    Promise是ES6中用来结局回调地狱的问题的但是并不能帮我们减少代码量 Promise是一个构造函数 new Promise() 得到一个Promise一个实例 在Promise上有两个函数分别是 ...

  7. 解决微信二次分享失败--后面被加上from=singlemessage&isappinstalled=0的解决方案

    首次分享成功,点开后再次分享或第三次分享就失败了 1.检查你分享的链接,看是否多了两个参数,微信分享会根据分享的不同,为原始链接拼接: 朋友圈   from=timeline&isappins ...

  8. Oracle、MySQL和Sqlserver的事务管理、分页和别名的区别

    1.在mysql中事务默认是自动提交的,只有设置autocommit为0的时候,才用自己commit(commit--rollback回滚) 2.但是在oracle中必须自己commit;不然就只能结 ...

  9. SAP-批量删除生产订单

    1.SE38运行:PPARCHP1 2.先用COOIS导出订单,已经CLSD,没有删除的

  10. 使用PowerShell 测试DNS

    运行环境:Windows Server 2012 R2 获取服务器DNS命令,下面的仅获取一个dns (nslookup sql.ciras.com)[1].split(':')[1].trim() ...