E - Word Ladder

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 300

問題文

英小文字からなる文字列 S, T が与えられます。ここで、ST の長さは等しいです。

X を空の配列とし、以下の操作を ST が等しくなるまで繰り返します。

  • S の文字を 1 文字書き換え、X の末尾に S を追加する。

こうして得られる文字列の配列 X のうち要素数最小のものを求めてください。要素数最小のものが複数考えられる場合は、そのうち辞書順最小のものを求めてください。

文字列の配列の辞書順とは

長さ N の文字列 S = S_1 S_2 \ldots S_N が長さ N の文字列 T = T_1 T_2 \ldots T_N より辞書順で小さいとは、ある整数 1 \leq i \leq N が存在して下記の 2 つがともに成り立つことをいいます。

  • S_1 S_2 \ldots S_{i-1} = T_1 T_2 \ldots T_{i-1}
  • S_iT_i よりアルファベット順で早い。

要素数 M の文字列の配列 X = (X_1,X_2,\ldots,X_M) が要素数 M の文字列の配列 Y = (Y_1,Y_2,\ldots,Y_M) より辞書順で小さいとは、ある整数 1 \leq j \leq M が存在して下記の 2 つがともに成り立つことをいいます。

  • (X_1,X_2,\ldots,X_{j-1}) = (Y_1,Y_2,\ldots,Y_{j-1})
  • X_jY_j より辞書順で小さい。

制約

  • S, T は英小文字からなる長さ 1 以上 100 以下の文字列
  • ST の長さは等しい

入力

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

S
T

出力

答えの要素数を M として M + 1 行出力せよ。

1 行目には M の値を出力せよ。

i + 1 (1 \leq i \leq M) 行目には答えの i 番目の要素を出力せよ。


入力例 1

adbe
bcbc

出力例 1

3
acbe
acbc
bcbc

はじめ、S = adbe です。

以下のように操作することで、X = ( acbe , acbc , bcbc ) とすることができます。

  • Sacbe に書き換え、X の末尾に acbe を追加する。

  • Sacbc に書き換え、X の末尾に acbc を追加する。

  • Sbcbc に書き換え、X の末尾に bcbc を追加する。


入力例 2

abcde
abcde

出力例 2

0

入力例 3

afwgebrw
oarbrenq

出力例 3

8
aawgebrw
aargebrw
aarbebrw
aarbebnw
aarbebnq
aarbeenq
aarbrenq
oarbrenq

Score : 300 points

Problem Statement

You are given two strings S and T consisting of lowercase English letters. Here, S and T have equal lengths.

Let X be an empty array, and repeat the following operation until S equals T:

  • Change one character in S, and append S to the end of X.

Find the array of strings X with the minimum number of elements obtained in this way. If there are multiple such arrays with the minimum number of elements, find the lexicographically smallest one among them.

What is lexicographical order on arrays of strings?

A string S = S_1 S_2 \ldots S_N of length N is lexicographically smaller than a string T = T_1 T_2 \ldots T_N of length N if there exists an integer 1 \leq i \leq N such that both of the following are satisfied:

  • S_1 S_2 \ldots S_{i-1} = T_1 T_2 \ldots T_{i-1}
  • S_i comes earlier than T_i in alphabetical order.

An array of strings X = (X_1,X_2,\ldots,X_M) with M elements is lexicographically smaller than an array of strings Y = (Y_1,Y_2,\ldots,Y_M) with M elements if there exists an integer 1 \leq j \leq M such that both of the following are satisfied:

  • (X_1,X_2,\ldots,X_{j-1}) = (Y_1,Y_2,\ldots,Y_{j-1})
  • X_j is lexicographically smaller than Y_j.

Constraints

  • S and T are strings consisting of lowercase English letters with length between 1 and 100, inclusive.
  • The lengths of S and T are equal.

Input

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

S
T

Output

Let M be the number of elements in the desired array. Print M + 1 lines.

The first line should contain the value of M.

The i + 1-th line (1 \leq i \leq M) should contain the i-th element of the array.


