C. Dasha and Password
来源:互联网 发布:python 接口开发 编辑:程序博客网 时间:2024/06/08 13:01
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
题意就是n个串,每个串的初始光标都位于0(列)处,问你怎样移动光标能够在凑出密码(每个串的光标位置表示一个密码的字符,密码至少包含3种字符:数字,小写字母,特殊符号)的情况下使得移动的光标步数最小。
思路:首先考虑到的便是深搜,不过50的阶乘的复杂度是撑不住的,我们要简化一下。
首先就是要这样考虑,对于一个字符串,我们要的其实就是一个属性,这个属性指的就是那3种字符里面的一种。可以想一下,如果前面的串已经凑齐了3种字符了,那么后面的串就完全没有必要去凑了,就是0。
所以说,其实对于每一个串,我们只需要记录3种状态的最小值就可以了。因为后面更大的状态完全没有必要。我们只是要求一个串到达某个属性就可以了。
这样复杂度就可以降到了50*49*48的情况。
之后看一下代码就懂啦。
#include <bits/stdc++.h>using namespace std;const int MAXN=55;const int inf =0x3f3f3f3f;int n,m;int w[MAXN][10];char s[MAXN];int ans;bool vis[MAXN];void dfs(int j,int sum){ if(j==3) { ans=min(ans,sum); return; } if(sum>ans)return; for(int i=0;i<n;++i) { if(!vis[i]) { vis[i]=1; dfs(j+1,sum+w[i][j]); vis[i]=0; } }}int main(){ int i,j; scanf("%d%d",&n,&m); memset(w,inf,sizeof(w)); for(i=0;i<n;++i) { scanf("%s",s); for(j=0;j<m;++j) { if(isdigit(s[j]))w[i][0]=min(w[i][0],min(j,m-j)); else if(islower(s[j]))w[i][1]=min(w[i][1],min(j,m-j)); else w[i][2]=min(w[i][2],min(j,m-j)); } } ans=inf; dfs(0,0); printf("%d\n",ans); return 0;}
- C. Dasha and Password
- Codeforces-Round 394#C-Dasha and Password'
- CF761 C. Dasha and Password (DP)
- Codeforces 761C DaSha and Password 暴力
- Codeforces 761C Dasha and Password 【模拟】
- Codeforces 761C-Dasha and Password
- 【Codeforces 761 C Dasha and Password 】
- CodeForces 761C Dasha and Password
- 761C Dasha and Password[简单dp]
- C. Dasha and Password(模拟,暴力)
- CodeForces 761C Dasha and Password
- Codeforces 761C Dasha and Password
- C. Dasha and Password-C. Dasha and Password-暴力|dp思想
- Codeforces Round #394 (Div. 2)(A. Dasha and Stairs,B. Dasha and friends,C. Dasha and Password)
- 【codeforces 761C】Dasha and Password(动态规划做法)
- 【codeforces 761C】Dasha and Password(贪心+枚举做法)
- Codeforces Round #394 (Div. 2) C. Dasha and Password
- Codeforces Round #394 (Div. 2)C. Dasha and Password【暴力】
- 算法训练 2的次幂表示
- PLSQL 变数的永续性
- 简洁精美源于分析透彻,构思明确、求精,逻辑练达。(11)
- 设置Python包搜索路径
- 炸金花:三条、同花顺、金花、顺子、对子等出现的几率
- C. Dasha and Password
- #WEBGIS#校园地图系统开发-地图数据发布
- CDQ分治——NKOJ3210 围牛群
- hql语句的增删改查
- python之简单主机批量管理工具
- win7 64位上 Loadrunner11打不开IE浏览器的问题终于解决啦
- PLSQL 定数宣言
- hibernate进阶之对象的状态
- 简洁精美源于分析透彻,构思明确、求精,逻辑练达。(12)