Original title address: https://code.google.com/codejam/contest/90101/dashboard#s=p0
Title Description:
problemafter years of study, scientists at Google Labs has discovered an alien language transmitted from a faraway planet. The alien language is very unique in that every word consists of exactly L lowercase letters. Also,There is exactly D words in this language. Once the dictionary of all the words in the alien language was built, the next breakthrough is to discover that the aliens has been transmitting messages to Earth for the past decade. Unfortunately, these signals is weakened due to the distance between our both planets and some of the words May is misinte Rpreted. In order to help them decipher these messages,The scientists has asked you to devise an algorithm that would determine the number of possible interpretations for a Given pattern. A pattern consists of exactly L tokens. Each token was either a single lowercase letter (the scientists was very sure that's the letter) or a group of unique Lowercase letters surrounded by parenthesis (and). For example: (AB) d (DC) means the first letter is either A or B, the second letter was definitely d and the last letter was either D or C. Therefore, the pattern (AB) d (DC) can stand for either one of these 4 Possibilities:add, ADC, BDD,BDC. Inputthe first line of input contains3 integers, L, D and N separated by a space. D lines follow, each containing one word of length L. These is the words that is known to exist in the alien language. N test Cases then follow,Each of the own line and each consisting of a pattern as described above. You may assume this all known words provided is unique. Outputfor each test case,outputcase #X: Kwhere X is the test Case number, starting from 1,And K indicates how many words in the alien language match the pattern. Limitssmall DataSet1≤l≤101≤d≤251≤n≤10Large DataSet1≤l≤151≤d≤50001≤n≤500sampleinput Output3 5 4ABCBCADACDBCCBA (AB) (BC) (CA) ABC (ABC) (ABC) (ZYX) Bccase #1:2Case #2:1Case #3:3Case #4:0
Algorithm code:
PackageCode;ImportJava.io.File;ImportJava.io.FileInputStream;Importjava.io.FileNotFoundException;ImportJava.io.InputStream;ImportJava.io.PrintWriter;ImportJava.util.Scanner;ImportJava.util.regex.Matcher;ImportJava.util.regex.Pattern; Public classSolution { Public Static voidMain (String args[])throwsexception{InputStream in=NewFileInputStream (NewFile ("Googlecodejam/a-large-practice.in")); File out=NewFile ("Googlecodejam/a-large-practice.out"); Output (in, out); System.out.println ("You can see the results."); In.close (); } /*** The algorithm makes full use of Java regular expressions to match strings, thus making it simple *@paramIn data input stream *@paramdata output stream for out saved results *@throwsFileNotFoundException*/ Public Static voidOutput (InputStream in,file out)throwsfilenotfoundexception{intL,d,n; Scanner Reader=NewScanner (in); L=Reader.nextint (); D=Reader.nextint (); N=Reader.nextint (); //The following three lines are printed in the console to prompt the user that the program is running correctly or notSystem.out.println ("l=" +m); System.out.println ("D=" +D); System.out.println ("N=" +N); String [] Pool=NewString[d]; Reader.nextline (); //L,d,n are all in the same line, after reading N, the rest of the newline is still in the input buffer, to use the nextline () function to remove it for(inti=0;i<d;i++) {Pool[i]=reader.nextline ();//read all the strings in the dictionary} printwriter writer=NewPrintWriter (out); intMatchcount=0; Pattern Pleft=pattern.compile ("\ \ ("); Pattern Pright=pattern.compile ("\ \)"); for(inti=1;i<=n;i++) {String patstr=reader.nextline ();//read a ciphertext, the following processing will process it into patternMatcher mleft=Pleft.matcher (PATSTR); String Temp=mleft.replaceall ("\\[");//change the opening parenthesis to the left parenthesisMatcher mright=Pright.matcher (temp); String Temp2=mright.replaceall ("\ \]");//Change the closing parenthesis to the right parenthesis, at which point the temp2 is the desired pattern stringPattern pattern=Pattern.compile (TEMP2); MatchCount=0; for(intj=0;j<d;j++) {Matcher Matcher=Pattern.matcher (Pool[j]); if(Matcher.find ()) {if(Matcher.start () ==0&&matcher.end () = =(Pool[j].length ())) {MatchCount++; }}} System.out.println ("Case #" +i+ ":" +matchcount);//displayed in the console, prompting the program to be runningWriter.println ("Case #" +i+ ":" +MatchCount); } writer.close (); }}
Java Learning Note (5)----use regular expressions to solve Google Code Jam Qualification2009 Alien Language