Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
英小文字からなる 2 つの文字列 S, T が与えられます。 次の操作を好きな回数( 0 回でも良い)行うことで、S を T と一致させることができるかを判定してください。
S において同じ文字が 2 文字連続しているところの間に、その文字と同じ文字を 1 つ挿入する。 すなわち、下記の 3 つの手順からなる操作を行う。
- 現在の S の長さを N とし、S = S_1S_2\ldots S_N とする。
- 1 以上 N-1 以下の整数 i であって、S_i = S_{i+1} を満たすものを 1 つ選択する。(ただし、そのような i が存在しない場合は、何もせずに手順 3.をスキップして操作を終了する。)
- S の i 文字目と i+1 文字目の間に文字 S_i(= S_{i+1}) を 1 つ挿入する。その結果、S は長さ N+1 の文字列 S_1S_2\ldots S_i S_i S_{i+1} \ldots S_N となる。
制約
- S と T はそれぞれ英小文字からなる長さ 2 以上 2 \times 10^5 以下の文字列
入力
入力は以下の形式で標準入力から与えられる。
S T
出力
S を T と一致させることができる場合は Yes
を、そうでない場合は No
を出力せよ。
ジャッジは英小文字と英大文字を厳密に区別することに注意せよ。
入力例 1
abbaac abbbbaaac
出力例 1
Yes
下記の 3 回の操作によって、S = abbaac
を T = abbbbaaac
に一致させることができます。
- まず、S の 2 文字目と 3 文字目の間に
b
を挿入する。その結果、S =abbbaac
となる。 - 次に、再び S の 2 文字目と 3 文字目の間に
b
を挿入する。その結果、S =abbbbaac
となる。 - 最後に、S の 6 文字目と 7 文字目の間に
a
を挿入する。その結果、S =abbbbaaac
となる。
よって、Yes
を出力します。
入力例 2
xyzz xyyzz
出力例 2
No
どのように操作を行っても、 S = xyzz
を T = xyyzz
に一致させることはできません。
よって、No
を出力します。
Score : 300 points
Problem Statement
You are given two strings S and T. Determine whether it is possible to make S equal T by performing the following operation some number of times (possibly zero).
Between two consecutive equal characters in S, insert a character equal to these characters. That is, take the following three steps.
- Let N be the current length of S, and S = S_1S_2\ldots S_N.
- Choose an integer i between 1 and N-1 (inclusive) such that S_i = S_{i+1}. (If there is no such i, do nothing and terminate the operation now, skipping step 3.)
- Insert a single copy of the character S_i(= S_{i+1}) between the i-th and (i+1)-th characters of S. Now, S is a string of length N+1: S_1S_2\ldots S_i S_i S_{i+1} \ldots S_N.
Constraints
- Each of S and T is a string of length between 2 and 2 \times 10^5 (inclusive) consisting of lowercase English letters.
Input
Input is given from Standard Input in the following format:
S T
Output
If it is possible to make S equal T, print Yes
; otherwise, print No
.
Note that the judge is case-sensitive.
Sample Input 1
abbaac abbbbaaac
Sample Output 1
Yes
You can make S = abbaac
equal T = abbbbaaac
by the following three operations.
- First, insert
b
between the 2-nd and 3-rd characters of S. Now, S =abbbaac
. - Next, insert
b
again between the 2-nd and 3-rd characters of S. Now, S =abbbbaac
. - Lastly, insert
a
between the 6-th and 7-th characters of S. Now, S =abbbbaaac
.
Thus, Yes
should be printed.
Sample Input 2
xyzz xyyzz
Sample Output 2
No
No sequence of operations makes S = xyzz
equal T = xyyzz
.
Thus, No
should be printed.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
英小文字からなる長さ N の文字列 S が与えられます。
S の空でない部分文字列であって、1 種類の文字のみからなるものの数を求めてください。 ただし、文字列として等しい部分文字列同士は、取り出し方が異なっても区別しません。
なお、S の空でない部分文字列とは、S の先頭から 0 文字以上、末尾から 0 文字以上削除して得られる文字列のうち、長さが 1 以上であるもののことをいいます。
例えば、ab
や abc
は abc
の空でない部分文字列ですが、ac
や空文字列は abc
の空でない部分文字列ではありません。
制約
- 1 \leq N \leq 2\times 10^5
- S は英小文字からなる長さ N の文字列
入力
入力は以下の形式で標準入力から与えられる。
N S
出力
S の空でない部分文字列であって、1 種類の文字のみからなるものの数を出力せよ。
入力例 1
6 aaabaa
出力例 1
4
S の空でない部分文字列であって、1 種類の文字のみからなるものは a
, aa
, aaa
, b
の 4 つです。
S から a
や aa
を取り出す方法は 1 通りではありませんが、それぞれ 1 回ずつしか数えないことに注意してください。
入力例 2
1 x
出力例 2
1
入力例 3
12 ssskkyskkkky
出力例 3
8
Score : 300 points
Problem Statement
You are given a string S of length N consisting of lowercase English letters.
Find the number of non-empty substrings of S that are repetitions of one character. Here, two substrings that are equal as strings are not distinguished even if they are obtained differently.
A non-empty substring of S is a string of length at least one obtained by deleting zero or more characters from the beginning and zero or more characters from the end of S. For example, ab
and abc
are non-empty substrings of abc
, while ac
and the empty string are not.
Constraints
- 1 \leq N \leq 2\times 10^5
- S is a string of length N consisting of lowercase English letters.
Input
The input is given from Standard Input in the following format:
N S
Output
Print the number of non-empty substrings of S that are repetitions of one character.
Sample Input 1
6 aaabaa
Sample Output 1
4
The non-empty substrings of S that are repetitions of one character are a
, aa
, aaa
, and b
; there are four of them. Note that there are multiple ways to obtain a
or aa
from S, but each should only be counted once.
Sample Input 2
1 x
Sample Output 2
1
Sample Input 3
12 ssskkyskkkky
Sample Output 3
8
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
H 行 W 列のグリッドがあります。上から i 行目、左から j 列目のマスをマス (i, j) と呼びます。
各マスには o
、x
、.
のうちいずれかの文字が書かれています。
各マスに書かれた文字は H 個の長さ W の文字列 S_1, S_2, \ldots, S_H で表され、
マス (i, j) に書かれた文字は、文字列 S_i の j 文字目と一致します。
このグリッドに対して、下記の操作を 0 回以上好きな回数だけ繰り返します。
.
が書かれているマスを 1 個選び、そのマスに書かれた文字をo
に変更する。
その結果、縦方向または横方向に連続した K 個のマスであってそれらに書かれた文字がすべて o
であるようなものが存在する(
すなわち、下記の 2 つの条件のうち少なくとも一方を満たす)ようにすることが可能かを判定し、可能な場合はそのために行う操作回数の最小値を出力してください。
- 1 \leq i \leq H かつ 1 \leq j \leq W-K+1 を満たす整数の組 (i, j) であって、マス (i, j), (i, j+1), \ldots, (i, j+K-1) に書かれた文字が
o
であるものが存在する。 - 1 \leq i \leq H-K+1 かつ 1 \leq j \leq W を満たす整数の組 (i, j) であって、マス (i, j), (i+1, j), \ldots, (i+K-1, j) に書かれた文字が
o
であるものが存在する。
制約
- H, W, K は整数
- 1 \leq H
- 1 \leq W
- H \times W \leq 2 \times 10^5
- 1 \leq K \leq \max\lbrace H, W \rbrace
- S_i は
o
、x
、.
のみからなる長さ W の文字列
入力
入力は以下の形式で標準入力から与えられる。
H W K S_1 S_2 \vdots S_H
出力
問題文中の条件を満たすことが不可能な場合は -1
を、可能な場合はそのために行う操作回数の最小値を出力せよ。
入力例 1
3 4 3 xo.x ..o. xx.o
出力例 1
2
操作を 2 回行って、例えばマス (2, 1) とマス (2, 2) に書かれた文字をそれぞれ o
に変更することで問題文中の条件を満たすことができ、これが最小の操作回数です。
入力例 2
4 2 3 .o .o .o .o
出力例 2
0
操作を一度も行わなくても問題文中の条件を満たします。
入力例 3
3 3 3 x.. ..x .x.
出力例 3
-1
問題文中の条件を満たすことは不可能なので、-1
を出力します。
入力例 4
10 12 6 ......xo.o.. x...x.....o. x........... ..o...x..... .....oo..... o.........x. ox.oox.xx..x ....o...oox. ..o.....x.x. ...o........
出力例 4
3
Score: 400 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 the j-th column from the left.
Each cell contains one of the characters o
, x
, and .
. The characters written in each cell are represented by H strings S_1, S_2, \ldots, S_H of length W; the character written in cell (i, j) is the j-th character of the string S_i.
For this grid, you may repeat the following operation any number of times, possibly zero:
- Choose one cell with the character
.
and change the character in that cell too
.
Determine if it is possible to have a sequence of K horizontally or vertically consecutive cells with o
written in all cells (in other words, satisfy at least one of the following two conditions). If it is possible, print the minimum number of operations required to achieve this.
- There is an integer pair (i, j) satisfying 1 \leq i \leq H and 1 \leq j \leq W-K+1 such that the characters in cells (i, j), (i, j+1), \ldots, (i, j+K-1) are all
o
. - There is an integer pair (i, j) satisfying 1 \leq i \leq H-K+1 and 1 \leq j \leq W such that the characters in cells (i, j), (i+1, j), \ldots, (i+K-1, j) are all
o
.
Constraints
- H, W, and K are integers.
- 1 \leq H
- 1 \leq W
- H \times W \leq 2 \times 10^5
- 1 \leq K \leq \max\lbrace H, W \rbrace
- S_i is a string of length W consisting of the characters
o
,x
, and.
.
Input
The input is given from Standard Input in the following format:
H W K S_1 S_2 \vdots S_H
Output
If it is impossible to satisfy the condition in the problem statement, print -1
. Otherwise, print the minimum number of operations required to do so.
Sample Input 1
3 4 3 xo.x ..o. xx.o
Sample Output 1
2
By operating twice, for example, changing the characters in cells (2, 1) and (2, 2) to o
, you can satisfy the condition in the problem statement, and this is the minimum number of operations required.
Sample Input 2
4 2 3 .o .o .o .o
Sample Output 2
0
The condition is satisfied without performing any operations.
Sample Input 3
3 3 3 x.. ..x .x.
Sample Output 3
-1
It is impossible to satisfy the condition, so print -1
.
Sample Input 4
10 12 6 ......xo.o.. x...x.....o. x........... ..o...x..... .....oo..... o.........x. ox.oox.xx..x ....o...oox. ..o.....x.x. ...o........
Sample Output 4
3
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
頂点に 1 から N の番号が、辺に 1 から M の番号がついた N 頂点 M 辺の単純有向グラフが与えられます。辺 i は頂点 u_i から頂点 v_i への有向辺です。
また、あなたは次の操作を 0 回以上何度でも行えます。
- 相異なる頂点 x,y であって頂点 x から頂点 y への有向辺が存在しないようなものを選ぶ。そして、頂点 x から頂点 y への有向辺を追加する。
このグラフが次の条件を満たす状態にするために最小で何回操作を行う必要があるかを求めてください。
- 相異なる頂点 a,b,c すべてについて、頂点 a から頂点 b への有向辺と頂点 b から頂点 c への有向辺がともに存在するならば頂点 a から頂点 c への有向辺も存在する。
制約
- 3 \leq N \leq 2000
- 0 \leq M \leq 2000
- 1 \leq u_i ,v_i \leq N
- u_i \neq v_i
- i \neq j ならば (u_i,v_i) \neq (u_j,v_j)
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N M u_1 v_1 \vdots u_M v_M
出力
答えを出力せよ。
入力例 1
4 3 2 4 3 1 4 3
出力例 1
3
初め、一例として頂点 2,4,3 について、頂点 2 から頂点 4 への有向辺と頂点 4 から頂点 3 への有向辺がともに存在するにもかかわらず、頂点 2 から頂点 3 への有向辺は存在せず、条件を満たさない状態です。
そこで、以下の 3 本の有向辺を追加すると条件を満たす状態になります。
- 頂点 2 から頂点 3 への有向辺
- 頂点 2 から頂点 1 への有向辺
- 頂点 4 から頂点 1 への有向辺
一方、3 本未満の追加で条件を満たす状態には出来ないため、答えは 3 です。
入力例 2
292 0
出力例 2
0
入力例 3
5 8 1 2 2 1 1 3 3 1 1 4 4 1 1 5 5 1
出力例 3
12
Score : 500 points
Problem Statement
You are given a simple directed graph with N vertices numbered 1 to N and M edges numbered 1 to M. Edge i is a directed edge from vertex u_i to vertex v_i.
You may perform the following operation zero or more times.
- Choose distinct vertices x and y such that there is no directed edge from vertex x to vertex y, and add a directed edge from vertex x to vertex y.
Find the minimum number of times you need to perform the operation to make the graph satisfy the following condition.
- For every triple of distinct vertices a, b, and c, if there are directed edges from vertex a to vertex b and from vertex b to vertex c, there is also a directed edge from vertex a to vertex c.
Constraints
- 3 \leq N \leq 2000
- 0 \leq M \leq 2000
- 1 \leq u_i ,v_i \leq N
- u_i \neq v_i
- (u_i,v_i) \neq (u_j,v_j) if i \neq j.
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N M u_1 v_1 \vdots u_M v_M
Output
Print the answer.
Sample Input 1
4 3 2 4 3 1 4 3
Sample Output 1
3
Initially, the condition is not satisfied because, for instance, for vertices 2, 4, and 3, there are directed edges from vertex 2 to vertex 4 and from vertex 4 to vertex 3, but not from vertex 2 to vertex 3.
You can make the graph satisfy the condition by adding the following three directed edges:
- one from vertex 2 to vertex 3,
- one from vertex 2 to vertex 1, and
- one from vertex 4 to vertex 1.
On the other hand, the condition cannot be satisfied by adding two or fewer edges, so the answer is 3.
Sample Input 2
292 0
Sample Output 2
0
Sample Input 3
5 8 1 2 2 1 1 3 3 1 1 4 4 1 1 5 5 1
Sample Output 3
12
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
この問題は インタラクティブな問題(あなたが作成したプログラムとジャッジプログラムが標準入出力を介して対話を行う形式の問題)です。
あなたとジャッジは下記の手順を行います。 手順はフェイズ 1 とフェイズ 2 からなり、まずフェイズ 1 を行った直後、続けてフェイズ 2 を行います。
(フェイズ 1 )
- ジャッジが 1 以上 10^9 以下の整数 N を決める。この整数は隠されている。
- あなたは 1 以上 110 以下の整数 M を出力する。
- さらにあなたは、すべての i = 1, 2, \ldots, M について 1 \leq A_i \leq M を満たす、長さ M の整数列 A=(A_1,A_2,\ldots,A_M) を出力する。
(フェイズ 2 )
- ジャッジから、長さ M の整数列 B=(B_1,B_2,\ldots,B_M) が与えられる。ここで、 B_i = f^N(i) である。 f(i) は 1 以上 M 以下の整数 i に対し f(i)=A_i で定められ、 f^N(i) は i を f(i) で置き換える操作を N 回行った際に得られる整数である。
- あなたは、B の情報から、ジャッジが決めた整数 N を特定し、N を出力する。
上記の手順を行った後、直ちにプログラムを終了することで正解となります。
制約
- N は 1 以上 10^9 以下の整数
入出力
この問題はインタラクティブな問題(あなたが作成したプログラムとジャッジプログラムが標準入出力を介して対話を行う形式の問題)です。
(フェイズ 1 )
- まず、1 以上 110 以下の整数 M を出力してください。出力後、必ず改行してください。
M
- その後、空白区切りで 1 以上 M 以下の整数からなる長さ M の整数列 A=(A_1,A_2,\ldots,A_M) を出力してください。出力後、必ず改行してください。
A_1 A_2 \ldots A_M
(フェイズ 2 )
- まず、長さ M の整数列 B=(B_1,B_2,\ldots,B_M) が入力から与えられます。
B_1 B_2 \ldots B_M
- 整数 N を求め、出力してください。出力後、必ず改行してください。
N
不正な出力がなされた場合、ジャッジは -1
を出力します。この時、提出はすでに不正解と判定されています。ジャッジプログラムはこの時点で終了するため、あなたのプログラムも終了するのが望ましいです。
注意点
- 出力を行うたびに、末尾に改行を入れて標準出力を flush してください。そうしなかった場合、ジャッジ結果が TLE となる可能性があります。
- 対話の途中で不正な出力を行った、あるいはプログラムが途中で終了した場合のジャッジ結果は不定です。
- 答えを出力したら(または
-1
を受け取ったら)直ちにプログラムを正常終了してください。そうしなかった場合、ジャッジ結果は不定です。 - 特に、余計な改行も不正なフォーマットの出力とみなされるので注意してください。
入出力例
以下は、N = 2 の場合の入出力例です。
入力 | 出力 | 説明 |
---|---|---|
ジャッジは N=2 と決めました。N は入力としては与えられず、隠されています。 | ||
4 |
M を出力します。 | |
2 3 4 4 |
A=(2,3,4,4) を出力します。 | |
3 4 4 4 |
f^2(1)=3,f^2(2)=4,f^2(3)=4,f^2(4)=4 なので、ジャッジは B=(3,4,4,4) をあなたのプログラムに与えます。 | |
2 |
B から N=2 であると特定しました。 N を出力し、プログラムを正常終了させてください。 | |
Score : 500 points
Problem Statement
This is an interactive task, where your and the judge's programs interact via Standard Input and Output.
You and the judge will follow the procedure below. The procedure consists of phases 1 and 2; phase 1 is immediately followed by phase 2.
(Phase 1)
- The judge decides an integer N between 1 and 10^9 (inclusive), which is hidden.
- You print an integer M between 1 and 110 (inclusive).
- You also print an integer sequence A=(A_1,A_2,\ldots,A_M) of length M such that 1 \leq A_i \leq M for all i = 1, 2, \ldots, M.
(Phase 2)
- The judge gives you an integer sequence B=(B_1,B_2,\ldots,B_M) of length M. Here, B_i = f^N(i). f(i) is defined by f(i)=A_i for all integers i between 1 and M (inclusive), and f^N(i) is the integer resulting from replacing i with f(i) N times.
- Based on the given B, you identify the integer N that the judge has decided, and print N.
After the procedure above, terminate the program immediately to be judged correct.
Constraints
- N is an integer between 1 and 10^9 (inclusive).
Input and Output
This is an interactive task, where your and the judge's programs interact via Standard Input and Output.
(Phase 1)
- First, print an integer M between 1 and 110 (inclusive). It must be followed by a newline.
M
- Then, print a sequence A=(A_1,A_2,\ldots,A_M) of length M consisting of integers between 1 and M (inclusive), with spaces in between. It must be followed by a newline.
A_1 A_2 \ldots A_M
(Phase 2)
- First, an integer sequence B=(B_1,B_2,\ldots,B_M) of length M is given from the input.
B_1 B_2 \ldots B_M
- Find the integer N and print it. It must be followed by a newline.
N
If you print something illegal, the judge prints -1
. In that case, your submission is already considered incorrect. Since the judge program terminates at this point, it is desirable that your program terminates too.
Notes
- After each output, add a newline and then flush Standard Output. Otherwise, you may get a TLE verdict.
- If an invalid output is printed during the interaction, or if the program terminates halfway, the verdict will be indeterminate.
- After you print the answer (or you receive
-1
), immediately terminate the program normally. Otherwise, the verdict will be indeterminate. - Note that an excessive newline is also considered an invalid input.
Sample Interaction
The following is a sample interaction with N = 2.
Input | Output | Description |
---|---|---|
The judge has decided that N=2. N is hidden: it is not given as an input. | ||
4 |
You print M. | |
2 3 4 4 |
You print A=(2,3,4,4). | |
3 4 4 4 |
f^2(1)=3,f^2(2)=4,f^2(3)=4, and f^2(4)=4, so the judge gives B=(3,4,4,4) to your program. | |
2 |
Based on B, you have identified that N=2. Print N and terminate the program normally. | |