Codeforces Round #394 (Div. 2) - C
题目链接: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的更多相关文章
- 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 ...
- Codeforces Round #394 (Div. 2) 颓废记
昨天晚上(今天凌晨),又忍不住去打CF.(本蒟弱到只能打Div.2)... 我觉得我可以用一个词概括我这次的CF: 呵呵 刚一开赛,我就codeforces访问失败.. 后来好不容易能上了,两三分钟才 ...
- 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 ...
- 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 ...
- Codeforces Round #394 (Div. 2) C. Dasha and Password 暴力
C. Dasha and Password 题目连接: http://codeforces.com/contest/761/problem/C Description After overcoming ...
- Codeforces Round #394 (Div. 2) B. Dasha and friends 暴力
B. Dasha and friends 题目连接: http://codeforces.com/contest/761/problem/B Description Running with barr ...
- 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 ...
- Codeforces Round #394 (Div. 2) E. Dasha and Puzzle(dfs)
http://codeforces.com/contest/761/problem/E 题意:给出一棵树,现在要把这棵树上的结点放置在笛卡尔坐标上,使得每一条边与x轴平行或者与y轴平行.输出可行解,即 ...
- Codeforces Round #394 (Div. 2) C.Dasha and Password(暴力)
http://codeforces.com/contest/761/problem/C 题意:给出n个串,每个串的初始光标都位于0(列)处,怎样移动光标能够在凑出密码(每个串的光标位置表示一个密码的字 ...
- Codeforces Round #394 (Div. 2) B. Dasha and friends(暴力)
http://codeforces.com/contest/761/problem/B 题意: 有一个长度为l的环形跑道,跑道上有n个障碍,现在有2个人,给出他们每过多少米碰到障碍,判断他们跑的是不是 ...
随机推荐
- ECS 按量付费转包年包月支持按周啦
功能场景 不需要别的理由,就是省钱. 以 华北1 ecs.t5-c1m2.xlarge ( 4vCPU 8GB ) 为例:按量付费一周需要 131元,而按周付费只需要 68元. 如果您正在使用按量付费 ...
- php in_array()函数 语法
php in_array()函数 语法 作用:搜索数组中是否存在指定的值.大理石机械构件价格 语法:in_array(search,array,type) 参数: 参数 描述 search 必需.规定 ...
- 【HDOJ6664】Andy and Maze(color coding)
题意:给定一张n点m边的无向带权图,问从任意结点出发,不能走已经经过的点,共经过k个点的最长路径的值 n,m<=1e4,k<=6 思路:color coding算法 考虑每次给每个点随机编 ...
- 20180826(05)- Java URL处理
Java URL处理 URL(Uniform Resource Locator)中文名为统一资源定位符,有时也被俗称为网页地址.表示为互联网上的资源,如网页或者FTP地址. 本章节我们将介绍Java是 ...
- 170817关于JSON知识点
1. JSON [1] JSON简介 JSON全称 JavaScript Object Notation ...
- [CSP-S模拟测试]:Market(背包DP)
题目描述 在比特镇一共有$n$家商店,编号依次为$1$到$n$.每家商店只会卖一种物品,其中第$i$家商店的物品单价为$c_i$,价值为$v_i$,且该商店开张的时间为$t_i$. $Byteasar ...
- 用 margin 还是用 padding ?
边界(margin):元素周围生成额外的空白区."空白区"通常是指其他元素不能出现且父元素背景可见的区域. 补白(padding):补白位于元素框的边界与内容区之间.很自然,用于影 ...
- php str_replace与substr_replace的区别
函数定义: str_replace() :函数替换字符串中的一些字符(区分大小写). substr_replace() :函数把字符串的一部分替换为另一个字符串. 区别: str_replace()和 ...
- 阿里云code下载代码和更新代码
1- 本地新建一个文件夹,进入文件夹下面右击打开git 2- Git init初始化一个.git文件夹 3- Git clone git@code.aliyun.com:username/space- ...
- LintCode之最长单词
题目描述: 分析:先建一个数组s用来存储每个字符串的长度,然后遍历数组s得到最大的数max,这个数就是词典中的最长单词的长度,由于可能有多个长度相等的单词,所以要循环整个词典,当一个单词的长度等于ma ...