Codeforces Round #394 (Div. 2) C. Dasha and Password —— 枚举
题目链接:http://codeforces.com/problemset/problem/761/C
2 seconds
256 megabytes
standard input
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 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 4
1**2
a3*0
c4**
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 暴力
C. Dasha and Password 题目连接: http://codeforces.com/contest/761/problem/C Description After overcoming ...
- Codeforces Round #394 (Div. 2) C. Dasha and Password
C. Dasha and Password time limit per test 2 seconds memory limit per test 256 megabytes input standa ...
- Codeforces Round #394 (Div. 2) C. Dasha and Password(简单DP)
C. Dasha and Password time limit per test 2 seconds memory limit per test 256 megabytes input standa ...
- Codeforces Round #394 (Div. 2) C.Dasha and Password(暴力)
http://codeforces.com/contest/761/problem/C 题意:给出n个串,每个串的初始光标都位于0(列)处,怎样移动光标能够在凑出密码(每个串的光标位置表示一个密码的字 ...
- 【枚举】Codeforces Round #394 (Div. 2) C. Dasha and Password
纪念死去的智商(虽然本来就没有吧……) 三重循环枚举将哪三个fix string作为数字.字母和符号位.记下最小的值就行了. 预处理之后这个做法应该是O(n^3)的,当然完全足够.不预处理是O(n^3 ...
- Codeforces Round #394 (Div. 2) E. Dasha and Puzzle(分形)
E. Dasha and Puzzle time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Codeforces Round #394 (Div. 2) E. Dasha and Puzzle 构造
E. Dasha and Puzzle 题目连接: http://codeforces.com/contest/761/problem/E Description Dasha decided to h ...
- Codeforces Round #394 (Div. 2) D. Dasha and Very Difficult Problem 贪心
D. Dasha and Very Difficult Problem 题目连接: http://codeforces.com/contest/761/problem/D Description Da ...
- Codeforces Round #394 (Div. 2) B. Dasha and friends 暴力
B. Dasha and friends 题目连接: http://codeforces.com/contest/761/problem/B Description Running with barr ...
随机推荐
- 济南day6
上午 60+0+5 数组开小了 暴力打挂了 下午 0+0+30 T1爆零 //T1,T2文件打错了.... 暴力打挂
- linux的history命令设置
history的历史记录,同一个用户的各个会话,读取到的内容也是不一样的,原因是它读取的是shell会话缓存里的内容.只有当用户退出当前会话的时候,会话里的缓存内容才会写入~/.bash_histor ...
- System::String *,char*,string 等的类型转换 [转]
在VC 的编程中,经常会用到各种类型的转换,在MFC中textbox等控件得到的返回类型是System::String *,而写入的文件要求是 const char *类型的,下面介绍一些转换的方法: ...
- django(一)--- 安装django
准备好虚拟环境:Python开发虚拟环境 安装前的准备 1. 下载django:django下载 本文使用的是django-1.5.9(不同版本号之间的差别还是比較大的.别搞错了) 2.准备djang ...
- windows ffmpeg 推送摄像头数据到rtmp服务
文本主要讲述windows系统下如何利用ffmpeg获取摄像机流并推送到rtmp服务,命令的用法前文 中有讲到过,这次是通过代码来实现.实现该项功能的基本流程如下: 图1 ffmpeg推流流程图 较前 ...
- Linux虚拟服务器--LVS
LVS 百科名片 LVS是一个开源的软件,由毕业于国防科技大学的章文嵩博士于1998年5月创立,可以实现LINUX平台下的简单负载均衡.LVS是Linux Virtual Server的缩写,意思是L ...
- HDU 3564 Another LIS splay(水
题意: 给定一个空序列 插入n个数(依次插入 1.2.3.4··n) 以下n个数表示i插在哪个位置. 每插入一个数后输出这个序列的lis 然后... 由于每次插入的数都是当前序列最大的数 所以不会影响 ...
- Django-select_related优化查询
对于一对一字段(OneToOneField)和外键字段(ForeignKey),可以使用select_related 来对QuerySet进行优化. select_related 返回一个QueryS ...
- mysql 数据类型+约束+关联
1.什么是存储引擎存储引擎就是表的类型,针对不同的存储引擎,mysql会有不同的处理逻辑 2.存储引擎介绍InnoDB| DEFAULT | Supports transactions, row-le ...
- python--网络编程--socket
网络通信标准---网络协议 互联网协议--osi七层协议 五层协议:应用层:应用层.表示层.会话层 传输层:传输层 网络层:网络层 数据链路层:数据链路层 物理层: ...