Codeforces Round #394 (Div. 2) C. Dasha and Password 贪心+预处理+枚举
2017-02-01 12:19
363 查看
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 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
output
input
output
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.
Source
Codeforces Round #394 (Div. 2)
My Solution
题意:长度为n的密码必须包含至少一个字母一个数字一个非字母非数字的字符,给出n个长度为m的字符串,每个串取一个字符,要求移动最少的步数使所成的密码为合法的密码。
贪心+预处理+枚举
先贪心的预处理出每个字符串 v[i]的v[i].c表示移动到字母的最少步数,v[i].d表示移动到数字的最少步数,v[i].f表示移动到其它字符的最少步数。
先把.c.d.f都初始化为INF,然后扫一遍字符串,如果不存在相应的请求则值依然是INF。
因为n <= 50 所以显然是n^3的枚举,即枚举每一个.c.d.f要求它们来自不同的3个ijk。这样就不会漏掉什么情况了 Y ^_^ Y。
复杂度 O(n^3)
Thank you!
------from ProLights
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 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.
Source
Codeforces Round #394 (Div. 2)
My Solution
题意:长度为n的密码必须包含至少一个字母一个数字一个非字母非数字的字符,给出n个长度为m的字符串,每个串取一个字符,要求移动最少的步数使所成的密码为合法的密码。
贪心+预处理+枚举
先贪心的预处理出每个字符串 v[i]的v[i].c表示移动到字母的最少步数,v[i].d表示移动到数字的最少步数,v[i].f表示移动到其它字符的最少步数。
先把.c.d.f都初始化为INF,然后扫一遍字符串,如果不存在相应的请求则值依然是INF。
因为n <= 50 所以显然是n^3的枚举,即枚举每一个.c.d.f要求它们来自不同的3个ijk。这样就不会漏掉什么情况了 Y ^_^ Y。
复杂度 O(n^3)
#include <iostream> #include <cstdio> #include <string> #include <cstring> using namespace std; typedef long long LL; const int maxn = 1e2 + 8; const int INF = 1e9 + 8; string s[maxn]; struct p{ int d, c, f; }v[maxn]; int main() { #ifdef LOCAL freopen("c.txt", "r", stdin); //freopen("c.out", "w", stdout); int T = 4; while(T--){ #endif // LOCAL ios::sync_with_stdio(false); cin.tie(0); int n, m, i, j, k, cnt; cin >> n >> m; for(i = 0; i < n; i++){ cin >> s[i]; } //memset(v, -1, sizeof v); for(i = 0; i < n; i++){ v[i].c = v[i].d = v[i].f = INF; cnt = 0; for(j = 0; j < m; j++){ if(isalpha(s[i][j])) v[i].c = min(v[i].c ,cnt); else if(isdigit(s[i][j])) v[i].d = min(v[i].d, cnt); else v[i].f = min(cnt, v[i].f); cnt++; } cnt = 1; for(j = m - 1; j >= 0; j--){ if(isalpha(s[i][j])) v[i].c = min(v[i].c ,cnt); else if(isdigit(s[i][j])) v[i].d = min(v[i].d, cnt); else v[i].f = min(cnt, v[i].f); cnt++; } } int ans = INF; for(i = 0; i < n; i++){ if(v[i].c == INF) continue; for(j = 0; j < n; j++){ if(v[j].d == INF || i == j) continue; for(k = 0; k < n; k++){ if(v[k].f == INF || i == k || j == k) continue; ans = min(ans, v[i].c + v[j].d + v[k].f); } } } cout << ans << endl; #ifdef LOCAL cout << endl; } #endif // LOCAL return 0; }
Thank you!
------from ProLights
相关文章推荐
- Codeforces Round #410 (Div. 2)(A,字符串,水坑,B,暴力枚举,C,思维题,D,区间贪心)
- Codeforces Round #311 (Div. 2) C. Arthur and Table (枚举+贪心+思维)
- Codeforces Round #280 (Div. 2)---B. Vanya and Lanterns (贪心 + 预处理)
- Jury Meeting Codeforces Round #433 (Div. 2, based on Olympiad of Metropolises)(贪心,预处理?)
- 【枚举】【贪心】 Codeforces Round #398 (Div. 2) B. The Queue
- Codeforces Round #408 (Div. 2) C. Bank Hacking 无根树、贪心、枚举
- 黑马程序员——C语言热身——预处理指令、变量类型、static和extern、结构体、枚举
- 【枚举+贪心】POJ2718-Smallest Difference
- Codeforces Round #398 (Div. 2) E. Change-free 贪心 优先队列
- 【Codeforces Round 335 (Div 2) C】【贪心 脑洞 有趣排序】Sorting Railway Cars 全排列排序可以提前后提后的最小操作数
- Codeforces Round #377 (Div. 2) B. Cormen — The Best Friend Of a Man(贪心)
- SRM507 DIV1 L2 [枚举+模拟]
- Codeforces Round #378 (Div. 2) D. Kostya the Sculptor【贪心】
- 【贪心】Codeforces Round #401 (Div. 2) D. Cloud of Hashtags
- Codeforces Round #365 (Div. 2) -- B. Mishka and trip (找规律枚举)
- 算法初步--求最大子数组和【暴力|优化枚举|贪心 】求解
- Codeforces Round #401(Div. 2)E. Hanoi Factory【贪心+栈】
- Codeforces Round #177 (Div. 2)---E. Polo the Penguin and XOR operation(贪心)
- poj Gone Fishing 枚举加贪心 当初做的很纠结啊!!终于A了,与大家分享一下经验
- Codeforces Round #444 (Div. 2) D. Ratings and Reality Shows 贪心