题目链接: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. NIO编程模式示例

    1. 服务端 import java.net.InetSocketAddress; import java.nio.ByteBuffer; import java.nio.channels.*; im ...

  2. FPGA资源平民化的新晋- F9 技术解析

    FPGA (现场可编程门阵列)由于其硬件并行加速能力和可编程特性,在传统通信领域和IC设计领域大放异彩.一路走来,FPGA并非一个新兴的硬件器件,由于其开发门槛过高,硬件加速算法的发布和部署保护要求非 ...

  3. 【HDOJ6646】A + B = C(模拟)

    题意 1<=a,b,c<=1e100000 思路: #include<bits/stdc++.h> using namespace std; typedef long long ...

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

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

  5. 如何安全修改cocoapods上的第三方代码

    其实搞java的都知道maven管理,解决第三方代码修改的办法就是架一个服务器,把这些修改的库放在这里,然后再maven里配置哪些需要用本地仓库的.其实cocoapods也可以做本地包管理: 大致方法 ...

  6. 一个最最简单的 log4j 的 入门级使用案例

    看了比较多的文档和博客,感觉这篇博客写得比较好,比较容易懂,先 mark 一下,回头做一个记录. 文章1:http://www.cnblogs.com/rushoooooo/archive/2011/ ...

  7. oracle系统对象

    select * from all_tab_comments-- 查询所有用户的表,视图等 select * from user_tab_comments  -- 查询本用户的表,视图等 select ...

  8. womenzijide_jiafenxiang

    <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Conten ...

  9. springboot启动报错start bean 'eurekaAutoServiceRegistration' NullPointerException

    解决方案参考:https://blog.csdn.net/hhj13978064496/article/details/82825365 我将eureka的依赖包放到了依赖包的最下面,启动报错, 如下 ...

  10. jsc2019_qualD Classified

    题目大意 给你一个有n个点的完全图 求一种方案是的给边染色后任何一点不能沿一种颜色的边走奇数条边回到这个点 要求颜色数最少 分析 考场上输出格式打错见祖宗... 我们每次找一个最大二分图将其染一个新颜 ...