Вы находитесь на странице: 1из 9

Problem Solving Set V

1. Chef and the Wildcard Matching


Chef wants to implement wildcard pattern matching supporting only the wildcard '?'. The wildcard
character '?' can be substituted by any single lower case English letter for matching. He has two
strings X and Y of equal length, made up of lower case letters and the character '?'. He wants to know
whether the strings X and Y can be matched or not.

Input
The first line of input contain an integer T denoting the number of test cases. Each test case consists of two
lines, the first line contains the string X and the second contains the string Y.

Output
For each test case, output a single line with the word Yes if the strings can be matched, otherwise
output No.

Constraints
 1 ≤ T ≤ 50
 Both X and Y have equal length and the length is between 1 and 10.
 Both X and Y consist of lower case letters and the character '?'.

Example
Input:
2
s?or?
sco??
stor?
sco??
Output:
Yes
No

Explanation
First Example: There are several ways the two strings can be matched, one of those is "score".
Second Example: There is no way to match the strings.

2. Farmer Feb
Farmer Feb has three fields with potatoes planted in them. He harvested x potatoes from the first
field, y potatoes from the second field and is yet to harvest potatoes from the third field. Feb is very
superstitious and believes that if the sum of potatoes he harvests from the three fields is a prime number
he'll make a huge profit. Please help him by calculating for him the minimum number of potatoes that if
harvested from the third field will make the sum of potatoes prime. At least one potato should be harvested
from the third field.

Input
The first line of the input contains an integer T denoting the number of test cases. Each of the next T lines
contain 2 integers separated by single space: x and y.

Output
For each test case, output a single line containing the answer.

Constraints
 1 ≤ T ≤ 1000
 1 ≤ x ≤ 1000
 1 ≤ y ≤ 1000
Example
Input:
2
13
43

Output:
1
4

Explanation
In example case 1: the farmer harvested a potato from the first field and 3 potatoes from the second field.
The sum is 4. If he is able to harvest a potato from the third field, that will make the sum 5, which is prime.
Hence the answer is 1(he needs one more potato to make the sum of harvested potatoes prime.)

3. Minimum Maximum
Chef loves to play with arrays by himself. Today, he has an array A consisting of N distinct integers. He
wants to perform the following operation on his array A.
 Select a pair of adjacent integers and remove the larger one of these two. This decreases the array size
by 1. Cost of this operation will be equal to the smaller of them.
Find out minimum sum of costs of operations needed to convert the array into a single element.

Input
First line of input contains a single integer T denoting the number of test cases. First line of each test case
starts with an integer N denoting the size of the array A. Next line of input contains N space separated
integers, where the ith integer denotes the value Ai.

Output
For each test case, print the minimum cost required for the transformation.

Constraints
 1 ≤ T ≤ 10
 2 ≤ N ≤ 50000
 1 ≤ Ai ≤ 105

Subtasks
 Subtask 1 : 2 ≤ N ≤ 15 : 35 pts
 Subtask 2 : 2 ≤ N ≤ 100 : 25 pts
 Subtask 3 : 2 ≤ N ≤ 50000 : 40 pts

Example
Input
2
2
34
3
425

Output
3
4

Explanation
Test 1 : Chef will make only 1 move: pick up both the elements (that is, 3 and 4), remove the larger one (4), incurring
a cost equal to the smaller one (3).
4. Coins And Triangle
Chef belongs to a very rich family which owns many gold mines. Today, he brought N gold coins and
decided to form a triangle using these coins. Isn't it strange?
Chef has a unusual way of forming a triangle using gold coins, which is described as follows:
st
 He puts 1 coin in the 1 row.
nd
 then puts 2 coins in the 2 row.
rd
 then puts 3 coins in the 3 row.
 and so on as shown in the given figure.

Chef is interested in forming a triangle with maximum possible height using at most N coins. Can you tell
him the maximum possible height of the triangle?

Input
The first line of input contains a single integer T denoting the number of test cases.
The first and the only line of each test case contains an integer N denoting the number of gold coins Chef
has.

Output
For each test case, output a single line containing an integer corresponding to the maximum possible height
of the triangle that Chef can get.

Constraints
 1 ≤ T ≤ 100
 1 ≤ N ≤ 109

Subtasks
 Subtask 1 (48 points) : 1 ≤ N ≤ 105
 Subtask 2 (52 points) : 1 ≤ N ≤ 109

Example
Input
3
3
5
7

Output
2
2
3

Explanation
 Test 1: Chef can't form a triangle with height > 2 as it requires atleast 6 gold coins.
 Test 2: Chef can't form a triangle with height > 2 as it requires atleast 6 gold coins.
 Test 3: Chef can't form a triangle with height > 3 as it requires atleast 10 gold coins.
5. Sum OR Difference
Write a program to take two numbers as input and print their difference if the first number is greater than
the second number otherwise print their sum.

Input:
 First line will contain the first number (N1N1)
 Second line will contain the second number (N2N2)

Output:
Output a single line containing the difference of 2 numbers (N1−N2)(N1−N2) if the first number is
greater than the second number otherwise output their sum (N1+N2)(N1+N2).

Constraints
 −1000≤N1≤1000−1000≤N1≤1000
 −1000≤N2≤1000−1000≤N2≤1000

Sample Input:
82
28

Sample Output:
54

6. Piece of cake
This is a very easy warm-up problem.
You are given a string. Your task is to determine whether number of occurrences of some character in the
string is equal to the sum of the numbers of occurrences of other characters in the string.

Input
The first line of the input contains an integer T denoting the number of test cases. Each of the next T lines
contains one string S consisting of lowercase latin letters.

Output
For each test case, output a single line containing "YES" if the string satisfies the condition given above
or "NO" otherwise.

Constraints
1 ≤ T ≤ 1000
1 ≤ length of S ≤ 50

Subtasks
Subtask #1[28 points]: S contains no more than 2 different letters.
Subtask #2[72 points]: No additional conditions

Example
Input:
4
acab
zzqzqq
abc
kklkwwww
Output:
YES
YES
NO
YES
7. Chef and Dolls
Chef is fan of pairs and he likes all things that come in pairs. He even has a doll collection in which all
dolls have paired. One day while going through his collection he found that there are odd number of dolls.
Someone had stolen a doll!!!
Help chef find which type of doll is missing..

Input
The first line contains the number of test cases.
Second line of the input contains the number of elements in the array.
The next n lines are the types of each doll that is left.

Output
Find the type of doll that doesn't have a pair

Constraints
1<=T<=10
1<=N<=100000 (10^5)
1<=ti<=100000
Input:
1
3
1
2
1

Output:
2

Input:
1
5
1
1
2
2
3
Output:
3

8. Help Lost Robot!


Robot Bunny is lost. It wants to reach its home as soon as possible. Currently it is standing at
coordinates (x1, y1) in 2-D plane. Its home is at coordinates (x2, y2). Bunny is extremely worried. Please
help it by giving a command by telling the direction in which it should to go so as to reach its home. If you
give it a direction, it will keep moving in that direction till it reaches its home. There are four possible
directions you can give as command - "left", "right", "up", "down". It might be possible that you can't
instruct the robot in such a way that it reaches its home. In that case, output "sad".

Input
First line of the input contains an integer T denoting the number of test cases. T test cases follow.
First line of each test case contains four space separated integers x1, y1, x2, y2.

Output
For each test case, output a single line containing "left" or "right" or "up" or "down" or "sad" (without
quotes).

Constraints
 1 ≤ T ≤ 5000
 0 ≤ x1, y1, x2, y2. ≤ 100
 It's guaranteed that the initial position of robot is not his home.

Example
Input
3
0010
0001
0011

Output:
right
up
sad

Explanation
Test case 1. If you give Bunny the command to move to the right, it will reach its home.

9. Malvika is peculiar about color of balloons


Little Malvika is very peculiar about colors. On her birthday, her mom wanted to buy balloons for
decorating the house. So she asked her about her color preferences. The sophisticated little person that
Malvika is, she likes only two colors — amber and brass. Her mom bought n balloons, each of which was
either amber or brass in color. You are provided this information in a string s consisting of characters 'a'
and 'b' only, where 'a' denotes that the balloon is amber, where 'b' denotes it being brass colored.
When Malvika saw the balloons, she was furious with anger as she wanted all the balloons of the same
color. In her anger, she painted some of the balloons with the opposite color (i.e., she painted some amber
ones brass and vice versa) to make all balloons appear to be the same color. As she was very angry, it took
her a lot of time to do this, but you can probably show her the right way of doing so, thereby teaching her a
lesson to remain calm in difficult situations, by finding out the minimum number of balloons needed to be
painted in order to make all of them the same color.

Input
 The first line of input contains a single integer T, denoting the number of test cases.
 The first and only line of each test case contains a string s.

Output
 For each test case, output a single line containing an integer — the minimum number of flips required.

Constraints
 1 ≤ T ≤ 100
 1 ≤ n ≤ 100, where n denotes to the length of the string s.

Example
Input:
3
ab
bb
baaba

Output:
1
0
2

Explanation
In the first example, you can change amber to brass or brass to amber. In both the cases, both the balloons
will have same colors. So, you will need to paint 1 balloon. So the answer is 1.
In the second example, As the color of all the balloons is already the same, you don't need to paint any of
them. So, the answer is 0.

10. Forgotten Language


Forgotten languages (also known as extinct languages) are languages that are no longer in use. Such
languages were, probably, widely used before and no one could have ever imagined that they will become
extinct at some point. Unfortunately, that is what happened to them. On the happy side of things, a
language may be dead, but some of its words may continue to be used in other languages.
Using something called as the Internet, you have acquired a dictionary of N words of a forgotten language.
Meanwhile, you also know K phrases used in modern languages. For each of the words of the forgotten
language, your task is to determine whether the word is still in use in any of these K modern phrases or not.

