CF761C.Dasha and Password

普及/提高-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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.

达莎爬完楼梯后到达了教室。她需要输入一个密码才能开始上课。该密码是一个长度为 nn 的字符串,需满足以下要求:

  • 字符串中至少包含一个数字;
  • 字符串中至少包含一个拉丁字母的小写字母;
  • 字符串中至少包含以下三个符号之一:'#'、'*'、'&'。

考虑到这是编程课,输入密码并不容易。

对于密码的每个字符,我们有一个固定长度为 mm 的字符串;在这些 nn 个字符串中的每一个上,都有一个指向某个字符的指针。屏幕上显示的第 ii 个字符,即为第 ii 个字符串中被指针所指向的字符。初始时,所有指针均指向各自字符串中索引为 1 的字符(所有位置编号均从 1 开始)。

在一次操作中,达莎可在某一个字符串中将指针向左或向右移动一个字符。字符串是循环的:即当指针位于索引为 1 的字符上并向左移动时,它会移动到索引为 mm 的字符;当指针位于索引为 mm 的字符上并向右移动时,它会移动到索引为 1 的字符。

你需要计算出使屏幕上显示的字符串成为一个合法密码所需的最少操作次数。

输入格式

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.

第一行包含两个整数 nn、mm(3 ≤ n ≤ 503 \leq n \leq 50,1 ≤ m ≤ 501 \leq m \leq 50)—— 分别表示密码的长度以及分配给密码各符号的字符串的长度。

接下来的 nn 行中,第 ii 行包含一个长度为 mm 的字符串,该字符串被分配给密码字符串的第 ii 个符号。该字符串仅由数字、小写英文字母以及字符 #、* 或 & 组成。

保证输入数据满足:总能构造出一个合法的密码。

输出格式

Print one integer — the minimum number of operations which is necessary to make the string, which is displayed on the screen, a valid password.

输出一个整数——使屏幕上显示的字符串成为有效密码所需的最少操作次数。

输入输出样例

  • 输入#1

    3 4
    1**2
    a3*0
    c4**

    输出#1

    1
  • 输入#2

    5 5
    #*&#*
    *a1c&
    &q2w*
    #a3c#
    *&#*&

    输出#2

    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.

在第一个测试中,需要将第三行的指针向左移动一位,以得到最优答案。

在第二个测试中,一种可能的算法为:

  • 将第二行的指针向右移动一次;
  • 将第三行的指针向右移动两次。

输入解题思路,AI测评打分。不知道怎么写?

首页