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个人,给出他们每过多少米碰到障碍,判断他们跑的是不是 ...
随机推荐
- mysql导出数据到csv文件
在日常工作中经常会遇见导出表中的数据到csv文件的操作,这里就简单总结一下导出的操作. 下面对csv文件的描述是摘录: 据RFC4180文档设置的,该文档全称Common Format and MIM ...
- oracle中where子句和having子句中的区别
1.where 不能放在GROUP BY 后面2.HAVING 是跟GROUP BY 连在一起用的,放在GROUP BY 后面,此时的作用相当于WHERE3.WHERE 后面的条件中不能有聚集函数 ...
- postman 简单使用教程
Postman 安装 Postman 接口测试(Collection) Postman 接口测试(测试用例)Postman 接口测试(变量与参数化)Postman 接口测试(非 UI 运行模式 ...
- 23 October
[HAOI2010] 最长公共子序列 求S串与T串的 最长公共子序列 的 长度 及其 个数. 动态规划递推式: \[ f(i,j)=\max\left\{ f(i-1,j), f(i,j-1) \ri ...
- oracle SQL in plsql
刚安装好的oracle和plsql,以oracle11g为例 1.刚安装好后有两个默认的系统账号和初始密码:sys/change_on_install,system/manager 2.如果忘记了或不 ...
- 面向对象编程思想(OOP)(转发)
本文我将从面向对象编程思想是如何解决软件开发中各种疑难问题的角度,来讲述我们面向对象编程思想的理解,梳理面向对象四大基本特性.七大设计原则和23种设计模式之间的关系. 软件开发中疑难问题: 软件复杂庞 ...
- scau 1079 三角形(暴力)
</pre>1079 三角形</h1></center><p align="center" style="margin-top: ...
- 多行文本溢出隐藏处理,兼容ie,火狐
问题 多行文本溢出隐藏,webkit内核浏览器如谷歌支持如下写法: overflow: hidden; text-overflow: ellipsis; display: -webkit-box; - ...
- innobackupex对MySQL做热备份,报错mysql库下数据字典表损坏
[root@node1 op]#mysql -Vmysql Ver 14.14 Distrib 5.6.29innobackupex热备份MySQL报错,报错信息:[root@node1 op]#in ...
- statistics——数学统计函数
statistics——数学统计函数 转自:https://blog.csdn.net/zhtysw/article/details/80005410 资源代码位置:Lib/statistixs.py ...