【CodeForces - 761C Dasha and Password】 暴力 + DP
2017-08-23 10:36
239 查看
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; }
相关文章推荐
- Codeforces 335D Rectangles and Square 暴力 + DP
- Codeforces 839A Arya and Bran【暴力】
- Codeforces 915C Permute Digits(模拟,暴力)
- Codeforces1063D Candies for Children 【分类讨论】【暴力】
- codeforces 514C Watto and Mechanism (分段暴力)
- CodeForces-699B One Bomb 暴力搜索
- Codeforces 673C Bear and Colors【暴力枚举】
- Codeforces 677D Vanya and Treasure 暴力+BFS
- Codeforces 548C - Mike and Frog(暴力循环节)
- Codeforces 392A Blocked Points(暴力)
- Codeforces Round #434 (Div. 2, based on Technocup 2018 Elimination Round 1)&&Codeforces 861A k-rounding【暴力】
- Codeforces 272E Dima and Horses【玄学暴力】
- CodeForces 474C Captain Marmot (数学,旋转,暴力)
- Codeforces 543B. Destroying Roads 最短路+暴力
- codeforces 814 C. An impassioned circulation of affection(暴力)
- Codeforces 490C Hacking Cypher【前缀模+后缀模+暴力】
- Codeforces 426 B Sereja and Mirroring【暴力枚举+模拟】水题
- CodeForces 446A DZY Loves Sequences (DP+暴力)
- CodeForces - 825B Five-In-a-Row (暴力模拟)
- Codeforces 463E Caisa and Tree(暴力)