実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 350 点
問題文
文字列 S が与えられます。次の操作を ちょうど 1 回 行った後の文字列としてあり得るものがいくつあるか求めてください。
- S の長さを N とする。 1\leq i<j\leq N をみたす整数の組 (i,j) を選び、S の i 文字目と j 文字目を入れ替える。
なお、この問題の制約下で操作を必ず行うことができることが証明できます。
制約
- S は英小文字からなる長さ 2 以上 10^6 以下の文字列
入力
入力は以下の形式で標準入力から与えられる。
S
出力
S に対して問題文中の操作をちょうど 1 回行った後の文字列としてあり得るものの個数を出力せよ。
入力例 1
abc
出力例 1
3
S の長さは 3 であるため、1\leq i<j\leq 3 をみたす整数の組 (i,j) としては、 (1,2), (1,3), (2,3) の 3 通りが考えられます。
- S の 1 文字目と 2 文字目を入れ替えた時、S は
bac
となります。 - S の 1 文字目と 3 文字目を入れ替えた時、S は
cba
となります。 - S の 2 文字目と 3 文字目を入れ替えた時、S は
acb
となります。
よって、abc
に対して操作を行った後の文字列としては、bac
, cba
, acb
の 3 つがあり得るため、3 を出力します。
入力例 2
aaaaa
出力例 2
1
どの 2 つの文字を入れ替えても S は aaaaa
のままです。よって、操作後の文字列としてあり得るものは 1 つです。
Points: 350 points
Problem Statement
You are given a string S. Find the number of strings that can result from performing the following operation exactly once.
- Let N be the length of S. Choose a pair of integers (i,j) such that 1\leq i<j\leq N and swap the i-th and j-th characters of S.
It can be proved that you can always perform it under the constraints of this problem.
Constraints
- S is a string of length between 2 and 10^6, inclusive, consisting of lowercase English letters.
Input
The input is given from Standard Input in the following format:
S
Output
Print the number of strings that can result from performing the operation in the problem statement exactly once on S.
Sample Input 1
abc
Sample Output 1
3
The length of S is 3, so 1\leq i<j\leq 3 is satisfied by three pairs of integers (i,j): (1,2), (1,3), and (2,3).
- Swapping the 1-st and 2-nd characters of S results in S being
bac
. - Swapping the 1-st and 3-rd characters of S results in S being
cba
. - Swapping the 2-nd and 3-rd characters of S results in S being
acb
.
Therefore, the operation on abc
results in one of the three strings: bac
, cba
, and acb
, so print 3.
Sample Input 2
aaaaa
Sample Output 2
1
Swapping any two characters results in S remaining aaaaa
. Thus, only one string can result from the operation.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
長さ N の正数列 A=(A_1,A_2,\dots,A_N) が与えられます。以下で定義される長さ N の数列 B = (B_1,B_2,\dots,B_N) を求めてください。
- i=1,2,\dots,N について、B_i を次のように定義する:
- A_i と等しい要素が i の直前に出現した位置を B_i とする。そのような位置が存在しなければ B_i=-1 とする。
より正確には、正整数 j であって,A_i = A_j となる j < i が存在するならば、そのうち最大の j を B_i とする。そのような j が存在しなければ B_i=-1 とする。
- A_i と等しい要素が i の直前に出現した位置を B_i とする。そのような位置が存在しなければ B_i=-1 とする。
制約
- 1 \leq N \leq 2 \times 10^5
- 1 \leq A_i \leq 10^9
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \dots A_N
出力
B の要素を空白区切りで1行に出力せよ。
入力例 1
5 1 2 1 1 3
出力例 1
-1 -1 1 3 -1
- i=1: A_1=1 より前に 1 は現れないので、B_1=-1 です。
- i=2: A_2=2 より前に 2 は現れないので、B_2=-1 です。
- i=3: A_3=1 の直前に現れた 1 は A_1 なので、B_3=1 です。
- i=4: A_4=1 の直前に現れた 1 は A_3 なので、B_4=3 です。
- i=5: A_5=3 より前に 3 は現れないので、B_5=-1 です。
入力例 2
4 1 1000000000 1000000000 1
出力例 2
-1 -1 2 1
Score : 300 points
Problem Statement
You are given a sequence of N positive numbers, A = (A_1, A_2, \dots, A_N). Find the sequence B = (B_1, B_2, \dots, B_N) of length N defined as follows.
- For i = 1, 2, \dots, N, define B_i as follows:
- Let B_i be the most recent position before i where an element equal to A_i appeared. If such a position does not exist, let B_i = -1.
More precisely, if there exists a positive integer j such that A_i = A_j and j < i, let B_i be the largest such j. If no such j exists, let B_i = -1.
- Let B_i be the most recent position before i where an element equal to A_i appeared. If such a position does not exist, let B_i = -1.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq A_i \leq 10^9
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N A_1 A_2 \dots A_N
Output
Print the elements of B in one line, separated by spaces.
Sample Input 1
5 1 2 1 1 3
Sample Output 1
-1 -1 1 3 -1
- i = 1: There is no 1 before A_1 = 1, so B_1 = -1.
- i = 2: There is no 2 before A_2 = 2, so B_2 = -1.
- i = 3: The most recent occurrence of 1 before A_3 = 1 is A_1, so B_3 = 1.
- i = 4: The most recent occurrence of 1 before A_4 = 1 is A_3, so B_4 = 3.
- i = 5: There is no 3 before A_5 = 3, so B_5 = -1.
Sample Input 2
4 1 1000000000 1000000000 1
Sample Output 2
-1 -1 2 1
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 425 点
問題文
AtCoder 諸島は N 個の島からなり、これらの島々は N 本の橋によって結ばれています。 島には 1 から N までの番号が付けられていて、i\ (1\leq i\leq N-1) 本目の橋は島 i と島 i+1 を、N 本目の橋は島 N と島 1 を双方向に結んでいます。 橋を渡る以外に島の間を行き来する方法は存在しません。
AtCoder 諸島では、島 X_1 から始めて島 X_2,X_3,\dots,X_M を順に訪れるツアーが定期的に催行されています。 移動の過程で訪れる島とは別の島を経由することもあり、ツアー中に橋を通る回数の合計がツアーの長さと定義されます。
厳密には、ツアーとは以下の条件を全て満たす l+1 個の島の列 a_0,a_1,\dots,a_l のことであり、その長さ は l として定義されます。
- 全ての j\ (0\leq j\leq l-1) について、島 a_j と島 a_{j+1} は橋で直接結ばれている
- ある 0 = y_1 < y_2 < \dots < y_M = l が存在して、全ての k\ (1\leq k\leq M) について a_{y_k} = X_k
財政難に苦しむ AtCoder 諸島では、維持費削減のため橋を 1 本封鎖することになりました。 封鎖する橋をうまく選んだとき、ツアーの長さの最小値がいくつになるか求めてください。
制約
- 3\leq N \leq 2\times 10^5
- 2\leq M \leq 2\times 10^5
- 1\leq X_k\leq N
- X_k\neq X_{k+1}\ (1\leq k\leq M-1)
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N M X_1 X_2 \dots X_M
出力
答えを整数として出力せよ。
入力例 1
3 3 1 3 2
出力例 1
2
- 1 本目の橋を封鎖した場合:通る島の列として (a_0,a_1,a_2)=(1,3,2) を取ることで島 1,3,2 を順に訪れることができ、長さ 2 のツアーが催行できます。これより短いツアーは存在しません。
- 2 本目の橋を封鎖した場合:通る島の列として (a_0,a_1,a_2,a_3)=(1,3,1,2) を取ることで島 1,3,2 を順に訪れることができ、長さ 3 のツアーが催行できます。これより短いツアーは存在しません。
- 3 本目の橋を封鎖した場合:通る島の列として (a_0,a_1,a_2,a_3)=(1,2,3,2) を取ることで島 1,3,2 を順に訪れることができ、長さ 3 のツアーが催行できます。これより短いツアーは存在しません。
よって、封鎖する橋をうまく選んだときのツアーの長さの最小値は 2 です。
以下の図は左から順に橋 1,2,3 を封鎖した場合を表し、数字の書かれた丸が島、丸同士を結ぶ線が橋、青い矢印が最短のツアーの経路を表します。
入力例 2
4 5 2 4 2 4 2
出力例 2
8
X_1,X_2,\dots,X_M の中に同じ島が複数回現れることもあります。
入力例 3
163054 10 62874 19143 77750 111403 29327 56303 6659 18896 64175 26369
出力例 3
390009
Score: 425 points
Problem Statement
The AtCoder Archipelago consists of N islands connected by N bridges. The islands are numbered from 1 to N, and the i-th bridge (1\leq i\leq N-1) connects islands i and i+1 bidirectionally, while the N-th bridge connects islands N and 1 bidirectionally. There is no way to travel between islands other than crossing the bridges.
On the islands, a tour that starts from island X_1 and visits islands X_2, X_3, \dots, X_M in order is regularly conducted. The tour may pass through islands other than those being visited, and the total number of times bridges are crossed during the tour is defined as the length of the tour.
More precisely, a tour is a sequence of l+1 islands a_0, a_1, \dots, a_l that satisfies all the following conditions, and its length is defined as l:
- For all j\ (0\leq j\leq l-1), islands a_j and a_{j+1} are directly connected by a bridge.
- There are some 0 = y_1 < y_2 < \dots < y_M = l such that for all k\ (1\leq k\leq M), a_{y_k} = X_k.
Due to financial difficulties, the islands will close one bridge to reduce maintenance costs. Determine the minimum possible length of the tour when the bridge to be closed is chosen optimally.
Constraints
- 3\leq N \leq 2\times 10^5
- 2\leq M \leq 2\times 10^5
- 1\leq X_k\leq N
- X_k\neq X_{k+1}\ (1\leq k\leq M-1)
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M X_1 X_2 \dots X_M
Output
Print the answer as an integer.
Sample Input 1
3 3 1 3 2
Sample Output 1
2
- If the first bridge is closed: By taking the sequence of islands (a_0, a_1, a_2) = (1, 3, 2), it is possible to visit islands 1, 3, 2 in order, and a tour of length 2 can be conducted. There is no shorter tour.
- If the second bridge is closed: By taking the sequence of islands (a_0, a_1, a_2, a_3) = (1, 3, 1, 2), it is possible to visit islands 1, 3, 2 in order, and a tour of length 3 can be conducted. There is no shorter tour.
- If the third bridge is closed: By taking the sequence of islands (a_0, a_1, a_2, a_3) = (1, 2, 3, 2), it is possible to visit islands 1, 3, 2 in order, and a tour of length 3 can be conducted. There is no shorter tour.
Therefore, the minimum possible length of the tour when the bridge to be closed is chosen optimally is 2.
The following figure shows, from left to right, the cases when bridges 1, 2, 3 are closed, respectively. The circles with numbers represent islands, the lines connecting the circles represent bridges, and the blue arrows represent the shortest tour routes.
Sample Input 2
4 5 2 4 2 4 2
Sample Output 2
8
The same island may appear multiple times in X_1, X_2, \dots, X_M.
Sample Input 3
163054 10 62874 19143 77750 111403 29327 56303 6659 18896 64175 26369
Sample Output 3
390009
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
文字列 x,y に対して f(x,y) を以下で定義します。
- x,y の最長共通接頭辞の長さを f(x,y) とする。
英小文字からなる N 個の文字列 (S_1,\ldots,S_N) が与えられます。次の式の値を求めてください。
制約
- 2\leq N\leq 3\times 10^5
- S_i は英小文字からなる文字列
- 1\leq |S_i|
- |S_1|+|S_2|+\ldots+|S_N|\leq 3\times 10^5
- 入力される数値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N S_1 \ldots S_N
出力
答えを出力せよ。
入力例 1
3 ab abc arc
出力例 1
4
- f(S_1,S_2)=2
- f(S_1,S_3)=1
- f(S_2,S_3)=1
なので、答えは f(S_1,S_2)+f(S_1,S_3)+f(S_2,S_3) = 4 です。
入力例 2
11 ab bb aaa bba baba babb aaaba aabbb a a b
出力例 2
32
Score: 500 points
Problem Statement
For strings x and y, define f(x, y) as follows:
- f(x, y) is the length of the longest common prefix of x and y.
You are given N strings (S_1, \ldots, S_N) consisting of lowercase English letters. Find the value of the following expression:
Constraints
- 2 \leq N \leq 3\times 10^5
- S_i is a string consisting of lowercase English letters.
- 1 \leq |S_i|
- |S_1|+|S_2|+\ldots+|S_N|\leq 3\times 10^5
- All input numbers are integers.
Input
The input is given from Standard Input in the following format:
N S_1 \ldots S_N
Output
Print the answer.
Sample Input 1
3 ab abc arc
Sample Output 1
4
- f(S_1,S_2)=2
- f(S_1,S_3)=1
- f(S_2,S_3)=1
Thus, the answer is f(S_1,S_2) + f(S_1,S_3) + f(S_2,S_3) = 4.
Sample Input 2
11 ab bb aaa bba baba babb aaaba aabbb a a b
Sample Output 2
32
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
以下の条件を全て満たす数列の個数を、998244353 で割った余りを求めてください。
- 数列の長さが N
- 数列の各項は 1 以上 M 以下の整数
- 最長増加部分列の長さがちょうど 3
注記
数列の部分列とは、数列から 0 個以上の要素を取り除いた後、残りの要素を元の順序で連結して得られる数列のことをいいます。
例えば、(10,30) は (10,20,30) の部分列ですが、(20,10) は (10,20,30) の部分列ではありません。
数列の最長増加部分列とは、数列の狭義単調増加な部分列のうち、長さが最大のもののことをいいます。
制約
- 3 \leq N \leq 1000
- 3 \leq M \leq 10
- 入力は全て整数である
入力
入力は以下の形式で標準入力から与えられる。
N M
出力
答えを出力せよ。
入力例 1
4 5
出力例 1
135
例えば (3,4,1,5) は条件を満たす数列です。
一方 (4,4,1,5) は最長増加部分列の長さが 2 なので条件を満たしません。
入力例 2
3 4
出力例 2
4
入力例 3
111 3
出力例 3
144980434
998244353 で割った余りを求めてください。
Score : 500 points
Problem Statement
Find the number of sequences that satisfy all of the conditions below, modulo 998244353.
- The length is N.
- Each of the elements is an integer between 1 and M (inclusive).
- Its longest increasing subsequence has the length of exactly 3.
Notes
A subsequence of a sequence is the result of removing zero or more elements from it and concatenating the remaining elements without changing the order. For example, (10,30) is a subsequence of (10,20,30), while (20,10) is not a subsequence of (10,20,30).
A longest increasing subsequence of a sequence is its strictly increasing subsequence with the greatest length.
Constraints
- 3 \leq N \leq 1000
- 3 \leq M \leq 10
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N M
Output
Print the answer.
Sample Input 1
4 5
Sample Output 1
135
One sequence that satisfies the conditions is (3,4,1,5).
On the other hand, (4,4,1,5) do not, because its longest increasing subsequence has the length of 2.
Sample Input 2
3 4
Sample Output 2
4
Sample Input 3
111 3
Sample Output 3
144980434
Find the count modulo 998244353.