文章标题 coderforces 761C : Dasha and Password(贪心+暴力)

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

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个串中每个串找出一个字符组成一个密码,这密码要求至少要有一个数字,一个字母和一个特殊字符,一开始每个字符串都指在第一个字符,每次移动可以往两边移动,也就是说可以往左右两边移动,要求所得到的密码移动的次数最少。
分析:要求最少的次数,所以移动最少的串,每个串移动最少的次数就行了,所以要求三个至少,所以我们可以找出三个串,每个串移动来找到一个条件,最后枚举最多50*49*48种情况,三个for循环就行。
代码:

#include
#include
#include
#include
#include
#include
#include
#include 
#include
using namespace std;
const int inf = 0x3f3f3f3f;
int num[55];//num[i]表示第i个串中最少需要移动的次数得到数字 
int zimu[55];//zimu[i]表示第i个串中最少需要移动的次数得到字母 
int fuhao[55];//fuhao[i]表示第i个串中最少需要移动的次数得到特殊符号 
int n,m;
string s[55]; 
int main ()
{
    while (cin>>n>>m){
        for (int i=0;i<55;i++){
            num[i]=zimu[i]=fuhao[i]=1e6;//一开始距离默认为无穷大 
        }
        for (int i=0;icin>>s[i];
        } 
        for (int i=0;ifor (int j=0;j//找数字,如果在第一个能找到 
                if (j==0&&s[i][j]<='9'&&s[i][j]>='0'){
                    num[i]=j;
                    break;
                }
                if (j!=0){//往两边找 
                    if ((s[i][j]<='9'&&s[i][j]>='0')||(s[i][m-j]<='9'&&s[i][m-j]>='0')){
                        num[i]=j;
                        break;
                    }
                }
            }
            for (int j=0;j//字母,同上 
                if (j==0&&s[i][j]<='z'&&s[i][j]>='a'){
                    zimu[i]=j;
                    break;
                }
                if (j!=0&&(s[i][j]<='z'&&s[i][j]>='a'||s[i][m-j]<='z'&&s[i][m-j]>='a')){
                    zimu[i]=j;
                    break;
                } 
            }
            for (int j=0;j//特殊符号,同上 
                if (j==0&&(s[i][j]=='#'||s[i][j]=='*'||s[i][j]=='&')){
                    fuhao[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]=='&')){
                    fuhao[i]=j;
                    break;
                } 
            }
        }
        int minx=inf;//找最小值,一开始默认为无穷大 
        for (int i=0;i//枚举所有情况 
            for (int j=0;jfor (int k=0;kif (i==j||i==k||k==j)continue;//三个串两两不能同一个串 
                    int tmp=num[i]+zimu[j]+fuhao[k];
                    minx=min(minx,tmp);
                }
            }
        }
        cout<return 0;
}

你可能感兴趣的:(贪心,暴力)