C. Dasha and Password

来源:互联网 发布:python 接口开发 编辑:程序博客网 时间:2024/06/08 13:01

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

题意就是n个串,每个串的初始光标都位于0(列)处,问你怎样移动光标能够在凑出密码(每个串的光标位置表示一个密码的字符,密码至少包含3种字符:数字,小写字母,特殊符号)的情况下使得移动的光标步数最小。

思路:首先考虑到的便是深搜,不过50的阶乘的复杂度是撑不住的,我们要简化一下。

 首先就是要这样考虑,对于一个字符串,我们要的其实就是一个属性,这个属性指的就是那3种字符里面的一种。可以想一下,如果前面的串已经凑齐了3种字符了,那么后面的串就完全没有必要去凑了,就是0。

所以说,其实对于每一个串,我们只需要记录3种状态的最小值就可以了。因为后面更大的状态完全没有必要。我们只是要求一个串到达某个属性就可以了。

这样复杂度就可以降到了50*49*48的情况。

之后看一下代码就懂啦。

#include <bits/stdc++.h>using namespace std;const int MAXN=55;const int inf =0x3f3f3f3f;int n,m;int w[MAXN][10];char s[MAXN];int ans;bool vis[MAXN];void dfs(int j,int sum){    if(j==3)    {        ans=min(ans,sum);        return;    }    if(sum>ans)return;    for(int i=0;i<n;++i)    {        if(!vis[i])        {            vis[i]=1;            dfs(j+1,sum+w[i][j]);            vis[i]=0;        }    }}int main(){    int i,j;    scanf("%d%d",&n,&m);    memset(w,inf,sizeof(w));    for(i=0;i<n;++i)    {        scanf("%s",s);        for(j=0;j<m;++j)        {            if(isdigit(s[j]))w[i][0]=min(w[i][0],min(j,m-j));            else if(islower(s[j]))w[i][1]=min(w[i][1],min(j,m-j));            else w[i][2]=min(w[i][2],min(j,m-j));        }    }    ans=inf;    dfs(0,0);    printf("%d\n",ans);    return 0;}




0 0