Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
ボールがいくつか刺さった棒が N 本あり、各ボールには英小文字が 1 個書かれています。
i = 1, 2, \ldots, N について、i 番目の棒に刺さった各ボールの英小文字は、文字列 S_i によって表されます。 具体的には、i 番目の棒には文字列 S_i の長さ |S_i| に等しい個数のボールが刺さっており、 刺さっているボールの英小文字を、棒のある端から順に並べたものは文字列 S_i と等しいです。
2 つの棒は、一方の棒に刺さっているボールの英小文字をどちらかの端から並べた列と、もう一方の棒に刺さっているボールの英小文字をどちらかの端から並べた列が一致するとき、同じ棒とみなされます。 より形式的には、1 以上 N 以下の整数 i, j について、i 本目の棒と j 本目の棒は、S_i が S_j と一致するか、S_i が S_j を前後反転したものと一致するとき、かつそのときに限り、同じとみなされます。
N 本の棒の中に、何種類の異なる棒があるかを出力してください。
制約
- N は整数
- 2 \leq N \leq 2 \times 10^5
- S_i は英小文字のみからなる文字列
- |S_i| \geq 1
- \sum_{i = 1}^N |S_i| \leq 2 \times 10^5
入力
入力は以下の形式で標準入力から与えられる。
N S_1 S_2 \vdots S_N
出力
答えを出力せよ。
入力例 1
6 a abc de cba de abc
出力例 1
3
- S_2 =
abcが S_4 =cbaを前後反転したものと一致するため、2 番目の棒と 4 番目の棒は同じとみなされます。 - S_2 =
abcが S_6 =abcと一致するため、2 番目の棒と 6 番目の棒は同じとみなされます。 - S_3 =
deが S_5 =deと一致するため、3 番目の棒と 5 番目の棒は同じとみなされます。
よって、6 本の棒の中に、1 本目の棒、2 本目の棒( 4, 6 本目の棒と同じ)、3 本目の棒( 5 本目の棒と同じ)の 3 種類の異なる棒があります。
Score : 300 points
Problem Statement
There are N sticks with several balls stuck onto them. Each ball has a lowercase English letter written on it.
For each i = 1, 2, \ldots, N, the letters written on the balls stuck onto the i-th stick are represented by a string S_i. Specifically, the number of balls stuck onto the i-th stick is the length |S_i| of the string S_i, and S_i is the sequence of letters on the balls starting from one end of the stick.
Two sticks are considered the same when the sequence of letters on the balls starting from one end of one stick is equal to the sequence of letters starting from one end of the other stick. More formally, for integers i and j between 1 and N, inclusive, the i-th and j-th sticks are considered the same if and only if S_i equals S_j or its reversal.
Print the number of different sticks among the N sticks.
Constraints
- N is an integer.
- 2 \leq N \leq 2 \times 10^5
- S_i is a string consisting of lowercase English letters.
- |S_i| \geq 1
- \sum_{i = 1}^N |S_i| \leq 2 \times 10^5
Input
The 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
6 a abc de cba de abc
Sample Output 1
3
- S_2 =
abcequals the reversal of S_4 =cba, so the second and fourth sticks are considered the same. - S_2 =
abcequals S_6 =abc, so the second and sixth sticks are considered the same. - S_3 =
deequals S_5 =de, so the third and fifth sticks are considered the same.
Therefore, there are three different sticks among the six: the first, second (same as the fourth and sixth), and third (same as the fifth).
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
長さ N の整数列 A=(A_1,A_2,\dots,A_N) が与えられます。
長さ M の A の連続部分列 B=(B_1,B_2,\dots,B_M) に対する、\displaystyle \sum_{i=1}^{M} i \times B_i の最大値を求めてください。
注記
数列の連続部分列とは、数列の先頭から 0 個以上、末尾から 0 個以上の要素を削除して得られる列のことをいいます。
例えば (2, 3) や (1, 2, 3) は (1, 2, 3, 4) の連続部分列ですが、(1, 3) や (3,2,1) は (1, 2, 3, 4) の連続部分列ではありません。
制約
- 1 \le M \le N \le 2 \times 10^5
- - 2 \times 10^5 \le A_i \le 2 \times 10^5
- 入力は全て整数。
入力
入力は以下の形式で標準入力から与えられる。
N M A_1 A_2 \dots A_N
出力
答えを出力せよ。
入力例 1
4 2 5 4 -1 8
出力例 1
15
B=(A_3,A_4) とした場合、\displaystyle \sum_{i=1}^{M} i \times B_i = 1 \times (-1) + 2 \times 8 = 15 となります。16 以上の値を達成することはできないため、解は 15 です。
B=(A_1,A_4) などを選ぶことができないことに注意してください。
入力例 2
10 4 -3 1 -4 1 -5 9 -2 6 -5 3
出力例 2
31
Score : 300 points
Problem Statement
You are given an integer sequence A=(A_1,A_2,\dots,A_N) of length N.
Find the maximum value of \displaystyle \sum_{i=1}^{M} i \times B_i for a contiguous subarray B=(B_1,B_2,\dots,B_M) of A of length M.
Notes
A contiguous subarray of a number sequence is a sequence that is obtained by removing 0 or more initial terms and 0 or more final terms from the original number sequence.
For example, (2, 3) and (1, 2, 3) are contiguous subarrays of (1, 2, 3, 4), but (1, 3) and (3,2,1) are not contiguous subarrays of (1, 2, 3, 4).
Constraints
- 1 \le M \le N \le 2 \times 10^5
- - 2 \times 10^5 \le A_i \le 2 \times 10^5
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N M A_1 A_2 \dots A_N
Output
Print the answer.
Sample Input 1
4 2 5 4 -1 8
Sample Output 1
15
When B=(A_3,A_4), we have \displaystyle \sum_{i=1}^{M} i \times B_i = 1 \times (-1) + 2 \times 8 = 15. Since it is impossible to achieve 16 or a larger value, the solution is 15.
Note that you are not allowed to choose, for instance, B=(A_1,A_4).
Sample Input 2
10 4 -3 1 -4 1 -5 9 -2 6 -5 3
Sample Output 2
31
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
長さ N の整数列 A=(A_1,A_2,\ldots,A_N) が与えられます。
あなたは以下の連続する操作をちょうど一度だけ行います。
-
整数 x\ (0\leq x \leq N) を選ぶ。x として 0 を選んだ場合何もしない。 x として 1 以上の整数を選んだ場合、A_1,A_2,\ldots,A_x をそれぞれ L で置き換える。
-
整数 y\ (0\leq y \leq N) を選ぶ。y として 0 を選んだ場合何もしない。 y として 1 以上の整数を選んだ場合、A_{N},A_{N-1},\ldots,A_{N-y+1} をそれぞれ R で置き換える。
操作後の A の要素の総和として考えられる最小値を求めてください。
制約
- 1 \leq N \leq 2\times 10^5
- -10^9 \leq L, R\leq 10^9
- -10^9 \leq A_i\leq 10^9
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N L R A_1 A_2 \ldots A_N
出力
答えを出力せよ。
入力例 1
5 4 3 5 5 0 6 3
出力例 1
14
x=2,y=2 として操作をすると、数列 A = (4,4,0,3,3) となり、要素の総和は 14 になります。
これが達成可能な最小値です。
入力例 2
4 10 10 1 2 3 4
出力例 2
10
x=0,y=0 として操作をすると、数列 A = (1,2,3,4) となり、要素の総和は 10 になります。
これが達成可能な最小値です。
入力例 3
10 -5 -3 9 -6 10 -1 2 10 -1 7 -15 5
出力例 3
-58
L,R,A_i は負であることがあります。
Score : 400 points
Problem Statement
You are given an integer sequence of length N: A=(A_1,A_2,\ldots,A_N).
You will perform the following consecutive operations just once:
-
Choose an integer x (0\leq x \leq N). If x is 0, do nothing. If x is 1 or greater, replace each of A_1,A_2,\ldots,A_x with L.
-
Choose an integer y (0\leq y \leq N). If y is 0, do nothing. If y is 1 or greater, replace each of A_{N},A_{N-1},\ldots,A_{N-y+1} with R.
Print the minimum possible sum of the elements of A after the operations.
Constraints
- 1 \leq N \leq 2\times 10^5
- -10^9 \leq L, R\leq 10^9
- -10^9 \leq A_i\leq 10^9
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N L R A_1 A_2 \ldots A_N
Output
Print the answer.
Sample Input 1
5 4 3 5 5 0 6 3
Sample Output 1
14
If you choose x=2 and y=2, you will get A = (4,4,0,3,3), for the sum of 14, which is the minimum sum achievable.
Sample Input 2
4 10 10 1 2 3 4
Sample Output 2
10
If you choose x=0 and y=0, you will get A = (1,2,3,4), for the sum of 10, which is the minimum sum achievable.
Sample Input 3
10 -5 -3 9 -6 10 -1 2 10 -1 7 -15 5
Sample Output 3
-58
L, R, and A_i may be negative.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 475 点
問題文
英大文字からなる長さ N の文字列 S と、英大文字からなる長さ M\ (\leq N) の文字列 T が与えられます。
# のみからなる長さ N の文字列 X があります。
以下の操作を好きな回数行うことで、X を S に一致させることができるか判定してください。
- X の中から連続する M 文字を選び、T で置き換える。
制約
- 1 \leq N \leq 2\times 10^5
- 1 \leq M \leq \min(N, 5)
- S は英大文字からなる長さ N の文字列
- T は英大文字からなる長さ M の文字列
入力
入力は以下の形式で標準入力から与えられる。
N M S T
出力
X を S に一致させることができるならば Yes を、できないならば No を出力せよ。
入力例 1
7 3 ABCBABC ABC
出力例 1
Yes
以下、X の l 文字目から r 文字目までの部分を X[l:r] と表記します。
次のように操作を行うことで、X を S に一致させることができます。
- X[3:5] を T で置き換える。X=
##ABC##になる。 - X[1:3] を T で置き換える。X=
ABCBC##になる。 - X[5:7] を T で置き換える。X=
ABCBABCになる。
入力例 2
7 3 ABBCABC ABC
出力例 2
No
どのように操作を行っても、X を S に一致させることはできません。
入力例 3
12 2 XYXXYXXYYYXY XY
出力例 3
Yes
Score : 475 points
Problem Statement
You are given two strings: S, which consists of uppercase English letters and has length N, and T, which also consists of uppercase English letters and has length M\ (\leq N).
There is a string X of length N consisting only of the character #. Determine whether it is possible to make X match S by performing the following operation any number of times:
- Choose M consecutive characters in X and replace them with T.
Constraints
- 1 \leq N \leq 2\times 10^5
- 1 \leq M \leq \min(N, 5)
- S is a string consisting of uppercase English letters with length N.
- T is a string consisting of uppercase English letters with length M.
Input
The input is given from Standard Input in the following format:
N M S T
Output
Print Yes if it is possible to make X match S; print No otherwise.
Sample Input 1
7 3 ABCBABC ABC
Sample Output 1
Yes
Below, let X[l:r] denote the part from the l-th through the r-th character of X.
You can make X match S by operating as follows.
- Replace X[3:5] with T. X becomes
##ABC##. - Replace X[1:3] with T. X becomes
ABCBC##. - Replace X[5:7] with T. X becomes
ABCBABC.
Sample Input 2
7 3 ABBCABC ABC
Sample Output 2
No
No matter how you operate, it is impossible to make X match S.
Sample Input 3
12 2 XYXXYXXYYYXY XY
Sample Output 3
Yes
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
無限に広がる二次元グリッドのマス (0, 0) に掃除ロボットが置かれています。
このロボットに、4 種類の文字 L 、R 、U 、D からなる文字列で表されたプログラムが与えられます。
ロボットは、与えられたプログラムの各文字を先頭の文字から順に読み、各文字について以下の行動を行います。
- ロボットの現在地をマス (x, y) とする
- 読んだ文字に応じて以下の通りに移動する:
Lを読んだとき: マス (x-1, y) に移動する。Rを読んだとき: マス (x+1, y) に移動する。Uを読んだとき: マス (x, y-1) に移動する。Dを読んだとき: マス (x, y+1) に移動する。
L 、R 、U 、D からなる文字列 S が与えられます。
ロボットが実行するプログラムは、文字列 S を K 個連接したものです。
ロボットが一度でも存在したことのあるマス(ロボットの初期位置であるマス (0, 0) を含む)は掃除されます。
ロボットがプログラムの実行を終えた後の時点で、掃除されているマスの個数を出力して下さい。
制約
- S は
L、R、U、Dからなる長さ 1 以上 2 \times 10^5 以下の文字列 - 1 \leq K \leq 10^{12}
入力
入力は以下の形式で標準入力から与えられる。
S K
出力
ロボットがプログラムの実行を終えた後の時点で、掃除されているマスの個数を出力せよ。
入力例 1
RDRUL 2
出力例 1
7
ロボットが実行するプログラムは RDRULRDRUL です。ロボットは初期位置であるマス (0, 0) からはじめ、
(0, 0) \rightarrow (1, 0) \rightarrow (1, 1) \rightarrow (2, 1) \rightarrow (2, 0) \rightarrow (1, 0) \rightarrow (2, 0) \rightarrow (2, 1) \rightarrow (3, 1) \rightarrow (3, 0) \rightarrow (2, 0) と移動します。
その後掃除されているマスは、(0, 0), (1, 0), (1, 1), (2, 0), (2, 1), (3, 0), (3, 1) の 7 個です。
入力例 2
LR 1000000000000
出力例 2
2
入力例 3
UUURRDDDRRRUUUURDLLUURRRDDDDDDLLLLLLU 31415926535
出力例 3
219911485785
Score : 500 points
Problem Statement
There is a cleaning robot on the square (0, 0) in an infinite two-dimensional grid.
The robot will be given a program represented as a string consisting of four kind of characters L, R, U, D.
It will read the characters in the program from left to right and perform the following action for each character read.
- Let (x, y) be the square where the robot is currently on.
- Make the following move according to the character read:
- if
Lis read: go to (x-1, y). - if
Ris read: go to (x+1, y). - if
Uis read: go to (x, y-1). - if
Dis read: go to (x, y+1).
- if
You are given a string S consisting of L, R, U, D.
The program that will be executed by the robot is the concatenation of K copies of S.
Squares visited by the robot at least once, including the initial position (0, 0), will be cleaned.
Print the number of squares that will be cleaned at the end of the execution of the program.
Constraints
- S is a string of length between 1 and 2 \times 10^5 (inclusive) consisting of
L,R,U,D. - 1 \leq K \leq 10^{12}
Input
Input is given from Standard Input in the following format:
S K
Output
Print the number of squares that will be cleaned at the end of the execution of the program.
Sample Input 1
RDRUL 2
Sample Output 1
7
The robot will execute the program RDRULRDRUL. It will start on (0, 0) and travel as follows:
(0, 0) \rightarrow (1, 0) \rightarrow (1, 1) \rightarrow (2, 1) \rightarrow (2, 0) \rightarrow (1, 0) \rightarrow (2, 0) \rightarrow (2, 1) \rightarrow (3, 1) \rightarrow (3, 0) \rightarrow (2, 0).
In the end, seven squares will get cleaned: (0, 0), (1, 0), (1, 1), (2, 0), (2, 1), (3, 0), (3, 1).
Sample Input 2
LR 1000000000000
Sample Output 2
2
Sample Input 3
UUURRDDDRRRUUUURDLLUURRRDDDDDDLLLLLLU 31415926535
Sample Output 3
219911485785