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

思路:
这题不难。暴力枚举每一行数字,字母,符号的最小距离,取最小值,求和就可以了。
但是,这道题说,指针可以左右移动,还有就是当一行取过之后,就不能再取了。所以最后
求最小值for循环的时候,用的是else if。

代码:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
using namespace std;
const int INF=1e9;
int a[55][3];
char s[55][55];
bool S_H(char t)
{
    if(t<='9'&&t>='0')
        return 1;
    return 0;
}
bool Z_F(char t)
{
    if(t<='z'&&t>='a')
        return 1;
    return 0;
}
bool F_H(char t)
{
    if(t=='#'||t=='&'||t=='*')
        return 1;
    return 0;
}
int main()
{
    int n,m,num=0;
    int p1,p2,p3;
    scanf("%d%d%*c",&n,&m);
    for(int i=0; i<n; i++)
        gets(s[i]);
    for(int i=0; i<n; i++)
        a[i][0]=a[i][1]=a[i][2]=1e9;
    for(int i=0; i<n; i++)
    {
        for(int j=0; j<m; j++)
        {
            if(S_H(s[i][j]))
                a[i][0]=min(j,a[i][0]);
            else if(Z_F(s[i][j]))
                a[i][1]=min(j,a[i][1]);
            else if(F_H(s[i][j]))
                a[i][2]=min(j,a[i][2]);
        }
        for(int j=m-1;j>=0;j--)
        {
            if(S_H(s[i][j]))
                a[i][0]=min(m-j,a[i][0]);
            else if(Z_F(s[i][j]))
                a[i][1]=min(m-j,a[i][1]);
            else if(F_H(s[i][j]))
                a[i][2]=min(m-j,a[i][2]);
        }
    }
//    for(int i=0; i<n; i++)
//        printf("%d %d %d\n",a[i][0],a[i][1],a[i][2]);
    int ms=1e9,mz=1e9,mf=1e9;
    for(int i=0; i<n; i++)
    {
        if(a[i][0]<ms)
            ms=a[i][0];
       else if(a[i][1]<mz)
            mz=a[i][1];
        else if(a[i][2]<mf)
            mf=a[i][2];
    }
    printf("%d\n",ms+mz+mf);
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值