SortMe: Difference between revisions
Jump to navigation
Jump to search
imported>Pbc8 Created page with "== Introduction == The problem essentially asks you to sort a list of string based on a new alphabet. The input of the problems gives you the number of strings to be sorted, t..." |
imported>Pbc8 No edit summary |
||
Line 47: | Line 47: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
[[category:iCPC Problems]] | [[category:iCPC Problems]] | ||
[[category:implementation | [[category:implementation Easy]] | ||
[[category:algorithm Easy]] | [[category:algorithm Easy]] | ||
[[category: | [[category:comparator]] | ||
Revision as of 02:59, 2 December 2015
Introduction
The problem essentially asks you to sort a list of string based on a new alphabet. The input of the problems gives you the number of strings to be sorted, the new alphabetical order in the form of a string, and the strings that are to be sorted.
Algorithm
Solution - Java
import java.io.*;
import java.util.*;
public class SortMe{
private static class Comp implements Comparator<String>{
private String alpha;
public Comp(String s){
alpha = s;
}
public int compare(String s1, String s2){
for(int i = 0;i < Math.min(s1.length(), s2.length());i++){
if(s1.charAt(i) != s2.charAt(i)){
return alpha.indexOf(s1.charAt(i)) - alpha.indexOf(s2.charAt(i));
}
}
return s1.length() - s2.length();
}
}
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int year = 1;
while(true){
int n = in.nextInt();
if(n == 0){
break;
}
String[] str = new String[n];
Comp comp = new Comp(in.next());
for(int i = 0;i < n;i++){
str[i] = in.next();
}
Arrays.sort(str, comp);
System.out.println("year "+year++);
for(String s : str){
System.out.println(s);
}
}
}
}