A - Intersection

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

数直線があり、高橋君はこれを赤色と青色で次のように塗りました。

  • X=L_1 から X=R_1 までをすべて赤色で塗る。
  • X=L_2 から X=R_2 までをすべて青色で塗る。

数直線のうち、赤色と青色の両方で塗られている部分の長さを求めてください。

制約

  • 0\leq L_1<R_1\leq 100
  • 0\leq L_2<R_2\leq 100
  • 入力はすべて整数

入力

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

L_1 R_1 L_2 R_2

出力

両方の色で塗られている部分の長さを整数で出力せよ。


入力例 1

0 3 1 5

出力例 1

2

X=0 から X=3 までが赤く、 X=1 から X=5 までが青く塗られています。

よって、両方の色で塗られている部分は X=1 から X=3 までであり、その長さは 2 となります。


入力例 2

0 1 4 5

出力例 2

0

両方の色で塗られている部分はありません。


入力例 3

0 3 3 7

出力例 3

0

赤色と青色で塗られている部分が接している場合でも、 両方の色で塗られている部分の長さは 0 となります。

Score : 100 points

Problem Statement

We have a number line. Takahashi painted some parts of this line, as follows:

  • First, he painted the part from X=L_1 to X=R_1 red.
  • Next, he painted the part from X=L_2 to X=R_2 blue.

Find the length of the part of the line painted both red and blue.

Constraints

  • 0\leq L_1<R_1\leq 100
  • 0\leq L_2<R_2\leq 100
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

L_1 R_1 L_2 R_2

Output

Print the length of the part of the line painted both red and blue, as an integer.


Sample Input 1

0 3 1 5

Sample Output 1

2

The part from X=0 to X=3 is painted red, and the part from X=1 to X=5 is painted blue.

Thus, the part from X=1 to X=3 is painted both red and blue, and its length is 2.


Sample Input 2

0 1 4 5

Sample Output 2

0

No part is painted both red and blue.


Sample Input 3

0 3 3 7

Sample Output 3

0

If the part painted red and the part painted blue are adjacent to each other, the length of the part painted both red and blue is 0.

B - Hamming Distance

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

正整数 N および、英小文字からなる長さ N の文字列 S,T が与えられます。

ST のハミング距離を求めてください。 すなわち、1 以上 N 以下の整数 i であって、Si 文字目と Ti 文字目が異なるようなものの個数を求めてください。

制約

  • 1\leq N \leq 100
  • N は整数
  • S,T はそれぞれ英小文字からなる長さ N の文字列

入力

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

N
S
T

出力

答えを出力せよ。


入力例 1

6
abcarc
agcahc

出力例 1

2

ST2,5 文字目が異なり、それ以外が等しいです。よって答えは 2 です。


入力例 2

7
atcoder
contest

出力例 2

7

入力例 3

8
chokudai
chokudai

出力例 3

0

入力例 4

10
vexknuampx
vzxikuamlx

出力例 4

4

Score : 100 points

Problem Statement

You are given a positive integer N and two strings S and T, each of length N and consisting of lowercase English letters.

Find the Hamming distance between S and T. That is, find the number of integers i such that 1 \leq i \leq N and the i-th character of S is different from the i-th character of T.

Constraints

  • 1\leq N \leq 100
  • N is an integer.
  • Each of S and T is a string of length N consisting of lowercase English letters.

Input

The input is given from Standard Input in the following format:

N
S
T

Output

Print the answer.


Sample Input 1

6
abcarc
agcahc

Sample Output 1

2

S and T differ in the 2nd and 5th characters, but not in other characters. Thus, the answer is 2.


Sample Input 2

7
atcoder
contest

Sample Output 2

7

Sample Input 3

8
chokudai
chokudai

Sample Output 3

0

Sample Input 4

10
vexknuampx
vzxikuamlx

Sample Output 4

4
C - Hit and Blow

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

長さ N の整数列 A = (A_1, A_2, \dots, A_N), B = (B_1, B_2, \dots, B_N) が与えられます。
A の要素はすべて異なります。B の要素もすべて異なります。

次の 2 つを出力してください。

  1. A にも B にも含まれ、その位置も一致している整数の個数。言い換えると、A_i = B_i を満たす整数 i の個数。
  2. A にも B にも含まれるが、その位置は異なる整数の個数。言い換えると、A_i = B_j, i \neq j を満たす整数の組 (i, j) の個数。

制約

  • 1 \leq N \leq 1000
  • 1 \leq A_i \leq 10^9
  • 1 \leq B_i \leq 10^9
  • A_1, A_2, \dots, A_N はすべて異なる。
  • B_1, B_2, \dots, B_N はすべて異なる。
  • 入力はすべて整数である。

入力

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

N
A_1 A_2 \dots A_N
B_1 B_2 \dots B_N

出力

答えを 2 行出力せよ。1 行目には 1. の個数、2 行目には 2. の個数を出力せよ。


入力例 1

4
1 3 5 2
2 3 1 4

出力例 1

1
2

A にも B にも含まれ、その位置も一致している整数は A_2 = B_2 = 31 個です。
A にも B にも含まれるが、その位置は異なる整数は A_1 = B_3 = 1A_4 = B_1 = 22 個です。


入力例 2

3
1 2 3
4 5 6

出力例 2

0
0

1., 2. ともに条件を満たす整数は存在しません。


入力例 3

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

出力例 3

3
2

Score : 200 points

Problem Statement

You are given integer sequences, each of length N: A = (A_1, A_2, \dots, A_N) and B = (B_1, B_2, \dots, B_N).
All elements of A are different. All elements of B are different, too.

Print the following two values.

  1. The number of integers contained in both A and B, appearing at the same position in the two sequences. In other words, the number of integers i such that A_i = B_i.
  2. The number of integers contained in both A and B, appearing at different positions in the two sequences. In other words, the number of pairs of integers (i, j) such that A_i = B_j and i \neq j.

Constraints

  • 1 \leq N \leq 1000
  • 1 \leq A_i \leq 10^9
  • 1 \leq B_i \leq 10^9
  • A_1, A_2, \dots, A_N are all different.
  • B_1, B_2, \dots, B_N are all different.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 \dots A_N
B_1 B_2 \dots B_N

Output

Print the answers in two lines: the answer to1. in the first line, and the answer to2. in the second line.


Sample Input 1

4
1 3 5 2
2 3 1 4

Sample Output 1

1
2

There is one integer contained in both A and B, appearing at the same position in the two sequences: A_2 = B_2 = 3.
There are two integers contained in both A and B, appearing at different positions in the two sequences: A_1 = B_3 = 1 and A_4 = B_1 = 2.


Sample Input 2

3
1 2 3
4 5 6

Sample Output 2

0
0

In both 1. and 2., no integer satisfies the condition.


Sample Input 3

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

Sample Output 3

3
2
D - Restaurant Queue

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

高橋君はAtCoderレストランの前の待ち行列の管理をしたいです。はじめ、待ち行列に並んでいる人はいません。 また、待ち行列に並ぶ人は必ず注文する料理のメニュー番号が書かれた食券を持って並びます。

Q 個のクエリが与えられるので順に処理してください。クエリは 2 種類あり、以下のいずれかの形式で与えられます。

  • 1 X: 待ち行列の末尾に 1 人並ぶ。このとき並ぶ人はメニュー番号が X の食券を持って並ぶ。
  • 2: 待ち行列の先頭にいる人をレストランに案内する。このとき案内される人が持っている食券のメニュー番号を出力する。

制約

  • 1 \leq Q \leq 100
  • 1 \leq X \leq 100
  • 2 つ目の形式のクエリについて、案内する前に待ち行列に並んでいる人がいる
  • 入力は全て整数

入力

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

Q
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q

各クエリは以下の 2 種類のいずれかの形式で与えられる。

1 X
2

出力

問題文の指示に従ってクエリへの答えを改行区切りで出力せよ。


入力例 1

6
1 3
1 1
1 15
2
1 3
2

出力例 1

3
1

はじめ、待ち行列に並んでいる人はいません。

  • 1 つ目のクエリについて、メニュー番号が 3 の食券を持った人が待ち行列の末尾に並びます。この時、待ち行列に並んでいる人が持っている食券のメニュー番号は先頭の人から順に 3 です。
  • 2 つ目のクエリについて、メニュー番号が 1 の食券を持った人が待ち行列の末尾に並びます。この時、待ち行列に並んでいる人が持っている食券のメニュー番号は先頭の人から順に 3,1 です。
  • 3 つ目のクエリについて、メニュー番号が 15 の食券を持った人が待ち行列の末尾に並びます。この時、待ち行列に並んでいる人が持っている食券のメニュー番号は先頭の人から順に 3,1,15 です。
  • 4 つ目のクエリについて、待ち行列の先頭にいる人をレストランに案内します。案内する人はメニュー番号が 3 の食券を持っているので 3 を出力します。この時、待ち行列に並んでいる人が持っている食券のメニュー番号は先頭の人から順に 1,15 です。
  • 5 つ目のクエリについて、メニュー番号が 3 の食券を持った人が待ち行列の末尾に並びます。この時、待ち行列に並んでいる人が持っている食券のメニュー番号は先頭の人から順に 1,15,3 です。
  • 6 つ目のクエリについて、待ち行列の先頭にいる人をレストランに案内します。案内する人はメニュー番号が 1 の食券を持っているので 1 を出力します。この時、待ち行列に並んでいる人が持っている食券のメニュー番号は先頭の人から順に 15,3 です。

入力例 2

7
1 3
1 1
1 4
1 1
1 5
1 9
1 2

出力例 2


2 つ目の形式のクエリがないことがあることに注意してください。

Score : 200 points

Problem Statement

Takahashi wants to manage the waiting line in front of the AtCoder Restaurant. Initially, the waiting line is empty. Each person who joins the line holds a meal ticket with the menu number of the dish they will order.

Process Q queries in order. There are two types of queries, given in the following formats:

  • 1 X: One person joins the end of the waiting line holding a ticket with menu number X.
  • 2: Takahashi guides the person at the front of the waiting line into the restaurant. Print the menu number on that person’s ticket.

Constraints

  • 1 \leq Q \leq 100
  • 1 \leq X \leq 100
  • For each query of the second type, there is at least one person in the line before guiding.
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

Q
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q

Each query has one of the following two formats:

1 X
2

Output

For each query, print the answer as specified in the problem statement, each on its own line.


Sample Input 1

6
1 3
1 1
1 15
2
1 3
2

Sample Output 1

3
1

Initially, the waiting line is empty.

  • For the first query, a person holding a ticket with menu number 3 joins the end of the line. The sequence of menu numbers held by people in line from front to back is 3.
  • For the second query, a person holding a ticket with menu number 1 joins the end of the line. The sequence becomes 3,1.
  • For the third query, a person holding a ticket with menu number 15 joins the end of the line. The sequence becomes 3,1,15.
  • For the fourth query, guide the person at the front into the restaurant. That person holds menu number 3, so print 3. The sequence becomes 1,15.
  • For the fifth query, a person holding a ticket with menu number 3 joins the end of the line. The sequence becomes 1,15,3.
  • For the sixth query, guide the person at the front into the restaurant. That person holds menu number 1, so print 1. The sequence becomes 15,3.

Sample Input 2

7
1 3
1 1
1 4
1 1
1 5
1 9
1 2

Sample Output 2


Note that there may be no queries of the second type.

E - kasaka

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

英小文字からなる文字列 S が与えられます。 S の先頭に a をいくつか( 0 個でも良い)つけ加えて回文にすることができるか判定してください。

ただし、長さ N の文字列 A=A_1A_2\ldots A_N が回文であるとは、すべての 1\leq i\leq N について A_i=A_{N+1-i} が成り立っていることをいいます。

制約

  • 1 \leq \lvert S \rvert \leq 10^6
  • S は英小文字のみからなる。

入力

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

S

出力

S の先頭に a をいくつかつけ加えて回文にすることができるならば Yes を、そうでないならば No を出力せよ。


入力例 1

kasaka

出力例 1

Yes

kasaka の先頭に a1 つ付け加えることによって、akasaka となり回文となるため Yes を出力します。


入力例 2

atcoder

出力例 2

No

atcoder の先頭に a をいくつ付け加えても回文となる事はありません。


入力例 3

php

出力例 3

Yes

php はそれ自体回文です。S の先頭に付け加える a0 個でも許されるため、Yes を出力します。

Score : 300 points

Problem Statement

Given is a string S consisting of lowercase English letters. Determine whether adding some number of a's (possibly zero) at the beginning of S can make it a palindrome.

Here, a string of length N, A=A_1A_2\ldots A_N, is said to be a palindrome when A_i=A_{N+1-i} for every 1\leq i\leq N.

Constraints

  • 1 \leq \lvert S \rvert \leq 10^6
  • S consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S

Output

If adding some number of a's (possibly zero) at the beginning of S can make it a palindrome, print Yes; otherwise, print No.


Sample Input 1

kasaka

Sample Output 1

Yes

By adding one a at the beginning of kasaka, we have akasaka, which is a palindrome, so Yes should be printed.


Sample Input 2

atcoder

Sample Output 2

No

Adding any number of a's at the beginning of atcoder does not make it a palindrome.


Sample Input 3

php

Sample Output 3

Yes

php itself is a palindrome. Adding zero a's at the beginning of S is allowed, so Yes should be printed.