C - Hurdle Parsing

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200 点

問題文

いろはちゃんは長さ N ( N \ge 1 ) の正整数列 A=(A_1,A_2,\dots,A_N) を持っています。
いろはちゃんは A を使って、次のように文字列 S を生成しました。

  • S= | から始める。
  • i=1,2,\dots,N の順に、次の操作を行う。
    • S の末尾に - を A_i 個追加する。
    • その後、 S の末尾に | を 1 個追加する。

生成された文字列 S が与えられるので、正整数列 A を復元してください。

制約

  • S は問題文中の方法で生成された長さ 3 以上 100 以下の文字列
  • A は長さ 1 以上の正整数列

入力

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

S

出力

答えを以下の形式で、 1 行に空白区切りで出力せよ。

A_1 A_2 \dots A_N

入力例 1

|---|-|----|-|-----|

出力例 1

3 1 4 1 5

S= |---|-|----|-|-----| が生成されるような A は (3,1,4,1,5) です。


入力例 2

|----------|

出力例 2

10

入力例 3

|-|-|-|------|

出力例 3

1 1 1 6

Score : 200 points

Problem Statement

Iroha has a sequence of positive integers A = (A_1, A_2, \dots, A_N) of length N (N \ge 1).
She generated a string S using A as follows:

  • Start with S = |.
  • For i = 1, 2, \dots, N, perform the following operations in order:
    • Append A_i copies of - to the end of S.
    • Then, append one | to the end of S.

Given the generated string S, reconstruct the sequence A.

Constraints

  • S is a string of length between 3 and 100, inclusive, generated by the method in the problem statement.
  • A is a sequence of positive integers of length at least 1.

Input

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

S

Output

Print the answer in the following format, with elements separated by spaces in a single line:

A_1 A_2 \dots A_N

Sample Input 1

|---|-|----|-|-----|

Sample Output 1

3 1 4 1 5

S = |---|-|----|-|-----| is generated by A = (3, 1, 4, 1, 5).


Sample Input 2

|----------|

Sample Output 2

10

Sample Input 3

|-|-|-|------|

Sample Output 3

1 1 1 6
D - Vacation Together

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200 点

問題文

1 から N までの番号がついた N 人の人がいます。
N 人の人の今後 D 日間の予定が与えられます。人 i の予定は長さ D の文字列 S_i で表されて、S_i の j 文字目が o ならば j 日目は暇であることを、x ならばそうでないことを意味します。

D 日間のうち全員が暇であるような 連続する 何日かを選ぶことを考えます。
選べる日数は最大で何日ですか?ただし、選べる日が存在しない場合は 0 日と答えてください。

制約

  • 1 \leq N \leq 100
  • 1 \leq D \leq 100
  • N, D は整数
  • S_i は o と x からなる長さ D の文字列

入力

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

N D
S_1
S_2
\vdots
S_N

出力

選べる日数の最大値を出力せよ。選べる日が存在しない場合は 0 を出力せよ。


入力例 1

3 5
xooox
oooxx
oooxo

出力例 1

2

2 日目と 3 日目は全員が暇な日なので選ぶことができます。
この 2 日間を選ぶと、連続する日にちを選ぶ方法の中で日数を最大にすることができます。


入力例 2

3 3
oxo
oxo
oxo

出力例 2

1

選ぶ日にちは連続している必要があるのに注意してください。(1 日目と 3 日目は全員が暇な日なので選ぶことができますが、この 2 つを同時に選ぶことはできません)


入力例 3

3 3
oox
oxo
xoo

出力例 3

0

選べる日が存在しない場合は 0 を出力してください。


入力例 4

1 7
ooooooo

出力例 4

7

入力例 5

5 15
oxooooooooooooo
oxooxooooooooox
oxoooooooooooox
oxxxooooooxooox
oxooooooooxooox

出力例 5

5

Score : 200 points

Problem Statement

There are N people numbered 1 to N.
You are given their schedule for the following D days. The schedule for person i is represented by a string S_i of length D. If the j-th character of S_i is o, person i is free on the j-th day; if it is x, they are occupied that day.

From these D days, consider choosing some consecutive days when all the people are free.
How many days can be chosen at most? If no day can be chosen, report 0.

Constraints

  • 1 \leq N \leq 100
  • 1 \leq D \leq 100
  • N and D are integers.
  • S_i is a string of length D consisting of o and x.

Input

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

N D
S_1
S_2
\vdots
S_N

Output

Print the maximum number of days that can be chosen, or 0 if no day can be chosen.


Sample Input 1

3 5
xooox
oooxx
oooxo

Sample Output 1

2

All the people are free on the second and third days, so we can choose them.
Choosing these two days will maximize the number of days among all possible choices.


