Sunteți pe pagina 1din 11

# Q1.

## Shortest Path (60 Points)

Given a list of cities. Each direct connection between two cities has its transportation cost (an integer bigger than 0). The goal is to find the paths of minimum cost between pairs of cities. Assume that the cost of each path (which is the sum of costs of all direct connections belonging to this path) is at most 200000. The name of a city is a string containing characters a,...,z and is at most 10 characters long

Input(Q1.in)
s [the number of tests <= 10] n [the number of cities <= 10000] NAME [city name] p [the number of neighbors of city NAME] nr cost [nr - index of a city connected to NAME (the index of the first city is 1)] [cost - the transportation cost] r [the number of paths to find <= 100] NAME1 NAME2 [NAME1 - source, NAME2 - destination] [Empty line separating the tests]

Output(Q1.out)
Cost [the minimum transportation cost from city NAME1 to city NAME2 (one per line)]

## Example Sample Input:

1 4 gdansk 2 21 33 bydgoszcz 3 11 31 44 torun

Sample Output:
3 2

## Q2. Spellings (40 Points)

J, of the Men In Black, has been learning an alien language and has a spelling test tomorrow. J, however, is bored of studying the nonsensical (and often unpronounceable) words. Instead, he is seeing how many ways he can reorder his spelling list. After making all possible permutations of word on his list, he sorts the rearranged lists lexicographically (by the first word, then the second...). After the sort, in what position, with the lexicographically first list being in position 1, is his original spelling list?

Input(Q2.in)
The first line is the number of spelling lists (no more than 10). For each spelling list, a line with the number of words (no more than 1000) is given, followed by the original list on the next line. All words within a spelling list are unique. Each word is composed of the letters a-z, is fewer than 100 characters, and is followed by a single space.

Output(Q2.out)
On separate lines, give the positions of the original lists.

Example Input:
4 4 a b c d 4 d c b a 1 mrsmith 6 a aaaaaa aaaaa aaaa b bb

Output:
1 24 1 55

## Q3. Fashion (40 Points)

A fashion show rates participants according to their level of hotness. Two different fashion shows were organized, one for men and the other for women. A date for the third is yet to be decided . Now the results of both fashion shows are out. The participants of both the fashion shows have decided to date each other, but as usual they have difficulty in choosing their partners. The Maximum Match dating service (MMDS) comes to their rescue and matches them in such a way that that maximizes the hotness bonds for all couples. If a man has been rated at hotness level x and a women at hotness level y, the value of their hotness bond is x*y. Both fashion shows contain N participants each. MMDS has done its job and your job is to find the sum of hotness bonds for all the couples that MMDS has proposed.

Input(Q3.in)
The first line of the input contains an integer t, the number of test cases. t test cases follow. Each test case consists of 3 lines: The first line contains a single integer N (1 <= N <= 1000). The second line contains N integers separated by single spaces denoting the hotness levels of the men. The third line contains N integers separated by single spaces denoting the hotness levels of the women.

## All hotness ratings are on a scale of 0 to 10.

Output(Q3.out)
For each test case output a single line containing a single integer denoting the sum of the hotness bonds for all pairs that MMDS has proposed.

## Example Sample input:

2 2 1 3 3 2 1 1 2 3 2 3 2

Sample output:
5 15

## Q4. Open Up Sim-Sim (Points 40)

Some of the secret doors contain a very interesting word puzzle. The team of archaeologists has to solve it to open those doors. Because there is no other way to open the doors, the puzzle is very important for us. There is a large number of magnetic plates on every door. Every plate has one word written on it. The plates must be arranged into a sequence in such a way that every word begins with the same letter as the previous word ends. For example, the word acm'' can be followed by the word motorola''. Your task is to write a computer program that will read the list of words and determine whether it is possible to arrange all of the plates in a sequence (according to the given rule) and consequently to open the door.

Input
The input consists of T test cases. The number of them (T, equal to about 500) is given on the first line of the input file. Each test case begins with a line containing a single integer number N that indicates the number of plates (1 <= N <= 100000). Then exactly N lines follow, each containing a single word. Each word contains at least two and at most 1000 lowercase characters, that means only letters 'a' through 'z' will appear in the word. The same word may appear several times in the list.

Output
Your program has to determine whether it is possible to arrange all the plates in a sequence such that the first letter of each word is equal to the last letter of the previous word. All the plates from the list must be used, each exactly once. The words mentioned several times must be used that number of times. If there exists such an ordering of plates, your program should print the sentence "Ordering is possible.". Otherwise, output the sentence "The door cannot be opened.".

Example Input(Q4.in):
3 2 acm ibm 3 acm malform mouse 2 ok ok

Output(Q4.out):
The door cannot be opened. Ordering is possible. The door cannot be opened.

## Q5 [SCORE = 20]. Unconditional Loop

Without Using any CONDITIONAL STATEMENT [i.e if-else, switch] print the first EVEN DIGIT from the RIGHT, if exists else print -1.

Input (Q5.in)
File Q1.in first line will have an Integer value [x], where 0 < x < 100 From Next line till the EOF will contain an integer number [y], where 0 < y < 1000000, one integer (i.e one test case input) per line.

Output (Q5.out)
File should be Q1.out; it will have first case as number of test cases [x] And then one output per line, represented as Case#<output number> = <Output>

Sample Input
4 123 123456 654321 3

Sample Output
4 Case #1 = 2 Case#2 = 6 Case#3 = 2 Case#4 = -1

Q6[Score 70].
Mr. Walker is a person who is known for his ability to walk fast. He accepts an attractive offer to get as much free land as he can cover by a walk on an open uneven huge piece of land within a specified time and following certain conditions. A walk is a sequence of paths, starting at a marked spot on the land and ending at the same spot where the walk starts. The first path in a walk begins at the marked spot and extends in one of the four directions: North (N), South (S), East (E) or West (W). It ends at the point where the direction changes. Each of the other paths in a walk begins at the point where the previous path ends and ends either at a point where the direction changes or when the walk ends. Mr. Walker may change directions, as and when he feels like, depending on the constraint of time and/or quality/quantity of the land he decides to get. When he changes a direction he must keep the direction always to one of the four directions N, S, E or W. Paths are distinct and nonintersecting; for example, a path in E or W direction cannot cross another in N or S direction. Given a walk, you are required to write a program that finds the area of land covered by the walk. Input (Q6.in) The input consists of multiple test cases. First line will be number of test cases, followed by the test cases, one per line. The line gives a walk defined by a sequence of paths. A direction followed, without any space, by a distance represents a path. A direction is denoted by one of the four letters N, S, E or W while a distance is measured in meters and is denoted by an integer. A space character appears between two paths in the given sequence. Output (Q6.out) For each test case print the area of land covered by the given walk. Sample Input 3 N3 W4 S8 E4 N5 W6 N2 E9 S6 W3 N4 W6 N2 W3 S4 W5 S4 E14 N6 Sample Output 3 Case #1 = 32 Case #1 = 30 Case #1 = 80

## Q7 [SCORE = 60]. Write a program to convert an integer to digital screen number.

Input (Q7.in) First line will be number of test cases. Each input set will start with an integer [x], where 0 < x < 9999. Where Single Digit Digital Screen is assumed to be of like _ |_| |_| Only characters allowed for creating a number are UNDERSCORE (_) SPACE ( ) & PIPE ( | ) Output (Q7.out) First line will be number of test cases. For each test set, there should be a one line output containing the LED screen display. There must be a single space between the two integers which are converted as digital number Sample Input 3 0 4 340

## Q8. Chocolate (70 Points)

We are given a bar of chocolate composed of m*n square pieces. One should break the chocolate into single squares. Parts of the chocolate may be broken along the vertical and horizontal lines as indicated by the broken lines in the picture. A single break of a part of the chocolate along a chosen vertical or horizontal line divides that part into two smaller ones. Each break of a part of the chocolate is charged a cost expressed by a positive integer. This cost does not depend on the size of the part that is being broken but only depends on the line the break goes along. Let us denote the costs of breaking along consecutive vertical lines with x1, x2, ..., xm-1 and along horizontal lines with y1, y2, ..., yn-1. The cost of breaking the whole bar into single squares is the sum of the successive breaks. One should compute the minimal cost of breaking the whole chocolate into single squares.

For example, if we break the chocolate presented in the picture first along the horizontal lines, and next each obtained part along vertical lines then the cost of that breaking will be y1+y2+y3+4*(x1+x2+x3+x4+x5).

Write a program that for each test case: Reads the numbers x1, x2, ..., xm-1 and y1, y2, ..., yn-1 Computes the minimal cost of breaking the whole chocolate into single squares, writes the result.

Input
One integer in the first line, stating the number of test cases, followed by a blank line. There will be not more than 20 tests. For each test case, at the first line there are two positive integers m and n separated by a single space, 2 <= m,n <= 1000. In the successive m-1 lines there are numbers x1, x2, ..., xm-1, one per line, 1 <= xi <= 1000. In the successive n-1 lines there are numbers y1, y2, ..., yn-1, one per line, 1 <= yi <= 1000. The test cases will be separated by a single blank line.

Output
For each test case : write one integer - the minimal cost of breaking the whole chocolate into single squares.

Example Input(Q8.in):
1

64 2 1 3 1 4 4 1 2

Output(Q8.out):
42