Codeforces Round #394 (Div. 2) C. Dasha and Password —— 枚举
来源:互联网 发布:nc数据库 编辑:程序博客网 时间:2024/05/22 10:57
题目链接:http://codeforces.com/problemset/problem/761/C
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.
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.
Print one integer — the minimum number of operations which is necessary to make the string, which is displayed on the screen, a valid password.
3 41**2a3*0c4**
1
5 5#*&#**a1c&&q2w*#a3c#*&#*&
3
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<=50,所以即使O(n^3)的复杂度仍绰绰有余。
1.op[i][t]记录:在第i行,跳到类型为t的字符的最少操作数。
2.三个for循环,枚举三种字符所在的行,取最少的操作数之和,即为答案。
代码如下:
#include<bits/stdc++.h>using namespace std;typedef long long LL;const double eps = 1e-6;const int INF = 2e9;const LL LNF = 9e18;const int mod = 1e9+7;const int maxn = 50*50+10;int n,m, op[55][4];char s[100];int main(){ cin>>n>>m; for(int i = 1; i<=n; i++) //初始化操作次数为无限大 for(int j = 1; j<=3; j++) op[i][j] = INF/3; for(int i = 1; i<=n; i++) { scanf("%s",s); for(int j = 0; j<m; j++) { int t; //t为type, 即字符的类型 if(s[j]=='#' ||s[j]=='*' || s[j]=='&') t = 1; else if(s[j]>='a' && s[j]<='z') t = 2; else t = 3; op[i][t] = min(op[i][t], min(j,m-j)); //左右两个方向的移动都需考虑 } } int ans = INF; for(int i = 1; i<=n; i++) //枚举" # * & "所在的行 { for(int j = 1; j<=n; j++) //枚举abcd……所在的行 { if(i==j) continue; for(int k = 1; k<=n; k++) //枚举123……所在的行 { if(k==i || k==j) continue; ans = min(ans, op[i][1]+op[j][2]+op[k][3]); //操作次数之和 } } } cout<<ans<<endl;}
- Codeforces Round #394 (Div. 2) C. Dasha and Password —— 枚举
- Codeforces Round #394 (Div. 2) C. Dasha and Password 贪心+预处理+枚举
- Codeforces Round #394 (Div. 2) C. Dasha and Password
- Codeforces Round #394 (Div. 2)C. Dasha and Password【暴力】
- Codeforces Round #394 (Div. 2) C. Dasha and Password
- Codeforces Round #394 (Div. 2)(A. Dasha and Stairs,B. Dasha and friends,C. Dasha and Password)
- Codeforces Round #394 (Div. 2) Dasha and Password
- Codeforces-Round 394#C-Dasha and Password'
- Codeforces Round #394 (Div. 2) B. Dasha and friends —— 暴力 or 最小表示法
- Codeforces Round #394 (Div. 2) D. Dasha and Very Difficult Problem —— 贪心
- Codeforces Round #394 (Div. 2) E. Dasha and Puzzle —— 构造 + 贪心
- 【codeforces 761C】Dasha and Password(贪心+枚举做法)
- Codeforces Round #394 (Div. 2)Dasha and friends
- Codeforces Round #394 (Div. 2)Dasha and Stairs
- Codeforces Round #394 (Div. 2)Dasha and Very Difficult Problem
- Codeforces Round #394(Div. 2)A.Dasha and Stairs【水题】
- Codeforces Round #394 (Div. 2) Dasha and Very Difficult Problem
- Codeforces Round #394 (Div. 2)-E. Dasha and Puzzle(dfs)
- Python:你需要知道的七七八八(基础篇)
- 实验楼 Linux 学习笔记
- centos 7下面装samba
- Linux 常用命令
- Java基础之重写(覆写)与重载区别
- Codeforces Round #394 (Div. 2) C. Dasha and Password —— 枚举
- centos7 firewall 防火墙 命令
- Android-自定义UI-设置画布的大小
- EffectiveJava(笔记五) 枚举和注解
- 20170708-leetcode-258-Add Digits
- js中取小数整数部分函数
- gulp的实时刷新、代码压缩、代码排错
- Java 获取指定日期的方法总结
- Android开发进阶—Android的消息机制