Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 350 点
問題文
英小文字からなる長さ N の文字列 S が与えられます。
文字列 S に対して操作を Q 回行います。 i 回目 (1\leq i\leq Q) の操作は文字の組 (c _ i,d _ i) で表され、次のような操作に対応します。
- S に含まれる文字 c _ i をすべて文字 d _ i で置き換える。
すべての操作が終わったあとの文字列 S を出力してください。
制約
- 1\leq N\leq2\times10^5
- S は英小文字からなる長さ N の文字列
- 1\leq Q\leq2\times10^5
- c _ i,d _ i は英小文字 (1\leq i\leq Q)
- N,Q は整数
入力
入力は以下の形式で標準入力から与えられる。
N S Q c _ 1 d _ 1 c _ 2 d _ 2 \vdots c _ Q d _ Q
出力
すべての操作が終わったあとの S を出力せよ。
入力例 1
7 atcoder 4 r a t e d v a r
出力例 1
recover
S は atcoder → atcodea → aecodea → aecovea → recover と変化します。
たとえば、4 番目の操作では S={}aecovea に含まれる a (1 文字目と 7 文字目)をすべて r に置き換えるので S={}recover となります。
すべての操作が終わったときには S={}recover となっているため、recover を出力してください。
入力例 2
3 abc 4 a a s k n n z b
出力例 2
abc
c _ i=d _ i であるような操作や S に c _ i が含まれないような操作もあります。
入力例 3
34 supercalifragilisticexpialidocious 20 g c l g g m c m r o s e a a o f f s e t t l d v p k v h x i h n n j i r s i u a
出力例 3
laklimamriiamrmrllrmlrkramrjimrial
Score: 350 points
Problem Statement
You are given a string S of length N consisting of lowercase English letters.
You will perform an operation Q times on the string S. The i-th operation (1\leq i\leq Q) is represented by a pair of characters (c _ i,d _ i), which corresponds to the following operation:
- Replace all occurrences of the character c _ i in S with the character d _ i.
Print the string S after all operations are completed.
Constraints
- 1\leq N\leq2\times10^5
- S is a string of length N consisting of lowercase English letters.
- 1\leq Q\leq2\times10^5
- c _ i and d _ i are lowercase English letters (1\leq i\leq Q).
- N and Q are integers.
Input
The input is given from Standard Input in the following format:
N S Q c _ 1 d _ 1 c _ 2 d _ 2 \vdots c _ Q d _ Q
Output
Print the string S after all operations are completed.
Sample Input 1
7 atcoder 4 r a t e d v a r
Sample Output 1
recover
S changes as follows: atcoder → atcodea → aecodea → aecovea → recover.
For example, in the fourth operation, all occurrences of a in S={}aecovea (the first and seventh characters) are replaced with r, resulting in S={}recover.
After all operations are completed, S={}recover, so print recover.
Sample Input 2
3 abc 4 a a s k n n z b
Sample Output 2
abc
There may be operations where c _ i=d _ i or S does not contain c _ i.
Sample Input 3
34 supercalifragilisticexpialidocious 20 g c l g g m c m r o s e a a o f f s e t t l d v p k v h x i h n n j i r s i u a
Sample Output 3
laklimamriiamrmrllrmlrkramrjimrial
Time Limit: 4 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
N 枚の靴下があります。i 枚目の靴下の色は A_i です。
あなたは以下の操作をできるだけ多い回数行いたいです。最大で何回行うことができますか?
- まだペアになっていない靴下の中から同じ色の靴下を 2 枚選んでペアにする。
制約
- 1\leq N \leq 5\times 10^5
- 1\leq A_i \leq 10^9
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \dots A_N
出力
答えを整数として出力せよ。
入力例 1
6 4 1 7 4 1 4
出力例 1
2
以下のようにして、2 回の操作を行うことができます。
- 色が 1 である靴下を 2 枚選んでペアにする。
- 色が 4 である靴下を 2 枚選んでペアにする。
このとき、色が 4 である靴下と 7 である靴下が 1 枚ずつ残るため、これ以上操作はできません。 また、どのように操作をしても 3 回以上操作を行うことはできないため、2 を出力します。
入力例 2
1 158260522
出力例 2
0
入力例 3
10 295 2 29 295 29 2 29 295 2 29
出力例 3
4
Score : 300 points
Problem Statement
You have N socks. The color of the i-th sock is A_i.
You want to perform the following operation as many times as possible. How many times can it be performed at most?
- Choose two same-colored socks that are not paired yet, and pair them.
Constraints
- 1\leq N \leq 5\times 10^5
- 1\leq A_i \leq 10^9
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N A_1 A_2 \dots A_N
Output
Print an integer representing the answer.
Sample Input 1
6 4 1 7 4 1 4
Sample Output 1
2
You can do the operation twice as follows.
- Choose two socks with the color 1 and pair them.
- Choose two socks with the color 4 and pair them.
Then, you will be left with one sock with the color 4 and another with the color 7, so you can no longer do the operation. There is no way to do the operation three or more times, so you should print 2.
Sample Input 2
1 158260522
Sample Output 2
0
Sample Input 3
10 295 2 29 295 29 2 29 295 2 29
Sample Output 3
4
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
X と . からなる文字列 S が与えられます。
S に対して、次の操作を 0 回以上 K 回以下行うことができます。
.をXに置き換える
操作後に、X を最大で何個連続させることができますか?
制約
- 1 \leq |S| \leq 2 \times 10^5
- S の各文字は
Xまたは.である - 0 \leq K \leq 2 \times 10^5
- K は整数である
入力
入力は以下の形式で標準入力から与えられる。
S K
出力
答えを出力せよ。
入力例 1
XX...X.X.X. 2
出力例 1
5
S の 7 文字目と 9 文字目の . を X に置き換えて XX...XXXXX. とすると、6 文字目から 10 文字目で X が 5 個連続しています。
X を 6 個以上連続させることはできないので、答えは 5 です。
入力例 2
XXXX 200000
出力例 2
4
操作を行う回数は 0 回でも構いません。
Score : 400 points
Problem Statement
Given is a string S consisting of X and ..
You can do the following operation on S between 0 and K times (inclusive).
- Replace a
.with anX.
What is the maximum possible number of consecutive Xs in S after the operations?
Constraints
- 1 \leq |S| \leq 2 \times 10^5
- Each character of S is
Xor.. - 0 \leq K \leq 2 \times 10^5
- K is an integer.
Input
Input is given from Standard Input in the following format:
S K
Output
Print the answer.
Sample Input 1
XX...X.X.X. 2
Sample Output 1
5
After replacing the Xs at the 7-th and 9-th positions with X, we have XX...XXXXX., which has five consecutive Xs at 6-th through 10-th positions.
We cannot have six or more consecutive Xs, so the answer is 5.
Sample Input 2
XXXX 200000
Sample Output 2
4
It is allowed to do zero operations.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
AtCoder 王国には都市 1,2,\ldots,N の N 個の都市と、道路 1,2,\ldots,M の M 本の道路があります。
道路 i は都市 A_i と B_i を双方向に結び、距離は C_i です。
どの都市間もいくつかの道路を通って行き来することができます。
財政難である王国は、どの都市間もいくつかの道路を通って行き来できるという条件を満たすように N-1 本の道路を保守し、それ以外の道路を廃道にすることにしました。
保守する道路のみを通って都市 1 から都市 i へ移動するときの距離を d_i とするとき、保守する道路の選び方であって、d_2+d_3+\ldots+d_N を最小化するようなものを 1 つ出力してください。
制約
- 2 \leq N \leq 2\times 10^5
- N-1 \leq M \leq 2\times 10^5
- 1 \leq A_i < B_i \leq N
- i\neq j のとき、(A_i,B_i)\neq(A_j,B_j)
- 1\leq C_i \leq 10^9
- どの都市間もいくつかの道路を通って行き来することができる
- 入力に含まれる値は全て整数である
入力
入力は以下の形式で標準入力から与えられる。
N M A_1 B_1 C_1 A_2 B_2 C_2 \vdots A_M B_M C_M
出力
保守するような道路の番号を空白区切りで出力せよ。出力の順序は問わない。
答えが複数存在する場合、どれを出力しても正解とみなされる。
入力例 1
3 3 1 2 1 2 3 2 1 3 10
出力例 1
1 2
保守する道路の選び方と d_i の値は次のようになります。
- 道路 1,2 を保守するとき、d_2=1, d_3=3
- 道路 1,3 を保守するとき、d_2=1, d_3=10
- 道路 2,3 を保守するとき、d_2=12, d_3=10
よって、道路 1,2 を保守するときに d_2+d_3 が最小になります。
入力例 2
4 6 1 2 1 1 3 1 1 4 1 2 3 1 2 4 1 3 4 1
出力例 2
3 1 2
Score : 500 points
Problem Statement
The Kingdom of AtCoder has N cities called City 1,2,\ldots,N and M roads called Road 1,2,\ldots,M.
Road i connects Cities A_i and B_i bidirectionally and has a length of C_i.
One can travel between any two cities using some roads.
Under financial difficulties, the kingdom has decided to maintain only N-1 roads so that one can still travel between any two cities using those roads and abandon the rest.
Let d_i be the total length of the roads one must use when going from City 1 to City i using only maintained roads. Print a choice of roads to maintain that minimizes d_2+d_3+\ldots+d_N.
Constraints
- 2 \leq N \leq 2\times 10^5
- N-1 \leq M \leq 2\times 10^5
- 1 \leq A_i < B_i \leq N
- (A_i,B_i)\neq(A_j,B_j) if i\neq j.
- 1\leq C_i \leq 10^9
- One can travel between any two cities using some roads.
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N M A_1 B_1 C_1 A_2 B_2 C_2 \vdots A_M B_M C_M
Output
Print the indices of roads to maintain, in arbitrary order, with spaces in between.
If multiple solutions exist, you may print any of them.
Sample Input 1
3 3 1 2 1 2 3 2 1 3 10
Sample Output 1
1 2
Here are the possible choices of roads to maintain and the corresponding values of d_i.
- Maintain Road 1 and 2: d_2=1, d_3=3.
- Maintain Road 1 and 3: d_2=1, d_3=10.
- Maintain Road 2 and 3: d_2=12, d_3=10.
Thus, maintaining Road 1 and 2 minimizes d_2+d_3.
Sample Input 2
4 6 1 2 1 1 3 1 1 4 1 2 3 1 2 4 1 3 4 1
Sample Output 2
3 1 2
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