time limit per test2 seconds

memory limit per test256 megabytes

inputstandard input

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

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.

【题目链接】:http://codeforces.com/contest/761/problem/C

【题解】



这题如果按照题目所给的思路会比较容易想一点;

即数字至少出现一次;

字母至少出现一次;

符号至少出现一次;

那么你就只要让他们仨都只出现一次就好(这样肯定是最优的);

然后枚举数字在哪一位出现,字母在哪一位出现,符号在哪一位出现;

一开始O(N*M)处理出每个位置变成数字、字母、符号的最少操作数(不需要操作就为0);

用O(N^3)3层循环枚举哪几位出现类数字、字母、符号;

当然不能同一个位置出现两种以上的类型;所以这3个位置都得不同;



【完整代码】

#include <bits/stdc++.h>
using namespace std; const int MAXN = 50+10; int n,m;
int a[MAXN][MAXN];
int change[MAXN][4];
char s[MAXN]; int main()
{
//freopen("F:\\rush.txt","r",stdin);
cin >> n >> m;
for (int i = 1;i <= n;i++)
{
scanf("%s",s+1);
for (int j = 1;j <= m;j++)
{
if (s[j]>='0' && s[j] <='9')
a[i][j]=1;
if (s[j]>='a' && s[j] <= 'z')
a[i][j]=2;
if (s[j]=='#' || s[j]=='*' || s[j] == '&')
a[i][j]=3;
}
}
for (int i = 1;i <= n;i++)
for (int j = 1;j <= 3;j++)
change[i][j] = 7e8;
for (int i = 1;i <= n;i++)
{
for (int j = 1;j <= m;j++)
change[i][a[i][j]] = min(change[i][a[i][j]],j-1);
for (int j = m;j >= 1;j--)
change[i][a[i][j]] = min(change[i][a[i][j]],m-j+1);
}
int ans = 7e8;
for (int i = 1;i <= n;i++)
for (int j = 1;j <= n;j++)
for (int k = 1;k <= n;k++)
{
if (i==j || i== k || j==k)
continue;
ans = min(ans,change[i][1]+change[j][2]+change[k][3]);
}
printf("%d\n",ans);
return 0;
}

【codeforces 761C】Dasha and Password(贪心+枚举做法)的更多相关文章

  1. Codeforces 761C Dasha and Password(枚举+贪心)

    题目链接 Dasha and Password 题目保证一定有解. 考虑到最多只有两行的指针需要移动,那么直接预处理出该行移动到字母数字或特殊符号的最小花费. 然后O(N^3)枚举求最小值即可. 时间 ...

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

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

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

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

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

  6. codeforces Gym 100338E Numbers (贪心,实现)

    题目:http://codeforces.com/gym/100338/attachments 贪心,每次枚举10的i次幂,除k后取余数r在用k-r补在10的幂上作为候选答案. #include< ...

  7. [Codeforces 1214A]Optimal Currency Exchange(贪心)

    [Codeforces 1214A]Optimal Currency Exchange(贪心) 题面 题面较长,略 分析 这个A题稍微有点思维难度,比赛的时候被孙了一下 贪心的思路是,我们换面值越小的 ...

  8. Div.2 C. Dasha and Password

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

  9. POJ 1018 Communication System 贪心+枚举

    看题传送门:http://poj.org/problem?id=1018 题目大意: 某公司要建立一套通信系统,该通信系统需要n种设备,而每种设备分别可以有m个厂家提供生产,而每个厂家生产的同种设备都 ...

随机推荐

  1. SurfaceFlinger与Surface概述

    基本原理: SF一个Client对应一个app中的SurfaceComposerClient, 分别是Binder的n端和b端,主要用来CreateSurface 一个app中有多个Activity, ...

  2. P1127

    题目描述 如果单词X的末字母与单词Y的首字母相同,则X与Y可以相连成X.Y.(注意:X.Y之间是英文的句号“.”).例如,单词dog与单词gopher,则dog与gopher可以相连成dog.goph ...

  3. Hdu 4143

    题目链接 好久没有在Hdu水题了,于是乎在无聊之际还是找了一道水题, 但是看完题目之后,明显是个数学题,我还是感觉有点打触的. 因为一直对数学题没有多大信心. 分析了一下,Y^2 = X^2 + n ...

  4. IO-01. 表格输出

    本题要求编写程序,按照规定格式输出表格. 输入格式: 本题目没有输入. 输出格式: 要求严格按照给出的格式输出下列表格: ------------------------------------Pro ...

  5. Thread class vs Runnnable interface(转)

    http://developer.51cto.com/art/201203/321042.htm Thread(Runnable target) Allocates a new Thread obje ...

  6. 阿里云发布SaaS加速器,用宜搭,像搭积木一样搭应用

    宜搭让不会编码的人也能快速搭建SaaS应用,大幅提升研发效率. (图:阿里云智能产品管理部总经理马劲在2019阿里云峰会·北京现场进行宜搭应用搭建演示. ) 3月21日,在2019阿里云峰会·北京上, ...

  7. n!素因子p的幂 swjtuOJ 2090【数论】

    原文地址:http://blog.csdn.net/u012717411/article/details/47334969(感谢作者) 素因子分解写的非常好!数论一道好题:给以两个大整数n,s(n&l ...

  8. Python 3.x版本中的字符串

  9. 快速删除项目中的输出日志console.log

    项目开发时,控制台往往有许多忘记删除或注释掉的输出日志.但是上线后总不能一个一个删吧,最近总结出几个解决思路 重写console.log方法,使其失去 输出能力 这个最直接有效,用vue框架的话放在m ...

  10. hdu 1561【树形dp+01背包】

    http://acm.hdu.edu.cn/showproblem.php?pid=1561 很容易想到如果是要攻克v城需要先攻克u城的话,可以建u到v的边.但是如果能够直接攻克u城呢?无边可建,这样 ...