Skip to content

Instantly share code, notes, and snippets.

@phaniram
Created May 2, 2012 00:28
Show Gist options
  • Save phaniram/2572608 to your computer and use it in GitHub Desktop.
Save phaniram/2572608 to your computer and use it in GitHub Desktop.
Codechef-MayChallenge-TWSTR -Accepted
package com.cyp.codechef;
/**
* @author Cypronmaya -Codechef- Accepted
*/
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;
class TWSTR {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
HashMap<String, Integer> hm = new HashMap<String, Integer>();
for (int i = 0; i < N; i++) {
String Si = sc.next();
int Vi = sc.nextInt();
hm.put(Si, Vi);
}
int Q = sc.nextInt();
for (int i = 0; i < Q; i++) {
HashMap<String, Integer> res = new HashMap<String, Integer>();
ValueComparator bvc = new ValueComparator(res);
TreeMap<String, Integer> sorted_map = new TreeMap<String, Integer>(
bvc);
String Qi = sc.next();
int count = 0;
for (String s : hm.keySet()) {
if (s.startsWith(Qi)) {
res.put(s, hm.get(s));
count++;
}
}
if (count != 0) {
sorted_map.putAll(res);
System.out.println(sorted_map.firstKey());
} else {
System.out.println("NO");
}
}
}
}
class ValueComparator implements Comparator {
Map base;
public ValueComparator(Map base) {
this.base = base;
}
public int compare(Object a, Object b) {
if ((Integer) base.get(a) < (Integer) base.get(b)) {
return 1;
} else if ((Integer) base.get(a) == (Integer) base.get(b)) {
return 0;
} else {
return -1;
}
}
}
@phaniram
Copy link
Author

phaniram commented May 2, 2012

Chef Jessie has a lot of recipes with her (N). She often remembered the starting few characters of the recipe and forgot the rest. As all the great chefs do, Jessie also numbered the recipes depending on the priority. So, given the list of recipes along with their priorities answer Jessie’s queries.

Jessie’s queries are as follows:
She gives you the first few characters of a recipe; you have to print the complete recipe with the highest priority.

Note:
Every recipe has a unique priority

Input
First line contains an integer N - the number of recipes.
Followed by N strings Si along with an integer each Vi.
Si stands for the recipe and Vi for the priority.
It is followed by an integer Q - the number of queries.
Followed by Q strings Qi.
Each string Si, Qi contain only lowercase Latin alphabets ('a' - 'z') and '-'.

Output

Q – lines, each contain the answer for each of the query.
If for a query no recipe matches print "NO". (Without quotes)

Constraints:

0 <= N <= 1000
0 <= Q <= 1000
-10^9 <= Vi <= 10^9
1 <= |Si| <= 1000 (length of Si)
1 <= |Qi| <= 1000 (length of Qi)

Example
Input:
4
flour-with-eggs 100
chicken-ham -10
flour-without-eggs 200
fish-with-pepper 1100
6
f
flour-with
flour-with-
c
fl
chik

Output:
fish-with-pepper
flour-without-eggs
flour-with-eggs
chicken-ham
flour-without-eggs
NO

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment