Dasha and Password (暴力+贪心)

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.

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

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
using namespace std;
#define INF 0x3f3f3f3f
int digit[100],alph[100],ch[100];//表示第i个串得到数组字母字符所要移动的最少次数
int n,m;
string s[60];//储存n个字符串
int main(){
    int i,j,k;
    while(~scanf("%d%d",&n,&m)){
          for (i = 0; i < 55; i++){
            digit[i]=alph[i]=ch[i]=1e6;//这里不太明白赋值为1e6才ac,赋值为INF测试7错误
        }      //这里必须把每个数组赋值为无穷大,因为如果i行字符串中根本没有,那么如果为零的话那就成了在第一个了,用无穷大表示不存在
        for(i = 0; i < n; i++){
            cin >> s[i];//输入
        }
        for(i = 0; i < n; i++){//一个字符串一个字符串的看
            //数字
            for(j = 0; j < m; j++){
                if(j==0&&s[i][j]>='0'&&s[i][j]<='9'){//如果是第一个直接赋值
                    digit[i] = j;
                    break;
                }
                if(j!=0&&((s[i][j]>='0'&&s[i][j]<='9')||(s[i][m-j]>='0'&&s[i][m-j]<='9'))){//如果不是第一个,那么两边都要看左移或右移j单位
                    digit[i] = j;
                    break;
                }
            }
            //字母
            for(j = 0; j < m; j++){
                if(j==0&&s[i][j]>='a'&&s[i][j]<='z'){
                    alph[i] = j;
                    break;
                }
                if(j!=0&&((s[i][j]>='a'&&s[i][j]<='z')||(s[i][m-j]>='a'&&s[i][m-j]<='z'))){
                    alph[i] = j;
                    break;
                }
            }
            //字符
            for(j = 0; j < m; j++){
                if(j==0&&(s[i][j]=='#'||s[i][j]=='*'||s[i][j]=='&')){
                    ch[i] = j;
                    break;
                }
                if(j!=0&&(s[i][j]=='#'||s[i][j]=='*'||s[i][j]=='&'||s[i][m-j]=='#'||s[i][m-j]=='*'||s[i][m-j]=='&')){
                    ch[i] = j;
                    break;
                }
            }
        }
        int mint = INF;
        for(i = 0; i < n; i++){//三重循环找三个需要移动的串,找移动次数最小。
            for(j = 0; j < n; j++){
                for(k = 0; k < n; k++){
                    if(i==j||i==k||k==j)continue;//保证三个串不相同
                    int temp = digit[i]+alph[j]+ch[k];
                    mint = min(mint,temp);
                }
            }
        }
        printf("%d\n",mint);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值