您的位置:首页 > 其它

【CodeForces - 761C Dasha and Password】 暴力 + DP

2017-08-23 10:36 239 查看


D - 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
4000
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.



题意:现在需要设置一种长度为n的密码,规定至少包含一个数字,至少包含一个小写字母,至少包含"#", "&", "*"中的一个。下面给出n个长度为m的串,在每个串中可以选择一位当做密码,光标会出现在每个串的首位字母下,选择字符需要将光标移动到该字符下(从第一位到最后一位只需向左移一位),问最小移动的次数为多少。

分析:注意到数据n最大只有50,那么就可以用一个dp[i][j]数组记录第i个串中第j中类型所需的移动次数(数字为类型1,字母为类型2,特殊符号为类型3,),最后3个for比较这三种类型移动次数和的最小值。

代码如下:
#include <map>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long

using namespace std;
const int MX = 55;
const int mod = 1e9 + 7;
const int INF = 2e9 + 5;

char s[MX][MX];
int dp[MX][5];

int main(){
int n, m;
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; i++){
scanf("%s", s[i]+1);
}
memset(dp, INF, sizeof(dp));
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
if(s[i][j] >= '0' && s[i][j] <= '9'){
dp[i][1] = min(dp[i][1], min(j - 1, m - j + 1));
}
else if(s[i][j] >= 'a' && s[i][j] <= 'z'){
dp[i][2] = min(dp[i][2], min(j - 1, m - j + 1));
}
else if(s[i][j] == '*' || s[i][j] == '#' || s[i][j] == '&'){
dp[i][3] = min(dp[i][3], min(j - 1, m - j + 1));
}
}
}
int ans = INF;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
if(j == i)  continue;
for(int k = 1; k <= n; k++){
if(k == i || k == j)    continue;
ans = min(ans, dp[i][1] + dp[j][2] + dp[k][3]);
}
}
}
printf("%d\n", ans);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: