A - Signboard

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 100

問題文

CODE FESTIVAL 2016が開催されます。開催にあたって、高橋君はCODE FESTIVAL 2016の看板を作ることにしました。

看板にはCODEFESTIVAL2016と書きたかったのですが、高橋君は間違えて異なる文字列Sを書いてしまいました。幸い、書いた文字列の長さは間違っていませんでした。

そこで高橋君は、ある文字を別の文字に書き換えるという操作を最小の回数行って、この文字列をCODEFESTIVAL2016に書き換えることにしました。

書き換えの回数の最小値を求めてください。

制約

  • Sの長さは16である。
  • S は英大文字、英小文字、数字からなる。

入力

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

S

出力

書き換えの回数の最小値を表す整数を出力せよ。


入力例 1

C0DEFESTIVAL2O16

出力例 1

2

2文字目の0Oに、14文字目のO0に書き換える必要があります。


入力例 2

FESTIVAL2016CODE

出力例 2

16

Score : 100 points

Problem Statement

CODE FESTIVAL 2016 is going to be held. For the occasion, Mr. Takahashi decided to make a signboard.

He intended to write CODEFESTIVAL2016 on it, but he mistakenly wrote a different string S. Fortunately, the string he wrote was the correct length.

So Mr. Takahashi decided to perform an operation that replaces a certain character with another in the minimum number of iterations, changing the string to CODEFESTIVAL2016.

Find the minimum number of iterations for the rewrite operation.

Constraints

  • S is 16 characters long.
  • S consists of uppercase and lowercase alphabet letters and numerals.

Input

Inputs are provided from Standard Input in the following form.

S

Output

Output an integer representing the minimum number of iterations needed for the rewrite operation.


Sample Input 1

C0DEFESTIVAL2O16

Sample Output 1

2

The second character 0 must be changed to O and the 14th character O changed to 0.


Sample Input 2

FESTIVAL2016CODE

Sample Output 2

16
B - Qualification simulator

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 200

問題文

CODE FESTIVAL 2016の予選にはN人が参加しました。参加者は、国内の学生であるか、海外の学生であるか、どちらでもないかのどれかです。

予選は国内または海外の学生のみが通過することができ、上位の学生から順に、以下の条件を満たすときに通過します。学生でない参加者は予選を通過できません。

  • 国内の学生は、現在予選の通過が確定した参加者がA+B人に満たなければ、予選を通過する
  • 海外の学生は、現在予選の通過が確定した参加者がA+B人に満たず、さらに海外の学生の中での順位がB位以内なら、予選を通過する

参加者の情報を表す文字列Sが与えられます。 Si文字目がaのとき予選でi位の参加者が国内の学生であることを、 Si文字目がbのとき予選でi位の参加者が海外の学生であることを、 Si文字目がcのとき予選でi位の参加者がそのどちらでもないことを表しています。

すべての参加者について、上位から順に、予選を通過した場合はYes、そうでない場合はNoを出力するプログラムを作成してください。

制約

  • 1 ≦ N,A,B ≦ 100000
  • A+B ≦ N
  • S の長さはNである。
  • S は文字abcのみからなる。

入力

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

N A B
S

出力

N 行出力せよ。i行目には、i位の参加者が予選を通過した場合Yes、そうでない場合Noを出力せよ。


入力例 1

10 2 3
abccabaabb

出力例 1

Yes
Yes
No
No
Yes
Yes
Yes
No
No
No

1,2,5,6,7位の参加者が予選を通過します。


入力例 2

12 5 2
cabbabaacaba

出力例 2

No
Yes
Yes
Yes
Yes
No
Yes
Yes
No
Yes
No
No

6位の参加者は海外の学生の中で3位なので、予選を通過しません。


入力例 3

5 2 2
ccccc

出力例 3

No
No
No
No
No

Score : 200 points

Problem Statement

There are N participants in the CODE FESTIVAL 2016 Qualification contests. The participants are either students in Japan, students from overseas, or neither of these.

Only Japanese students or overseas students can pass the Qualification contests. The students pass when they satisfy the conditions listed below, from the top rank down. Participants who are not students cannot pass the Qualification contests.

  • A Japanese student passes the Qualification contests if the number of the participants who have already definitively passed is currently fewer than A+B.
  • An overseas student passes the Qualification contests if the number of the participants who have already definitively passed is currently fewer than A+B and the student ranks B-th or above among all overseas students.

A string S is assigned indicating attributes of all participants. If the i-th character of string S is a, this means the participant ranked i-th in the Qualification contests is a Japanese student; b means the participant ranked i-th is an overseas student; and c means the participant ranked i-th is neither of these.

Write a program that outputs for all the participants in descending rank either Yes if they passed the Qualification contests or No if they did not pass.

Constraints

  • 1≦N,A,B≦100000
  • A+B≦N
  • S is N characters long.
  • S consists only of the letters a, b and c.

Input

Inputs are provided from Standard Input in the following form.

N A B
S

Output

Output N lines. On the i-th line, output Yes if the i-th participant passed the Qualification contests or No if that participant did not pass.


Sample Input 1

10 2 3
abccabaabb

Sample Output 1

Yes
Yes
No
No
Yes
Yes
Yes
No
No
No

The first, second, fifth, sixth, and seventh participants pass the Qualification contests.


Sample Input 2

12 5 2
cabbabaacaba

Sample Output 2

No
Yes
Yes
Yes
Yes
No
Yes
Yes
No
Yes
No
No

The sixth participant is third among overseas students and thus does not pass the Qualification contests.


Sample Input 3

5 2 2
ccccc

Sample Output 3

No
No
No
No
No
C - Gr-idian MST

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 500

問題文

xy平面上の0 ≦ x ≦ W, 0 ≦ y ≦ Hをみたす領域にあるx,yともに整数である点すべてに、ひとつずつ家があります。

x座標が等しくy座標の差が1であるか、y座標が等しくx座標の差が1であるような2点の組のうち、両方の点に家が存在するような全てのものに対し、その2点の間には舗装されていない道路があります。

座標(i,j)(i+1,j)にある家の間の道路を舗装するのにはjの値にかかわらずコストがp_i、 座標(i,j)(i,j+1)にある家の間の道路を舗装するのにはiの値にかかわらずコストがq_jかかります。

高橋君は、このうちいくつかの道路を舗装し、舗装された道路のみを通って任意の2つの家の間を行き来できるようにしたいです。 かかるコストの総和の最小値を求めてください。

制約

  • 1 ≦ W,H ≦ 10^5
  • 1 ≦ p_i ≦ 10^8(0 ≦ i ≦ W-1)
  • 1 ≦ q_j ≦ 10^8(0 ≦ j ≦ H-1)
  • p_i(0 ≦ i ≦ W-1)は整数である
  • q_j(0 ≦ j ≦ H-1)は整数である

入力

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

W H
p_0
:
p_{W-1}
q_0
:
q_{H-1}

出力

コストの総和の最小値を表す整数を出力せよ。


入力例 1

2 2
3
5
2
7

出力例 1

29

次の8本の道路を舗装すればよいです。

  • (0,0)(0,1)にある家を結ぶ道路
  • (0,1)(1,1)にある家を結ぶ道路
  • (0,2)(1,2)にある家を結ぶ道路
  • (1,0)(1,1)にある家を結ぶ道路
  • (1,0)(2,0)にある家を結ぶ道路
  • (1,1)(1,2)にある家を結ぶ道路
  • (1,2)(2,2)にある家を結ぶ道路
  • (2,0)(2,1)にある家を結ぶ道路

入力例 2

4 3
2
4
8
1
2
9
3

出力例 2

60

Score : 500 points

Problem Statement

On an xy plane, in an area satisfying 0 ≤ x ≤ W, 0 ≤ y ≤ H, there is one house at each and every point where both x and y are integers.

There are unpaved roads between every pair of points for which either the x coordinates are equal and the difference between the y coordinates is 1, or the y coordinates are equal and the difference between the x coordinates is 1.

The cost of paving a road between houses on coordinates (i,j) and (i+1,j) is p_i for any value of j, while the cost of paving a road between houses on coordinates (i,j) and (i,j+1) is q_j for any value of i.

Mr. Takahashi wants to pave some of these roads and be able to travel between any two houses on paved roads only. Find the solution with the minimum total cost.

Constraints

  • 1 ≦ W,H ≦ 10^5
  • 1 ≦ p_i ≦ 10^8(0 ≦ i ≦ W-1)
  • 1 ≦ q_j ≦ 10^8(0 ≦ j ≦ H-1)
  • p_i (0 ≦ i ≦ W−1) is an integer.
  • q_j (0 ≦ j ≦ H−1) is an integer.

Input

Inputs are provided from Standard Input in the following form.

W H
p_0
:
p_{W-1}
q_0
:
q_{H-1}

Output

Output an integer representing the minimum total cost.


Sample Input 1

2 2
3
5
2
7

Sample Output 1

29

