**1- Beautiful Strings**

**The problem statement is given below:**

**s**, little Johnny defined the beauty of the string as the sum of the beauty of the letters in it.

**m**followed by

**m**lines.

**x**:

**y**” where

**x**is the case number (with 1 being the first case in the input file, 2 being the second, etc.) and

**y**is the maximum beauty for that test case.

**m**≤ 50

2 ≤ length of

**s**≤ 500

`5`

`ABbCcc`

`Good luck in the Facebook Hacker Cup this year!`

`Ignore punctuation, please :)`

`Sometimes test cases are hard to make up.`

`So I just go consult Professor Dalves`

`Case #1: 152`

`Case #2: 754`

`Case #3: 491`

`Case #4: 729`

`Case #5: 646`

` `

`Here goes my solution for the “Beautiful Strings” problem:`

`import java.io.File;`

import java.io.FileReader;

import java.io.FileWriter;

import java.io.LineNumberReader;

import java.util.ArrayList;

import java.util.Collections;

/**

*

* @author VIK

*/

public class Problem1 {

private static ArrayList < Integer > integers;

private static File f=new File("Solution1.txt");

public static void main(String[] args) throws Exception {

FileReader fr = new FileReader("beautiful_stringstxt.txt");

LineNumberReader lnr = new LineNumberReader(fr);

int t = Integer.parseInt(lnr.readLine().trim());

FileWriter fw=new FileWriter(f);

for (int i = 0; i < t; i++) {

int sum= getMax(lnr.readLine().trim());

if ( i < t-1 )

fw.write ( "Case #" + ( i+1 ) + ": " + sum+ "\n" );

else

fw.write( "Case #" + (i+1) + ": " + sum );

fw.flush();

}

}

`private static int getMax ( String string ) throws Exception {`

integers = new ArrayList < Integer > ();

for ( int i = 65, j = 97; i <= 90 && j <= 123 ; i++, j++ ) {

String s = string.replaceAll ( "[^" + (char) i + (char) j + "]", "" );

s.trim();

integers.add(s.length());

}

int sum = 0;

Collections.sort(integers);

for ( int i = integers.size() - 1, j = 26; i >= 0 && j >= 1; --i, --j) {

sum += integers.get(i) * j;

}

return sum;

}}

Download Source Code :2-Balanced SmileysThe problem statement is given below:Your friend John uses a lot of emoticons when you talk to him on Messenger. In addition to being a person who likes to express himself through emoticons, he hates unbalanced parenthesis so much that it makes him goSometimes he puts emoticons within parentheses, and you find it hard to tell if a parenthesis really is a parenthesis or part of an emoticon.A message has balanced parentheses if it consists of one of the following:1. An empty string “”2. One or more of the following characters: ‘a’ to ‘z’, ‘ ‘ (a space) or ‘:’ (a colon)3. An open parenthesis ‘(‘, followed by a message with balanced parentheses, followed by a close parenthesis ‘)’.4. A message with balanced parentheses followed by another message with balanced parentheses.5. A smiley face “:)” or a frowny face “:(“Write a program that determines if there is a way to interpret his message while leaving the parentheses balanced.InputThe first line of the input contains a numberT(1 ≤T≤ 50), the number of test cases.

The followingTlines each contain a message of lengthsthat you got from John.OutputFor each of the test cases numbered in order from 1 toT, output “Case #i: ” followed by a string stating whether or not it is possible that the message had balanced parentheses. If it is, the string should be “YES”, else it should be “NO” (all quotes for clarity only)Constraints1 ≤ length ofs≤ 100Sample Input5:((i am sick today (:()(:)hacker cup: started :):))(Sample OutputCase #1: NOCase #2: YESCase #3: YESCase #4: YESCase #5: NO

`Here goes my solution for the “Beautiful Strings” problem:`

//Problem2.java`import java.io.File;`

import java.io.FileReader;

import java.io.FileWriter;

import java.io.LineNumberReader;

import java.util.ArrayList;

/**

*

* @author VIK

*

*/

public class Problem2 {

private static File f = new File("Solution2.txt");

public static void main(String[] args) throws Exception {

FileReader fr = new FileReader("balanced_smileystxt.txt");

LineNumberReader lnr = new LineNumberReader(fr);

int t = Integer.parseInt(lnr.readLine().trim());

ArrayList < String > list = new ArrayList < String >();

FileWriter fw = new FileWriter(f);

for (int i = 0; i < t; i++) {

list.add(lnr.readLine().trim());

String res = printResult(list.get(i), i + 1);

if ( i < t - 1 ) {

fw.write("Case #" + (i + 1) + ": " + res + "\n");

} else {

fw.write("Case #" + (i + 1) + ": " + res);

}

fw.flush();

}

}

`private static String printResult ( String string, int index) {`

String s = string.replaceAll ( "[^:\\(\\)]", "" );

for (int i = 0; i < s.length() / 2; i++) {

String lead = "";

String trail = "";

if (s.charAt(i) == '(' && s.charAt(s.length() - i - 1) == ')') {

if (i > 0) {

lead = s.substring(0, i);

trail = s.substring (s.length() - i, s.length());

}

s = lead + s.substring ( i + 1, s.length() - i - 1 ) + trail;

--i;

}

}

s = s.replaceAll( ":\\(", "" );

s = s.replaceAll( ":\\)", "" );

for (int i = 0; i < s.length(); i++) {

if (s.charAt(i) == '(' || s.charAt(i) == ')') {

return "NO";

}

}

return "YES";

}

}

`Download Source Code :`

3- Find the MinAfter sending smileys, John decided to play with arrays. Did you know that hackers enjoy playing with arrays? John has a zero-based index array, m, which contains n non-negative integers. However, only the first k values of the array are known to him, and he wants to figure out the rest.

John knows the following: for each index i, where k <= i < n, m[i] is the minimum non-negative integer which is *not* contained in the previous *k* values of m.

For example, if k = 3, n = 4 and the known values of m are [2, 3, 0], he can figure out that m[3] = 1.

John is very busy making the world more open and connected, as such, he doesn’t have time to figure out the rest of the array. It is your task to help him.

Given the first k values of m, calculate the nth value of this array. (i.e. m[n – 1]).

Because the values of n and k can be very large, we use a pseudo-random number generator to calculate the first k values of m. Given positive integers a, b, c and r, the known values of m can be calculated as follows:

m[0] = a

m[i] = (b * m[i – 1] + c) % r, 0 < i < k

Input

The first line contains an integer T (T <= 20), the number of test cases.

This is followed by T test cases, consisting of 2 lines each.

The first line of each test case contains 2 space separated integers, n, k (1 <= k <= 105, k < n <= 109).

The second line of each test case contains 4 space separated integers a, b, c, r (0 <= a, b, c <= 109, 1 <= r <= 109).

Output

For each test case, output a single line containing the case number and the nth element of m.

Example input:

`5`

97 39

34 37 656 97

186 75

68 16 539 186

137 49

48 17 461 137

98 59

6 30 524 98

46 18

7 11 9 46Example output

`Case #1: 8`

Case #2: 38

Case #3: 41

Case #4: 40

Case #5: 12Solutions by Vikash verma (@vikashvverma) &Sobre Krolzaum

Read more: **Facebook Hackercup 2013 Qualification Round Solutions**