Sample Input 1

adbe
bcbc

Sample Output 1

3
acbe
acbc
bcbc

Initially, S = adbe.

We can obtain X = ( acbe , acbc , bcbc ) by performing the following operations:

  • Change S to acbe and append acbe to the end of X.

  • Change S to acbc and append acbc to the end of X.

  • Change S to bcbc and append bcbc to the end of X.


Sample Input 2

abcde
abcde

Sample Output 2

0

Sample Input 3

afwgebrw
oarbrenq

Sample Output 3

8
aawgebrw
aargebrw
aarbebrw
aarbebnw
aarbebnq
aarbeenq
aarbrenq
oarbrenq
F - Slot Strategy 2 (Easy)

実行時間制限: 2 sec / メモリ制限: 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 MtM で割ったあまりを表します。

高橋君は全てのリールを止めた上で、表示されている文字が全て同じであるようにしたいです。
高橋君が目標を達成できるように全てのリールを止めるまでに、スロットが回り始めてから最小で何秒かかるかを求めてください。
そのようなことが不可能であればそのことを報告してください。

制約

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

G - Take ABC

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 425

問題文

A , B , C3 種類の文字のみからなる文字列 S が与えられます。

S が連続な部分文字列として文字列 ABC を含む限り、下記の操作を繰り返します。

S に連続な部分文字列として含まれる文字列 ABC のうち、S の中で最も左にあるものを、S から削除する。

上記の手順を行った後の、最終的な S を出力してください。

制約

  • SA , B , C のみからなる長さ 1 以上 2\times 10^5 以下の文字列

入力

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

S

出力

答えを出力せよ。


入力例 1

BAABCBCCABCAC

出力例 1

BCAC

与えられた文字列 S = BAABCBCCABCAC に対して、下記の通りに操作が行われます。

  • 1 回目の操作で、S = BAABCBCCABCAC3 文字目から 5 文字目の ABC が削除され、その結果 S = BABCCABCAC となります。
  • 2 回目の操作で、S = BABCCABCAC2 文字目から 4 文字目の ABC が削除され、その結果 S = BCABCAC となります。
  • 3 回目の操作で、S = BCABCAC3 文字目から 5 文字目の ABC が削除され、その結果 S = BCAC となります。

よって、最終的な SBCAC です。


入力例 2

ABCABC

出力例 2



この入力例では、最終的な S は空文字列です。


入力例 3

AAABCABCABCAABCABCBBBAABCBCCCAAABCBCBCC

出力例 3

AAABBBCCC

Score : 425 points

Problem Statement

You are given a string S consisting of three different characters: A, B, and C.

As long as S contains the string ABC as a consecutive substring, repeat the following operation:

Remove the leftmost occurrence of the substring ABC from S.

Print the final string S after performing the above procedure.

Constraints

  • S is a string of length between 1 and 2 \times 10^5, inclusive, consisting of the characters A, B, and C.

Input

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

S

Output

Print the answer.


Sample Input 1

BAABCBCCABCAC

Sample Output 1

BCAC

For the given string S = BAABCBCCABCAC, the operations are performed as follows.

  • In the first operation, the ABC from the 3-rd to the 5-th character in S = BAABCBCCABCAC is removed, resulting in S = BABCCABCAC.
  • In the second operation, the ABC from the 2-nd to the 4-th character in S = BABCCABCAC is removed, resulting in S = BCABCAC.
  • In the third operation, the ABC from the 3-rd to the 5-th character in S = BCABCAC is removed, resulting in S = BCAC.

Therefore, the final S is BCAC.


Sample Input 2

ABCABC

Sample Output 2



In this example, the final S is an empty string.


Sample Input 3

AAABCABCABCAABCABCBBBAABCBCCCAAABCBCBCC

Sample Output 3

AAABBBCCC
H - Hungry Takahashi

実行時間制限: 3 sec / メモリ制限: 1024 MiB

配点 : 450

問題文