Input
The first line of the input contains an integer T denoting the number of test cases. The description of T test
cases follows.
The first line of a test case description contains two space separated positive integers N and K.
The second line of the description contains N strings denoting a dictionary of the forgotten language.
Each of the next K lines of the description starts with one positive integer L denoting the number of words
in the corresponding phrase in modern languages. The integer is followed by L strings (not necessarily
distinct) denoting the phrase.

Output
For each test case, output a single line containing N tokens (space-separated): if the ith word of the dictionary exists in
at least one phrase in modern languages, then you should output YES as the ith token, otherwise NO.

Constraints
 1 ≤ T ≤ 20
 1 ≤ N ≤ 100
 1 ≤ K, L ≤ 50
 1 ≤ length of any string in the input ≤ 5

Example
Input:
2
32
piygu ezyfo rzotm
1 piygu
6 tefwz tefwz piygu ezyfo tefwz piygu
41
kssdy tjzhy ljzym kegqz
4 kegqz kegqz kegqz vxvyj

Output:
YES YES NO
NO NO NO YES

11. Farmer And His Plot


Santosh has a farm at Byteland. He has a very big family to look after. His life takes a sudden turn and he
runs into a financial crisis. After giving all the money he has in his hand, he decides to sell some parts of
his plots. The specialty of his plot is that it is rectangular in nature. Santosh comes to know that he will get
more money if he sells square shaped plots. So keeping this in mind, he decides to divide his plot into
minimum possible square plots so that he can get maximum profit out of this.
So your task is to find the minimum number of square plots that can be formed out of the rectangular plot.

Input
The input consists of T number of test cases. T lines follow. Each line consists of two integers N and M
which denotes the length and breadth of the rectangle.

Output
Output is a single line which denotes the minimum number of square plots that can be formed

Constraints
1<=T<=20
1<=M<=10000
1<=N<=10000
Input:
2
10 15
46

Output:
6
6

12. Kitchen Timetable


There are N students living in the dormitory of Berland State University. Each of them sometimes wants to
use the kitchen, so the head of the dormitory came up with a timetable for kitchen's usage in order to avoid
the conflicts:
 The first student starts to use the kitchen at the time 0 and should finish the cooking not later than at the
time A1.
 The second student starts to use the kitchen at the time A1 and should finish the cooking not later than
at the time A2.
 And so on.
 The N-th student starts to use the kitchen at the time AN-1 and should finish the cooking not later than at
the time AN
The holidays in Berland are approaching, so today each of these N students wants to cook some pancakes.
The i-th student needs Bi units of time to cook.
The students have understood that probably not all of them will be able to cook everything they want. How
many students will be able to cook without violating the schedule?

Input
The first line of the input contains an integer T denoting the number of test cases. The description of T test
cases follows.
The first line of each test case contains a single integer N denoting the number of students.
The second line contains N space-separated integers A1, A2, ..., AN denoting the moments of time by when
the corresponding student should finish cooking.
The third line contains N space-separated integers B1, B2, ..., BN denoting the time required for each of the
students to cook.

Output
For each test case, output a single line containing the number of students that will be able to finish the
cooking.

Constraints
Should contain all the constraints on the input data that you may have. Format it like:
 1 ≤ T ≤ 10
 1 ≤ N ≤ 10
4
9
 0 < A1 < A2 < ... < AN < 10
 1 ≤ Bi ≤ 10
9

Example
Input:
2
3
1 10 15
1 10 3
3
10 20 30
15 5 20

Output:
2
1

Explanation
Example case 1. The first student has 1 unit of time - the moment 0. It will be enough for her to cook. The
second student has 9 units of time, but wants to cook for 10 units of time, and won't fit in time. The third
student has 5 units of time and will fit in time, because needs to cook only for 3 units of time.
Example case 2. Each of students has 10 units of time, but only the second one will be able to fit in time.

13. Chef and digits of a number


Chef has a number D containing only digits 0's and 1's. He wants to make the number to have all the digits
same. For that, he will change exactly one digit, i.e. from 0 to 1 or from 1 to 0. If it is possible to make all
digits equal (either all 0's or all 1's) by flipping exactly 1 digit then output "Yes", else print "No" (quotes for
clarity)

Input
The first line will contain an integer T representing the number of test cases.
Each test case contain a number made of only digits 1's and 0's on newline

Output
Print T lines with a "Yes" or a "No", depending on whether its possible to make it all 0s or 1s or not.

Constraints
Subtask #1: (40 points)
 1 ≤ T ≤ 50
 1 ≤ Length of the number D ≤ 50

Subtask #2: (60 points)


 1 ≤ T ≤ 10
 1 ≤ Length of the number D ≤ 105

Example
Input:
2
101
11
Output:
Yes
No

Explanation
Example case 1. In 101, the 0 can be flipped to make it all 1..
Example case 2. No matter whichever digit you flip, you will not get the desired string.

Вам также может понравиться