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;
}

最新文章

  1. java开发命名规范
  2. Jetty与Tomcat的区别 转
  3. Android打Path的方法
  4. Jquery API Hybrid APP调研
  5. cocos2d-x 游戏暂停界面,监听home键,返回键,Menu键 解决方案
  6. Zend Studio 上 安装使用Aptana插件(html,css,js代码提示功能) .
  7. NopCommerce 数据库初始化
  8. nginx 显示discuz页面
  9. iOS动态管理AutoLayout的约束NSLayoutConstraint
  10. cnBlogs_代码着色
  11. CRC循环校验码
  12. C#的WebBrowser控制浏览
  13. 原来在ARC下还有这么多不同?!
  14. 纯CSS实现垂直居中的7种方法
  15. Vue CLI 3+tinymce 5富文本编辑器整合
  16. 【SoftwareTesting】Lab 2
  17. GCC&amp;&amp;GDB在OI中的介绍
  18. JS面试典型常见问题与解答
  19. 用docker搭建ss访问火星
  20. eclipse中文字体大小修改,让中英文字体协调

热门文章

  1. mysql各个引擎区别
  2. 移动端fixed定位在底部,出现键盘后消失
  3. golang之常量
  4. Hashkell 第一篇
  5. Directx11 教程(1) 基本的windows应用程序框架(1)
  6. 解决VS+Qt不生成moc文件问题
  7. python内置函数and匿名函数
  8. Myeclipse 设置默认注释
  9. @loj - 2480@ 「CEOI2017」One-Way Streets
  10. Hbase API: 读Bigtable