HW 列のグリッドがあります。 上から i 行目、左から j 列目のマスのことを (i,j) と表記します。 各マスには何枚かのコインが置かれており、マス (i,j) に置かれているコインは A_{i,j} 枚です。

高橋君ははじめマス (1,1) におり、x 枚のコインを持っています。 これから H+W-1 日間にかけていくつかの出来事が起こります。 k\ (1\leq k\leq H+W-1) 日目には以下のことが順に起こります:

  1. 高橋君が、そのときいるマスに置かれたコインを全て回収する。
  2. お腹の空いた高橋君が、手持ちのコインを P_k 枚消費してご飯を買う。ただし、所持しているコインが P_k 枚未満の場合ご飯を買うことはできず、空腹のあまり倒れてしまう。
  3. k < H+W-1 ならば、高橋君が、そのときいるマスの 1 つ右または 1 つ下のいずれかのマスを選んでそこに移動する。ただし、グリッドの外に出てしまうような移動はできない。k=H+W-1 ならば、何もしない。

高橋君が一度も空腹で倒れることなくこれからの H+W-1 日間を終えられるような方法が存在するとき、 高橋君がはじめに持っているコインの枚数 x としてありうる最小の非負整数を求めてください。

制約

  • H,W\geq 1
  • H\times W \leq 2\times 10^5
  • 1\leq A_{i,j}\leq 10^9
  • 1\leq P_k\leq 10^9
  • 入力は全て整数

入力

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

H W
A_{1,1} A_{1,2} \dots A_{1,W}
A_{2,1} A_{2,2} \dots A_{2,W}
\vdots
A_{H,1} A_{H,2} \dots A_{H,W}
P_1 P_2 \dots P_{H+W-1}

出力

答えを出力せよ。


入力例 1

2 2
3 2
4 1
1 3 6

出力例 1

2

x=2 のとき、高橋君は次のように行動することで、一度も空腹で倒れずに済みます。

  • 最初、マス (1,1) におり、2 枚のコインを所持している。
  • 1 日目:
    1. マス (1,1) に置かれたコイン 3 枚を回収し、手持ちのコインが 5 枚になる。
    2. コインを 1 枚消費してご飯を買い、手持ちのコインが 4 枚になる。
    3. 1 つ下にあるマス (2,1) に移動する。
  • 2 日目:
    1. マス (2,1) に置かれたコイン 4 枚を回収し、手持ちのコインが 8 枚になる。
    2. コインを 3 枚消費してご飯を買い、手持ちのコインが 5 枚になる。
    3. 1 つ右にあるマス (2,2) に移動する。
  • 3 日目:
    1. マス (2,2) に置かれたコイン 1 枚を回収し、手持ちのコインが 6 枚になる。
    2. コインを 6 枚消費してご飯を買い、手持ちのコインが 0 枚になる。

x1 以下のとき、高橋君はどのように行動してもいずれかのタイミングで空腹で倒れてしまいます。 よって答えは 2 です。


入力例 2

1 1
5
3

出力例 2

0

最初に 1 枚もコインを持っていなかったとしても、高橋君は空腹で倒れることはありません。


入力例 3

4 7
35 29 36 88 58 15 25
99 7 49 61 67 4 57
96 92 3 49 49 36 90
72 89 40 44 24 53 45
55 43 23 71 77 6 94 19 27 21

出力例 3

20

Score : 450 points

Problem Statement

There is a grid with H rows and W columns. Let (i,j) denote the cell at the i-th row from the top and j-th column from the left. Some coins are placed on each cell, and there are A_{i,j} coins on cell (i,j).

Takahashi is initially at cell (1,1) and has x coins. Over the next H+W-1 days, several events will occur. On the k-th day (1\leq k\leq H+W-1), the following things happen in order:

  1. Takahashi collects all the coins placed on the cell where he is currently located.
  2. Hungry Takahashi consumes P_k coins from his hand to buy food. However, if he has fewer than P_k coins, he cannot buy food and collapses from hunger.
  3. If k < H+W-1, Takahashi moves either one cell right or one cell down. He cannot leave the grid. If k=H+W-1, he does nothing.

