A - Exponential Plant

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

高橋君は植物を育てています。その植物の発芽時の高さは 0\,\mathrm{cm} です。発芽した日を 0 日目としたとき、発芽してから i (0 \le i) 日目の夜には 2^i\,\mathrm{cm} 植物の高さが伸びます。

高橋君の身長は H\,\mathrm{cm} です。

高橋君は毎朝この植物と背比べをします。植物の高さが高橋君の身長より高くなるのは発芽から何日目の朝か求めてください。

制約

  • 1 \leq H \leq 10^{9}
  • 入力は全て整数である。

入力

入力は以下の形式で標準入力から与えられる。

H

出力

植物の高さが高橋君の身長より高くなる日は発芽から何日目の朝か出力せよ。


入力例 1

54

出力例 1

6

植物が発芽してからの 1 日ごとの朝の高さは 1\,\mathrm{cm},3\,\mathrm{cm},7\,\mathrm{cm}, 15\,\mathrm{cm},31\,\mathrm{cm},63\,\mathrm{cm} となります。 6 日目の朝に高橋君より高くなるため、 6 を出力します。


入力例 2

7

出力例 2

4

植物が発芽してから 3 日目の朝の高さは 7\,\mathrm{cm} で、 4 日目の朝の高さは 15\,\mathrm{cm} です。 4 日目の朝に高橋君より高くなるため、 4 を出力します。3 日目の朝のときは高橋君と同じ高さですが、高橋君より高くないことに注意してください。


入力例 3

262144

出力例 3

19

Score : 100 points

Problem Statement

Takahashi is growing a plant. Its height at the time of germination is 0\,\mathrm{cm}. Considering the day of germination as day 0, its height increases by 2^i\,\mathrm{cm} day i's night (0 \le i).

Takahashi's height is H\,\mathrm{cm}.

Every morning, Takahashi measures his height against this plant. Find the first day such that the plant's height is strictly greater than Takahashi's height in the morning.

Constraints

  • 1 \leq H \leq 10^{9}
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

H

Output

Print an integer representing the first day such that the plant's height is greater than Takahashi's height in the morning.


Sample Input 1

54

Sample Output 1

6

The plant's height in the mornings of days 1, 2, 3, 4, 5, 6 will be 1\,\mathrm{cm}, 3\,\mathrm{cm}, 7\,\mathrm{cm}, 15\,\mathrm{cm}, 31\,\mathrm{cm}, 63\,\mathrm{cm}, respectively. The plant becomes taller than Takahashi in the morning day 6, so print 6.


Sample Input 2

7

Sample Output 2

4

The plant's height will be 7\,\mathrm{cm} in the morning of day 3 and 15\,\mathrm{cm} in the morning day 4. The plant becomes taller than Takahashi in the morning of day 4, so print 4. Note that, in the morning of day 3, the plant is as tall as Takahashi, but not taller.


Sample Input 3

262144

Sample Output 3

19
B - Middle Letter

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

英小文字からなる長さが奇数の文字列 S が与えられます。

S の中央の文字を出力してください。

中央の文字とは ある長さが奇数の文字列 T について、 T の長さを |T| として、T の前から \frac{|T|+1}{2} 番目の文字を中央の文字とします。

制約

  • S は英小文字からなる長さが奇数の文字列
  • S の長さは 1 以上 99 以下

入力

入力は以下の形式で標準入力から与えられる。

S

出力

答えを出力せよ。


入力例 1

atcoder

出力例 1

o

atcoder の中央の文字は o です。


入力例 2

a

出力例 2

a

Score : 100 points

Problem Statement

You are given an odd-length string S consisting of lowercase English letters.

Print the central character of S.

What is the central character? For an odd-length string T, its central character is the \frac{|T|+1}{2}-th character from the beginning, where |T| is the length of T.

Constraints

  • S is an odd-length string consisting of lowercase English letters.
  • The length of S is between 1 and 99 (inclusive).

Input

Input is given from Standard Input in the following format:

S

Output

Print the answer.


Sample Input 1

atcoder

Sample Output 1

o

The central character of atcoder is o.


Sample Input 2

a

Sample Output 2

a
C - String Shifting

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

空でない文字列に対し、先頭の文字を末尾に移動する操作を左シフト、末尾の文字を先頭に移動する操作を右シフトと呼びます。

例えば、abcde に対して左シフトを 1 回行うと bcdea となり、右シフトを 2 回行うと deabc となります。

英小文字からなる空でない文字列 S が与えられます。S に対し左シフトと右シフトをそれぞれ好きな回数(0 回でもよい)行って得られる文字列のうち、辞書順で最小のものと最大のものを求めてください。

辞書順とは?

辞書順とは簡単に説明すると「単語が辞書に載っている順番」を意味します。より厳密な説明として、英小文字からなる相異なる文字列 S, T の大小を判定するアルゴリズムを以下に説明します。

以下では「 Si 文字目の文字」を S_i のように表します。また、 ST より辞書順で小さい場合は S \lt T 、大きい場合は S \gt T と表します。

  1. S, T のうち長さが大きくない方の文字列の長さを L とします。i=1,2,\dots,L に対して S_iT_i が一致するか調べます。
  2. S_i \neq T_i である i が存在する場合、そのような i のうち最小のものを j とします。そして、S_jT_j を比較して、S_jT_j よりアルファベット順で小さい場合は S \lt T 、そうでない場合は S \gt T と決定して、アルゴリズムを終了します。
  3. S_i \neq T_i である i が存在しない場合、ST の長さを比較して、ST より短い場合は 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 , baaa4 通りの文字列を得ることができます。 これらのうち辞書順で最小、最大のものはそれぞれ 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.

  1. 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.
  2. 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.
  3. 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
D - Chessboard

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 です。

グリッドの状態を表す長さ 88 つの文字列 S_1,\ldots,S_8 が与えられます。
S_ij 文字目は、グリッドの上から 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 is b, 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 is 2, 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
E - Takahashi Gets Lost

Time Limit: 3 sec / Memory Limit: 1024 MiB

配点 : 250

問題文

HW 列のグリッドがあります。

グリッドの各マスはのどちらかであり、 その情報は H 個の長さ W の文字列 S_1, S_2, \ldots, S_H で与えられます。 上から i 行目、左から j 列目のマスを (i, j) で表すと、 S_ij 文字目が . のときマス (i, j) は陸であり、# のときマス (i, j) は海です。

ここで、グリッドの外周のマス(すなわち、i = 1i = Hj = 1j = W のうち少なくとも 1 個以上を満たすマス (i, j) )については、すべて海であることが制約として保証されます。

高橋君が乗った宇宙船が、グリッド上のあるマスに不時着してしまいました。 その後、高橋君は LRUD のみからなる長さ N の文字列 T で表される手順に沿って、グリッド上を N 回移動しました。 i = 1, 2, \ldots, N について、Ti 文字目は 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
  • TLRUD のみからなる長さ 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, and D.
  • 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