Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
空でない文字列に対し、先頭の文字を末尾に移動する操作を左シフト、末尾の文字を先頭に移動する操作を右シフトと呼びます。
例えば、abcde
に対して左シフトを 1 回行うと bcdea
となり、右シフトを 2 回行うと deabc
となります。
英小文字からなる空でない文字列 S が与えられます。S に対し左シフトと右シフトをそれぞれ好きな回数(0 回でもよい)行って得られる文字列のうち、辞書順で最小のものと最大のものを求めてください。
辞書順とは?
辞書順とは簡単に説明すると「単語が辞書に載っている順番」を意味します。より厳密な説明として、英小文字からなる相異なる文字列 S, T の大小を判定するアルゴリズムを以下に説明します。
以下では「 S の i 文字目の文字」を S_i のように表します。また、 S が T より辞書順で小さい場合は S \lt T 、大きい場合は S \gt T と表します。
- S, T のうち長さが大きくない方の文字列の長さを L とします。i=1,2,\dots,L に対して S_i と T_i が一致するか調べます。
- S_i \neq T_i である i が存在する場合、そのような i のうち最小のものを j とします。そして、S_j と T_j を比較して、S_j が T_j よりアルファベット順で小さい場合は S \lt T 、そうでない場合は S \gt T と決定して、アルゴリズムを終了します。
- S_i \neq T_i である i が存在しない場合、S と T の長さを比較して、S が T より短い場合は S \lt T 、長い場合は S \gt T と決定して、アルゴリズムを終了します。
制約
- S は英小文字からなる。
- S の長さは 1 以上 1000 以下である。
入力
入力は以下の形式で標準入力から与えられる。
S
出力
2 行にわたって出力せよ。S に対し左シフトと右シフトをそれぞれ好きな回数(0 回でもよい)行って得られる文字列のうち、辞書順で最小のものと最大のものをそれぞれ S_{\min}, S_{\max} とおいたとき、1 行目には S_{\min} を、2 行目には S_{\max} を出力せよ。
入力例 1
aaba
出力例 1
aaab baaa
操作によって、aaab
, aaba
, abaa
, baaa
の 4 通りの文字列を得ることができます。
これらのうち辞書順で最小、最大のものはそれぞれ aaab
, baaa
です。
入力例 2
z
出力例 2
z z
どのように操作を行っても、得られる文字列は z
のみです。
入力例 3
abracadabra
出力例 3
aabracadabr racadabraab
Score : 200 points
Problem Statement
On a non-empty string, a left shift moves the first character to the end of the string, and a right shift moves the last character to the beginning of the string.
For example, a left shift on abcde
results in bcdea
, and two right shifts on abcde
result in deabc
.
You are given a non-empty S consisting of lowercase English letters. Among the strings that can be obtained by performing zero or more left shifts and zero or more right shifts on S, find the lexicographically smallest string and the lexicographically largest string.
What is the lexicographical order?
Simply speaking, the lexicographical order is the order in which words are listed in a dictionary. As a more formal definition, here is the algorithm to determine the lexicographical order between different strings S and T.
Below, let S_i denote the i-th character of S. Also, if S is lexicographically smaller than T, we will denote that fact as S \lt T; if S is lexicographically larger than T, we will denote that fact as S \gt T.
- Let L be the smaller of the lengths of S and T. For each i=1,2,\dots,L, we check whether S_i and T_i are the same.
- If there is an i such that S_i \neq T_i, let j be the smallest such i. Then, we compare S_j and T_j. If S_j comes earlier than T_j in alphabetical order, we determine that S \lt T and quit; if S_j comes later than T_j, we determine that S \gt T and quit.
- If there is no i such that S_i \neq T_i, we compare the lengths of S and T. If S is shorter than T, we determine that S \lt T and quit; if S is longer than T, we determine that S \gt T and quit.
Constraints
- S consists of lowercase English letters.
- The length of S is between 1 and 1000 (inclusive).
Input
Input is given from Standard Input in the following format:
S
Output
Print two lines. The first line should contain S_{\min}, and the second line should contain S_{\max}. Here, S_{\min} and S_{\max} are respectively the lexicographically smallest and largest strings obtained by performing zero or more left shifts and zero or more right shifts on S.
Sample Input 1
aaba
Sample Output 1
aaab baaa
By performing shifts, we can obtain four strings: aaab
, aaba
, abaa
, baaa
. The lexicographically smallest and largest among them are aaab
and baaa
, respectively.
Sample Input 2
z
Sample Output 2
z z
Any sequence of operations results in z
.
Sample Input 3
abracadabra
Sample Output 3
aabracadabr racadabraab
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
チェス盤のどこにコマが置かれているか答えてください。
縦 8 マス、横 8 マスのグリッドがあります。グリッドの各マスには、次のルールで定められる長さ 2 の文字列の名前がついています。
- 左から 1 列目にあるマスの名前の 1 文字目は
a
である。同様に、左から 2,3,\ldots,8 列目にあるマスの名前の 1 文字目はb
,c
,d
,e
,f
,g
,h
である。 - 下から 1 行目にあるマスの名前の 2 文字目は
1
である。同様に、下から 2,3,\ldots,8 行目にあるマスの名前の 2 文字目は2
,3
,4
,5
,6
,7
,8
である。
例えば、グリッドの左下のマスの名前は a1
、右下のマスの名前は h1
、右上のマスの名前は h8
です。
グリッドの状態を表す長さ 8 の 8 つの文字列 S_1,\ldots,S_8 が与えられます。
S_i の j 文字目は、グリッドの上から i 行目 左から j 列目のマスにコマが置かれているとき *
、置かれていないとき .
であり、S_1,\ldots,S_8 の中に文字 *
はちょうど 1 つ存在します。
コマが置かれているマスの名前を求めてください。
制約
- S_i は
.
および*
のみからなる長さ 8 の文字列である - S_1,\ldots,S_8 の中に文字
*
はちょうど 1 つ存在する。
入力
入力は以下の形式で標準入力から与えられる。
S_1 S_2 S_3 S_4 S_5 S_6 S_7 S_8
出力
答えを出力せよ。
入力例 1
........ ........ ........ ........ ........ ........ ........ *.......
出力例 1
a1
問題文中で説明したとおり、グリッドの左下のマスの名前は a1
です。
入力例 2
........ ........ ........ ........ ........ .*...... ........ ........
出力例 2
b3
Score : 200 points
Problem Statement
Locate a piece on a chessboard.
We have a grid with 8 rows and 8 columns of squares. Each of the squares has a 2-character name determined as follows.
- The first character of the name of a square in the 1-st column from the left is
a
. Similarly, the first character of the name of a square in the 2-nd, 3-rd, \ldots, 8-th column from the left isb
,c
,d
,e
,f
,g
,h
, respectively. - The second character of the name of a square in the 1-st row from the bottom is
1
. Similarly, the second character of the name of a square in the 2-nd, 3-rd, \ldots, 8-th row from the bottom is2
,3
,4
,5
,6
,7
,8
, respectively.
For instance, the bottom-left square is named a1
, the bottom-right square is named h1
, and the top-right square is named h8
.
You are given 8 strings S_1,\ldots,S_8, each of length 8, representing the state of the grid.
The j-th character of S_i is *
if the square at the i-th row from the top and j-th column from the left has a piece on it, and .
otherwise.
The character *
occurs exactly once among S_1,\ldots,S_8.
Find the name of the square that has a piece on it.
Constraints
- S_i is a string of length 8 consisting of
.
and*
. - The character
*
occurs exactly once among S_1,\ldots,S_8.
Input
The input is given from Standard Input in the following format:
S_1 S_2 S_3 S_4 S_5 S_6 S_7 S_8
Output
Print the answer.
Sample Input 1
........ ........ ........ ........ ........ ........ ........ *.......
Sample Output 1
a1
As explained in the problem statement, the bottom-left square is named a1
.
Sample Input 2
........ ........ ........ ........ ........ .*...... ........ ........
Sample Output 2
b3
Time Limit: 3 sec / Memory Limit: 1024 MiB
配点 : 250 点
問題文
H 行 W 列のグリッドがあります。
グリッドの各マスは陸か海のどちらかであり、
その情報は H 個の長さ W の文字列 S_1, S_2, \ldots, S_H で与えられます。
上から i 行目、左から j 列目のマスを (i, j) で表すと、
S_i の j 文字目が .
のときマス (i, j) は陸であり、#
のときマス (i, j) は海です。
ここで、グリッドの外周のマス(すなわち、i = 1 、i = H 、j = 1 、j = W のうち少なくとも 1 個以上を満たすマス (i, j) )については、すべて海であることが制約として保証されます。
高橋君が乗った宇宙船が、グリッド上のあるマスに不時着してしまいました。
その後、高橋君は L
、R
、U
、D
のみからなる長さ N の文字列 T で表される手順に沿って、グリッド上を N 回移動しました。
i = 1, 2, \ldots, N について、T の i 文字目は i 回目の移動の内容を下記の通り表します。
L
のとき、左に 1 マス移動したことを表す。すなわち、移動前のマスを (i, j) とするとき、移動後のマスは (i, j-1) である。R
のとき、右に 1 マス移動したことを表す。すなわち、移動前のマスを (i, j) とするとき、移動後のマスは (i, j+1) である。U
のとき、上に 1 マス移動したことを表す。すなわち、移動前のマスを (i, j) とするとき、移動後のマスは (i-1, j) である。D
のとき、下に 1 マス移動したことを表す。すなわち、移動前のマスを (i, j) とするとき、移動後のマスは (i+1, j) である。
高橋君の移動経路上のマス(不時着したマスおよび現在いるマスを含む)はいずれも海でないことがわかっています。 高橋君が現在いるマスとしてあり得るものの個数を出力してください。
制約
- H, W, N は整数
- 3 \leq H, W \leq 500
- 1 \leq N \leq 500
- T は
L
、R
、U
、D
のみからなる長さ N の文字列 - S_i は
.
と#
のみからなる長さ W の文字列 - 高橋君が現在いるマスとしてあり得るものが少なくとも 1 個存在する。
- グリッドの外周のマスはすべて海である。
入力
入力は以下の形式で標準入力から与えられる。
H W N T S_1 S_2 \vdots S_H
出力
答えを出力せよ。
入力例 1
6 7 5 LULDR ####### #...#.# ##...## #.#...# #...#.# #######
出力例 1
2
下記の 2 つの場合がありえるため、高橋君が現在いるマスとしてあり得るものは (3, 4) と (4, 5) の 2 個です。
- マス (3, 5) に不時着し、(3, 5) \rightarrow (3, 4) \rightarrow (2, 4) \rightarrow (2, 3) \rightarrow (3, 3) \rightarrow (3, 4) と移動した場合
- マス (4, 6) に不時着し、(4, 6) \rightarrow (4, 5) \rightarrow (3, 5) \rightarrow (3, 4) \rightarrow (4, 4) \rightarrow (4, 5) と移動した場合
入力例 2
13 16 9 ULURDLURD ################ ##..##.#..####.# ###.#..#.....#.# #..##..#####.### #...#..#......## ###.##.#..#....# ##.#####....##.# ###.###.#.#.#..# ######.....##..# #...#.#.######.# ##..###..#..#.## #...#.#.#...#..# ################
出力例 2
6
Score: 250 points
Problem Statement
There is a grid with H rows and W columns.
Each cell of the grid is land or sea, which is represented by H strings S_1, S_2, \ldots, S_H of length W. Let (i, j) denote the cell at the i-th row from the top and j-th column from the left, and (i, j) is land if the j-th character of S_i is .
, and (i, j) is sea if the character is #
.
The constraints guarantee that all cells on the perimeter of the grid (that is, the cells (i, j) that satisfy at least one of i = 1, i = H, j = 1, j = W) are sea.
Takahashi's spaceship has crash-landed on a cell in the grid. Afterward, he moved N times on the grid following the instructions represented by a string T of length N consisting of L
, R
, U
, and D
. For i = 1, 2, \ldots, N, the i-th character of T describes the i-th move as follows:
L
indicates a move of one cell to the left. That is, if he is at (i, j) before the move, he will be at (i, j-1) after the move.R
indicates a move of one cell to the right. That is, if he is at (i, j) before the move, he will be at (i, j+1) after the move.U
indicates a move of one cell up. That is, if he is at (i, j) before the move, he will be at (i-1, j) after the move.D
indicates a move of one cell down. That is, if he is at (i, j) before the move, he will be at (i+1, j) after the move.
It is known that all cells along his path (including the cell where he crash-landed and the cell he is currently on) are not sea. Print the number of cells that could be his current position.
Constraints
- H, W, and N are integers.
- 3 \leq H, W \leq 500
- 1 \leq N \leq 500
- T is a string of length N consisting of
L
,R
,U
, andD
. - S_i is a string of length W consisting of
.
and#
. - There is at least one cell that could be Takahashi's current position.
- All cells on the perimeter of the grid are sea.
Input
The input is given from Standard Input in the following format:
H W N T S_1 S_2 \vdots S_H
Output
Print the answer.
Sample Input 1
6 7 5 LULDR ####### #...#.# ##...## #.#...# #...#.# #######
Sample Output 1
2
The following two cases are possible, so there are two cells that could be Takahashi's current position: (3, 4) and (4, 5).
- He crash-landed on cell (3, 5) and moved (3, 5) \rightarrow (3, 4) \rightarrow (2, 4) \rightarrow (2, 3) \rightarrow (3, 3) \rightarrow (3, 4).
- He crash-landed on cell (4, 6) and moved (4, 6) \rightarrow (4, 5) \rightarrow (3, 5) \rightarrow (3, 4) \rightarrow (4, 4) \rightarrow (4, 5).
Sample Input 2
13 16 9 ULURDLURD ################ ##..##.#..####.# ###.#..#.....#.# #..##..#####.### #...#..#......## ###.##.#..#....# ##.#####....##.# ###.###.#.#.#..# ######.....##..# #...#.#.######.# ##..###..#..#.## #...#.#.#...#..# ################
Sample Output 2
6
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
この問題は G 問題の簡易版です。
3 個のリールからなるスロットがあります。
i 番目のリールの配列は文字列 S_i によって表されます。ここで、S_i は数字のみからなる長さ M の文字列です。
それぞれのリールには対応するボタンがついています。高橋君は各非負整数 t について、スロットが回り始めてからちょうど t 秒後にボタンを 1 つ選んで押す、または何もしないことができます。
スロットが回り始めてから t 秒後に i 番目のリールに対応するボタンを押すと、i 番目のリールは S_i の (t \bmod M)+1 文字目を表示して止まります。
ただし、t \bmod M で t を M で割ったあまりを表します。
高橋君は全てのリールを止めた上で、表示されている文字が全て同じであるようにしたいです。
高橋君が目標を達成できるように全てのリールを止めるまでに、スロットが回り始めてから最小で何秒かかるかを求めてください。
そのようなことが不可能であればそのことを報告してください。
制約
- 1 \leq M \leq 100
- M は整数
- S_i は数字のみからなる長さ M の文字列
入力
入力は以下の形式で標準入力から与えられる。
M S_1 S_2 S_3
出力
全てのリールを止めた上で、表示されている文字が全て同じであるようにすることができないなら -1
を出力せよ。
できるなら、スロットが回り始めてからそのような状態にするまでに最小で何秒かかるか出力せよ。
入力例 1
10 1937458062 8124690357 2385760149
出力例 1
6
高橋君は次のようにそれぞれのリールを止めることでスロットが回り始めてから 6 秒後にリールに表示される文字を 8
で揃えることができます。
- スロットの回転開始から 0 秒後に 2 番目のリールに対応するボタンを押します。2 番目のリールは S_2 の (0 \bmod 10)+1=1 文字目である
8
を表示して止まります。 - スロットの回転開始から 2 秒後に 3 番目のリールに対応するボタンを押します。3 番目のリールは S_3 の (2 \bmod 10)+1=3 文字目である
8
を表示して止まります。 - スロットの回転開始から 6 秒後に 1 番目のリールに対応するボタンを押します。1 番目のリールは S_1 の (6 \bmod 10)+1=7 文字目である
8
を表示して止まります。
5 秒以下で全てのリールに表示されている文字を揃える方法はないため、6 を出力します。
入力例 2
20 01234567890123456789 01234567890123456789 01234567890123456789
出力例 2
20
全てのリールを止めた上で、表示されている文字を揃える必要がある事に注意してください。
入力例 3
5 11111 22222 33333
出力例 3
-1
表示されている文字が全て同じであるようにリールを止めることはできません。
このとき -1
を出力してください。
Score : 300 points
Problem Statement
This problem is an easier version of Problem G.
There is a slot machine with three reels.
The arrangement of symbols on the i-th reel is represented by the string S_i. Here, S_i is a string of length M consisting of digits.
Each reel has a corresponding button. For each non-negative integer t, Takahashi can either choose and press one button or do nothing exactly t seconds after the reels start spinning.
If he presses the button corresponding to the i-th reel exactly t seconds after the reels start spinning, the i-th reel will stop and display the ((t \bmod M)+1)-th character of S_i.
Here, t \bmod M denotes the remainder when t is divided by M.
Takahashi wants to stop all the reels so that all the displayed characters are the same.
Find the minimum possible number of seconds from the start of the spin until all the reels are stopped so that his goal is achieved.
If this is impossible, report that fact.
Constraints
- 1 \leq M \leq 100
- M is an integer.
- S_i is a string of length M consisting of digits.
Input
The input is given from Standard Input in the following format:
M S_1 S_2 S_3
Output
If it is impossible to stop all the reels so that all the displayed characters are the same, print -1
.
Otherwise, print the minimum possible number of seconds from the start of the spin until such a state is achieved.
Sample Input 1
10 1937458062 8124690357 2385760149
Sample Output 1
6
Takahashi can stop each reel as follows so that 6 seconds after the reels start spinning, all the reels display 8
.
- Press the button corresponding to the second reel 0 seconds after the reels start spinning. The second reel stops and displays
8
, the ((0 \bmod 10)+1=1)-st character of S_2. - Press the button corresponding to the third reel 2 seconds after the reels start spinning. The third reel stops and displays
8
, the ((2 \bmod 10)+1=3)-rd character of S_3. - Press the button corresponding to the first reel 6 seconds after the reels start spinning. The first reel stops and displays
8
, the ((6 \bmod 10)+1=7)-th character of S_1.
There is no way to make the reels display the same character in 5 or fewer seconds, so print 6.
Sample Input 2
20 01234567890123456789 01234567890123456789 01234567890123456789
Sample Output 2
20
Note that he must stop all the reels and make them display the same character.
Sample Input 3
5 11111 22222 33333
Sample Output 3
-1
It is impossible to stop the reels so that all the displayed characters are the same.
In this case, print -1
.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 450 点
問題文
一辺の長さが 1 のマスからなる H 行 W 列のマス目と、N 枚のタイルがあります。
i (1\leq i\leq N) 枚目のタイルは A_i\times B_i の長方形です。
以下の条件をすべてみたすようにタイルをマス目に置くことができるか判定してください。
- 全てのマスがちょうど 1 枚のタイルで覆われている。
- 使用されないタイルがあっても良い。
- 使用するタイルは 回転したり裏返したりして置かれていても良い。ただし、各タイルはマスの線に合わせてマス目からはみ出ることがないように置かれていなければならない。
制約
- 1\leq N\leq 7
- 1 \leq H,W \leq 10
- 1\leq A_i,B_i\leq 10
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N H W A_1 B_1 A_2 B_2 \vdots A_N B_N
出力
問題文中の条件をみたすようにタイルをマス目に置くことができるならば Yes
を、そうでないならば No
を出力せよ。
入力例 1
5 5 5 1 1 3 3 4 4 2 3 2 5
出力例 1
Yes
2,4,5 枚目のタイルを使用して次のように置くと、マス目の各マスをちょうど 1 枚のタイルで覆うことができます。
よって、Yes
を出力します。
入力例 2
1 1 2 2 3
出力例 2
No
マス目からはみ出さないようにタイルを置くことはできません。
よって、No
を出力します。
入力例 3
1 2 2 1 1
出力例 3
No
全てのマスを覆うようにタイルを置くことができません。
よって、No
を出力します。
入力例 4
5 3 3 1 1 2 2 2 2 2 2 2 2
出力例 4
No
全てのマスはちょうど 1 枚のタイルで覆われている必要があることに注意してください。
Score: 450 points
Problem Statement
There is a grid of H rows and W columns, each cell having a side length of 1, and we have N tiles.
The i-th tile (1\leq i\leq N) is a rectangle of size A_i\times B_i.
Determine whether it is possible to place the tiles on the grid so that all of the following conditions are satisfied:
- Every cell is covered by exactly one tile.
- It is fine to have unused tiles.
- The tiles may be rotated or flipped when placed. However, each tile must be aligned with the edges of the cells without extending outside the grid.
Constraints
- 1\leq N\leq 7
- 1 \leq H,W \leq 10
- 1\leq A_i,B_i\leq 10
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N H W A_1 B_1 A_2 B_2 \ldots A_N B_N
Output
If it is possible to place the tiles on the grid so that all of the conditions in the problem statement are satisfied, print Yes
; otherwise, print No
.
Sample Input 1
5 5 5 1 1 3 3 4 4 2 3 2 5
Sample Output 1
Yes
Placing the 2-nd, 4-th, and 5-th tiles as shown below covers every cell of the grid by exactly one tile.
Hence, print Yes
.
Sample Input 2
1 1 2 2 3
Sample Output 2
No
It is impossible to place the tile without letting it extend outside the grid.
Hence, print No
.
Sample Input 3
1 2 2 1 1
Sample Output 3
No
It is impossible to cover all cells with the tile.
Hence, print No
.
Sample Input 4
5 3 3 1 1 2 2 2 2 2 2 2 2
Sample Output 4
No
Note that each cell must be covered by exactly one tile.