When there exists a way for Takahashi to finish the next H+W-1 days without ever collapsing from hunger, find the minimum non-negative integer x that can be the number of coins Takahashi initially has.

Constraints

  • H,W\geq 1
  • H\times W \leq 2\times 10^5
  • 1\leq A_{i,j}\leq 10^9
  • 1\leq P_k\leq 10^9
  • All input values are integers.

Input

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

H W
A_{1,1} A_{1,2} \dots A_{1,W}
A_{2,1} A_{2,2} \dots A_{2,W}
\vdots
A_{H,1} A_{H,2} \dots A_{H,W}
P_1 P_2 \dots P_{H+W-1}

Output

Output the answer.


Sample Input 1

2 2
3 2
4 1
1 3 6

Sample Output 1

2

When x=2, Takahashi can act as follows to avoid collapsing from hunger:

  • Initially, he is at cell (1,1) and has 2 coins.
  • Day 1:
    1. He collects 3 coins placed on cell (1,1), so he has 5 coins.
    2. He consumes 1 coin to buy food, so he has 4 coins.
    3. He moves to cell (2,1) which is 1 below.
  • Day 2:
    1. He collects 4 coins placed on cell (2,1), so he has 8 coins.
    2. He consumes 3 coins to buy food, so he has 5 coins.
    3. He moves to cell (2,2) which is 1 to the right.
  • Day 3:
    1. He collects 1 coin placed on cell (2,2), so he has 6 coins.
    2. He consumes 6 coins to buy food, so he has 0 coins.

When x is 1 or less, Takahashi will collapse from hunger at some point no matter how he acts. Therefore, the answer is 2.


Sample Input 2

1 1
5
3

Sample Output 2

0

Even if Takahashi initially has no coins, he will not collapse from hunger.


Sample Input 3

4 7
35 29 36 88 58 15 25
99 7 49 61 67 4 57
96 92 3 49 49 36 90
72 89 40 44 24 53 45
55 43 23 71 77 6 94 19 27 21

Sample Output 3

20
I - Balanced Rectangles

実行時間制限: 3 sec / メモリ制限: 1024 MiB

配点 : 525

問題文

H \times W のグリッドが与えられ、各マスには #. のどちらかが書かれています。
各マスに書かれている記号の情報は H 個の長さ W の文字列 S_1,S_2,\dots,S_H として与えられ、上から i 行目、左から j 列目にあるマスには S_ij 文字目と同じ記号が書かれています。
このグリッドに対し、以下の条件を全て満たす長方領域がいくつあるか求めてください。

  • 長方領域に含まれる # が書かれたマスの個数と . が書かれたマスの個数が等しい。

厳密には、次の条件を全て満たす 4 つの整数の組 (u,d,l,r) の数を求めてください。

  • 1 \le u \le d \le H
  • 1 \le l \le r \le W
  • グリッドのうち上から u 行目から d 行目、左から l 列目から r 列目の部分を取り出す。このとき、取り出した部分に含まれる # が書かれたマスの個数と . が書かれたマスの個数が等しい。

T 個のテストケースが与えられるので、それぞれについて答えを求めてください。

制約

  • 1 \le T \le 25000
  • 1 \le H,W
  • ひとつの入力に含まれる H \times W の総和は 3 \times 10^5 を超えない。
  • S_i#. からなる長さ W の文字列である。

入力

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

T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T

\mathrm{case}_ii 番目のテストケースを表す。 各テストケースは以下の形式で与えられる。

H W
S_1
S_2
\vdots
S_H

出力

T 行出力せよ。i 行目 には i 番目のテストケースに対する答えを出力せよ。


入力例 1

