C - Minimize Ordering

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

文字列 S が与えられます。S の各文字を並び替えて得られる文字列 S' のうち、辞書順で最小のものを出力してください。

なお、相異なる 2 つの文字列 s = s_1 s_2 \ldots s_nt = t_1 t_2 \ldots t_m について、それらが以下の条件のいずれかを満たすとき、辞書順で s \lt t であるとします。

  • ある整数 i\ (1 \leq i \leq \min(n,m)) が存在し、s_i \lt t_i かつすべての整数 j\ (1 \leq j \lt i) について s_j=t_j
  • すべての整数 i\ (1 \leq i \leq \min(n,m)) について s_i = t_i かつ、n \lt m

制約

  • S は英小文字のみからなる長さ 1 以上 2 \times 10^5 以下の文字列

入力

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

S

出力

S の各文字を並び替えて得られる文字列 S' のうち、辞書順で最小のものを出力せよ。


入力例 1

aba

出力例 1

aab

S= aba を並び替えて得られる文字列は以下の 3 つが考えられます。

  • aba
  • aab
  • baa

この中で辞書順で最小のものは、aab です。


入力例 2

zzzz

出力例 2

zzzz

Score : 200 points

Problem Statement

You are given a string S. Find the lexicographically smallest string S' obtained by permuting the characters of S.

Here, for different two strings s = s_1 s_2 \ldots s_n and t = t_1 t_2 \ldots t_m, s \lt t holds lexicographically when one of the conditions below is satisfied.

  • There is an integer i\ (1 \leq i \leq \min(n,m)) such that s_i \lt t_i and s_j=t_j for all integers j\ (1 \leq j \lt i).
  • s_i = t_i for all integers i\ (1 \leq i \leq \min(n,m)), and n \lt m.

Constraints

  • S is a string of length between 1 and 2 \times 10^5 (inclusive) consisting of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S

Output

Print the lexicographically smallest string S' obtained by permuting the characters in S.


Sample Input 1

aba

Sample Output 1

aab

Three strings can be obtained by permuting aba:

  • aba
  • aab
  • baa

The lexicographically smallest among them is aab.


Sample Input 2

zzzz

Sample Output 2

zzzz
D - Everyone is Friends

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

1,2,\ldots,N の番号がついた N 人の人がいます。

M 回の舞踏会が行われました。 i (1\leq i \leq M) 回目の舞踏会には k_i 人が参加し、参加した人は人 x_{i,1},x_{i,2},\ldots,x_{i,k_i} でした。

どの二人も少なくとも 1 回同じ舞踏会に参加したか判定してください。

制約

  • 2\leq N \leq 100
  • 1\leq M \leq 100
  • 2\leq k_i \leq N
  • 1\leq x_{i,1}<x_{i,2}<\ldots < x_{i,k_i}\leq N
  • 入力は全て整数

入力

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

N M
k_1 x_{1,1} x_{1,2} \ldots x_{1,k_1}
\vdots
k_M x_{M,1} x_{M,2} \ldots x_{M,k_M}

出力

どの二人も少なくとも 1 回同じ舞踏会に参加した場合 Yes を、そうでない場合 No を出力せよ。


入力例 1

3 3
2 1 2
2 2 3
2 1 3

出力例 1

Yes

1 と人 2 は共に 1 回目の舞踏会に参加しています。

2 と人 3 は共に 2 回目の舞踏会に参加しています。

1 と人 3 は共に 3 回目の舞踏会に参加しています。

以上よりどの二人も少なくとも 1 回同じ舞踏会に参加したので、答えは Yes です。


入力例 2

4 2
3 1 2 4
3 2 3 4

出力例 2

No

1 と人 31 回も同じ舞踏会に参加していないので、答えは No です。

Score : 200 points

Problem Statement

There are N people numbered 1,2,\ldots,N.

M parties were held. k_i people attended the i-th (1\leq i \leq M) party, and they were People x_{i,1},x_{i,2},\ldots,x_{i,k_i}.

Determine if every two people attended the same party at least once.

Constraints

  • 2\leq N \leq 100
  • 1\leq M \leq 100
  • 2\leq k_i \leq N
  • 1\leq x_{i,1}<x_{i,2}<\ldots < x_{i,k_i}\leq N
  • All values in the input are integers.

Input

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

N M
k_1 x_{1,1} x_{1,2} \ldots x_{1,k_1}
\vdots
k_M x_{M,1} x_{M,2} \ldots x_{M,k_M}

Output

Print Yes if every two people attended the same party at least once; print No otherwise.


Sample Input 1

3 3
2 1 2
2 2 3
2 1 3

Sample Output 1

Yes

Both Person 1 and Person 2 attended the 1-st party.

Both Person 2 and Person 3 attended the 2-nd party.

Both Person 1 and Person 3 attended the 3-rd party.

Therefore, every two people attended the same party at least once, so the answer is Yes.


Sample Input 2

4 2
3 1 2 4
3 2 3 4

Sample Output 2

No

Person 1 and Person 3 did not attend the same party, so the answer is No.

E - Merge Sequences

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

長さ N の狭義単調増加列 A=(A _ 1,A _ 2,\ldots,A _ N) と、長さ M の狭義単調増加列 B=(B _ 1,B _ 2,\ldots,B _ M) が与えられます。 ここで、すべての i,j\ (1\leq i\leq N,1\leq j\leq M) について A _ i\neq B _ j が成り立っています。

長さ N+M の狭義単調増加列 C=(C _ 1,C _ 2,\ldots,C _ {N+M}) を、次の操作を行った結果得られる列として定めます。

  • CAB を連結した列とする。厳密には、i=1,2,\ldots,N について C _ i=A _ i とし、i=N+1,N+2,\ldots,N+M について C _ i=B _ {i-N} とする。
  • C を昇順にソートする。

A _ 1,A _ 2,\ldots,A _ NB _ 1,B _ 2,\ldots,B _ M がそれぞれ C では何番目にあるか求めてください。 より厳密には、まず i=1,2,\ldots,N について C _ k=A _ i を満たす k を順に求めたのち、j=1,2,\ldots,M について C _ k=B _ j を満たす k を順に求めてください。

制約

  • 1\leq N,M\leq 10^5
  • 1\leq A _ 1\lt A _ 2\lt\cdots\lt A _ N\leq 10^9
  • 1\leq B _ 1\lt B _ 2\lt\cdots\lt B _ M\leq 10^9
  • すべての i,j\ (1\leq i\leq N,1\leq j\leq M) について A _ i\neq B _ j
  • 入力はすべて整数

入力

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

N M
A _ 1 A _ 2 \ldots A _ N
B _ 1 B _ 2 \ldots B _ M

出力

答えを 2 行で出力せよ。
1 行目には A _ 1,A _ 2,\ldots,A _ N がそれぞれ C では何番目にあるかを空白区切りで出力せよ。
2 行目には B _ 1,B _ 2,\ldots,B _ M がそれぞれ C では何番目にあるかを空白区切りで出力せよ。


入力例 1

4 3
3 14 15 92
6 53 58

出力例 1

1 3 4 7
2 5 6

C(3,6,14,15,53,58,92) となります。 A=(3,14,15,92) の要素はそれぞれ 1,3,4,7 番目にあり、B=(6,53,58) の要素はそれぞれ 2,5,6 番目にあります。


入力例 2

4 4
1 2 3 4
100 200 300 400

出力例 2

1 2 3 4
5 6 7 8

入力例 3

8 12
3 4 10 15 17 18 22 30
5 7 11 13 14 16 19 21 23 24 27 28

出力例 3

1 2 5 9 11 12 15 20
3 4 6 7 8 10 13 14 16 17 18 19

Score : 300 points

Problem Statement

You are given strictly increasing sequences of length N and M: A=(A _ 1,A _ 2,\ldots,A _ N) and B=(B _ 1,B _ 2,\ldots,B _ M). Here, A _ i\neq B _ j for every i and j (1\leq i\leq N,1\leq j\leq M).

Let C=(C _ 1,C _ 2,\ldots,C _ {N+M}) be a strictly increasing sequence of length N+M that results from the following procedure.

  • Let C be the concatenation of A and B. Formally, let C _ i=A _ i for i=1,2,\ldots,N, and C _ i=B _ {i-N} for i=N+1,N+2,\ldots,N+M.
  • Sort C in ascending order.

For each of A _ 1,A _ 2,\ldots,A _ N, B _ 1,B _ 2,\ldots,B _ M, find its position in C. More formally, for each i=1,2,\ldots,N, find k such that C _ k=A _ i, and for each j=1,2,\ldots,M, find k such that C _ k=B _ j.

Constraints

  • 1\leq N,M\leq 10^5
  • 1\leq A _ 1\lt A _ 2\lt\cdots\lt A _ N\leq 10^9
  • 1\leq B _ 1\lt B _ 2\lt\cdots\lt B _ M\leq 10^9
  • A _ i\neq B _ j for every i and j (1\leq i\leq N,1\leq j\leq M).
  • All values in the input are integers.

Input

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

N M
A _ 1 A _ 2 \ldots A _ N
B _ 1 B _ 2 \ldots B _ M

Output

Print the answer in two lines.
The first line should contain the positions of A _ 1,A _ 2,\ldots,A _ N in C, with spaces in between.
The second line should contain the positions of B _ 1,B _ 2,\ldots,B _ M in C, with spaces in between.


Sample Input 1

4 3
3 14 15 92
6 53 58

Sample Output 1

1 3 4 7
2 5 6

C will be (3,6,14,15,53,58,92). Here, the 1-st, 3-rd, 4-th, 7-th elements are from A=(3,14,15,92), and the 2-nd, 5-th, 6-th elements are from B=(6,53,58).


Sample Input 2

4 4
1 2 3 4
100 200 300 400

Sample Output 2

1 2 3 4
5 6 7 8

Sample Input 3

8 12
3 4 10 15 17 18 22 30
5 7 11 13 14 16 19 21 23 24 27 28

Sample Output 3

1 2 5 9 11 12 15 20
3 4 6 7 8 10 13 14 16 17 18 19
F - LRUD Instructions 2

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

二次元平面上に高橋君がいます。高橋君は原点から移動を N 回行いました。

N 回の移動は長さ N の文字列で表され、意味は次の通りです。

  • i 回目の高橋君の移動後の座標は、移動前の座標を (x,y) として、
    • Si 文字目が R であるとき (x+1,y)
    • Si 文字目が L であるとき (x-1,y)
    • Si 文字目が U であるとき (x,y+1)
    • Si 文字目が D であるとき (x,y-1)

N 回の移動 (始点と終点を含む) で、高橋君が同じ座標にいたことがあるかどうかを判定してください。

制約

  • 1 \leq N \leq 2\times 10^5
  • N は整数
  • SR, L, U, D のみからなる長さ N の文字列

入力

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

N
S

出力

N 回の移動 (始点と終点を含む) で、高橋君が同じ座標にいたことがあれば Yes、なければ No と出力せよ。


入力例 1

5
RLURU

出力例 1

Yes

高橋君のいる座標は (0,0)\to (1,0)\to (0,0)\to (0,1)\to (1,1)\to (1,2) と変化します。


入力例 2

20
URDDLLUUURRRDDDDLLLL

出力例 2

No

Score : 300 points

Problem Statement

Takahashi is on a two-dimensional plane. Starting from the origin, he made N moves.

The N moves are represented by a string of length N as described below:

  • Takahashi's coordinates after the i-th move are:

    • (x+1,y) if the i-th character of S is R;
    • (x-1,y) if the i-th character of S is L;
    • (x,y+1) if the i-th character of S is U; and
    • (x,y-1) if the i-th character of S is D,

    where (x,y) is his coordinates before the move.

Determine if Takahashi visited the same coordinates multiple times in the course of the N moves (including the starting and ending points).

Constraints

  • 1 \leq N \leq 2\times 10^5
  • N is an integer.
  • S is a string of length N consisting of R, L, U, and D.

Input

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

N
S

Output

Print Yes if Takahashi visited the same coordinates multiple times in the course of the N moves; print No otherwise.


Sample Input 1

5
RLURU

Sample Output 1

Yes

Takahashi's coordinates change as follows: (0,0)\to (1,0)\to (0,0)\to (0,1)\to (1,1)\to (1,2).


Sample Input 2

20
URDDLLUUURRRDDDDLLLL

Sample Output 2

No
G - Unique Username

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400

問題文

高橋君はあるサービスで使うユーザー名を決めるのに困っています。彼を助けるプログラムを書いてください。

以下の条件をすべて満たす文字列 X1 つ求めてください。

  • X は次の手順で得られる文字列である。
    • N 個の文字列 S_1,S_2,\ldots,S_N を好きな順番で並べたものを S_1', S_2', \ldots,S_N' とする。そして、S_1'、( 1 個以上の _ )、S_2'、( 1 個以上の _ )、\ldots、( 1 個以上の _ )、S_N' をこの順番で連結したものを X とする。
  • X の文字数は 3 以上 16 以下である。
  • XM 個の文字列 T_1,T_2,\ldots,T_M のいずれとも一致しない。

ただし、条件をすべて満たす文字列 X が存在しない場合は代わりに -1 と出力してください。

制約

  • 1 \leq N \leq 8
  • 0 \leq M \leq 10^5
  • N,M は整数
  • 1 \leq |S_i| \leq 16
  • N-1+\sum{|S_i|} \leq 16
  • i \neq j ならば S_i \neq S_j
  • S_i は英小文字のみからなる文字列
  • 3 \leq |T_i| \leq 16
  • i \neq j ならば T_i \neq T_j
  • T_i は英小文字と _ のみからなる文字列

入力

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

N M
S_1
S_2
\vdots
S_N
T_1
T_2
\vdots
T_M

出力

条件をすべて満たす文字列 X1 つ出力せよ。ただし、条件をすべて満たす文字列 X が存在しない場合は代わりに -1 を出力せよ。
答えが複数存在する場合、どれを出力しても良い。


入力例 1

1 1
chokudai
chokudai

出力例 1

-1

条件のうち 1 番目と 2 番目を満たす文字列は X= chokudai しかありませんが、これは T_1 と一致します。
このため、条件をすべて満たす文字列 X が存在せず、-1 が正しい出力となります。


入力例 2

2 2
choku
dai
chokudai
choku_dai

出力例 2

dai_choku

この他に、choku__dai (chokudai の間の _2 つ) 等も条件をすべて満たします。


入力例 3

2 2
chokudai
atcoder
chokudai_atcoder
atcoder_chokudai

出力例 3

-1

chokudai__atcoderatcoder__chokudai (chokudaiatcoder の間の _2 つ)は文字数が 17 なので 2 番目の条件を満たしません。


入力例 4

4 4
ab
cd
ef
gh
hoge
fuga
____
_ab_cd_ef_gh_

出力例 4

ab__ef___cd_gh

1 番目の条件で記述されている操作で得られないような文字列が T_i として与えられる場合があります。

Score : 400 points

Problem Statement

Takahashi is having trouble with deciding a username for a service. Write a code to help him.

Find a string X that satisfies all of the following conditions:

  • X is obtained by the following procedure:
    • Let S_1', S_2', \ldots,S_N' be a permutation of S_1, S_2, \ldots,S_N. Let X be the concatenation of S_1', (1 or more copies of _), S_2', (1 or more copies of _), \ldots, (1 or more copies of _), and S_N', in this order.
  • The length of X is between 3 and 16, inclusive.
  • X does not coincide with any of M strings T_1,T_2,\ldots,T_M.

If there is no X that satisfies all of the conditions, print -1 instead.

Constraints

  • 1 \leq N \leq 8
  • 0 \leq M \leq 10^5
  • N and M are integers.
  • 1 \leq |S_i| \leq 16
  • N-1+\sum{|S_i|} \leq 16
  • S_i \neq S_j if i \neq j.
  • S_i is a string consisting of lowercase English letters.
  • 3 \leq |T_i| \leq 16
  • T_i \neq T_j if i \neq j.
  • T_i is a string consisting of lowercase English letters and _.

Input

Input is given from Standard Input in the following format:

N M
S_1
S_2
\vdots
S_N
T_1
T_2
\vdots
T_M

Output

Print a string X that satisfies all of the conditions. If there is no X that satisfies all of the conditions, print -1 instead.
If there are multiple solutions, print any of them.


Sample Input 1

1 1
chokudai
chokudai

Sample Output 1

-1

The only string that satisfies the first and second conditions is X= chokudai, but it coincides with T_1.
Thus, there is no X that satisfies all of the conditions, so -1 should be printed.


Sample Input 2

2 2
choku
dai
chokudai
choku_dai

Sample Output 2

dai_choku

Strings like choku__dai (which has two _'s between choku and dai) also satisfy all of the conditions.


Sample Input 3

2 2
chokudai
atcoder
chokudai_atcoder
atcoder_chokudai

Sample Output 3

-1

chokudai__atcoder and atcoder__chokudai (which have two _'s between chokudai and atcoder) have a length of 17, which violates the second condition.


Sample Input 4

4 4
ab
cd
ef
gh
hoge
fuga
____
_ab_cd_ef_gh_

Sample Output 4

ab__ef___cd_gh

The given T_i may contain a string that cannot be obtained by the procedure described in the first condition.