実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
英小文字のみからなる長さ N の文字列 S が与えられます。
S の文字を並び替えて得られる文字列(S 自身を含む)であって、長さ K の回文を部分文字列として 含まない ものの個数を求めてください。
ただし、長さ N の文字列 T が「長さ K の回文を部分文字列として含む」とは、
ある (N-K) 以下の非負整数 i が存在して、1 以上 K 以下の任意の整数 j について T_{i+j}=T_{i+K+1-j} が成り立つことをいいます。
ここで、T_k は文字列 T の k 文字目を表すものとします。
制約
- 2\leq K \leq N \leq 10
- N,K は整数
- S は英小文字のみからなる長さ N の文字列
入力
入力は以下の形式で標準入力から与えられる。
N K S
出力
S の文字を並び替えて得られる文字列であって、長さ K の回文を部分文字列として含まないものの個数を出力せよ。
入力例 1
3 2 aab
出力例 1
1
aab を並び替えて得られる文字列は aab, aba, baa の 3 つであり、このうち aab および baa は長さ 2 の回文 aa を部分文字列として含んでいます。
よって、条件をみたす文字列は aba のみであり、1 を出力します。
入力例 2
5 3 zzyyx
出力例 2
16
zzyyx を並べて得られる文字列は 30 個ありますが、そのうち長さ 3 の回文を含まないようなものは 16 個です。よって、16 を出力します。
入力例 3
10 5 abcwxyzyxw
出力例 3
440640
Score : 300 points
Problem Statement
You are given a string S of length N consisting only of lowercase English letters.
Find the number of strings obtained by permuting the characters of S (including the string S itself) that do not contain a palindrome of length K as a substring.
Here, a string T of length N is said to "contain a palindrome of length K as a substring" if and only if there exists a non-negative integer i not greater than (N-K) such that T_{i+j} = T_{i+K+1-j} for every integer j with 1 \leq j \leq K.
Here, T_k denotes the k-th character of the string T.
Constraints
- 2 \leq K \leq N \leq 10
- N and K are integers.
- S is a string of length N consisting only of lowercase English letters.
Input
The input is given from Standard Input in the following format:
N K S
Output
Print the number of strings obtained by permuting S that do not contain a palindrome of length K as a substring.
Sample Input 1
3 2 aab
Sample Output 1
1
The strings obtained by permuting aab are aab, aba, and baa. Among these, aab and baa contain the palindrome aa of length 2 as a substring.
Thus, the only string that satisfies the condition is aba, so print 1.
Sample Input 2
5 3 zzyyx
Sample Output 2
16
There are 30 strings obtained by permuting zzyyx, 16 of which do not contain a palindrome of length 3. Thus, print 16.
Sample Input 3
10 5 abcwxyzyxw
Sample Output 3
440640
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
長さ N の文字列 S が与えられます。S_i\ (1\leq i \leq N) を S の左から i 番目の文字とします。
あなたは以下の 2 種類の操作を好きな順番で 0 回以上好きな回数行うことができます。
-
A 円払う。 S の左端の文字を右端に移動する。すなわち、S_1S_2\ldots S_N を S_2\ldots S_NS_1 に変える。
-
B 円払う。 1 以上 N 以下の整数 i を選び、 S_i を好きな英小文字で置き換える。
S を回文にするためには最低で何円必要ですか?
回文とは
ある文字列 T について、 T の長さを |T| として、全ての整数 i (1 \le i \le |T|) について、 T の前から i 文字目と後ろから i 文字目が同じであるとき、またそのときに限って、 T は回文です。制約
- 1\leq N \leq 5000
- 1\leq A,B\leq 10^9
- S は英小文字からなる長さ N の文字列
- S 以外の入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N A B S
出力
答えを整数として出力せよ。
入力例 1
5 1 2 rrefa
出力例 1
3
最初に 2 番目の操作を 1 回行います。2 円払い、i=5 として S_5 を e で置き換えます。 S は rrefe となります。
次に 1 番目の操作を 1 回行います。1 円払い、S は refer となります。これは回文です。
よって 3 円払うことで S を回文にすることができました。 2 円以下払うことで S を回文にすることは不可能なので、これが答えです。
入力例 2
8 1000000000 1000000000 bcdfcgaa
出力例 2
4000000000
答えは 32 bit 整数に収まらない場合があることに注意してください。
Score : 300 points
Problem Statement
You are given a string S of length N. Let S_i\ (1\leq i \leq N) be the i-th character of S from the left.
You may perform the following two kinds of operations zero or more times in any order:
-
Pay A yen (the currency in Japan). Move the leftmost character of S to the right end. In other words, change S_1S_2\ldots S_N to S_2\ldots S_NS_1.
-
Pay B yen. Choose an integer i between 1 and N, and replace S_i with any lowercase English letter.
How many yen do you need to pay to make S a palindrome?
What is a palindrome?
A string T is a palindrome if and only if the i-th character from the left and the i-th character from the right are the same for all integers i (1 \le i \le |T|), where |T| is the length of T.Constraints
- 1\leq N \leq 5000
- 1\leq A,B\leq 10^9
- S is a string of length N consisting of lowercase English letters.
- All values in the input except for S are integers.
Input
The input is given from Standard Input in the following format:
N A B S
Output
Print the answer as an integer.
Sample Input 1
5 1 2 rrefa
Sample Output 1
3
First, pay 2 yen to perform the operation of the second kind once: let i=5 to replace S_5 with e. S is now rrefe.
Then, pay 1 yen to perform the operation of the first kind once. S is now refer, which is a palindrome.
Thus, you can make S a palindrome for 3 yen. Since you cannot make S a palindrome for 2 yen or less, 3 is the answer.
Sample Input 2
8 1000000000 1000000000 bcdfcgaa
Sample Output 2
4000000000
Note that the answer may not fit into a 32-bit integer type.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
H 行 W 列のグリッドがあります。 以下、上から i 行目、左から j 列目のマスを (i,j) と表記します。 グリッドの各マスには英小文字が書かれており、(i,j) に書かれた文字は与えられる文字列 S_i の j 文字目と一致します。
すぬけくんは、辺で隣接するマスに移動することを繰り返して (1,1) から (H,W) まで移動しようと思っています。
訪れるマス (最初の (1,1) と 最後の (H,W) を含む)に書かれた文字が、
訪れる順に s \rightarrow n \rightarrow u \rightarrow k
\rightarrow e \rightarrow s \rightarrow n \rightarrow \dots
となるような経路が存在するか判定してください。
なお、2 つのマス (i_1,j_1),(i_2,j_2) は |i_1-i_2|+|j_1-j_2| = 1 を満たすとき、またそのときに限り「辺で隣接する」といいます。
より厳密には、マスの列 ((i_1,j_1),(i_2,j_2),\dots,(i_k,j_k)) であって以下の条件を全て満たすものが存在するか判定してください。
- (i_1,j_1) = (1,1),(i_k,j_k) = (H,W)
- すべての t\ (1 \leq t < k) について、(i_t,j_t) と (i_{t+1},j_{t+1}) は辺で隣接する
- すべての t\ (1 \leq t \leq k) について、(i_t,j_t) に書かれた文字は
snukeの ((t-1) \bmod 5) + 1 文字目と一致する
制約
- 2\leq H,W \leq 500
- H,W は整数
- S_i は英小文字からなる長さ W の文字列
入力
入力は以下の形式で標準入力から与えられる。
H W S_1 S_2 \vdots S_H
出力
問題文中の条件を満たす経路が存在するならば Yes を、存在しないならば No を出力せよ。
入力例 1
2 3 sns euk
出力例 1
Yes
(1,1) \rightarrow (1,2) \rightarrow (2,2) \rightarrow (2,3) という経路は、訪れたマスに書かれた文字が訪れた順に
s \rightarrow n \rightarrow u \rightarrow k となるため条件を満たします。
入力例 2
2 2 ab cd
出力例 2
No
入力例 3
5 7 skunsek nukesnu ukeseku nsnnesn uekukku
出力例 3
Yes
Score : 400 points
Problem Statement
We have a grid with H horizontal rows and W vertical columns. We denote by (i,j) the cell at the i-th row from the top and j-th column from the left. Each cell in the grid has a lowercase English letter written on it. The letter written on (i,j) equals the j-th character of a given string S_i.
Snuke will repeat moving to an adjacent cell sharing a side to travel from (1,1) to (H,W).
Determine if there is a path
in which the letters written on the visited cells (including initial (1,1) and final (H,W)) are
s \rightarrow n \rightarrow u \rightarrow k
\rightarrow e \rightarrow s \rightarrow n \rightarrow \dots, in the order of visiting.
Here, a cell (i_1,j_1) is said to be an adjacent cell of (i_2,j_2) sharing a side if and only if |i_1-i_2|+|j_1-j_2| = 1.
Formally, determine if there is a sequence of cells ((i_1,j_1),(i_2,j_2),\dots,(i_k,j_k)) such that:
- (i_1,j_1) = (1,1),(i_k,j_k) = (H,W);
- (i_{t+1},j_{t+1}) is an adjacent cell of (i_t,j_t) sharing a side, for all t\ (1 \leq t < k); and
- the letter written on (i_t,j_t) coincides with the (((t-1) \bmod 5) + 1)-th character of
snuke, for all t\ (1 \leq t \leq k).
Constraints
- 2\leq H,W \leq 500
- H and W are integers.
- S_i is a string of length W consisting of lowercase English letters.
Input
The input is given from Standard Input in the following format:
H W S_1 S_2 \vdots S_H
Output
Print Yes if there is a path satisfying the conditions in the problem statement; print No otherwise.
Sample Input 1
2 3 sns euk
Sample Output 1
Yes
The path (1,1) \rightarrow (1,2) \rightarrow (2,2) \rightarrow (2,3) satisfies the conditions
because they have s \rightarrow n \rightarrow u \rightarrow k written on them, in the order of visiting.
Sample Input 2
2 2 ab cd
Sample Output 2
No
Sample Input 3
5 7 skunsek nukesnu ukeseku nsnnesn uekukku
Sample Output 3
Yes
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 450 点
問題文
1 から N の番号がついた N 個のマスが一列に並んでいます。
1 \leq i < N について、マス i とマス i+1 は隣接しています。
最初、マス i は色 i で塗られています。
クエリが Q 個与えられるので、順に処理してください。クエリは次の 2 種類のいずれかです。
1 x c: マス x から始めて「いまいるマスと同じ色に塗られている隣接するマス」への移動を繰り返すことで到達可能なマスを全て色 c に塗り替える2 c: 色 c で塗られているマスの個数を答える
制約
- 1 \leq N \leq 5\times 10^5
- 1 \leq Q \leq 2\times 10^5
- 1 種類目のクエリにおいて、1 \leq x \leq N
- 1,2 種類目のクエリにおいて、1 \leq c \leq N
- 2 種類目のクエリが少なくとも 1 つ存在する
- 入力は全て整数である
入力
入力は以下の形式で標準入力から与えられる。
N Q
\mathrm{query}_1
\vdots
\mathrm{query}_Q
各クエリは以下の 2 種類のいずれかの形式で与えられる。
1 x c
2 c
出力
2 種類目のクエリの回数を q として、q 行出力せよ。
i 行目には、i 回目のそのようなクエリに対する答えを出力せよ。
入力例 1
5 6 1 5 4 1 4 2 2 2 1 3 2 1 2 3 2 3
出力例 1
3 4
クエリにより、マスの色は図のように塗り替えられていきます。

