Last active
June 23, 2019 09:42
-
-
Save vikashvverma/4649414 to your computer and use it in GitHub Desktop.
This is my implementation in java for Facebook Hacker Cup 2013 for Beautiful Strings problem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//visit http://vikash-thiswillgoaway.blogspot.com for solution of other problems | |
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 http://www.facebook.com/vikashvverma | |
*/ | |
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Facebook Hacker Cup 2013: “Beautiful Strings” Solution | |
The problem statement is given below: | |
When John was a little kid he didn't have much to do. There was no internet, no Facebook, and no programs to hack on. So he did the only thing he could... he evaluated the beauty of strings in a quest to discover the most beautiful string in the world. | |
Given a string s, little Johnny defined the beauty of the string as the sum of the beauty of the letters in it. | |
The beauty of each letter is an integer between 1 and 26, inclusive, and no two letters have the same beauty. Johnny doesn't care about whether letters are uppercase or lowercase, so that doesn't affect the beauty of a letter. (Uppercase 'F' is exactly as beautiful as lowercase 'f', for example.) | |
You're a student writing a report on the youth of this famous hacker. You found the string that Johnny considered most beautiful. What is the maximum possible beauty of this string? | |
Input | |
The input file consists of a single integer m followed by m lines. | |
Output | |
Your output should consist of, for each test case, a line containing the string "Case #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. | |
Constraints | |
5 ≤ m ≤ 50 | |
2 ≤ length of s ≤ 500 | |
Sample Input | |
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 | |
Sample Output | |
Case #1: 152 | |
Case #2: 754 | |
Case #3: 491 | |
Case #4: 729 | |
Case #5: 646 |
If you have competed, this is not for you, Although I am helping my frndzs...:D. If you have competed then why are you seeking solutions...::D
Not the cleanest solution, but works :)
@vikashverma you think you are helping your friends, but in reality you are not. This is not a group competition
Nice one, I reckon you'll get away with it. They'll have no idea who to disqualify.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
And why have you posted a solution, publicly visible when the round is still in progress?