It is enough to pave the following eight roads.

  • Road connecting houses at (0,0) and (0,1)
  • Road connecting houses at (0,1) and (1,1)
  • Road connecting houses at (0,2) and (1,2)
  • Road connecting houses at (1,0) and (1,1)
  • Road connecting houses at (1,0) and (2,0)
  • Road connecting houses at (1,1) and (1,2)
  • Road connecting houses at (1,2) and (2,2)
  • Road connecting houses at (2,0) and (2,1)

Sample Input 2

4 3
2
4
8
1
2
9
3

Sample Output 2

60
D - Greedy customers

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 700

問題文

高橋商店の前には、N 人の人が 1 列に並んでいます。前から i 人目の人の所持金は正整数 A_i です。

店主の高橋君は、「品物を選んでその価格を表す正の整数 P を設定し、前の人から順にその品物を見せていく」というステップを繰り返します。

各ステップにおいて、各人は、品物を見せられた時、その価格 P が現時点でのその人の所持金以下だった場合その品物を購入し、高橋君の 1 回のステップは終了します。 すなわち、所持金が P 以上の最初の人の所持金が P 減少し、次のステップに移ります。

高橋君は正整数 P の値を、ステップごとに独立に決めることができます。

高橋君はできるだけ多くの品物を売りたいですが、品物を売った人の所持金が 0 になってしまうと、その人は帰れなくなってしまいます。人が帰れなくなってしまうと高橋君は困ってしまうので、どの人の所持金も 0 にしてはいけません。

高橋君に代わって、各人の最初の所持金が与えられたとき、高橋君が最大でいくつの品物を売ることができるかを求めるプログラムを作成してください。

制約

  • 1 ≦ N ≦ 100000
  • 1 ≦ A_i ≦ 10^9(1 ≦ i ≦ N)
  • 入力はすべて整数である

入力

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

N
A_1
:
A_N

出力

高橋君が売ることのできる商品の最大数を表す整数を出力せよ。


入力例 1

3
3
2
5

出力例 1

3

Pの値として、順に1,4,1と選べばよいです。


入力例 2

15
3
1
4
1
5
9
2
6
5
3
5
8
9
7
9

出力例 2

18

Score : 700 points

Problem Statement

N people are waiting in a single line in front of the Takahashi Store. The cash on hand of the i-th person from the front of the line is a positive integer A_i.

Mr. Takahashi, the shop owner, has decided on the following scheme: He picks a product, sets a positive integer P indicating its price, and shows this product to customers in order, starting from the front of the line. This step is repeated as described below.

At each step, when a product is shown to a customer, if price P is equal to or less than the cash held by that customer at the time, the customer buys the product and Mr. Takahashi ends the current step. That is, the cash held by the first customer in line with cash equal to or greater than P decreases by P, and the next step begins.

Mr. Takahashi can set the value of positive integer P independently at each step.

He would like to sell as many products as possible. However, if a customer were to end up with 0 cash on hand after a purchase, that person would not have the fare to go home. Customers not being able to go home would be a problem for Mr. Takahashi, so he does not want anyone to end up with 0 cash.

Help out Mr. Takahashi by writing a program that determines the maximum number of products he can sell, when the initial cash in possession of each customer is given.

Constraints

  • 1 ≦ | N | ≦ 100000
  • 1 ≦ A_i ≦ 10^9(1 ≦ i ≦ N)
  • All inputs are integers.

Input

Inputs are provided from Standard Inputs in the following form.

N
A_1
:
A_N

Output

Output an integer representing the maximum number of products Mr. Takahashi can sell.


Sample Input 1

3
3
2
5

Sample Output 1

3

As values of P, select in order 1, 4, 1.


Sample Input 2

15
3
1
4
1
5
9
2
6
5
3
5
8
9
7
9

Sample Output 2

18
E - Lexicographical disorder

Time Limit: 6 sec / Memory Limit: 512 MB

配点 : 1100

問題文

英小文字のみからなる文字列がN個あります。i番目の文字列はS_iです。すべての文字列は相異なります。

次のQ個のクエリに答えてください。i番目のクエリは、以下のような形式です。

クエリ: 整数k_iと、{'a','b',...,'z'}の並び替えである文字列p_{i,1}p_{i,2}...p_{i,26}が与えられる。文字の順序がp_{i,1} < p_{i,2} < ... < p_{i,26}のとき、文字列S_{k_i}N個の文字列たちの中で辞書順で何番目か出力せよ。

制約

  • 1 ≦ N,Q ≦ 100000
  • 1 ≦ | S_i | (1 ≦ i ≦ N)
  • S_i (1 ≦ i ≦ N)は英小文字のみからなる。
  • | S_i |たちの合計は400000以下。
  • S_iたちはすべて相異なる。
  • 1 ≦ k_i ≦ N(1 ≦ i ≦ Q)
  • すべての1 ≦ i ≦ Qに対し、p_{i,1}p_{i,2}...p_{i,26}は"abcd...z"の並び替えである。

