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

题意:已知n个长度为m的字符串(看作环),每个字符串的首字符上有一个指针,可以通过一次操作使指针左移或右移一位,求最少多少次操作可使这n个指针指向的字符构成一个合法字符串(即存在特殊符号#*&其一、数字和小写字母)。 

算法:枚举数字、字母和特殊符号分别取自哪一个字符串,计算需要操作次数并取最小值即可。

//这题纯暴力,感开始在预处理的时候有点问题,wa了好多次,暴力枚举三种字符再哪个字符串上就行了。
#include <iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
int ans,n,m;
char ch[55];
int a[55],b[55],c[55];
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
    {
        scanf("%s",&ch);
        a[i]=m;
        b[i]=m;
        c[i]=m;
        for(int j=0;j<m;j++)
        {
             if (ch[j]>='0' && ch[j]<='9') a[i]=min(a[i],min(j,m-j));
             else
             if (ch[j]>='a' && ch[j]<='z') b[i]=min(b[i],min(j,m-j));
             else
             if (ch[j]=='&' || ch[j]=='*' || ch[j]=='#') c[i]=min(c[i],min(j,m-j));
        }
    }
    //for(int i=1;i<=n;i++)
      //printf("%d %d %d
",a[i],b[i],c[i]);
    ans=300;
    for(int i=1;i<=n;i++)
    {
       if (a[i]==m) continue;
         for(int j=1;j<=n;j++)
         {
          if (i==j || b[j]==m) continue;
          for(int k=1;k<=n;k++)
          {
            if (k==i || k==j ||c[k]==m) continue;
            ans=min(ans,a[i]+b[j]+c[k]);
          }
         }
    }
     printf("%d
",ans);
    return 0;
}
原文地址:https://www.cnblogs.com/stepping/p/6361531.html