A - First ABC 2

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

A, B, C からなる長さ N の文字列 S が与えられます。
S の中で ABC が(連続な)部分文字列として初めて現れる位置を答えてください。すなわち、以下の条件を全て満たす整数 n のうち最小のものを答えてください。

  • 1 \leq n \leq N - 2
  • Sn 文字目から n+2 文字目までを取り出して出来る文字列は ABC である。

ただし、ABCS に現れない場合は -1 を出力してください。

制約

  • 3 \leq N \leq 100
  • SA, B, C からなる長さ N の文字列

入力

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

N
S

出力

S の中で ABC が部分文字列として初めて現れる位置を出力せよ。ただし、ABCS に現れない場合は -1 を出力せよ。


入力例 1

8
ABABCABC

出力例 1

3

S の中で ABC が初めて現れるのは 3 文字目から 5 文字目までの位置です。よって 3 が答えになります。


入力例 2

3
ACB

出力例 2

-1

ABCS に現れない場合は -1 を出力してください。


入力例 3

20
BBAAABBACAACABCBABAB

出力例 3

13

Score : 100 points

Problem Statement

You are given a string S of length N consisting of A, B, and C.
Find the position where ABC first appears as a (contiguous) substring in S. In other words, find the smallest integer n that satisfies all of the following conditions.

  • 1 \leq n \leq N - 2.
  • The string obtained by extracting the n-th through (n+2)-th characters of S is ABC.

If ABC does not appear in S, print -1.

Constraints

  • 3 \leq N \leq 100
  • S is a string of length N consisting of A, B, and C.

Input

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

N
S

Output

Print the position where ABC first appears as a substring in S, or -1 if it does not appear in S.


Sample Input 1

8
ABABCABC

Sample Output 1

3

ABC first appears in S at the 3-rd through 5-th characters of S. Therefore, the answer is 3.


Sample Input 2

3
ACB

Sample Output 2

-1

If ABC does not appear in S, print -1.


Sample Input 3

20
BBAAABBACAACABCBABAB

Sample Output 3

13
B - Full House 2

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

4 枚のカードがあり、それぞれのカードには整数 A,B,C,D が書かれています。
ここに 1 枚カードを加え、フルハウスとできるか判定してください。

ただし、 5 枚組のカードは以下の条件を満たすとき、またそのときに限って、フルハウスであると呼ばれます。

  • 異なる整数 x,y について、 x が書かれたカード 3 枚と y が書かれたカード 2 枚からなる。

制約

  • 入力は全て整数
  • 1 \le A,B,C,D \le 13

入力

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

A B C D

出力

1 枚カードを加えてフルハウスとできる場合は Yes 、そうでないときは No と出力せよ。


入力例 1

7 7 7 1

出力例 1

Yes

7,7,7,11 を加えた時、フルハウスとなります。


入力例 2

13 12 11 10

出力例 2

No

13,12,11,10 に何を加えてもフルハウスにはなりません。


入力例 3

3 3 5 5

出力例 3

Yes

3,3,5,53 を加えた時、フルハウスとなります。
また、 5 を加えてもフルハウスとなります。


入力例 4

8 8 8 8

出力例 4

No

8,8,8,8 に何を加えてもフルハウスにはなりません。
同じ 5 枚のカードはフルハウスではないことに注意してください。


入力例 5

1 3 4 1

出力例 5

No

Score : 100 points

Problem Statement

There are four cards with integers A,B,C,D written on them.
Determine whether a Full House can be formed by adding one card.

A set of five cards is called a Full House if and only if the following condition is satisfied:

  • For two distinct integers x and y, there are three cards with x written on them and two cards with y written on them.

Constraints

  • All input values are integers.
  • 1 \le A,B,C,D \le 13

Input

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

A B C D

Output

If adding one card can form a Full House, print Yes; otherwise, print No.


Sample Input 1

7 7 7 1

Sample Output 1

Yes

Adding 1 to 7,7,7,1 forms a Full House.


Sample Input 2

13 12 11 10

Sample Output 2

No

Adding anything to 13,12,11,10 does not form a Full House.


Sample Input 3

3 3 5 5

Sample Output 3

Yes

Adding 3,3,5,5 to 3 forms a Full House.
Also, adding 5 forms a Full House.


Sample Input 4

8 8 8 8

Sample Output 4

No

Adding anything to 8,8,8,8 does not form a Full House.
Note that five identical cards do not form a Full House.


Sample Input 5

1 3 4 1

Sample Output 5

