题目链接:http://codeforces.com/problemset/problem/761/C

C. Dasha and Password
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

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.

Input

The first line contains two integers nm (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.

Output

Print one integer — the minimum number of operations which is necessary to make the string, which is displayed on the screen, a valid password.

Examples
input
3 4
1**2
a3*0
c4**
output
1
input
5 5
#*&#*
*a1c&
&q2w*
#a3c#
*&#*&
output
3
Note

In the first test it is necessary to move the pointer of the third string to one left to get the optimal answer.

In the second test one of possible algorithms will be:

  • to move the pointer of the second symbol once to the right.
  • to move the pointer of the third symbol twice to the right.

题解:

n<=50,所以即使O(n^3)的复杂度仍绰绰有余。

1.op[i][t]记录:在第i行,跳到类型为t的字符的最少操作数。

2.三个for循环,枚举三种字符所在的行,取最少的操作数之和,即为答案。

代码如下:

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const double eps = 1e-6;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+7;
const int maxn = 50*50+10; int n,m, op[55][4];
char s[100]; int main()
{
cin>>n>>m;
for(int i = 1; i<=n; i++) //初始化操作次数为无限大
for(int j = 1; j<=3; j++)
op[i][j] = INF/3; for(int i = 1; i<=n; i++)
{
scanf("%s",s);
for(int j = 0; j<m; j++)
{
int t; //t为type, 即字符的类型
if(s[j]=='#' ||s[j]=='*' || s[j]=='&')
t = 1;
else if(s[j]>='a' && s[j]<='z')
t = 2;
else
t = 3;
op[i][t] = min(op[i][t], min(j,m-j)); //左右两个方向的移动都需考虑
}
} int ans = INF;
for(int i = 1; i<=n; i++) //枚举" # * & "所在的行
{
for(int j = 1; j<=n; j++) //枚举abcd……所在的行
{
if(i==j) continue;
for(int k = 1; k<=n; k++) //枚举123……所在的行
{
if(k==i || k==j) continue;
ans = min(ans, op[i][1]+op[j][2]+op[k][3]); //操作次数之和
}
}
}
cout<<ans<<endl;
}

Codeforces Round #394 (Div. 2) C. Dasha and Password —— 枚举的更多相关文章

  1. Codeforces Round #394 (Div. 2) C. Dasha and Password 暴力

    C. Dasha and Password 题目连接: http://codeforces.com/contest/761/problem/C Description After overcoming ...

  2. 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 ...

  3. Codeforces Round #394 (Div. 2) C. Dasha and Password(简单DP)

    C. Dasha and Password time limit per test 2 seconds memory limit per test 256 megabytes input standa ...

  4. Codeforces Round #394 (Div. 2) C.Dasha and Password(暴力)

    http://codeforces.com/contest/761/problem/C 题意:给出n个串,每个串的初始光标都位于0(列)处,怎样移动光标能够在凑出密码(每个串的光标位置表示一个密码的字 ...

  5. 【枚举】Codeforces Round #394 (Div. 2) C. Dasha and Password

    纪念死去的智商(虽然本来就没有吧……) 三重循环枚举将哪三个fix string作为数字.字母和符号位.记下最小的值就行了. 预处理之后这个做法应该是O(n^3)的,当然完全足够.不预处理是O(n^3 ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. Codeforces Round #394 (Div. 2) B. Dasha and friends 暴力

    B. Dasha and friends 题目连接: http://codeforces.com/contest/761/problem/B Description Running with barr ...

随机推荐

  1. ML| EM

    What's xxx The EM algorithm is used to find the maximum likelihood parameters of a statistical model ...

  2. Maven的构建配置文件(Build Profiles)

    在命令行使用构建配置文件时,是-P,比如:mvn -Pinput 注意:这里的构建配置文件并不是一个真正的文件,而是通过指定参数来做特定的事. 以下内容引用自https://ayayui.gitboo ...

  3. php 打印今天,昨天,本周,上周,当月,上月,本季,上季,今年,去年数据

    if($filter['mode']) { switch ($filter['mode']) { case 1://今天的数据 $time_start = date("Y-m-d H:i:s ...

  4. Cesium之3D拉伸显示行政区

    转自原文 Cesium之3D拉伸显示行政区含GeoJSON数据生成过程GDAL的ogr2ogr Cesiumjs 是一套javascript库,用来渲染3D地球,2D区域地图,和多种GIS要素.不需要 ...

  5. 【java】Java transient关键字使用小记【转】

    转载地址:https://www.cnblogs.com/lanxuezaipiao/p/3369962.html 1. transient的作用及使用方法 我们都知道一个对象只要实现了Seriliz ...

  6. GO --微服务框架(二) goa

    之前用过go语言的反射来做一些代码生成,参考这篇. 但是这种方式,入侵太强,需要执行对应的申明调用, 所以对GOA框架的自动生成非常感兴趣,于是仔细研究了一下,发现用的比较巧妙, 这里先卖个关子,先看 ...

  7. MYSQL查询的四种情况

    1 普通连接查询 select 表1字段1,表2字段2,from 表1,表2,where 表1.字段1==表2.字段2 2 inner join查询 select 表1字段1 ,表2字段2,from ...

  8. ajax——dom对xml和html的操作

    上篇文章说到了dom的基础,dom能够操作xml和html,这次主要写利用dom的api去如何去操作xml和html文档. dom操作xml dom操作xml文档之前必须把xml文档装载到xml do ...

  9. Exchange 2013 Database Move to New Partition

    建议不要删除默认数据库,可以通过修改默认数据库名称.路径等实现您的需求. 客戶:The HK Anti-Cancer Society. 要求:遷移數據庫(01)到新分區,實際是遷移成為數據庫(05)  ...

  10. react 路由传参

    今天,我们要讨论的是react router中Link传值的三种表现形式.分别为通过通配符传参.query传参和state传参. ps:进入正题前,先说明一下,以下的所有内容都是在react-rout ...