Sample Input 2

3 3
oxo
oxo
oxo

Sample Output 2

1

Note that the chosen days must be consecutive. (All the people are free on the first and third days, so we can choose either of them, but not both.)


Sample Input 3

3 3
oox
oxo
xoo

Sample Output 3

0

Print 0 if no day can be chosen.


Sample Input 4

1 7
ooooooo

Sample Output 4

7

Sample Input 5

5 15
oxooooooooooooo
oxooxooooooooox
oxoooooooooooox
oxxxooooooxooox
oxooooooooxooox

Sample Output 5

5
E - XX to XXX

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300 点

問題文

英小文字からなる 2 つの文字列 S, T が与えられます。 次の操作を好きな回数( 0 回でも良い)行うことで、S を T と一致させることができるかを判定してください。

S において同じ文字が 2 文字連続しているところの間に、その文字と同じ文字を 1 つ挿入する。 すなわち、下記の 3 つの手順からなる操作を行う。

  1. 現在の S の長さを N とし、S = S_1S_2\ldots S_N とする。
  2. 1 以上 N-1 以下の整数 i であって、S_i = S_{i+1} を満たすものを 1 つ選択する。(ただし、そのような i が存在しない場合は、何もせずに手順 3.をスキップして操作を終了する。)
  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.

  1. Let N be the current length of S, and S = S_1S_2\ldots S_N.
  2. 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.)
  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.

F - kasaka

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300 点

問題文

英小文字からなる文字列 S が与えられます。 S の先頭に a をいくつか( 0 個でも良い)つけ加えて回文にすることができるか判定してください。

ただし、長さ N の文字列 A=A_1A_2\ldots A_N が回文であるとは、すべての 1\leq i\leq N について A_i=A_{N+1-i} が成り立っていることをいいます。

制約

  • 1 \leq \lvert S \rvert \leq 10^6
  • S は英小文字のみからなる。

入力

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

S

出力

S の先頭に a をいくつかつけ加えて回文にすることができるならば Yes を、そうでないならば No を出力せよ。


入力例 1

kasaka

出力例 1

Yes

kasaka の先頭に a を 1 つ付け加えることによって、akasaka となり回文となるため Yes を出力します。


入力例 2

atcoder

出力例 2

No

atcoder の先頭に a をいくつ付け加えても回文となる事はありません。


入力例 3

php

出力例 3

Yes

php はそれ自体回文です。S の先頭に付け加える a は 0 個でも許されるため、Yes を出力します。

Score : 300 points

Problem Statement

Given is a string S consisting of lowercase English letters. Determine whether adding some number of a's (possibly zero) at the beginning of S can make it a palindrome.

Here, a string of length N, A=A_1A_2\ldots A_N, is said to be a palindrome when A_i=A_{N+1-i} for every 1\leq i\leq N.

Constraints

  • 1 \leq \lvert S \rvert \leq 10^6
  • S consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S

Output

If adding some number of a's (possibly zero) at the beginning of S can make it a palindrome, print Yes; otherwise, print No.


Sample Input 1

kasaka

Sample Output 1

Yes

By adding one a at the beginning of kasaka, we have akasaka, which is a palindrome, so Yes should be printed.


Sample Input 2

atcoder

Sample Output 2

No

Adding any number of a's at the beginning of atcoder does not make it a palindrome.


Sample Input 3

php

Sample Output 3

Yes

php itself is a palindrome. Adding zero a's at the beginning of S is allowed, so Yes should be printed.

G - Pigeon Swap

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 350 点

問題文

鳩 1, 鳩 2,\ldots, 鳩 N の N 羽の鳩と、巣 1, 巣 2,\ldots, 巣 N の N 個の巣があります。

はじめ、鳩 i (1\leq i\leq N) は巣 i にいます。

鳩たちに対して Q 回の操作を行います。

操作は次の 3 種類のうちのいずれかです。

  • 種類 1 : 整数 a,b (1\leq a\leq N,1\leq b\leq N) が与えられる。鳩 a を今いる巣から取り出し、巣 b へ移動する。
  • 種類 2 : 整数 a,b (1\leq a\lt b\leq N) が与えられる。巣 a にいる鳩をすべて巣 b へ移動し、巣 b にいる鳩をすべて巣 a へ移動する。これら 2 つの移動は一斉に行われる。
  • 種類 3 : 整数 a (1\leq a\leq N) が与えられる。鳩 a が今いる巣の番号を報告する。

Q 回の操作を順に行ったときの、種類 3 の各操作における報告の結果を出力してください。

制約

  • 1\leq N\leq10 ^ 6
  • 1\leq Q\leq3\times10 ^ 5
  • 入力される操作はすべて種類 1, 種類 2, 種類 3 のいずれかである。
  • 入力される操作は問題文中の制約を満たす。
  • 入力される操作のうちに種類 3 の操作が 1 つ以上含まれる。
  • 入力はすべて整数

入力

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

N Q
op _ 1
op _ 2
\vdots
op _ Q

ただし、i+1 行目の入力 op _ i は i 番目の操作を表しており、op _ i は次のいずれかの形式である。

i 番目の操作が種類 1 の操作のとき、i+1 行目は次の形式で与えられる。

1 a b

これは、与えられる整数を a と b として種類 1 の操作を行うことを表している。

i 番目の操作が種類 2 の操作のとき、i+1 行目は次の形式で与えられる。

2 a b

これは、与えられる整数を a と b として種類 2 の操作を行うことを表している。

i 番目の操作が種類 3 の操作のとき、i+1 行目は次の形式で与えられる。

3 a

これは、与えられる整数を a として種類 3 の操作を行うことを表している。

出力

与えられる種類 3 の操作の個数を q 個として、q 行にわたって出力せよ。

i 行目 (1\leq i\leq q) には、種類 3 の操作のうち i 番目の操作で報告される巣の番号を出力せよ。


入力例 1

6 8
1 2 4
1 3 6
3 2
2 4 5
3 2
1 4 2
3 4
3 2

出力例 1

4
5
2
5

与えられる操作で、鳩は以下の図のように移動します。

種類 3 の操作でそれぞれ報告すべき巣の番号は 4,5,2,5 なので、4 5 2 5 を 4 行にわたって出力してください。


入力例 2

1 2
1 1 1
3 1

出力例 2

1

種類 1 の操作で、鳩を取り出した巣にそのまま戻す場合もあります。


入力例 3

30 15
3 3
2 8 30
2 12 15
2 2 17
1 19 1
2 7 30
3 12
3 8
2 25 26
1 13 10
1 16 10
2 16 29
2 1 21
2 6 11
1 21 8

出力例 3

3
15
7

Score : 350 points

Problem Statement

There are N pigeons labeled 1, 2, \ldots, N and N nests labeled 1, 2, \ldots, N.

Initially, pigeon i (1 \leq i \leq N) is in nest i.

You will perform Q operations on these pigeons.

There are three types of operations:

  • Type 1: You are given integers a and b (1 \leq a \leq N, 1 \leq b \leq N). Take pigeon a out of its current nest and move it to nest b.
  • Type 2: You are given integers a and b (1 \leq a < b \leq N). Move all pigeons in nest a to nest b, and move all pigeons in nest b to nest a. These two moves happen simultaneously.
  • Type 3: You are given an integer a (1 \leq a \leq N). Pigeon a reports the label of the nest it is currently in.

Print the result of each Type 3 operation in the order the operations are given.

Constraints

  • 1 \leq N \leq 10^6
  • 1 \leq Q \leq 3 \times 10^5
  • Every operation is of Type 1, 2, or 3.
  • All operations are valid according to the problem statement.
  • There is at least one Type 3 operation.
  • All input values are integers.

Input

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

N Q
op _ 1
op _ 2
\vdots
op _ Q

Here, op _ i on the line i+1 represents the i-th operation in one of the following formats.

If the i-th operation is of Type 1, op _ i is in the following format:

1 a b

This corresponds to a Type 1 operation with the given integers being a and b.

If the i-th operation is of Type 2, op _ i is in the following format:

2 a b

This corresponds to a Type 2 operation with the given integers being a and b.

If the i-th operation is of Type 3, op _ i is in the following format:

3 a

This corresponds to a Type 3 operation with the given integer being a.

Output

Let the number of Type 3 operations be q. Print q lines. The i-th line (1 \leq i \leq q) should contain the nest number reported in the i-th Type 3 operation.


Sample Input 1

6 8
1 2 4
1 3 6
3 2
2 4 5
3 2
1 4 2
3 4
3 2

Sample Output 1

4
5
2
5

In the operations given, the pigeons move as shown in the figure below:

The nest numbers to be reported for the Type 3 operations are 4,5,2,5. Print 4, 5, 2, 5 on separate lines.


Sample Input 2

1 2
1 1 1
3 1

Sample Output 2

1

The destination nest of a Type 1 operation may be the same as the nest the pigeon is currently in.


Sample Input 3

30 15
3 3
2 8 30
2 12 15
2 2 17
1 19 1
2 7 30
3 12
3 8
2 25 26
1 13 10
1 16 10
2 16 29
2 1 21
2 6 11
1 21 8

Sample Output 3

3
15
7