No
C - Number Box

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

正整数 N が与えられます。

NN 列のマス目があり、上から i 行目、左から j 列目のマスには数字 A_{i,j} が書かれています。

このマス目は上下および左右がつながっているものとします。つまり以下が全て成り立ちます。

  • (1,i) の上のマスは (N,i) であり、(N,i) の下のマスは (1,i) である。(1\le i\le N)
  • (i,1) の左のマスは (i,N) であり、(i,N) の右のマスは (i,1) である。(1\le i\le N)

高橋君は、上下左右および斜めの 8 方向のうちいずれかを初めに選びます。そして、好きなマスから決めた方向に 1 マス移動することを N-1 回繰り返します。

高橋君は N 個のマス上を移動することになりますが、高橋君が通ったマスに書かれている数字を左から通った順番に並べた整数としてあり得る最大のものを求めてください。

制約

  • 1 \le N \le 10
  • 1 \le A_{i,j} \le 9
  • 入力はすべて整数。

入力

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

N
A_{1,1}A_{1,2}\dots A_{1,N}
A_{2,1}A_{2,2}\dots A_{2,N}
\vdots
A_{N,1}A_{N,2}\dots A_{N,N}

出力

答えを出力せよ。


入力例 1

4
1161
1119
7111
1811

出力例 1

9786

高橋君が上から 2 行目、左から 4 列目のマスから出発し、右下に進むことで、通ったマスに書かれた数字を並べ 9786 を作ることができます。 9786 より大きい値を作ることはできないため、9786 が解です。


入力例 2

10
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111

出力例 2

1111111111

32bit整数型に答えが収まるとは限らないことに注意してください。

Score : 200 points

Problem Statement

You are given a positive integer N.

We have a grid with N rows and N columns, where the square at the i-th row from the top and j-th column from the left has a digit A_{i,j} written on it.

Assume that the upper and lower edges of this grid are connected, as well as the left and right edges. In other words, all of the following holds.

  • (N,i) is just above (1,i), and (1,i) is just below (N,i). (1\le i\le N).
  • (i,N) is just to the left of (i,1), and (i,1) is just to the right of (i,N). (1\le i\le N).

Takahashi will first choose one of the following eight directions: up, down, left, right, and the four diagonal directions. Then, he will start on a square of his choice and repeat moving one square in the chosen direction N-1 times.

In this process, Takahashi visits N squares. Find the greatest possible value of the integer that is obtained by arranging the digits written on the squares visited by Takahashi from left to right in the order visited by him.

Constraints

  • 1 \le N \le 10
  • 1 \le A_{i,j} \le 9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_{1,1}A_{1,2}\dots A_{1,N}
A_{2,1}A_{2,2}\dots A_{2,N}
\vdots
A_{N,1}A_{N,2}\dots A_{N,N}

Output

Print the answer.


Sample Input 1

4
1161
1119
7111
1811

Sample Output 1

9786

If Takahashi starts on the square at the 2-nd row from the top and 4-th column from the left and goes down and to the right, the integer obtained by arranging the digits written on the visited squares will be 9786. It is impossible to make a value greater than 9786, so the answer is 9786.


Sample Input 2

10
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111

Sample Output 2

1111111111

Note that the answer may not fit into a 32-bit integer.

D - Adjacency Matrix

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 150

問題文

N 頂点の単純無向グラフ G があり、グラフの頂点には 1,2,\ldots, N の番号が付けられています。

G の隣接行列 (A_{i,j}) が与えられます。すなわち、GA_{i,j} = 1 であるとき、またそのときに限り頂点 i と頂点 j を結ぶ辺を持ちます。

i = 1, 2, \ldots, N について、頂点 i と直接結ばれている頂点の番号を昇順に出力してください。

ただし、頂点 i と頂点 j が直接結ばれているとは、頂点 i と頂点 j を結ぶ辺が存在することをいいます。

制約

  • 2 \leq N \leq 100
  • A_{i,j} \in \lbrace 0,1 \rbrace
  • A_{i,i} = 0
  • A_{i,j} = A_{j,i}
  • 入力される値はすべて整数

入力

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

N
A_{1,1} A_{1,2} \ldots A_{1,N}
A_{2,1} A_{2,2} \ldots A_{2,N}
\vdots
A_{N,1} A_{N,2} \ldots A_{N,N}

出力

N 行出力せよ。 i 行目には頂点 i と直接結ばれている頂点の番号を昇順に空白区切りで出力せよ。


入力例 1

4
0 1 1 0
1 0 0 1
1 0 0 0
0 1 0 0

出力例 1

2 3
1 4
1
2

頂点 1 と直接結ばれている頂点は頂点 2, 3 です。したがって、1 行目には 2, 3 をこの順で出力します。

同様に、2 行目には 1, 4 をこの順に、3 行目には 1 を、4 行目には 2 を出力します。


入力例 2

2
0 0
0 0

出力例 2



G に辺が存在しないこともあります。


入力例 3

5
0 1 0 1 1
1 0 0 1 0
0 0 0 0 1
1 1 0 0 1
1 0 1 1 0

出力例 3

2 4 5
1 4
5
1 2 5
1 3 4

Score: 150 points

Problem Statement

There is a simple undirected graph G with N vertices labeled with numbers 1, 2, \ldots, N.

You are given the adjacency matrix (A_{i,j}) of G. That is, G has an edge connecting vertices i and j if and only if A_{i,j} = 1.

For each i = 1, 2, \ldots, N, print the numbers of the vertices directly connected to vertex i in ascending order.

Here, vertices i and j are said to be directly connected if and only if there is an edge connecting vertices i and j.

Constraints

  • 2 \leq N \leq 100
  • A_{i,j} \in \lbrace 0,1 \rbrace
  • A_{i,i} = 0
  • A_{i,j} = A_{j,i}
  • All input values are integers.

Input

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

N
A_{1,1} A_{1,2} \ldots A_{1,N}
A_{2,1} A_{2,2} \ldots A_{2,N}
\vdots
A_{N,1} A_{N,2} \ldots A_{N,N}

Output

Print N lines. The i-th line should contain the numbers of the vertices directly connected to vertex i in ascending order, separated by a space.


Sample Input 1

4
0 1 1 0
1 0 0 1
1 0 0 0
0 1 0 0

Sample Output 1

2 3
1 4
1
2

Vertex 1 is directly connected to vertices 2 and 3. Thus, the first line should contain 2 and 3 in this order.

Similarly, the second line should contain 1 and 4 in this order, the third line should contain 1, and the fourth line should contain 2.


Sample Input 2

2
0 0
0 0

Sample Output 2



G may have no edges.


Sample Input 3

5
0 1 0 1 1
1 0 0 1 0
0 0 0 0 1
1 1 0 0 1
1 0 1 1 0

Sample Output 3

2 4 5
1 4
5
1 2 5
1 3 4
E - Dislike Foods

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

AtCoderレストランでは 1 から N までの番号がつけられている N 種類の食材を扱っています。

また、AtCoderレストランでは 1 から M までの番号がつけられている M 個の料理を提供しています。料理 i には K_i 種類の食材が使われており、食材 A_{i,1}, A_{i,2}, \ldots, A_{i,K_i} が使われています。

すぬけくんは現在 N 種類の食材がすべて苦手です。また、すぬけ君は苦手な食材が 1 種類でも使われている料理を食べることができず、苦手な食材が 1 種類も使われていない料理を食べることができます。

すぬけ君はこれから N 日間かけて苦手な食材を克服しようとしています。 すぬけ君は i 日目に食材 B_i を克服し、それ以降苦手な食材でなくなります。

i=1,2,\ldots,N について以下の値を求めてください。

  • i 日目にすぬけ君が食材 B_i を克服した直後、すぬけ君が食べることができるAtCoderレストランの料理の個数

制約

  • 1 \leq N \leq 3 \times 10^{5}
  • 1 \leq M \leq 3 \times 10^{5}
  • 1 \leq K_i \leq N (1 \leq i \leq M)
  • K_i の総和は 3 \times 10^{5} 以下
  • 1 \leq A_{i,j} \leq N (1 \leq i \leq M, 1 \leq j \leq K_i)
  • A_{i,j} \neq A_{i,k} (1 \leq i \leq M, j \neq k)
  • 1 \leq B_i \leq N (1 \leq i \leq N)
  • B_i \neq B_j (i \neq j )
  • 入力は全て整数

入力

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

N M
K_1 A_{1,1} A_{1,2} \ldots A_{1,K_1}
K_2 A_{2,1} A_{2,2} \ldots A_{2,K_2}
\vdots
K_M A_{M,1} A_{M,2} \ldots A_{M,K_M}
B_1 B_2 \ldots B_N

出力

N 行出力せよ。k 行目には、i=k のときの値を出力せよ。


入力例 1