3
3 2
##
#.
..
6 6
..#...
..#..#
#.#.#.
.###..
######
.###..
15 50
.......................#...........###.###.###.###
....................#..#..#..........#.#.#...#.#..
.................#...#####...#.....###.#.#.###.###
..................#..##.##..#......#...#.#.#.....#
...................#########.......###.###.###.###
....................#.....#.......................
.###........##......#.....#..#...#.####.####.##..#
#..#.........#......#.....#..#...#.#....#....##..#
#..#.........#......#.....#..#...#.#....#....##..#
#.....##...###..##..#.....#..#...#.#....#....#.#.#
#....#..#.#..#.#..#.#..##.#..#...#.####.####.#.#.#
#....#..#.#..#.####.#....##..#...#.#....#....#.#.#
#....#..#.#..#.#....#.....#..#...#.#....#....#..##
#..#.#..#.#..#.#..#.#....#.#.#...#.#....#....#..##
.##...##...####.##...####..#..###..####.####.#..##

出力例 1

4
79
4032

この入力には 3 個のテストケースが含まれます。

1 番目のケースについて、以下の 4 個の長方領域が問題文中の条件を満たします。

  • 上から 1 行目から 2 行目、左から 2 列目から 2 列目
  • 上から 2 行目から 3 行目、左から 1 列目から 1 列目
  • 上から 2 行目から 2 行目、左から 1 列目から 2 列目
  • 上から 1 行目から 3 行目、左から 1 列目から 2 列目

Score : 525 points

Problem Statement

You are given an H \times W grid, where each cell contains # or ..
The information about the symbols written in each cell is given as H strings S_1,S_2,\dots,S_H of length W, where the cell in the i-th row from the top and j-th column from the left contains the same symbol as the j-th character of S_i.
Find the number of rectangular regions in this grid that satisfy all of the following conditions:

  • The number of cells containing # and the number of cells containing . in the rectangular region are equal.

Formally, find the number of quadruples of integers (u,d,l,r) that satisfy all of the following conditions:

  • 1 \le u \le d \le H
  • 1 \le l \le r \le W
  • When extracting the part of the grid from the u-th through d-th rows from the top and from the l-th through r-th columns from the left, the number of cells containing # and the number of cells containing . in the extracted part are equal.

You are given T test cases. Find the answer for each of them.

Constraints

  • 1 \le T \le 25000
  • 1 \le H,W
  • The sum of H \times W over all test cases in one input does not exceed 3 \times 10^5.
  • S_i is a string of length W consisting of # and ..

Input

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

T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T

\mathrm{case}_i represents the i-th test case. Each test case is given in the following format:

H W
S_1
S_2
\vdots
S_H

Output

Output T lines. The i-th line should contain the answer for the i-th test case.


Sample Input 1

3
3 2
##
#.
..
6 6
..#...
..#..#
#.#.#.
.###..
######
.###..
15 50
.......................#...........###.###.###.###
....................#..#..#..........#.#.#...#.#..
.................#...#####...#.....###.#.#.###.###
..................#..##.##..#......#...#.#.#.....#
...................#########.......###.###.###.###
....................#.....#.......................
.###........##......#.....#..#...#.####.####.##..#
#..#.........#......#.....#..#...#.#....#....##..#
#..#.........#......#.....#..#...#.#....#....##..#
#.....##...###..##..#.....#..#...#.#....#....#.#.#
#....#..#.#..#.#..#.#..##.#..#...#.####.####.#.#.#
#....#..#.#..#.####.#....##..#...#.#....#....#.#.#
#....#..#.#..#.#....#.....#..#...#.#....#....#..##
#..#.#..#.#..#.#..#.#....#.#.#...#.#....#....#..##
.##...##...####.##...####..#..###..####.####.#..##

Sample Output 1

4
79
4032

This input contains 3 test cases.

For the 1st case, the following 4 rectangular regions satisfy the conditions in the problem statement:

  • From the 1st to 2nd rows from the top, from the 2nd to 2nd columns from the left
  • From the 2nd to 3rd rows from the top, from the 1st to 1st columns from the left
  • From the 2nd to 2nd rows from the top, from the 1st to 2nd columns from the left
  • From the 1st to 3rd rows from the top, from the 1st to 2nd columns from the left