102522: [AtCoder]ABC252 C - Slot Strategy

Memory Limit:256 MB Time Limit:2 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

Score : $300$ points

Problem Statement

There is a slot machine with $N$ reels.
The placement of symbols on the $i$-th reel is represented by a string $S_i$ of length $10$ containing each of 0, 1, $\ldots$, 9 exactly once.

Each reel has a corresponding button. For each non-negative integer $t$, Takahashi can press one of the buttons of his choice (or do nothing) $t$ seconds after the reels start spinning.
If the button for the $i$-th reel is pressed $t$ seconds after the start of the spin, the $i$-th reel will stop to display the $((t\bmod{10})+1)$-th character of $S_i$.
Here, $t\bmod{10}$ denotes the remainder when $t$ is divided by $10$.

Takahashi wants to stop all reels to make them display the same character.
Find the minimum number of seconds needed to achieve his objective after the start of the spin.

Constraints

  • $2\leq N\leq 100$
  • $N$ is an integer.
  • $S_i$ is a string of length $10$ containing each of 0, 1, $\ldots$, 9 exactly once.

Input

Input is given from Standard Input in the following format:

$N$
$S_1$
$S_2$
$\vdots$
$S_N$

Output

Print the minimum number of seconds needed to achieve Takahashi's objective after the start of the spin.


Sample Input 1

3
1937458062
8124690357
2385760149

Sample Output 1

6

Takahashi can make all reels display 8 in $6$ seconds after the start of the spin by stopping them as follows.

  • $0$ seconds after the start of the spin, press the button for the $2$-nd reel, making it stop to display the $((0\bmod{10})+1=1)$-st character of $S_2$, 8.
  • $2$ seconds after the start of the spin, press the button for the $3$-rd reel, making it stop to display the $((2\bmod{10})+1=3)$-rd character of $S_3$, 8.
  • $6$ seconds after the start of the spin, press the button for the $1$-st reel, making it stop to display the $((6\bmod{10})+1=7)$-th character of $S_1$, 8.

There is no way to make all reels display the same character in five seconds or less, so the answer is $6$.


Sample Input 2

5
0123456789
0123456789
0123456789
0123456789
0123456789

Sample Output 2

40

Note that he must stop all reels to make them display the same character.

Input

Output

得分:300分

问题描述

有一个有N个转轮的老虎机。
第i个转轮上的符号放置表示为一个长度为10的字符串$S_i$,其中恰好包含01,$\ldots$,9各一次。

每个转轮都有一个对应的按钮。对于每个非负整数$t$,Takahashi可以在转轮开始旋转后的$t$秒内选择按下其中一个按钮(或不做任何操作)。
如果在转轮开始旋转后的$t$秒内按下第i个转轮的按钮,那么第i个转轮将停止显示$S_i$的第$((t\bmod{10})+1)$个字符。
在这里,$t\bmod{10}$表示将$t$除以10的余数。

Takahashi想要停止所有转轮,使它们显示相同的字符。
找出在转轮开始旋转后实现他的目标所需的最短秒数。

约束条件

  • $2\leq N\leq 100$
  • $N$是一个整数。
  • $S_i$是一个长度为10的字符串,其中恰好包含01,$\ldots$,9各一次。

输入

输入从标准输入中给出,格式如下:

$N$
$S_1$
$S_2$
$\vdots$
$S_N$

输出

打印在转轮开始旋转后实现Takahashi的目标所需的最短秒数。


样例输入1

3
1937458062
8124690357
2385760149

样例输出1

6

Takahashi可以通过以下方式在转轮开始旋转后6秒内使所有转轮显示8

  • 在转轮开始旋转后0秒,按下第2个转轮的按钮,使其停止显示$S_2$的第一个字符8
  • 在转轮开始旋转后2秒,按下第3个转轮的按钮,使其停止显示$S_3$的第三个字符8
  • 在转轮开始旋转后6秒,按下第1个转轮的按钮,使其停止显示$S_1$的第七个字符8

无法在5秒或更短的时间内使所有转轮显示相同的字符,所以答案是6。


样例输入2

5
0123456789
0123456789
0123456789
0123456789
0123456789

样例输出2

40

注意,他必须停止所有转轮,使它们显示相同的字符。

加入题单

算法标签: