A - Five Integers

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

与えられる 5 つの整数 A, B, C, D, E の中に何種類の整数があるかを出力してください。

制約

  • 0 \leq A, B, C, D, E \leq 100
  • 入力はすべて整数

入力

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

A B C D E

出力

答えを出力せよ。


入力例 1

31 9 24 31 24

出力例 1

3

与えられる 5 つの整数 31, 9, 24, 31, 24 の中には、9, 24, 31 という 3 種類の整数があります。 よって、3 を出力します。


入力例 2

0 0 0 0 0

出力例 2

1

Score : 100 points

Problem Statement

Print how many distinct integers there are in given five integers A, B, C, D, and E.

Constraints

  • 0 \leq A, B, C, D, E \leq 100
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B C D E

Output

Print the answer.


Sample Input 1

31 9 24 31 24

Sample Output 1

3

In the given five integers 31, 9, 24, 31, and 24, there are three distinct integers 9, 24, and 31. Thus, 3 should be printed.


Sample Input 2

0 0 0 0 0

Sample Output 2

1
B - Prefix?

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 200

問題文

英小文字のみからなる 2 つの文字列 S, T が与えられます。 ST の接頭辞かどうかを判定してください。

接頭辞とは 長さ N の文字列 T_1T_2\ldots T_N の接頭辞とは、 0 \leq i \leq N を満たすある整数 i によって、T の先頭 i 文字目までの文字列 T_1T_2\ldots T_i として表される文字列です。例えば、T = abc のとき、T の接頭辞は、空文字列、a 、ab 、abc の 4 つです。

制約

  • ST はそれぞれ英小文字のみからなる長さが 1 以上 100 以下の文字列

入力

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

S
T

出力

ST の接頭辞である場合は Yes を、そうでない場合は No を出力せよ。 ジャッジは英小文字と英大文字を厳密に区別することに注意せよ。


入力例 1

atco
atcoder

出力例 1

Yes

atcoatcoder の接頭辞です。よって、Yes を出力します。


入力例 2

code
atcoder

出力例 2

No

codeatcoder の接頭辞ではありません。よって、No を出力します。


入力例 3

abc
abc

出力例 3

Yes

文字列全体もその文字列の接頭辞であることに注意してください。


入力例 4

aaaa
aa

出力例 4

No

Score : 200 points

Problem Statement

You are given two strings S and T consisting of lowercase English letters. Determine if S is a prefix of T.

What is a prefix? A prefix of a string T_1T_2\ldots T_N of length N is a string expressed as the first i characters of T, T_1T_2\ldots T_i, where i is an integer such that 0 \leq i \leq N. For example, when T = abc, there are four prefixes of T: an empty string, a, ab, and abc.

Constraints

  • S and T are strings of lengths between 1 and 100 (inclusive) consisting of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S
T

Output

Print Yes if S is a prefix of T; print No otherwise. Note that the judge is case-sensitive.


Sample Input 1

atco
atcoder

Sample Output 1

Yes

atco is a prefix of atcoder. Thus, Yes should be printed.


Sample Input 2

code
atcoder

Sample Output 2

No

code is not a prefix of atcoder. Thus, No should be printed.


Sample Input 3

abc
abc

Sample Output 3

Yes

Note that a string is also a prefix of itself.


Sample Input 4

aaaa
aa

Sample Output 4

No
C - Chinese Restaurant

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 300

問題文

回転テーブルの周りに人 0、人 1\ldots、人 N-1 がこの順番で反時計回りに等間隔で並んでいます。また、人 i の目の前には料理 p_i が置かれています。
あなたは次の操作を 0 回以上何度でも行うことが出来ます。

  • 回転テーブルを反時計回りに 1 周の \frac{1}{N} だけ回す。これによって、(この操作の直前に)人 i の目の前にあった料理は人 (i+1) \bmod N の目の前に移動する。

操作を完了させた後において、人 i は料理 i が人 (i-1) \bmod N、人 i、人 (i+1) \bmod N のいずれかの目の前に置かれていると喜びます。
喜ぶ人数の最大値を求めてください。

a \bmod m とは 整数 a と正整数 m に対し、a \bmod ma-xm の倍数となるような 0 以上 m 未満の整数 x を表します。(このような x は一意に定まることが証明できます)

制約

  • 3 \leq N \leq 2 \times 10^5
  • 0 \leq p_i \leq N-1
  • i \neq j ならば p_i \neq p_j
  • 入力はすべて整数

入力

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

N
p_0 \ldots p_{N-1}

出力

答えを出力せよ。


入力例 1

4
1 2 0 3

出力例 1

4

操作を 1 回行うと下の画像のようになります。

この時、4 人が喜ぶことを以下のように確かめられます。

  • 0 は料理 0 が人 3\ (=(0-1) \bmod 4) の目の前に置かれているので喜びます。
  • 1 は料理 1 が人 1\ (=1) の目の前に置かれているので喜びます。
  • 2 は料理 2 が人 2\ (=2) の目の前に置かれているので喜びます。
  • 3 は料理 3 が人 0\ (=(3+1) \bmod 4) の目の前に置かれているので喜びます。

5 人以上が喜ぶことは無いため、答えは 4 です。


入力例 2

3
0 1 2

出力例 2

3

入力例 3

10
3 9 6 1 7 2 8 0 5 4

出力例 3

5

Score : 300 points

Problem Statement

Person 0, Person 1, \ldots, and Person (N-1) are sitting around a turntable in their counterclockwise order, evenly spaced. Dish p_i is in front of Person i on the table.
You may perform the following operation 0 or more times:

  • Rotate the turntable by one N-th of a counterclockwise turn. As a result, the dish that was in front of Person i right before the rotation is now in front of Person (i+1) \bmod N.

When you are finished, Person i is happy if Dish i is in front of Person (i-1) \bmod N, Person i, or Person (i+1) \bmod N.
Find the maximum possible number of happy people.

What is a \bmod m? For an integer a and a positive integer m, a \bmod m denotes the integer x between 0 and (m-1) (inclusive) such that (a-x) is a multiple of m. (It can be proved that such x is unique.)

Constraints

  • 3 \leq N \leq 2 \times 10^5
  • 0 \leq p_i \leq N-1
  • p_i \neq p_j if i \neq j.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
p_0 \ldots p_{N-1}

Output

Print the answer.


Sample Input 1

4
1 2 0 3

Sample Output 1

4

The figure below shows the table after one operation.

Here, there are four happy people:

  • Person 0 is happy because Dish 0 is in front of Person 3\ (=(0-1) \bmod 4);
  • Person 1 is happy because Dish 1 is in front of Person 1\ (=1);
  • Person 2 is happy because Dish 2 is in front of Person 2\ (=2);
  • Person 3 is happy because Dish 3 is in front of Person 0\ (=(3+1) \bmod 4).

There cannot be five or more happy people, so the answer is 4.


Sample Input 2

3
0 1 2

Sample Output 2

3

Sample Input 3

10
3 9 6 1 7 2 8 0 5 4

Sample Output 3

5
D - Unique Username

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 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.

E - Chinese Restaurant (Three-Star Version)

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

回転テーブルの周りに人 0、人 1\ldots、人 N-1 がこの順番で反時計回りに等間隔で並んでいます。また、人 i の目の前には料理 p_i が置かれています。
あなたは次の操作を 0 回以上何度でも行うことが出来ます。

  • 回転テーブルを反時計回りに 1 周の \frac{1}{N} だけ回す。これによって、(この操作の直前に)人 i の目の前にあった料理は人 (i+1) \bmod N の目の前に移動する。

操作を完了させた後において、人 i の不満度は料理 i が人 (i-k) \bmod N、人 (i+k) \bmod N のいずれかの目の前に置かれているような最小の非負整数 k です。
N 人の不満度の総和の最小値を求めてください。

a \bmod m とは 整数 a と正整数 m に対し、a \bmod ma-xm の倍数となるような 0 以上 m 未満の整数 x を表します。(このような x は一意に定まることが証明できます)

制約

  • 3 \leq N \leq 2 \times 10^5
  • 0 \leq p_i \leq N-1
  • i \neq j ならば p_i \neq p_j
  • 入力はすべて整数

入力

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

N
p_0 \ldots p_{N-1}

出力

答えを出力せよ。


入力例 1

4
1 2 0 3

出力例 1

2

操作を 1 回行うと下の画像のようになります。

この時、不満度の総和が 2 になることを以下のように確かめられます。

  • 0 の不満度は、料理 0 が人 3\ (=(0-1) \bmod 4) の目の前に置かれているので 1 です。
  • 1 の不満度は、料理 1 が人 1\ (=(1+0) \bmod 4) の目の前に置かれているので 0 です。
  • 2 の不満度は、料理 2 が人 2\ (=(2+0) \bmod 4) の目の前に置かれているので 0 です。
  • 3 の不満度は、料理 3 が人 0\ (=(3+1) \bmod 4) の目の前に置かれているので 1 です。

不満度の総和を 2 より小さくすることは出来ないため、答えは 2 です。


入力例 2

3
0 1 2

出力例 2

0

入力例 3

10
3 9 6 1 7 2 8 0 5 4

出力例 3

20

Score : 500 points

Problem Statement

Person 0, Person 1, \ldots, and Person (N-1) are sitting around a turntable in counterclockwise order, evenly spaced. Dish p_i is in front of Person i on the table.
You may perform the following operation 0 or more times:

  • Rotate the turntable by one N-th of a counterclockwise turn. The dish that was in front of Person i right before the rotation is now in front of Person (i+1) \bmod N.

When you are finished, Person i gains frustration of k, where k is the minimum integer such that Dish i is in front of either Person (i-k) \bmod N or Person (i+k) \bmod N.
Find the minimum possible sum of frustration of the N people.

What is a \bmod m? For an integer a and a positive integer m, a \bmod m denotes the integer x between 0 and (m-1) (inclusive) such that (a-x) is a multiple of m. (It can be proved that such x is unique.)

Constraints

  • 3 \leq N \leq 2 \times 10^5
  • 0 \leq p_i \leq N-1
  • p_i \neq p_j if i \neq j.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
p_0 \ldots p_{N-1}

Output

Print the answer.


Sample Input 1

4
1 2 0 3

Sample Output 1

2

The figure below shows the table after one operation.

Here, the sum of their frustration is 2 because:

  • Person 0 gains a frustration of 1 since Dish 0 is in front of Person 3\ (=(0-1) \bmod 4);
  • Person 1 gains a frustration of 0 since Dish 1 is in front of Person 1\ (=(1+0) \bmod 4);
  • Person 2 gains a frustration of 0 since Dish 2 is in front of Person 2\ (=(2+0) \bmod 4);
  • Person 3 gains a frustration of 1 since Dish 3 is in front of Person 0\ (=(3+1) \bmod 4).

We cannot make the sum of their frustration less than 2, so the answer is 2.


Sample Input 2

3
0 1 2

Sample Output 2

0

Sample Input 3

10
3 9 6 1 7 2 8 0 5 4

Sample Output 3

20
F - Best Concatenation

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

1 から 9 の数字および X のみからなる N 個の文字列 S_1, S_2, \ldots, S_N が与えられます。

(1, 2, \ldots, N) を並べ替えた列 P = (P_1, P_2, \ldots, P_N) を一つ選び、 文字列 T = S_{P_1} + S_{P_2} + \cdots + S_{P_N} を作ります。ここで、+ は文字列の連結を表します。

そして、文字列 T = T_1T_2\ldots T_{|T|} の「スコア」を計算します( |T| は文字列 T の長さを表します)。
T のスコアは、スコアが 0 の状態から始め、下記の 9 個の手順を行うことで計算されます。

  • 1 \leq i \lt j \leq |T| および T_i = X かつ T_j = 1 を満たす整数の組 (i, j) の個数だけ、スコアを 1 点加算する 。
  • 1 \leq i \lt j \leq |T| および T_i = X かつ T_j = 2 を満たす整数の組 (i, j) の個数だけ、スコアを 2 点加算する。
  • 1 \leq i \lt j \leq |T| および T_i = X かつ T_j = 3 を満たす整数の組 (i, j) の個数だけ、スコアを 3 点加算する。
  • \cdots
  • 1 \leq i \lt j \leq |T| および T_i = X かつ T_j = 9 を満たす整数の組 (i, j) の個数だけ、スコアを 9 点加算する。

P を任意に選ぶときの、T のスコアとしてあり得る最大値を出力してください。

制約

  • 2 \leq N \leq 2 \times 10^5
  • N は整数
  • S_i1 から 9 の数字および X のみからなる長さ 1 以上の文字列
  • S_1, S_2, \ldots, S_N の長さの総和は 2 \times 10^5 以下

入力

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

N
S_1
S_2
\vdots
S_N

出力

答えを出力せよ。


入力例 1

3
1X3
59
XXX

出力例 1

71

P = (3, 1, 2) とすると、T = S_3 + S_1 + S_2 = XXX1X359 です。 このとき、T のスコアは次の通り計算されます。

  • 1 \leq i \lt j \leq |T| および T_i = X かつ T_j = 1 を満たす整数の組 (i, j)3 個あり、
  • 1 \leq i \lt j \leq |T| および T_i = X かつ T_j = 3 を満たす整数の組 (i, j)4 個あり、
  • 1 \leq i \lt j \leq |T| および T_i = X かつ T_j = 5 を満たす整数の組 (i, j)4 個あり、
  • 1 \leq i \lt j \leq |T| および T_i = X かつ T_j = 9 を満たす整数の組 (i, j)4 個あります。

よって、T のスコアは 1 \times 3 + 3 \times 4 + 5 \times 4 + 9 \times 4 = 71 であり、これが考えられる最大値です。


入力例 2

10
X63X395XX
X2XX3X22X
13
3716XXX6
45X
X6XX
9238
281X92
1XX4X4XX6
54X9X711X1

出力例 2

3010

Score : 500 points

Problem Statement

You are given N strings S_1, S_2, \ldots, S_N consisting of digits from 1 through 9 and the character X.

We will choose a permutation P = (P_1, P_2, \ldots, P_N) of (1, 2, \ldots, N) to construct a string T = S_{P_1} + S_{P_2} + \cdots + S_{P_N}, where + denotes a concatenation of strings.

Then, we will calculate the "score" of the string T = T_1T_2\ldots T_{|T|} (where |T| denotes the length of T).
The score is calculated by the following 9 steps, starting from the initial score 0:

  • Add 1 point to the score as many times as the number of integer pairs (i, j) such that 1 \leq i \lt j \leq |T|, T_i = X, and T_j = 1.
  • Add 2 points to the score as many times as the number of integer pairs (i, j) such that 1 \leq i \lt j \leq |T|, T_i = X, and T_j = 2.
  • Add 3 points to the score as many times as the number of integer pairs (i, j) such that 1 \leq i \lt j \leq |T|, T_i = X, and T_j = 3.
  • \cdots
  • Add 9 points to the score as many times as the number of integer pairs (i, j) such that 1 \leq i \lt j \leq |T|, T_i = X, and T_j = 9.

Find the maximum possible score of T when P can be chosen arbitrarily.

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • N is an integer.
  • S_i is a string of length at least 1 consisting of digits from 1 through 9 and the character X.
  • The sum of lengths of S_1, S_2, \ldots, S_N is at most 2 \times 10^5.

Input

Input is given from Standard Input in the following format:

N
S_1
S_2
\vdots
S_N

Output

Print the answer.


Sample Input 1

3
1X3
59
XXX

Sample Output 1

71

When P = (3, 1, 2), we have T = S_3 + S_1 + S_2 = XXX1X359. Then, the score of T is calculated as follows:

  • there are 3 integer pairs (i, j) such that 1 \leq i \lt j \leq |T|, T_i = X, and T_j = 1;
  • there are 4 integer pairs (i, j) such that 1 \leq i \lt j \leq |T|, T_i = X, and T_j = 3;
  • there are 4 integer pairs (i, j) such that 1 \leq i \lt j \leq |T|, T_i = X, and T_j = 5;
  • there are 4 integer pairs (i, j) such that 1 \leq i \lt j \leq |T|, T_i = X, and T_j = 9.

Therefore, the score of T is 1 \times 3 + 3 \times 4 + 5 \times 4 + 9 \times 4 = 71, which is the maximum possible value.


Sample Input 2

10
X63X395XX
X2XX3X22X
13
3716XXX6
45X
X6XX
9238
281X92
1XX4X4XX6
54X9X711X1

Sample Output 2

3010
G - Random Student ID

Time Limit: 3 sec / Memory Limit: 1024 MB

配点 : 600

問題文

高橋小学校には N 人の新入生がおり、i = 1, 2, \ldots, N について i 番目の新入生の名前は S_i (英小文字のみからなる文字列)です。 N 人の名前は相異なります。

N 人の新入生には、名前が辞書順で小さい者から順に 1, 2, 3, \ldots, N と学籍番号が付与されます。 ただしその際には、a が最小で z が最大である通常の英小文字の順序の代わりに、下記で定まる順序を用います。

  • まず高橋校長が、長さ 26 の文字列 abcdefghijklmnopqrstuvwxyz を並べ替えて得られる 26! 個の文字列の中から 1 つを、等確率でランダムに文字列 P として選ぶ。
  • P で前にある英小文字ほど小さい英小文字とみなす。

N 人の新入生それぞれについて、与えられる学籍番号の期待値を \mathrm{mod}\, 998244353 で出力してください(注記参照)。

辞書順で小さいとは?

文字列 S = S_1S_2\ldots S_{|S|} が文字列 T = T_1T_2\ldots T_{|T|} より辞書順で小さいとは、下記の 1. と 2. のどちらかが成り立つことを言います。 ここで、|S|, |T| はそれぞれ S, T の長さを表します。

  1. |S| \lt |T| かつ S_1S_2\ldots S_{|S|} = T_1T_2\ldots T_{|S|}
  2. ある整数 1 \leq i \leq \min\lbrace |S|, |T| \rbrace が存在して、下記の 2 つがともに成り立つ。
    • S_1S_2\ldots S_{i-1} = T_1T_2\ldots T_{i-1}
    • S_iT_i より小さい文字である。

注記

求める期待値は必ず有理数となることが証明できます。またこの問題の制約下では、その値を互いに素な 2 つの整数 P, Q を用いて \frac{P}{Q} と表したとき、R \times Q \equiv P\pmod{998244353} かつ 0 \leq R \lt 998244353 を満たす整数 R がただ一つ存在することが証明できます。この R を求めてください。

制約

  • 2 \leq N
  • N は整数
  • S_i は英小文字のみからなる長さ 1 以上の文字列
  • 与えられる文字列の長さの総和は 5 \times 10^5 以下
  • i \neq j \Rightarrow S_i \neq S_j

入力

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

N
S_1
S_2
\vdots
S_N

出力

N行出力せよ。 i = 1, 2, \ldots, N について、i 行目には i 番目の新入生に与えられる学籍番号の期待値を \mathrm{mod}\, 998244353 で出力せよ。


入力例 1

3
a
aa
ab

出力例 1

1
499122179
499122179

1 番目の新入生に与えられる学籍番号の期待値は 1 であり、2, 3 番目の新入生に与えられる学籍番号の期待値は \frac{5}{2} です。

答えを \mathrm{mod}\, 998244353 で出力することに注意してください。 例えば、2, 3 番目の新入生についての出力では、求める期待値が \frac{5}{2} であり、 2 \times 499122179 \equiv 5\pmod{998244353} が成り立つので、 499122179 を出力します。


入力例 2

3
a
aa
aaa

出力例 2

1
2
3

Score : 600 points

Problem Statement

Takahashi Elementary School has N new students. For i = 1, 2, \ldots, N, the name of the i-th new student is S_i (which is a string consisting of lowercase English letters). The names of the N new students are distinct.

The N students will be assigned a student ID 1, 2, 3, \ldots, N in ascending lexicographical order of their names. However, instead of the ordinary order of lowercase English letters where a is the minimum and z is the maximum, we use the following order:

  • First, Principal Takahashi chooses a string P from the 26! permutations of the string abcdefghijklmnopqrstuvwxyz of length 26, uniformly at random.
  • The lowercase English characters that occur earlier in P are considered smaller.

For each of the N students, find the expected value, modulo 998244353, of the student ID assigned (see Notes).

What is the lexicographical order?

A string S = S_1S_2\ldots S_{|S|} is said to be lexicographically smaller than a string T = T_1T_2\ldots T_{|T|} if one of the following 1. and 2. holds. Here, |S| and |T| denote the lengths of S and T, respectively.

  1. |S| \lt |T| and S_1S_2\ldots S_{|S|} = T_1T_2\ldots T_{|S|}.
  2. There exists an integer 1 \leq i \leq \min\lbrace |S|, |T| \rbrace satisfying the following two conditions:
    • S_1S_2\ldots S_{i-1} = T_1T_2\ldots T_{i-1}
    • S_i is a smaller character than T_i.

Notes

We can prove that the sought expected value is always a rational number. Moreover, under the Constraints of this problem, when the value is represented as \frac{P}{Q} by two coprime integers P and Q, we can prove that there is a unique integer R such that R \times Q \equiv P\pmod{998244353} and 0 \leq R \lt 998244353. Find such R.

Constraints

  • 2 \leq N
  • N is an integer.
  • S_i is a string of length at least 1 consisting of lowercase English letters.
  • The sum of lengths of the given strings is at most 5 \times 10^5.
  • i \neq j \Rightarrow S_i \neq S_j

Input

Input is given from Standard Input in the following format:

N
S_1
S_2
\vdots
S_N

Output

Print N lines. For each i = 1, 2, \ldots, N, the i-th line should contain the expected value, modulo 998244353, of the student ID assigned to Student i.


Sample Input 1

3
a
aa
ab

Sample Output 1

1
499122179
499122179

The expected value of the student ID assigned to Student 1 is 1; the expected values of the student ID assigned to Student 2 and 3 are \frac{5}{2}.

Note that the answer should be printed modulo 998244353. For example, the sought expected value for Student 2 and 3 is \frac{5}{2}, and we have 2 \times 499122179 \equiv 5\pmod{998244353}, so 499122179 should be printed.


Sample Input 2

3
a
aa
aaa

Sample Output 2

1
2
3
Ex - Taboo

Time Limit: 4 sec / Memory Limit: 1024 MB

配点 : 600

問題文

文字列 S が与えられます。また、高橋君は次の操作を 0 回以上行うことが出来ます。

  • 1 \leq i \leq |S| なる整数 i を選び、Si 文字目を * に変える。

高橋君の目的は、S部分文字列として N 個の文字列 T_1,T_2,\ldots,T_N がいずれも現れないようにすることです。
これを達成するために必要な操作の回数の最小値を求めてください。

制約

  • 1 \leq |S| \leq 5 \times 10^5
  • 1 \leq N
  • N は整数
  • 1 \leq |T_i|
  • \sum{|T_i|} \leq 5 \times 10^5
  • i \neq j ならば T_i \neq T_j
  • S, T_i は英小文字のみからなる文字列

入力

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

S
N
T_1
T_2
\vdots
T_N

出力

答えを出力せよ。


入力例 1

abcdefghijklmn
3
abcd
ijk
ghi

出力例 1

2

i として 19 を選んで操作をすると S*bcdefgh*jklmn となり、abcdijkghi がいずれも部分文字列として現れなくなります。


入力例 2

atcoderbeginnercontest
1
abc

出力例 2

0

操作をする必要がありません。


入力例 3

aaaaaaaaa
2
aa
xyz

出力例 3

4

Score : 600 points

Problem Statement

You are given a string S. Takahashi may perform the following operation 0 or more times:

  • Choose an integer i such that 1 \leq i \leq |S| and change the i-th character of S to *.

Takahashi's objective is to make S not contain any of N strings T_1,T_2,\ldots,T_N as a substring.
Find the minimum number of operations required to achieve the objective.

Constraints

  • 1 \leq |S| \leq 5 \times 10^5
  • 1 \leq N
  • N is an integer.
  • 1 \leq |T_i|
  • \sum{|T_i|} \leq 5 \times 10^5
  • T_i \neq T_j if i \neq j.
  • S and T_i are strings consisting of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S
N
T_1
T_2
\vdots
T_N

Output

Print the answer.


Sample Input 1

abcdefghijklmn
3
abcd
ijk
ghi

Sample Output 1

2

If he performs the operation twice by choosing 1 and 9 for i, S becomes *bcdefgh*jklmn; now it does not contain abcd, ijk, or ghi as a substring.


Sample Input 2

atcoderbeginnercontest
1
abc

Sample Output 2

0

No operation is needed.


Sample Input 3

aaaaaaaaa
2
aa
xyz

Sample Output 3

4