TopCoder 500 point problem
Problem Statement
I want a list of BioServices that are associated with each KindOfInput (such as “gene”, “DNAFragment”, “genome”, etc.). What is available from my service provider is a list of strings, each containing the name of a service followed by all the KindsOfInput it requires.
Given a String[] services, return a String[] in which each element contains a KindOfInput followed by the names of all the services that use that kind of input.
Each KindOfInput should appear exactly once in the return. Within each element of the return, the service names should be in alphabetical order, and should be separated by the 2 characters “, ” (comma space). The KindOfInput should be separated from the first service name by the 5 characters ” ==> ” (space eq eq gt space). The returned list should be in alphabetical order.
Note that all names are case-sensitive, and that “alphabetical order” refers to the ASCII ordering, where, for example, ‘Z’ precedes ‘a’.
Definition
Class:
ServiceNames
Method:
makeList
Parameters:
String[]
Returns:
String[]
Method signature:
String[] makeList(String[] services)
(be sure your method is public)
Constraints
-
services will contain between 1 and 50 elements, inclusive.
-
Each element of services will contain between 1 and 50 characters, inclusive.
-
Each element of services will contain tokens separated by a single space (‘ ‘).
-
Each token will consist of 1 or more letters (‘A’-'Z’ or ‘a’-'z’).
-
The first tokens of the elements in services will be distinct.
-
Within each element of services the KindsOfInput will be distinct.
Examples
0)
{“BLAST Genome Annotation Sensitivity”,”PING”,”X Annotation”}
Returns: {“Annotation ==> BLAST, X”, “Genome ==> BLAST”, “Sensitivity ==> BLAST” }
1)
{“PING”}
Returns: { }
There are no KindsOfInput so the return has 0 elements.
2)
{“BLAST Genome annotation Sensitivity”,”PING”,”X Annotation”,”Apple X ample”}
Returns:
{“Annotation ==> X”,
“Genome ==> BLAST”,
“Sensitivity ==> BLAST”,
“X ==> Apple”,
“ample ==> Apple”,
“annotation ==> BLAST” }
annotation and Annotation are distinct kinds of input. annotation comes later alphabetically than any name that starts with an uppercase letter.
Solution:
import java.util.*;
public class ServiceNames {
public String[] makeList(String[] services){
HashMap<String,HashSet<String>> map=new HashMap<String,HashSet<String>>();
for(String service:services){
String[] comp=service.split(” “);
if(comp.length<2) continue;
HashSet<String> list=null;
for(int i=1;i<comp.length;i++){
if(!map.containsKey(comp[i])){
map.put(comp[i], new HashSet<String>());
}
list=map.get(comp[i]);
list.add(comp[0]);
}
}
ArrayList<String> ret=new ArrayList<String>();
StringBuilder result=new StringBuilder();
String[] keys=map.keySet().toArray(new String[0]);
for(String key:keys){
result=new StringBuilder(key+” ==> “);
HashSet<String> vals=map.get(key);
String[] svals=vals.toArray(new String[0]);
if(svals.length==0) continue;
Arrays.sort(svals);
result.append(svals[0]);
for(int i=1;i<svals.length;i++){
result.append(“, “+svals[i]);
}
ret.add(result.toString());
}
String[] sret=ret.toArray(new String[0]);
Arrays.sort(sret);
return sret;
}
}
No comments yet.
Leave a comment
| Next »
-
Recent
-
Links
-
Archives
- August 2009 (1)
- July 2009 (2)
- April 2009 (4)
- March 2009 (6)
- February 2009 (5)
- January 2009 (4)
- December 2008 (3)
- November 2008 (35)
- October 2008 (20)
-
Categories
-
RSS
Entries RSS
Comments RSS