UVa 12786: Friendship Networks (Solution)

Friendship Networks
For a group of N people registered at a social network, a friendship network can be constructed with the N people as the nodes and a non-directed edge between every pair of friends. For such a network, it is usual to say that a node, i.e., a person, has degree d iff there are d nodes connected to it. Note that the degree of a node is exactly his/her number of friends. Since nobody is friends with himself / herself, the degree of each node is less than N.

A key property of a friendship network is the number of friends for each one of its members, i.e., its members’ degrees. However, given an intended number of network friends for each one of the members, such a network may or may not exist. For example, for a group of 4 people there is a friendship network in which the people have 3, 2, 2, and 3 friends. However, there is no friendship network corresponding to the enumeration 1, 3, 2, and 3 for a group of 4 people.

You are working at a company that researches friendship networks with the business idea of eventually developing applications on them. Your specific job is part of a test data validation. More precisely, you must write a program that given an enumeration of positive integers, decides if there exists a friendship network with such number of friends for each of its members.

Input

The input consists of several cases. Each test case is given in a single line by a blank-separated list of integers N, d1, d2, . . . , dN, with N (2 ≤ N ≤ 1000) the number of people in the social network and the d(i) (1 ≤ i ≤ N) an enumeration of a possible friendship network. You can assume that 0 < di < N for 1 ≤ i ≤ N.

The input must be read from standard input.

Output

Output a single line for each test case. If d1, d2, . . . , dN describes a friendship network with such number of friends for each of its members, then output 1; otherwise output 0.

The output must be written to standard output.

Sample Input
4 3 2 2 3
4 1 3 2 3
8 2 5 4 5 4 3 5 2

Sample Output

1
0
1


SOLUTION: Python


UVa 11475: Extend to Palindrome (Solution)

Extend to Palindrome

Your task is, given an integer N, to make a palidrome (word that reads the same when you reverse it) of length at least N. Any palindrome will do. Easy, isn’t it? That’s what you thought before you passed it on to your inexperienced team-mate. When the contest is almost over, you find out that that problem still isn’t solved. The problem with the code is that the strings generated are often not palindromic. There’s not enough time to start again from scratch or to debug his messy code. Seeing that the situation is desperate, you decide to simply write some additional code that takes the output and adds just enough extra characters to it to make it a palindrome and hope for the best. Your solution should take as its input a string and produce the smallest palindrome that can be formed by adding zero or more characters at its end.

Input

Input will consist of several lines ending in EOF. Each line will contain a non-empty string made up of upper case and lower case English letters (‘A’-‘Z’ and ‘a’-‘z’). The length of the string will be less than or equal to 100,000.

Output

For each line of input, output will consist of exactly one line. It should contain the palindrome formed by adding the fewest number of extra letters to the end of the corresponding input string.

Sample Input

aaaa
abba
amanaplanacanal
xyz

Sample Output

aaaa
abba
amanaplanacanalpanama
xyzyx

SOLUTION: Python


UVa 10810: Ultra-QuickSort (Solution)

Ultra-QuickSort

In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a sequence of n distinct integers by swapping two adjacent sequence elements until the sequence is sorted in ascending order. For the input sequence

9 1 0 5 4

Ultra-QuickSort produces the output
0 1 4 5 9

Your task is to determine how many swap operations Ultra-QuickSort needs to perform in order to sort a given input sequence.

Input

The input contains several test cases. Every test case begins with a line that contains a single integer n < 50000, the length of the input sequence. Each of the the following n lines contains a single integer 0 ≤ a[i] < 10^10, the i-th input sequence element.

Input is terminated by a sequence of length n = 0. This sequence must not be processed.

The input must be read from standard input.

Output

For every input sequence, your program prints a single line containing the minimum number of swap operations necessary to sort the given input sequence.

The output must be written to standard output.

Sample Input

5
9
1
0
5
4
3
1
2
3
0
Sample Output

6
0

SOLUTION: Python


UVa 10100: Longest Match (Solution)


Longest Match
A newly opened detective agency is struggling with their limited intelligence to find out a secret information passing technique among its detectives. Since they are new in this profession, they know well that their messages will easily be trapped and hence modified by other groups. They want to guess the intensions of other groups by checking the changed sections of messages. First they have to get the length of longest match. You are going to help them.


Input

