And a solution in Scala (Well nearly Java ;-))...

object AlienLanguage {

  def main(args: Array[String]) = {
    if (args.length == 0) {
      throw new IllegalArgumentException("Could not find input file in
arguments")
    }

    val input = args(0)
    val outputFile = new File((input substring (0, input lastIndexOf (".")))
+ ".out")
    val reader = Source fromFile (input) getLines
    val ar = reader nextIntArray
    val (tokens, nrWords, nrProblems) = (ar(0), ar(1), ar(2))
    val results = new Array[String](nrProblems)

    val words = (1 to nrWords).map(x => reader.trimmedLine).toList
    for (val i <- 1 to nrProblems) {
      results(i - 1) = "Case #" + i + ": " + solveProblem(reader, words)
    }
    outputFile write results
  }

  /**
   * Convert the pattern received from the aliens to a regex and match it
against the dictionary
   */
  def solveProblem(reader: Iterator[String], words: List[String]): String =
{
    val pattern = reader.trimmedLine
    val regex = pattern.replaceAll("\\(", "[").replaceAll("\\)", "]").r

    matchWords(regex, words.head, words.tail).toString
  }

  /**
   * Recursive method to sum up all the matches
   */
  def matchWords(regex: scala.util.matching.Regex, word: String, words:
List[String]): Int = {
    words.isEmpty match {
      case true => matchWord(regex, word)
      case false => matchWord(regex, word) + matchWords(regex, words.head,
words.tail)
    }
  }

  /**
   * Return 1 for a match, 0 for none
   */
  def matchWord(regex: scala.util.matching.Regex, word: String): Int = {
    regex.findFirstIn(word) match {
      case Some(x) => 1
      case None => 0
    }
  }
}


On Fri, May 6, 2011 at 7:15 AM, Seydou TAPSOBA <taps...@gmail.com> wrote:

> This is my Solution in Java!!
>
>
> import java.io.File;
> import java.io.FileNotFoundException;
> import java.io.PrintWriter;
> import java.util.ArrayList;
> import java.util.List;
> import java.util.Scanner;
>
>
> public class alien {
>
>     /**
>      * @param args
>      * @throws FileNotFoundException
>      */
>     public static void main(String[] args) throws FileNotFoundException {
>
>         Scanner in = new Scanner(new File("aliens.in"));
>         PrintWriter out = new PrintWriter(new File("aliens.out"));
>
>         String[] data = in.nextLine().split(" ");
>
>         int l = Integer.parseInt(data[0]);
>         int d = Integer.parseInt(data[1]);
>         int cas = Integer.parseInt(data[2]);
>
>         String rawWord;
>         int count = 0;
>
>
>         String[] dico = new String[d];
>
>         for (int i = 0; i < dico.length; i++) {
>             dico[i] = in.nextLine();
>         }
>
>         for (int i = 1; i <= cas; i++) {
>
>             rawWord = in.nextLine();
>
>             List<String> cases = new ArrayList<String>();
>             String mot = "";
>             char currentDictionnaryChar;
>             String letters = "";
>
>             cases = getTokens(rawWord);
>
>
>
>
>              // on traite chaque mot du dico pour vérifier si oui ou non il
> peut
>              // être construit avec la chaine reçue
>              for (int j = 0; j < dico.length; j++) {
>
>                 mot = dico[j];
>
>                 boolean found = true;
>
>                     for (int k = 0; k < l; k++) {
>
>
>                         currentDictionnaryChar = mot.charAt(k);
>
>                         letters = cases.get(k);
>                         if(letters.indexOf(""+currentDictionnaryChar) ==
> -1){
>                             found = false;
>                             break;
>                         }
>
>                     }
>                     if(found) count++;
>             } // tous les mots du dico sont traités
>
>              out.print("Case #"+ i +": "+count);
>              count = 0;
>              if(in.hasNext()) out.println();
>         }
>         out.close();
>
>
>     }
>
>    public static List<String> getTokens(String raw) {
>
>        List<String> temp = new ArrayList<String>();
>        StringBuffer rawTemp = new StringBuffer(raw);
>
>        int i = 0, j = 0;
>        while(rawTemp.capacity() != 0){
>             i = rawTemp.indexOf("(") ;
>             j = rawTemp.indexOf(")") ;
>             switch(i) {
>             case 0:
>
>                    temp.add("" + rawTemp.substring(i+1, j));
>                    rawTemp =  rawTemp.delete(i, j+1);
>                    break;
>             case -1 :
>
>                     for (int j2 = 0; j2 < rawTemp.length(); j2++) {
>                         temp.add("" + rawTemp.charAt(j2));
>                     }
>
>                       rawTemp =  rawTemp.delete(0, rawTemp.length());
>                       break;
>               default :
>
>                       for (int j2 = 0; j2 < i; j2++) {
>                           temp.add("" + rawTemp.charAt(j2));
>                     }
>                        rawTemp = rawTemp.delete(0, i);
>                         break;
>                }
>
>
>
>            rawTemp.trimToSize();
>
>         }
>        return temp;
>
>    }
>
>
>
>
>
> }
>
>
>
>
>
>
> 2011/5/6 이홍일 <sisco...@gmail.com>
>
>> You can get source code on scoreboard.
>> That is not difficult problem.
>> You can get solution if you think little more.
>>
>> 2011/5/6 Matty <matthewmcconc...@gmail.com>
>>
>>> Does anybody have a solution code for this problem written in Java?
>>> I'm stuck...
>>>
>>> Thanks in advance,
>>>
>>> Matt
>>>
>>> --
>>> You received this message because you are subscribed to the Google Groups
>>> "google-codejam" group.
>>> To post to this group, send email to google-code@googlegroups.com.
>>> To unsubscribe from this group, send email to
>>> google-code+unsubscr...@googlegroups.com.
>>> For more options, visit this group at
>>> http://groups.google.com/group/google-code?hl=en.
>>>
>>>
>>  --
>> You received this message because you are subscribed to the Google Groups
>> "google-codejam" group.
>> To post to this group, send email to google-code@googlegroups.com.
>> To unsubscribe from this group, send email to
>> google-code+unsubscr...@googlegroups.com.
>> For more options, visit this group at
>> http://groups.google.com/group/google-code?hl=en.
>>
>
>  --
> You received this message because you are subscribed to the Google Groups
> "google-codejam" group.
> To post to this group, send email to google-code@googlegroups.com.
> To unsubscribe from this group, send email to
> google-code+unsubscr...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/google-code?hl=en.
>

-- 
You received this message because you are subscribed to the Google Groups 
"google-codejam" group.
To post to this group, send email to google-code@googlegroups.com.
To unsubscribe from this group, send email to 
google-code+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/google-code?hl=en.

Reply via email to