入力

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

N
S_1
:
S_N
Q
k_1 p_{1,1}p_{1,2}...p_{1,26}
:
k_Q p_{Q,1}p_{Q,2}...p_{Q,26}

出力

Q行出力せよ。

i行目には、i番目のクエリに対し、文字列S_{k_i}N個の文字列たちの中で辞書順で何番目かを表す整数を出力せよ。


入力例 1

5
aa
abbaa
abbba
aaab
aaaaaba
5
1 abcdefghijklmnopqrstuvwxyz
2 bacdefghijklmnopqrstuvwxyz
3 abcdefghijklmnopqrstuvwxyz
4 bacdefghijklmnopqrstuvwxyz
5 abcdefghijklmnopqrstuvwxyz

出力例 1

1
2
5
4
2

文字の順序が"a" < "b"のとき、入力の文字列を辞書順にソートすると"aa","aaaaaba","aaab","abbaa","abbba"となるので、 1,3,5番目のクエリにはそれぞれ1,5,2と答えます。

また、文字の順序が"b" < "a"のとき、入力の文字列を辞書順にソートすると"abbba","abbaa","aa","aaab","aaaaaba"となるので、 2,4番目のクエリにはそれぞれ2,4と答えます。


入力例 2

8
abrakatabra
abadaba
abracadabra
atcoder
grand
contest
ababa
a
6
3 abcdefghijklmnopqrstuvwxyz
6 qwertyuiopasdfghjklzxcvbnm
8 poiuytrewqlkjhgfdsamnbvcxz
2 qazwsxedcrfvtgbyhnujmikolp
1 plokmijnuhbygvtfcrdxeszwaq
4 mnbvcxzasdfghjklpoiuytrewq

出力例 2

4
8
2
3
4
7

Score : 1100 points

Problem Statement

There are N strings of lowercase alphabet only. The i-th string is S_i. Every string is unique.

Provide answers for the Q queries below. The i-th query has the following format:

Query: An integer k_i and a string p_{i,1}p_{i,2}...p_{i,26} that results from permuting {a,b,...,z} are given. Output the sequence of the string S_{k_i} among the N strings in lexicographical order when the literal sequence is p_{i,1}<p_{i,2}<...<p_{i,26}.

Constraints

  • 1 ≦ N,Q ≦ 100000
  • 1 ≦ |S_i| (1 ≦ i ≦ N)
  • S_i (1 ≦ i ≦ N) is a string of lowercase alphabet.
  • The sum of |S_i| is no more than 400000.
  • Every S_i is unique.
  • 1 ≦ k_i ≦ N (1 ≦ i ≦ Q)
  • For all 1 ≦ i ≦ Q, p_{i,1}p_{i,2}...p_{i,26} is a permutation of abcd...z.

Input

Inputs are provided from standard inputs in the following form.

N
S_1
:
S_N
Q
k_1 p_{1,1}p_{1,2}...p_{1,26}
:
k_Q p_{Q,1}p_{Q,2}...p_{Q,26}

Output

Output Q lines.

On line i, for the i-th query, output an integer indicating the sequence of the string S_{k_i} among the N strings in lexicographical order.


Sample Input 1

5
aa
abbaa
abbba
aaab
aaaaaba
5
1 abcdefghijklmnopqrstuvwxyz
2 bacdefghijklmnopqrstuvwxyz
3 abcdefghijklmnopqrstuvwxyz
4 bacdefghijklmnopqrstuvwxyz
5 abcdefghijklmnopqrstuvwxyz

Sample Output 1

1
2
5
4
2

When the literal sequence is a < b, sorting the input strings in lexicographical order yields aa, aaaaaba, aaab, abbaa, abbba. The answers to queries 1, 3, and 5 are thus 1, 5, and 2, respectively.

When the literal sequence is b < a, sorting the input strings in lexicographical order yields abbba, abbaa, aa, aaab, aaaaaba. The answers to queries 2 and 4 are thus 2 and 4, respectively.


Sample Input 2

8
abrakatabra
abadaba
abracadabra
atcoder
grand
contest
ababa
a
6
3 abcdefghijklmnopqrstuvwxyz
6 qwertyuiopasdfghjklzxcvbnm
8 poiuytrewqlkjhgfdsamnbvcxz
2 qazwsxedcrfvtgbyhnujmikolp
1 plokmijnuhbygvtfcrdxeszwaq
4 mnbvcxzasdfghjklpoiuytrewq

Sample Output 2

4
8
2
3
4
7