5 4
2 1 2
3 3 4 5
3 1 2 5
1 3
1 3 2 5 4

出力例 1

0
1
2
3
4

以下のようにすぬけ君は苦手な食材を克服します。

  • 1 日目: 食材 1 を克服する。このとき、どの料理にも苦手な食材が使われているため 0 を出力する。
  • 2 日目: 食材 3 を克服する。このとき、料理 4 は苦手な食材が使われなくなるため食べられるようになる。料理 4 以外の料理は苦手な食材が使われているため、 1 を出力する。
  • 3 日目: 食材 2 を克服する。このとき、料理 1 は苦手な食材が使われなくなるため食べられるようになる。料理 1,4 以外の料理は苦手な食材が使われているため、 2 を出力する。
  • 4 日目: 食材 5 を克服する。このとき、料理 3 は苦手な食材が使われなくなるため食べられるようになる。料理 1,3,4 以外の料理は苦手な食材が使われているため、 3 を出力する。
  • 5 日目: 食材 4 を克服する。このとき、料理 2 は苦手な食材が使われなくなるため食べられるようになる。全ての料理に苦手な食材が使われていないため、 4 を出力する。

入力例 2

9 8
1 4
5 6 9 7 4 3
4 2 4 1 3
1 1
5 7 9 8 1 5
2 9 8
1 2
1 1
6 5 2 7 8 4 1 9 3

出力例 2

0
0
1
1
1
2
4
6
8

Score : 300 points

Problem Statement

The AtCoder Restaurant uses N types of ingredients numbered from 1 to N.

The restaurant offers M dishes numbered from 1 to M. Dish i uses K_i types of ingredients, namely A_{i,1}, A_{i,2}, \ldots, A_{i,K_i}.

Snuke currently dislikes all N ingredients. He cannot eat any dish that uses one or more ingredients he dislikes, and he can eat a dish that uses none of the disliked ingredients.

Over the next N days, he will overcome his dislikes one ingredient per day. On day i, he overcomes ingredient B_i, and from then on he no longer dislikes it.

For each i=1,2,\ldots,N, find:

  • the number of dishes at the AtCoder Restaurant that he can eat immediately after overcoming ingredient B_i on day i.

Constraints

  • 1 \leq N \leq 3 \times 10^{5}
  • 1 \leq M \leq 3 \times 10^{5}
  • 1 \leq K_i \leq N (1 \leq i \leq M)
  • The sum of K_i is at most 3 \times 10^{5}.
  • 1 \leq A_{i,j} \leq N (1 \leq i \leq M, 1 \leq j \leq K_i)
  • A_{i,j} \neq A_{i,k} (1 \leq i \leq M, j \neq k)
  • 1 \leq B_i \leq N (1 \leq i \leq N)
  • B_i \neq B_j (i \neq j )
  • All input values are integers.

Input

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

N M
K_1 A_{1,1} A_{1,2} \ldots A_{1,K_1}
K_2 A_{2,1} A_{2,2} \ldots A_{2,K_2}
\vdots
K_M A_{M,1} A_{M,2} \ldots A_{M,K_M}
B_1 B_2 \ldots B_N

Output

Print N lines. The k-th line should contain the answer for i=k.


Sample Input 1

5 4
2 1 2
3 3 4 5
3 1 2 5
1 3
1 3 2 5 4

Sample Output 1

0
1
2
3
4

Snuke overcomes his disliked ingredients as follows:

  • Day 1: He overcomes ingredient 1. At this time, every dish still uses a disliked ingredient, so print 0.
  • Day 2: He overcomes ingredient 3. Dish 4 no longer uses any disliked ingredient and becomes edible; all other dishes still use disliked ingredients, so print 1.
  • Day 3: He overcomes ingredient 2. Dish 1 no longer uses any disliked ingredient and becomes edible; all dishes except 1 and 4 still use disliked ingredients, so print 2.
  • Day 4: He overcomes ingredient 5. Dish 3 no longer uses any disliked ingredient and becomes edible; all dishes except 1, 3, and 4 still use disliked ingredients, so print 3.
  • Day 5: He overcomes ingredient 4. Dish 2 no longer uses any disliked ingredient and becomes edible; now all dishes have no disliked ingredients, so print 4.

Sample Input 2

9 8
1 4
5 6 9 7 4 3
4 2 4 1 3
1 1
5 7 9 8 1 5
2 9 8
1 2
1 1
6 5 2 7 8 4 1 9 3

Sample Output 2

0
0
1
1
1
2
4
6
8