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 indexes 1in 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 nm (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...

代码:

 #include "cstdio"
 #include "algorithm"
 #include "cstring"
 #include "queue"
 #include "cmath"
 using  namespace std;
 ;
 #define  inf 0x3f3f
 ][];
 ][];//
 int main(){
     int n,m;
     scanf("%d%d",&n,&m);
     ;i<;i++){
         ;j<;j++){
             d[i][j]=;
         }
     }
     ;i<n;i++){
         scanf("%s",s[i]);
     }
 //    memset(d,inf, sizeof(d)); 用inf初始化会溢出导致结果❌卡了好一会
     ;i<n;i++){
         ;j<=m/;j++){
             '){
                 d[i][]=min(d[i][],j);
             }
             if(s[i][j]>='a'&&s[i][j]<='z'){
                 d[i][]=min(d[i][],j);
             }
             if(s[i][j]=='#'||s[i][j]=='*'||s[i][j]=='&'){
                 d[i][]=min(d[i][],j);
             }
             ){
                 ;k<=m/;k++){
                    '){
                        d[i][]=min(d[i][],k);
                    }
                     else if(s[i][m-k]>='a'&&s[i][m-k]<='z'){
                        d[i][]=min(d[i][],k);
                    }
                     ]=min(d[i][],k);}
                 }
             }
         }
     }
     int a,b,c,x=inf;
     ;i<n;i++){
         ;j<n;j++){
             ;k<n;k++){
                 a=min(d[i][]+d[j][]+d[k][],d[i][]+d[k][]+d[j][]);
                 b=min(d[k][]+d[i][]+d[j][],d[k][]+d[j][]+d[i][]);
                 c=min(d[j][]+d[k][]+d[i][],d[j][]+d[i][]+d[k][]);
                 x=min(x,min(min(a,b),c));
             }
         }
     }

     printf("%d\n",x);
     ;
 }

Div.2 C. Dasha and Password的更多相关文章

  1. 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 ...

  2. Codeforces Round #394 (Div. 2) C. Dasha and Password 暴力

    C. Dasha and Password 题目连接: http://codeforces.com/contest/761/problem/C Description After overcoming ...

  3. 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 ...

  4. Codeforces Round #394 (Div. 2) C. Dasha and Password —— 枚举

    题目链接:http://codeforces.com/problemset/problem/761/C C. Dasha and Password time limit per test 2 seco ...

  5. Codeforces Round #394 (Div. 2) C.Dasha and Password(暴力)

    http://codeforces.com/contest/761/problem/C 题意:给出n个串,每个串的初始光标都位于0(列)处,怎样移动光标能够在凑出密码(每个串的光标位置表示一个密码的字 ...

  6. 【枚举】Codeforces Round #394 (Div. 2) C. Dasha and Password

    纪念死去的智商(虽然本来就没有吧……) 三重循环枚举将哪三个fix string作为数字.字母和符号位.记下最小的值就行了. 预处理之后这个做法应该是O(n^3)的,当然完全足够.不预处理是O(n^3 ...

  7. 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 ...

  8. Codeforces 761C Dasha and Password(枚举+贪心)

    题目链接 Dasha and Password 题目保证一定有解. 考虑到最多只有两行的指针需要移动,那么直接预处理出该行移动到字母数字或特殊符号的最小花费. 然后O(N^3)枚举求最小值即可. 时间 ...

  9. 【codeforces 761C】Dasha and Password(动态规划做法)

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

随机推荐

  1. Keepalived详细介绍简介

    1.1.Keepalived简介 Keepalived是Linux下一个轻量级别的高可用解决方案.高可用(High Avalilability,HA),其实两种不同的含义:广义来讲,是指整个系统的高可 ...

  2. AFNetworking3.0为何弃用了NSURLConnection

    http://blog.csdn.net/qq_34101611/article/details/51698524 上篇博客说到AFNetworking3.0只提供了NSURLSession的支持.其 ...

  3. ubuntu12.04+fuerte 下跑通lsd-slam——数据集

    lsd-slam(下载链接:https://github.com/tum-vision/lsd_slam)提供了两种方法,一种是用数据集(下载地址http://vision.in.tum.de/lsd ...

  4. BLE 安卓APP控制LED灯的实现(转)

    源:BLE 安卓APP控制LED灯的实现 //注:参考AmoMcu源代码修改. 打开APP,检查蓝牙是否打开 BluetoothAdapter mBluetoothAdapter; final Blu ...

  5. iOS开发中视图控制器ViewControllers之间的数据传递

    iOS开发中视图控制器ViewControllers之间的数据传递 这里我们用一个demo来说明ios是如何在视图控制器之间传递重要的参数的.本文先从手写UI来讨论,在下一篇文章中讨论在storybo ...

  6. Django 设置cookies与获取cookies.

    在Django里面,使用Cookie和Session看起来好像是一样的,使用的方式都是request.COOKIES[XXX]和request.session[XXX],其中XXX是您想要取得的东西的 ...

  7. Python - Headless Selenium WebDriver Tests using PyVirtualDisplay

    Here is some Python code showing WebDriver with a virtual display provided by Xvfb: #!/usr/bin/env p ...

  8. 背景图片等比缩放的写法background-size简写法

    1.背景图片或图标也可像img一样给其宽高就能指定其缩放大小了. 比如一个实际宽高36*28的图标,要缩小一半引用进来的写法就是: background:rgba(0, 0, 0, 0) url(&q ...

  9. Java经典案例之-“最大公约数和最小公倍数”

    /** * 描述:输入两个正整数m和n,求其最大公约数和最小公倍数.(最大公约数:最大公约数, * 也称最大公因数.最大公因子,指两个或多个整数共有约数中最大的一个.) * (最小公倍数:几个数共有的 ...

  10. JS 上传文件 Uploadify 网址及 v3.2.1 参数说明

    http://www.uploadify.com/ 一.属性 属性名称 默认值 说明 auto true 设置为true当选择文件后就直接上传了,为false需要点击上传按钮才上传 . buttonC ...