题目链接:http://codeforces.com/contest/761/problem/C

题意:给定n个长度为m的字符串。每个字符串(字符串下标从0到m-1)都有一个指针,初始指针指向第0个位置。现在让你把每个字符串的指针移动到某个位置使得n个字符串中个个字符串的指针指向的字符组成一个新的密码串。并且这个密码串要合法。 一个合法的密码串一个满足:至少有一个数字,一个小写字母,一个给定的符号中的其中一个。问组成合法密码串的最小总移动步数。
思路:因为只有数字,字母,符号三个要求,并且密码串中都存在一个即可。所以先处理出n个字符串指针移动到这三种的最小步数。然后简单状压dp一下即可。 dp[i][j]表示目前处理到字符串i,状态为j时的最小移动步数(j只有三位分别对应三种要求,当某位为1时说明当前存在这种字符)

import java.io.PrintWriter;
import java.util.*; public class Main {
public static final int MAXN=50+5;
public static final int MAXVAL=MAXN;
public static String str[]=new String [MAXN];
public static int val[][]=new int [MAXN][3];
public static int dp[][]=new int [MAXN][(1<<3)];
public static int getVal(char s){
if(Character.isDigit(s)==true){
return 0;
}
if(Character.isLowerCase(s)==true){
return 1;
}
return 2;
}
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
int n=cin.nextInt(),m=cin.nextInt();
for(int i=1;i<=n;i++){
str[i]=cin.next();
Arrays.fill(val[i], MAXVAL);
for(int j=0;j<m;j++){
int pos=getVal(str[i].charAt(j));
val[i][pos]=Math.min(val[i][pos],Math.min(j,m-j));
}
}
for(int i=0;i<=n;i++){
Arrays.fill(dp[i], MAXVAL*MAXVAL);
}
dp[0][0]=0;
for(int i=1;i<=n;i++){
for(int j=0;j<(1<<3);j++){
dp[i][j]=dp[i-1][j];
}
for(int j=0;j<(1<<3);j++){
for(int k=0;k<3;k++){
if(((j&(1<<k))==0)&&val[i][k]!=MAXVAL){
dp[i][(j|(1<<k))]=Math.min(dp[i][(j|(1<<k))], dp[i-1][j]+val[i][k]);
}
}
}
// for(int j=0;j<(1<<3);j++){
// out.printf("%d ", dp[i][j]);
// }
// out.println();
}
out.println(dp[n][(1<<3)-1]);
cin.close();
out.flush();
}
}

Codeforces Round #394 (Div. 2) - C的更多相关文章

  1. Codeforces Round #394 (Div. 2) E. Dasha and Puzzle(分形)

    E. Dasha and Puzzle time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  2. Codeforces Round #394 (Div. 2) 颓废记

    昨天晚上(今天凌晨),又忍不住去打CF.(本蒟弱到只能打Div.2)... 我觉得我可以用一个词概括我这次的CF: 呵呵 刚一开赛,我就codeforces访问失败.. 后来好不容易能上了,两三分钟才 ...

  3. Codeforces Round #394 (Div. 2) E. Dasha and Puzzle 构造

    E. Dasha and Puzzle 题目连接: http://codeforces.com/contest/761/problem/E Description Dasha decided to h ...

  4. Codeforces Round #394 (Div. 2) D. Dasha and Very Difficult Problem 贪心

    D. Dasha and Very Difficult Problem 题目连接: http://codeforces.com/contest/761/problem/D Description Da ...

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

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

  6. Codeforces Round #394 (Div. 2) B. Dasha and friends 暴力

    B. Dasha and friends 题目连接: http://codeforces.com/contest/761/problem/B Description Running with barr ...

  7. Codeforces Round #394 (Div. 2) A. Dasha and Stairs 水题

    A. Dasha and Stairs 题目连接: http://codeforces.com/contest/761/problem/A Description On her way to prog ...

  8. Codeforces Round #394 (Div. 2) E. Dasha and Puzzle(dfs)

    http://codeforces.com/contest/761/problem/E 题意:给出一棵树,现在要把这棵树上的结点放置在笛卡尔坐标上,使得每一条边与x轴平行或者与y轴平行.输出可行解,即 ...

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

    http://codeforces.com/contest/761/problem/C 题意:给出n个串,每个串的初始光标都位于0(列)处,怎样移动光标能够在凑出密码(每个串的光标位置表示一个密码的字 ...

  10. Codeforces Round #394 (Div. 2) B. Dasha and friends(暴力)

    http://codeforces.com/contest/761/problem/B 题意: 有一个长度为l的环形跑道,跑道上有n个障碍,现在有2个人,给出他们每过多少米碰到障碍,判断他们跑的是不是 ...

随机推荐

  1. mysql 5.7以上版本下载及安装

    一.下载 1.mysql官网下载地址:https://downloads.mysql.com/archives/community/ 2.下载完成后解压,解压后如图: 3.放置位置,把解压好的文件夹放 ...

  2. pandas学习(一)

    pandas.DataFrame.sort_index 用法 sort_index(axis=0, level=None, ascending=True, inplace=False, kind='q ...

  3. MySql不区分大小写。

    解决方案: 1:给相关字段添加上让其区分大小写. alter table 表名 modify column 字段名 varchar(100) binary character set utf8

  4. web大文件断点续传

    1,项目调研 因为需要研究下断点上传的问题.找了很久终于找到一个比较好的项目. 在GoogleCode上面,代码弄下来超级不方便,还是配置hosts才好,把代码重新上传到了github上面. http ...

  5. Windows无法启动MapGIS DataStorage Service服务

    但是启动又启动不了,查看属性 发现计算机服务器确实少了该文件目录.. 可能是不小心删除了? 之前确实有删过一些文件 下次直接把.net禁止就可以了,不用删除,不然不小心删除了其它服务.. 参考文献:h ...

  6. 李满春与WebGIS

    李满春 ,男,1964年6月生,博士,教授,博导.现任南京大学科技处处长.地理信息科学系主任(兼).地理信息系统与遥感研究所所长(兼).国际地球系统科学研究所(学科特区)常务副所长(兼).第六届高等学 ...

  7. 学习日记7、mvc +easyui datagrid excel上传

    1.首先获取datagrid所有行的数据 var rows = $("#List").datagrid("getRows"); 2.进行数据转换转化成JSON格 ...

  8. [CSP-S模拟测试]:Divisors(数学)

    题目描述 给定$m$个不同的正整数$a_1,a_2,...,a_m$,请对$0$到$m$每一个$k$计算,在区间$[1,n]$里有多少正整数是$a$中恰好$k$个数的约数. 输入格式 第一行包含两个正 ...

  9. Facebook发布Tweaks:让微调iOS应用变得更简单

    假设,你正在开发一款iOS应用. 你的iOS应用有很多动画效果,而你(或你的设计师)希望让那些动画效果的持续时间恰到好处.那华丽的抽屉特效是应该耗时半秒钟,还是四分之三秒呢? 通常情况下,开发者会对合 ...

  10. vue实现动态显示与隐藏底部导航的方法分析

    本文实例讲述了vue实现动态显示与隐藏底部导航的方法.分享给大家供大家参考,具体如下: 在日常项目中,总有几个页面是要用到底部导航的,总有那么些个页面,是不需要底部导航的,这里列举一下页面底部导航的显 ...