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 ...
随机推荐
- BZOJ3242 [Noi2013]快餐店 【环套树 + 单调队列dp】
题目链接 BZOJ3242 题解 题意很清楚,找一点使得最远点最近 如果是一棵树,就是直径中点 现在套上了一个环,我们把环单独拿出来 先求出环上每个点外向树直径更新答案,并同时求出环上每个点外向的最远 ...
- win10 update orchestratorservere禁用
1 Windows 10系统中有一项Update Orchestrator Service(更新协调器办事),在当地办事窗口中,我们发现 Update Orchestrator Service 办 ...
- BEGIN TRAN;
USE master Create Database TestDb on Primary ( name='TestDb_data', filename='G:\TempData\Db\TestDb_d ...
- BZOJ1415: [Noi2005]聪聪和可可 最短路 期望概率dp
首先这道题让我回忆了一下最短路算法,所以我在此做一个总结: 带权: Floyed:O(n3) SPFA:O(n+m),这是平均复杂度实际上为O(玄学) Dijkstra:O(n+2m),堆优化以后 因 ...
- 精通javascript笔记(智能社)——简易tab选项卡及应用面向对象方法实现
javascript代码(常规方式/面向过程): <script type="text/javascript"> window.onload=function(){ v ...
- 如何去掉Json字符串中反斜杠
做项目的时候,遇到了这样的问题,前台传来的Json字符串在实体类中不对应(无法转换为实体类),而且传来的数据项是跟着数据库中的表的变动而变动的(不能重写实体类). 前台Json字符串为: string ...
- 【BZOJ3942】Censoring [KMP]
Censoring Time Limit: 10 Sec Memory Limit: 128 MB[Submit][Status][Discuss] Description 有一个S串和一个T串,长 ...
- 【BZOJ1996】【HNOI2010】合唱队 [区间DP]
合唱队 Time Limit: 4 Sec Memory Limit: 64 MB[Submit][Status][Discuss] Description Input Output Sample ...
- 【洛谷 P4777】 【模板】扩展中国剩余定理(EXCRT)
注意一下:: 题目是 \[x≡b_i\pmod {a_i}\] 我总是习惯性的把a和b交换位置,调了好久没调出来,\(qwq\). 本题解是按照 \[x≡a_i\pmod {b_i}\] 讲述的,请注 ...
- [bzoj3277==bzoj3473]出现k次子串计数——广义后缀自动机+STL
Brief Description 给定n个字符串,对于每个字符串,您需要求出在所有字符串中出现次数大于等于k次的子串个数. Algorithm Design 先建立一个广义后缀自动机,什么是广义后缀 ...