Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
別世界の AtCoder で開催されている AtCoder Big Contest では、 10^{16} 問の問題が一度に出題されます。
問題の ID は 1 問目から順に A, B, ..., Z, AA, AB, ..., ZZ, AAA, ... と付けられています。
つまり、 ID は以下の順番で付けられています。
- 長さ 1 の英大文字からなる文字列を辞書順に並べたもの
- 長さ 2 の英大文字からなる文字列を辞書順に並べたもの
- 長さ 3 の英大文字からなる文字列を辞書順に並べたもの
- ...
このコンテストに含まれる問題の ID である文字列 S が与えられるので、それが何問目か答えてください。
制約
- S は AtCoder Big Contest に含まれる問題の ID として正しい
入力
入力は以下の形式で標準入力から与えられる。
S
出力
答えを整数として出力せよ。
入力例 1
AB
出力例 1
28
ID が AB である問題は、 AtCoder Big Contest の 28 問目です。
入力例 2
C
出力例 2
3
ID が C である問題は、 AtCoder Big Contest の 3 問目です。
入力例 3
BRUTMHYHIIZP
出力例 3
10000000000000000
ID が BRUTMHYHIIZP である問題は、 AtCoder Big Contest の 10^{16} 問目、すなわち最終問題です。
Score : 300 points
Problem Statement
In a parallel universe, AtCoder holds AtCoder Big Contest, where 10^{16} problems are given at once.
The IDs of the problems are as follows, from the 1-st problem in order: A, B, ..., Z, AA, AB, ..., ZZ, AAA, ...
In other words, the IDs are given in the following order:
- the strings of length 1 consisting of uppercase English letters, in lexicographical order;
- the strings of length 2 consisting of uppercase English letters, in lexicographical order;
- the strings of length 3 consisting of uppercase English letters, in lexicographical order;
- ...
Given a string S that is an ID of a problem given in this contest, find the index of the problem. (See also Samples.)
Constraints
- S is a valid ID of a problem given in AtCoder Big Contest.
Input
The input is given from Standard Input in the following format:
S
Output
Print the answer as an integer.
Sample Input 1
AB
Sample Output 1
28
The problem whose ID is AB is the 28-th problem of AtCoder Big Contest, so 28 should be printed.
Sample Input 2
C
Sample Output 2
3
The problem whose ID is C is the 3-rd problem of AtCoder Big Contest, so 3 should be printed.
Sample Input 3
BRUTMHYHIIZP
Sample Output 3
10000000000000000
The problem whose ID is BRUTMHYHIIZP is the
10^{16}-th (last) problem of AtCoder Big Contest, so 10^{16} should be printed as an integer.
Time Limit: 3 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
N 行 N 列のマス目があり、それぞれのマスは白または黒で塗られています。
マス目の状態は N 個の文字列 S_i で表され、
S_i の j 文字目が # であることはマス目の上から i 行目、左から j 列目のマスが黒く塗られていることを、
. であることは白く塗られていることをさします。
高橋君はこのマス目のうち高々 2 つの白く塗られているマスを選び、黒く塗ることができます。
マス目の中に、黒く塗られたマスが縦、横、ななめのいずれかの向きに 6 つ以上連続するようにできるか判定してください。
ただし、黒く塗られたマスがななめに 6 つ以上連続するとは、N 行 N 列のマス目に完全に含まれる 6 行 6 列のマス目であって、その少なくとも一方の対角線上のマスがすべて黒く塗られているようなものが存在する事をさします。
制約
- 6 \leq N \leq 1000
- \lvert S_i\rvert =N
- S_i は
#と.のみからなる。
入力
入力は以下の形式で標準入力から与えられる。
N S_1 S_2 \vdots S_N
出力
高々 2 つのマス目を黒く塗ることで条件をみたすようにできるなら Yes を、そうでないならば No を出力せよ。
入力例 1
8 ........ ........ .#.##.#. ........ ........ ........ ........ ........
出力例 1
Yes
上から 3 行目の左から 3, 6 番目のマスを塗ることで横方向に 6 つの黒く塗られたマスを連続させることができます。
入力例 2
6 ###### ###### ###### ###### ###### ######
出力例 2
Yes
高橋君はマス目を新たに黒く塗ることはできませんが、すでにこのマス目は条件をみたしています。
入力例 3
10 .......... #..##..... .......... .......... ....#..... ....#..... .#...#..#. .......... .......... ..........
出力例 3
No
Score : 300 points
Problem Statement
There is a grid with N horizontal rows and N vertical columns, where each square is painted white or black.
The state of the grid is represented by N strings, S_i.
If the j-th character of S_i is #, the square at the i-th row from the top and the j-th column from the left is painted black.
If the character is ., then the square is painted white.
Takahashi can choose at most two of these squares that are painted white, and paint them black.
Determine if it is possible to make the grid contain 6 or more consecutive squares painted black aligned either vertically, horizontally, or diagonally.
Here, the grid is said to be containing 6 or more consecutive squares painted black aligned diagonally if the grid with N rows and N columns completely contains a subgrid with 6 rows and 6 columns such that all the squares on at least one of its diagonals are painted black.
Constraints
- 6 \leq N \leq 1000
- \lvert S_i\rvert =N
- S_i consists of
#and..
Input
Input is given from Standard Input in the following format:
N S_1 S_2 \vdots S_N
Output
If it is possible to fulfill the condition by painting at most two squares, then print Yes; otherwise, print No.
Sample Input 1
8 ........ ........ .#.##.#. ........ ........ ........ ........ ........
Sample Output 1
Yes
By painting the 3-rd and the 6-th squares from the left in the 3-rd row from the top, there will be 6 squares painted black aligned horizontally.
Sample Input 2
6 ###### ###### ###### ###### ###### ######
Sample Output 2
Yes
While Takahashi cannot choose a square to paint black, the grid already satisfies the condition.
Sample Input 3
10 .......... #..##..... .......... .......... ....#..... ....#..... .#...#..#. .......... .......... ..........
Sample Output 3
No
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
英小文字および (, ) からなる長さ N の文字列 S が与えられます。
以下の操作を可能な限り繰り返したあとの S を出力してください。
- S の連続部分文字列であって、最初の文字が
(かつ 最後の文字が)かつ 最初と最後以外に(も)も含まないものを自由に 1 つ選び削除する
なお、操作を可能な限り繰り返したあとの S は操作の手順によらず一意に定まることが証明できます。
制約
- 1 \leq N \leq 2 \times 10^5
- N は整数である
- S は英小文字および
(,)からなる長さ N の文字列である
入力
入力は以下の形式で標準入力から与えられる。
N S
出力
答えを出力せよ。
入力例 1
8 a(b(d))c
出力例 1
ac
例えば、以下の手順により操作後の S が ac となります。
- S の 4 文字目から 6 文字目までからなる部分文字列
(d)を削除する。S はa(b)cとなる。 - S の 2 文字目から 4 文字目までからなる部分文字列
(b)を削除する。S はacとなる。 - これ以上操作を行うことはできない。
入力例 2
5 a(b)(
出力例 2
a(
入力例 3
2 ()
出力例 3
操作後の S は空文字列になる可能性があります。
入力例 4
6 )))(((
出力例 4
)))(((
Score : 400 points
Problem Statement
You are given a string S of length N consisting of lowercase English letters and the characters ( and ).
Print the string S after performing the following operation as many times as possible.
- Choose and delete a contiguous substring of S that starts with
(, ends with), and does not contain(or)other than the first and last characters.
It can be proved that the string S after performing the operation as many times as possible is uniquely determined without depending on how it is performed.
Constraints
- 1 \leq N \leq 2 \times 10^5
- N is an integer.
- S is a string of length N consisting of lowercase English letters and the characters
(and).
Input
The input is given from Standard Input in the following format:
N S
Output
Print the answer.
Sample Input 1
8 a(b(d))c
Sample Output 1
ac
Here is one possible procedure, after which S will be ac.
- Delete the substring
(d)formed by the fourth to sixth characters of S, making ita(b)c. - Delete the substring
(b)formed by the second to fourth characters of S, making itac. - The operation can no longer be performed.
Sample Input 2
5 a(b)(
Sample Output 2
a(
Sample Input 3
2 ()
Sample Output 3
The string S after the procedure may be empty.
Sample Input 4
6 )))(((
Sample Output 4
)))(((
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : {500} 点
問題文
H 行 W 列のマス目があります。 上から i 行目、左から j 列目のマスをマス (i,j) と呼ぶことにします。各マスには鏡が高々 1 枚置いてあります。
高橋君はマス (1,1) の左側、青木くんはマス (H,W) の右側に立っています。高橋君は懐中電灯を持っており、マス (1,1) の左側から右に向かって光を入れています。ここで、懐中電灯の光は拡散せず、まっすぐに進む光線であるとします。
高橋君の目標は、マス目にある鏡を利用して懐中電灯の光を青木君に届けることです。
鏡の置き方は次の 3 種類あります。光が鏡に当たると、鏡の置き方に応じて光の進む向きが変わります。それぞれの鏡の置き方について、光が入る方向に対する出る方向は下図のようになります。
- タイプ A (鏡は置かれていない)

- タイプ B (左上と右下を結ぶ対角線上に鏡が置かれている)

- タイプ C (右上と左下を結ぶ対角線上に鏡が置かれている)

マス目の鏡の置き方は H 個の長さ W の文字列 S_1,S_2,\ldots,S_H で表されます。S_i の j 文字目が A のときマス (i,j) はタイプ A、B のときマス (i,j) はタイプ B、C のときマス (i,j) はタイプ C です。
高橋君は、青木君に光を届けるために以下の操作を好きな回数行うことができます。
- あるマスを 1 つ選び、そのマスの鏡の置き方を別のタイプに変更する
青木君に光を届けるためには最低何回操作を行う必要があるか求めてください。
T 個のテストケースが与えられるので、それぞれについて答えを求めてください。
制約
- 1\leq T
- 1\leq H,W
- HW\leq 2\times 10^5
- S_i は
A,B,Cからなる長さ W の文字列 - T,H,W は整数
- 全てのテストケースに対する HW の総和は 2\times 10^5 以下
入力
入力は以下の形式で標準入力から与えられる。
T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T
各テストケースは以下の形式で与えられる。
H W S_1 S_2 \vdots S_H
出力
T 行出力せよ。i 行目には i 番目のテストケースに対する答えを出力せよ。
入力例 1
4 3 4 ABCB CACC BCBA 2 2 CB AA 1 10 BCBCBCBCBC 10 10 CCAABACAAA CCCBACACCA BACAABCBBA ACCCAACCCA CCAAAACCBA AACBBACCAA BCCCACBBAB CBBCAACCCC CBBCCBCBCA BBACABBACC
出力例 1
0 2 10 5
1 つ目のテストケースについて、操作を行わなくても青木君に光を届けることができます。

2 つ目のテストケースについて、マス (1,1) の鏡の置き方をタイプ A に、マス (2,2) の鏡の置き方をタイプ B に変更することで、下図のように青木君に光を届けることができます。1 回以下の操作で青木君に光を届けることはできないので、答えは 2 です。

Score : 500 points
Problem Statement
There is a grid with H rows and W columns. We will refer to the cell at the i-th row from the top and j-th column from the left as cell (i,j). Each cell has at most one mirror placed on it.
Takahashi is standing on the left side of cell (1,1), and Aoki is standing on the right side of cell (H,W). Takahashi has a flashlight and is shining light from the left side of cell (1,1) toward the right. Here, assume that the flashlight's light does not diffuse and is a light ray that travels straight.
Takahashi's goal is to deliver the flashlight's light to Aoki by using the mirrors in the grid.
There are three types of mirror placements. When light hits a mirror, the direction of the light changes according to the mirror placement. For each mirror placement, the exit direction for each entry direction is as shown in the figures below.
- Type A (no mirror is placed)

- Type B (a mirror is placed on the diagonal connecting the upper-left and lower-right)

- Type C (a mirror is placed on the diagonal connecting the upper-right and lower-left)

The mirror placement on the grid is represented by H strings of length W: S_1,S_2,\ldots,S_H. When the j-th character of S_i is A, cell (i,j) is type A; when it is B, cell (i,j) is type B; when it is C, cell (i,j) is type C.
Takahashi can perform the following operation any number of times to deliver the light to Aoki:
- Choose one cell and change the mirror placement of that cell to a different type.
Find the minimum number of operations needed to deliver the light to Aoki.
You are given T test cases; find the answer for each.
Constraints
- 1\leq T
- 1\leq H,W
- HW\leq 2\times 10^5
- S_i is a string of length W consisting of
A,B,C. - T, H, and W are integers.
- The sum of HW over all test cases is at most 2\times 10^5.
Input
The input is given from Standard Input in the following format:
T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T
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
4 3 4 ABCB CACC BCBA 2 2 CB AA 1 10 BCBCBCBCBC 10 10 CCAABACAAA CCCBACACCA BACAABCBBA ACCCAACCCA CCAAAACCBA AACBBACCAA BCCCACBBAB CBBCAACCCC CBBCCBCBCA BBACABBACC
Sample Output 1
0 2 10 5
For the first test case, the light can be delivered to Aoki without performing any operations.

For the second test case, by changing the mirror placement of cell (1,1) to type A and the mirror placement of cell (2,2) to type B, the light can be delivered to Aoki as shown in the figure below. It is impossible to deliver the light to Aoki with one or fewer operations, so the answer is 2.

Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 525 点
問題文
この問題は C 問題 (Operate 1) を完全に含んでおり、 K \le 20 です。
この問題に正解するコードを C 問題に提出することで、 C 問題に正解できます。
文字列 S に対して以下の操作を 0 回以上 K 回以下行って、文字列 T と一致させられるか判定してください。
- 次の 3 種類の操作のうちひとつを選択し、実行する。
- S 中の (先頭や末尾を含む) 任意の位置に、任意の文字を 1 つ挿入する。
- S 中の文字を 1 つ選び、削除する。
- S 中の文字を 1 つ選び、別の 1 つの文字に変更する。
制約
- S,T は英小文字からなる長さ 1 以上 500000 以下の文字列
- K は \color{red}{1 \le K \le 20} を満たす整数
入力
入力は以下の形式で標準入力から与えられる。
K S T
出力
K 回以下の操作で S を T に一致させられる時 Yes 、そうでない時 No と出力せよ。
入力例 1
3 abc awtf
出力例 1
Yes
例えば、次のように操作することで、 3 回の操作で abc を awtf に変換できます。
- 2 文字目の
bをwに変更する。操作後の文字列はawcとなる。 - 3 文字目の
cをfに変更する。操作後の文字列はawfとなる。 - 2 文字目と 3 文字目の間に
tを挿入する。操作後の文字列はawtfとなる。
入力例 2
2 abc awtf
出力例 2
No
2 回以下の操作では abc を awtf に変換できません。
入力例 3
17 twothousandtwentyfour happynewyear
出力例 3
Yes
Score : 525 points
Problem Statement
This problem fully contains Problem C (Operate 1), with K \le 20.
You can solve Problem C by submitting a correct solution to this problem for Problem C.
Determine whether it is possible to perform the following operation on string S between 0 and K times, inclusive, to make it identical to string T.
- Choose one of the following three operations and execute it.
- Insert any one character at any position in S (possibly the beginning or end).
- Delete one character from S.
- Choose one character in S and replace it with another character.
Constraints
- Each of S and T is a string of length between 1 and 500000, inclusive, consisting of lowercase English letters.
- K is an integer satisfying \color{red}{1 \le K \le 20}.
Input
The input is given from Standard Input in the following format:
K S T
Output
If S can be made identical to T with at most K operations, print Yes; otherwise, print No.
Sample Input 1
3 abc awtf
Sample Output 1
Yes
For example, here is a way to convert abc to awtf with three operations:
- Replace the second character
bwithw. After the operation, the string becomesawc. - Replace the third character
cwithf. After the operation, the string becomesawf. - Insert
tbetween the second and third characters. After the operation, the string becomesawtf.
Sample Input 2
2 abc awtf
Sample Output 2
No
abc cannot be converted to awtf with two or fewer operations.
Sample Input 3
17 twothousandtwentyfour happynewyear
Sample Output 3
Yes