题目链接: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. Pycharn破解补丁激活

    Pycharn破解补丁激活 到期时间: 1.  下载 https://pan.baidu.com/s/1mcQM8CLUnweY02ahKEr4PQ 并将 JetbrainsCrack-release ...

  2. VSCode必备插件

    通用组件 1.汉化插件 https://marketplace.visualstudio.com/items?itemName=MS-CEINTL.vscode-language-pack-zh-ha ...

  3. CKEDITOR无缝粘贴word

    由于工作需要必须将word文档内容粘贴到编辑器中使用 但发现word中的图片粘贴后变成了file:///xxxx.jpg这种内容,如果上传到服务器后其他人也访问不了,网上找了很多编辑器发现没有一个能直 ...

  4. 百度ueditor新增的将word内容导入到富文本编辑框的功能.

    如何做到 ueditor批量上传word图片? 1.前端引用代码 <!DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN& ...

  5. 20180910-Java 文档注释

    Java 文档注释 Java只是三种注释方式.前两种分别是// 和/* */,第三种被称作说明注释,它以/** 开始,以 */结束. // /* */ /** */ 说明注释允许你在程序中嵌入关于程序 ...

  6. Oracle DB 查看预警日志

    “Database(数据库)”主页>“Related Links相关链接)”区域> “Alert Log Content (预警日志内容)” 查看预警日志每个数据库都有一个alert_&l ...

  7. Spring JDBCTemplate 简单使用

    Spring JDBCTemplate applicationContext.xml配置 <?xml version="1.0" encoding="UTF-8&q ...

  8. xiugai-去除js注释

    <div class="myLoading"> <div class="svg-wrap"> <svg width="8 ...

  9. CodeChef FNCS (分块+树状数组)

    题目:https://www.codechef.com/problems/FNCS 题解: 我们知道要求区间和的时候,我们用前缀和去优化.这里也是一样,我们要求第 l 个函数到第 r 个函数 [l, ...

  10. js对url的编码和解码

    最近做公众号相关, 需要在公众号里面配菜单, 才发现菜单的链接部分是编码过的, 如这样http%3A%2F%2Fw3cschool.cn%2Fmy%20test.asp%3Fname%3Dst%C3% ...