题目链接: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. codeforces 213div(2) 365 A.Good Number 365 B.The Fibonacci Segment

    #include <stdio.h> #include <string.h> bool vis[11]; int n, k; bool judge(int x) { memse ...

  2. sed流编辑器

    一.前言 (一).sed 工作流程 sed 是一种在线的.非交互式的流编辑器,它一次处理一行内容.处理时,把当做前处理的行存储在临时缓存区中,成为“模式空间”(pattern space),接着用se ...

  3. jquery EasyUi 添加节点、展开所有节点、默认选中第一个节点

    感觉easyUi 的树用起来不如 Ext 的树方便,首先,root节点不太好自定义, 异步加载时,只能通过后台判断生成root节点,但是这样一来有一个问题,就是第一次访问界面时, 树的初始化比较慢,大 ...

  4. JavaWeb配置详解(结合框架SpringMVC)

    详解 先说一说常识性的东西,我们的JavaWeb程序运行一开始走的是web.xml文件,这是我们的核心文件,可以说没有web.xml文件我们就无法运行项目,这个文件长什么样子,读者自己新建一个web项 ...

  5. RabbitMQ的基本介绍及与Spring整合

    一,场景回顾 ​ 最近做电商购物项目,在分布式中搜索服务,商品详情服务都是独立的模块.那么有一个问题就是: 商品的原始数据保存在数据库中,增删改查都在数据库中完成. 搜索服务数据来源是索引库,如果数据 ...

  6. c# NPOI 导出23万条记录耗时12秒

    先上测试代码: string connectionString = "Server=localhost;Initial Catalog=******;User ID=sa;Password= ...

  7. 决策树ID3原理及R语言python代码实现(西瓜书)

    决策树ID3原理及R语言python代码实现(西瓜书) 摘要: 决策树是机器学习中一种非常常见的分类与回归方法,可以认为是if-else结构的规则.分类决策树是由节点和有向边组成的树形结构,节点表示特 ...

  8. .NET中使用WebService,以及和一般处理程序、类库的区别

    首先我们来看一下如何创建Web Service 首先在解决方案中新建项,选择ASP.NETWeb应用程序 然后选择一个空的项目就可以,单击确定 项目建完之后,在项目上右键-->添加-->新 ...

  9. R 包 rgl 安装失败, 报错 X11 not found but required, configure aborted 及解决方法

    R 包 rgl 安装失败, X11 not found but required, configure aborted * installing *source* package ‘rgl’ ... ...

  10. [FJOI2015]火星商店问题(线段树分治,可持久化,Trie树)

    [FJOI2015]火星商店问题 前天考了到线段树分治模板题,全场都切了,就我不会QAQ 于是切题无数的Tyher巨巨就告诉我:"你可以去看看火星商店问题,看了你就会了." 第一道 ...