Score : 450 points
Problem Statement
There are N cells in a row, numbered 1 to N.
For each 1 \leq i < N, cells i and i+1 are adjacent.
Initially, cell i is painted with color i.
You are given Q queries. Process them in order. Each query is of one of the following two types.
1 x c: Repaint the following to color c: all reachable cells reachable from cell x by repeatedly moving to an adjacent cell painted in the same color as the current cell.2 c: Print the number of cells painted with color c.
Constraints
- 1 \leq N \leq 5 \times 10^5
- 1 \leq Q \leq 2 \times 10^5
- In queries of the first type, 1 \leq x \leq N.
- In queries of the first and second types, 1 \leq c \leq N.
- There is at least one query of the second type.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N Q
\mathrm{query}_1
\vdots
\mathrm{query}_Q
Each query is given in one of the following two formats:
1 x c
2 c
Output
Let q be the number of queries of the second type. Print q lines.
The i-th line should contain the answer to the i-th such query.
Sample Input 1
5 6 1 5 4 1 4 2 2 2 1 3 2 1 2 3 2 3
Sample Output 1
3 4
The queries recolor the cells as shown in the figure.

実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
長さ N の文字列 S および整数 i\ (0\leq i\leq N) に対して、f_i(S) を、
- S の先頭 i 文字
- S を反転した文字列
- S の末尾 N-i 文字
をこの順に連結した文字列と定義します。
例えば、S= abc、i=2 のとき、f_i(S)= abcbac です。
長さ 2N の文字列 T が与えられます。 f_i(S)=T を満たす長さ N の文字列 S と整数 i\ (0\leq i\leq N) の組を 1 つ見つけてください。 そのような S,i の組が存在しない場合は、それを報告してください。
制約
- 1\leq N \leq 10^6
- N は整数
- T は英小文字からなる長さ 2N の文字列
入力
入力は以下の形式で標準入力から与えられる。
N T
出力
条件を満たす S,i の組が存在しないならば、-1 と出力せよ。
存在するならば、S,i を改行区切りで出力せよ。
条件を満たす S,i の組が複数存在する場合は、そのどれを出力しても良い。
入力例 1
3 abcbac
出力例 1
abc 2
問題文中に書いた通り、S= abc、i=2 とすると f_i(S)= abcbac となって T に一致するため、abc と 2 を出力します。
入力例 2
4 abababab
出力例 2
abab 1
S= abab、i=3 としても条件を満たします。
入力例 3
3 agccga
出力例 3
cga 0
S= agc、i=3 としても条件を満たします。
入力例 4
4 atcodeer
出力例 4
-1
条件を満たす S,i の組が存在しない場合は -1 を出力してください。
Score : 500 points
Problem Statement
For a string S of length N and an integer i\ (0\leq i\leq N), let us define the string f_i(S) as the concatenation of:
- the first i characters of S,
- the reversal of S, and
- the last (N-i) characters of S,
in this order.
For instance, if S= abc and i=2, we have f_i(S)= abcbac.
You are given a string T of length 2N. Find a pair of a string S of length N and an integer i\ (0\leq i\leq N) such that f_i(S)=T. If no such pair of S and i exists, report that fact.
Constraints
- 1\leq N \leq 10^6
- N is an integer.
- T is a string of length 2N consisting of lowercase English letters.
Input
The input is given from Standard Input in the following format:
N T
Output
If no pair of S and i satisfies the condition, print -1.
Otherwise, print S and i, separated by a newline.
If multiple pairs of S and i satisfy the condition, you may print any of them.
Sample Input 1
3 abcbac
Sample Output 1
abc 2
As mentioned in the problem statement, if S= abc and i=2, we have f_i(S)= abcbac, which equals T, so you should print abc and 2.
Sample Input 2
4 abababab
Sample Output 2
abab 1
S= abab and i=3 also satisfy the condition.
Sample Input 3
3 agccga
Sample Output 3
cga 0
S= agc and i=3 also satisfy the condition.
Sample Input 4
4 atcodeer
Sample Output 4
-1
If no pair of S and i satisfies the condition, print -1.