This is in JAVA, by the way…
This is as easy as it sounds. They give you a dictionary of words, and a few misspelled ones, and you have to find the closest spelling to the word. All words are the same length, so you can simply measure the total number of similar letters within the words (that’s how the problem tells you to measure similarity, i.e. the word with the most common letters is the correct spelling).
[size=5]USAGE[/size]
The first 5 represents the dictionary length, the 7 is the number of test cases. The next five words compose the dictionary, and the next seven compose the test cases.
5 7
usually
rainbow
product
science
medical
produce
scyence
radical
ideally
bawling
project
painful
And the corrected words…
product
science
medical
usually
rainbow
product
rainbow
[size=5]SOURCE[/size]
import java.util.*;
import java.io.*;
public class pr92 {
public static void main (String args[]) throws IOException {
Scanner scanner = new Scanner (new FileReader("pr92.txt"));
int dictionaryLength = scanner.nextInt();
ArrayList<String> words = new ArrayList<String>();
int testCases = scanner.nextInt();
for (int i=0; i<dictionaryLength; i++) {
words.add(scanner.next());
}
for (int i=0; i<testCases; i++) {
String misspelled = scanner.next();
int correct = -1;
int mostSimilar = 0;
for (int j=0; j<dictionaryLength; j++) {
if (pr92.similarLetters(words.get(j), misspelled)>mostSimilar) {
correct = j;
mostSimilar=pr92.similarLetters(words.get(j), misspelled);
}
}
System.out.println(words.get(correct));
}
}
public static int similarLetters (String first, String second) {
int total = 0;
for (int i=0; i<first.length(); i++) {
if (first.charAt(i)==second.charAt(i))
total++;
}
return total;
}
}
Keep in mind these contests are all about speed.
This uses Java 1.5.
Hope you like it.