The input may contain multiple test cases. Each case will contain two successive lines of string. Blank lines and non-letter printable punctuation characters may appear; any letter or digit is considered to be a letter character. Each line will be no longer than 1000 characters. Length of each word will at most 20 characters long.

The input must be read from standard input.


Output


For each case of input, you have to output a line starting with the case number right justified in a field width of two columns, followed by the longest match as shown in the sample output. In case of at least one blank line for each input output ‘Blank!’. Consider the non-letter characters as white-spaces.

The output must be written to standard output.


Sample Input

This is a test.
test
Hello!

The document provides late-breaking information
late breaking.

Sample Output

1. Length of longest match: 1
2. Blank!
3. Length of longest match: 2

SOLUTION: Python


UVa 12640: Largest Sum Game (Solution)

Largest Sum Game

After a long beer party and when ready for the check, John and his friends play the largest sum game and whoever wins the game will not have to pay a dime of the check. The largest sum game consists in finding the largest sum of consecutive values in a sequence of numbers; the winner is the one quickest to answer.

For example, in the sequence 23,−1,−24, 2, 23 the largest sum of consecutive values is 25 and whoever finds this value first, is the winner of the game.

Although simple (and geeky), the game is challenging because beer and arithmetic do not mix well together. However, since the group of friends are amateur programmers, each have implemented an algorithmic solution for finding the largest sum and have agreed to select the winner of the game in the form of a programming challenge: they connect their laptops to a central server that produces a random sequence of numeric values, run the solutions on this data, and the program quickest to answer wins the game.

John is tired of paying night after night without ever winning the game and he is determined to stop this situation tonight. John has hired you to write a highly efficient computer program that could beat the others in the largest sum game.

Input

The input consists of several test cases, each one dened by a line containing a sequence of N blank-separated integers x1, x2, ... ,xN (1 ≤ N ≤ 10^5, -10^3 ≤ xi ≤ 10^3 for each 1 ≤ i ≤ N)

The input must be read from standard input.

Output

For each test case, output a line with the largest sum of consecutive values in x1, x2, . . . , xN.

The output must be written to standard output.

Sample Input

1 2 3 4 5 6 7 8 9
-1 -1 -1
23 -1 -24 2 23
1 -14 -4 14 -11 -7

Sample Output

45
0
25
14

SOLUTION: Python

UVa 111: History grading (Solution)

History Grading

Many problems in Computer Science involve maximizing some measure according to constraints.

Consider a history exam in which students are asked to put several historical events into chronological order. Students who order all the events correctly will receive full credit, but how should partial credit be awarded to students who incorrectly rank one or more of the historical events?

Some possibilities for partial credit include:

  1. 1 point for each event whose rank matches its correct rank
  2. 1 point for each event in the longest (not necessarily contiguous) sequence of events which are in the correct order relative to each other.

For example, if four events are correctly ordered 1 2 3 4 then the order 1 3 2 4 would receive a score of 2 using the first method (events 1 and 4 are correctly ranked) and a score of 3 using the second method (event sequences 1 2 4 and 1 3 4 are both in the correct order relative to each other).

In this problem you are asked to write a program to score such questions using the second method.


The Problem

Given the correct chronological order of n events 1, 2, . . . , n  as c1, c2, . . . , cn where 1 ≤ ci ≤ n denotes the ranking of event i in the correct chronological order and a sequence of student responses r1,r2, . . . ,rn where 1 ≤ ri ≤ n denotes the chronological rank given by the student to event i; determine the length of the longest (not necessarily contiguous) sequence of events in the student responses that are in the correct chronological order relative to each other.


Input

The first line of the input will consist of one integer n indicating the number of events with 2 ≤ n ≤ 200 . The second line will containn integers, indicating the correct chronological order of n events. The remaining lines will each consist of n integers with each line representing a student's chronological ordering of the n events. All lines will contain n numbers in the range [1..n], with each number appearing exactly once per line, and with each number separated from other numbers on the same line by one or more spaces.


Output

For each student ranking of events your program should print the score for that ranking. There should be one line of output for each student ranking.



Sample Input



4 3

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

Sample Output

1
2
3
6
5
10
9

SOLUTION: C++

HOME

Welcome to "Uva Problems - Python", in this web site may achieve the UVa problems solved in Python, C ++ language and more.

But it is the only website that shows issues resolved in Python language, also can comment, give opinios and ask about all UVa Problems, on page UVa Online Judge