Codeforces Round #394 (Div. 2) C. Dasha and Password(简单DP)
2 seconds
256 megabytes
standard input
standard output
After overcoming the stairs Dasha came to classes. She needed to write a password to begin her classes. The password is a string of length n which satisfies the following requirements:
- There is at least one digit in the string,
- There is at least one lowercase (small) letter of the Latin alphabet in the string,
- There is at least one of three listed symbols in the string: '#', '*', '&'.
Considering that these are programming classes it is not easy to write the password.
For each character of the password we have a fixed string of length m, on each of these n strings there is a pointer on some character. The i-th character displayed on the screen is the pointed character in the i-th string. Initially, all pointers are on characters with indexes 1 in the corresponding strings (all positions are numbered starting from one).
During one operation Dasha can move a pointer in one string one character to the left or to the right. Strings are cyclic, it means that when we move the pointer which is on the character with index 1 to the left, it moves to the character with the index m, and when we move it to the right from the position m it moves to the position 1.
You need to determine the minimum number of operations necessary to make the string displayed on the screen a valid password.
The first line contains two integers n, m (3 ≤ n ≤ 50, 1 ≤ m ≤ 50) — the length of the password and the length of strings which are assigned to password symbols.
Each of the next n lines contains the string which is assigned to the i-th symbol of the password string. Its length is m, it consists of digits, lowercase English letters, and characters '#', '*' or '&'.
You have such input data that you can always get a valid password.
Print one integer — the minimum number of operations which is necessary to make the string, which is displayed on the screen, a valid password.
3 4
1**2
a3*0
c4**
1
5 5
#*&#*
*a1c&
&q2w*
#a3c#
*&#*&
3
In the first test it is necessary to move the pointer of the third string to one left to get the optimal answer.
【分析】题意很简单,就是定义一个合格的密码,必须包括数字(0~9),字母,符号这三样,然后给你一个二维字符串,在每一行选一个字符,使得每一行选的字符组合起来的字符串为合格的密码。一开始每一行的指针都在第一列,然后每次只能向左右移动一步,若在最左边,向左移可到达最右边,在最右边向右移则到达最左边。问最小步骤。简单DP一下就行了。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <stack>
#include <queue>
#include <vector>
#define inf 0x3f3f3f3f
#define met(a,b) memset(a,b,sizeof a)
#define pb push_back
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int N = ;
const int M = 1e5+;
int n,m,ans=;
char s[][];
int dist[][];
int main()
{
int i,j,k;
scanf ("%d%d",&n,&m);
for(int i=;i<;i++)for(int j=;j<;j++)dist[i][j]=;
for (i=;i<=n;i++)
for (j=;j<=m;j++)
scanf (" %c",&s[i][j]);
for (i=;i<=n;i++)
{ for (j=;j<=m;j++)
if (s[i][j]>=''&&s[i][j]<='') dist[i][]=min(dist[i][],min(j-,m-j+));
else if (s[i][j]>='a'&&s[i][j]<='z') dist[i][]=min(dist[i][],min(j-,m-j+));
else dist[i][]=min(dist[i][],min(j-,m-j+));
}
for (i=;i<=n;i++)
for (j=;j<=n;j++) if (j!=i)
for (k=;k<=n;k++) if (k!=i&&k!=j)
ans=min(ans,dist[i][]+dist[j][]+dist[k][]);
cout<<ans<<endl;
return ;
}
Codeforces Round #394 (Div. 2) C. Dasha and Password(简单DP)的更多相关文章
- 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) C. Dasha and Password —— 枚举
题目链接:http://codeforces.com/problemset/problem/761/C C. Dasha and Password time limit per test 2 seco ...
- Codeforces Round #394 (Div. 2) C. Dasha and Password
C. Dasha and Password time limit per test 2 seconds memory limit per test 256 megabytes input standa ...
- Codeforces Round #394 (Div. 2) C.Dasha and Password(暴力)
http://codeforces.com/contest/761/problem/C 题意:给出n个串,每个串的初始光标都位于0(列)处,怎样移动光标能够在凑出密码(每个串的光标位置表示一个密码的字 ...
- 【枚举】Codeforces Round #394 (Div. 2) C. Dasha and Password
纪念死去的智商(虽然本来就没有吧……) 三重循环枚举将哪三个fix string作为数字.字母和符号位.记下最小的值就行了. 预处理之后这个做法应该是O(n^3)的,当然完全足够.不预处理是O(n^3 ...
- 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) 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) B. Dasha and friends 暴力
B. Dasha and friends 题目连接: http://codeforces.com/contest/761/problem/B Description Running with barr ...
随机推荐
- 【题解】SCOI2010幸运数字
最近在学习容斥相关,于是就看到了这个题.一开始以为是补集转化,但是观察一下马上发现不可行,好像直接做会比较容易一些.一个数满足要求的充要条件即为是一个幸运数字的倍数,那么容斥可以轻松搞定,只要枚举是一 ...
- JUnit4.11 理论机制 @Theory 完整解读
最近在研究JUnit4,大部分基础技术都是通过百度和JUnit的官方wiki学习的,目前最新的发布版本是4.11,结合代码实践,发现官方wiki的内容或多或少没有更新,Theory理论机制章节情况尤为 ...
- 给DOM元素绑定click事件也有学问
最简单的莫过于使用click方法: 1 <input id="btn" type="button" value="BUTTON" on ...
- Ubuntu下安装LNMP之php7的安装并配置Nginx支持php及卸载php
据了解,php7是比之前的版本性能快很多的.http://php.net/get/php-7.2.2.tar.gz/from/a/mirror 安装前也可提前将相关依赖库安装好,或者在安装php时若安 ...
- HDU1054 Strategic Game(最小点覆盖)
Strategic Game Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- wyh的天鹅~vector的使用
链接:https://www.nowcoder.com/acm/contest/93/L来源:牛客网 时间限制:C/C++ 3秒,其他语言6秒 空间限制:C/C++ 262144K,其他语言52428 ...
- 稀疏编码概率解释(基于1996年Olshausen与Field的理论 )
一.Sparse Coding稀疏编码 稀疏编码算法是一种无监督学习方法,它用来寻找一组“超完备”基向量来更高效地表示样本数据.稀疏编码算法的目的就是找到一组基向量 ,使得我们能将输入向量 表示为这些 ...
- [bzoj2049][Sdoi2008]Cave 洞穴勘测——lct
Brief Description 给定一个森林,您需要支持两种操作: 链接两个节点. 断开两个节点之间的链接. Algorithm Design 对于树上的操作,我们现在已经有了树链剖分可以处理这些 ...
- bzoj 1041 数学推理
原题传送门http://www.lydsy.com/JudgeOnline/problem.php?id=1041 我们只需要求第一象限内(不包括坐标轴)的点数然后ans=ans*4+4就好了 首先我 ...
- HDU1028 (整数拆分)
Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...