Posts

Showing posts from September, 2016

【POJ】Cow Bowling

Problem  here Description The cows don’t use actual bowling balls when they go bowling. They each take a number (in the range 0..99), though, and line up in a standard bowling-pin-like triangle like this: 7 3 8 8 1 0 2 7 4 4 4 5 2 6 5  Then the other cows traverse the triangle starting from its tip and moving “down” to one of the two diagonally adjacent cows until the “bottom” row is reached. The cow’s score is the sum of the numbers of the cows visited along the way. The cow with the highest score wins that frame. Given a triangle with N (1 <= N <= 350) rows, determine the highest possible sum achievable. Input Line 1: A single integer, N Lines 2..N+1: Line i+1 contains i space-separated integers that represent row i of the triangle. Output Line 1: The largest sum achievable using the traversal rules Sample Input 5  7  3 8  8 1 0  2 7 4 4  4 5 2 6 5 Sample Output 30 Hint...

【UVa】10298 - Power Strings

Problem  here Given two strings a and b we define a ∗ b to be their concatenation. For  example, if a = ‘abc’ and b = ‘def’ then a ∗ b = ‘abcdef’. If we think of  concatenation as multiplication, exponentiation by a non-negative integer  is defined in the normal way: a  0 = ‘’ (the empty string) and a  (n+1) =  a ∗ (a  n). Input Each test case is a line of input representing s, a string of printable characters.  The length of s will be at least 1 and will not exceed 1 million  characters. A line containing a period follows the last test case. Output For each s you should print the largest n such that s = a  n for some string  a. Sample Input abcd  aaaa  ababab  . Sample Output 1  4  3 Solution #include <iostream> #include <string> //#include <fstream> #define MAX_NUM 9999999 using namespace std ; //ifstream fin("10298.in"); //ofstream fout("10298.out"); ...

【UVa】455 - Periodic Strings

Problem  here   A character string is said to have period k if it can be formed by concatenating one or more repetitions  of another string of length k. For example, the string ”abcabcabcabc” has period 3, since it is formed  by 4 repetitions of the string ”abc”. It also has periods 6 (two repetitions of ”abcabc”) and 12 (one  repetition of ”abcabcabcabc”).  Write a program to read a character string and determine its smallest period. Input The first line oif the input file will contain a single integer N indicating how many test case that your  program will test followed by a blank line. Each test case will contain a single character string of up  to 80 non-blank characters. Two consecutive input will separated by a blank line. Output An integer denoting the smallest period of the input string for each input. Two consecutive output are  separated by a blank line. Sample Input 1  HoHoHo Sample Output 2 Solution #inclu...

【TopCoder】ZigZag - 2003 TCCC Semifinals 3

Problem Statement A sequence of numbers is called a zig-zag sequence if the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either positive or negative. A sequence with fewer than two elements is trivially a zig-zag sequence.  For example, 1,7,4,9,2,5 is a zig-zag sequence because the differences (6,-3,5,-7,3) are alternately positive and negative. In contrast, 1,4,7,2,5 and 1,7,4,5,5 are not zig-zag sequences, the first because its first two differences are positive and the second because its last difference is zero.  Given a sequence of integers, sequence, return the length of the longest subsequence of sequence that is a zig-zag sequence. A subsequence is obtained by deleting some number of elements (possibly zero) from the original sequence, leaving the remaining elements in their original order. Solution class ZigZag{ public : int longestZigZag( vector < int > n...