Question:
After years of study, scientists at Google Labs have
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 are exactly D words in this language.
Once the dictionary of all the words in the alien language
was built, the next breakthrough was to discover that the aliens have been
transmitting messages to Earth for the past decade. Unfortunately, these
signals are weakened due to the distance between our two planets and some of
the words may be misinterpreted. In order to help them decipher these messages,
the scientists have asked you to devise an algorithm that will determine the
number of possible interpretations for a given pattern.
A pattern consists of exactly L tokens. Each token
is either a single lowercase letter (the scientists are very sure that this is
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 is definitely d and the last letter is either d or c. Therefore,
the pattern (ab)d(dc) can stand for either one of these 4 possibilities: add,
adc, bdd, bdc.
Input
The first line of input contains 3 integers, L, D and N separated
by a space. D lines follow, each containing one word of length L.
These are the words that are known to exist in the alien language. N test
cases then follow, each on its own line and each consisting of a pattern as
described above. You may assume that all known words provided are unique.
Output
For each test case, output
Case #X: K
where X is the test case number, starting from 1,
and K indicates how many words in the alien language match the
pattern.
Limits
Small dataset
1 ≤ L ≤ 10
1 ≤ D ≤ 25
1 ≤ N ≤ 10
1 ≤ D ≤ 25
1 ≤ N ≤ 10
Large dataset
1 ≤ L ≤ 15
1 ≤ D ≤ 5000
1 ≤ N ≤ 500
1 ≤ D ≤ 5000
1 ≤ N ≤ 500
Sample
Input Output
3 5 4
abc Case #1: 2
3 5 4
abc Case #1: 2
bca Case #2: 1
dac Case #3: 3
dbc Case #4: 0
cba
(ab)(bc)(ca)
abc
(abc)(abc)(abc)
(zyx)bc
dac Case #3: 3
dbc Case #4: 0
cba
(ab)(bc)(ca)
abc
(abc)(abc)(abc)
(zyx)bc
Tricky Part
1. make your own rex parser
2. make a manager who manages all test cases
3. checker which ueses rex parser and tell whether
Try yourself First..:)
Try yourself First..:)
Solution
1. make grid of store pattern in 2d array as pattern[number of letters][26]
2. make the dictionary which contain all alien language words.
package alienLanguage; public class AlienLanguageDictionary { char dic[][]; int words; int letters; public AlienLanguageDictionary(char[][] ch) { super(); this.dic = ch; words = ch.length; letters = ch[0].length; } }
package alienLanguage; import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.Scanner; public class AlienLanguageManager { private static Scanner s; static File f1;// This class is used for creation of files and directories, static File f2; // file searching, file deletion etc. static int length;// max length of word. static int lines; // no.of length static int patterns; // no.of patterns static char dic[][]; private static PrintWriter pw; public static void main(String[] args) { f1 = new File("A-large-practice.in"); f2 = new File("A-large-practice.out"); // below code might throw anexception try { s = new Scanner(f1); pw = new PrintWriter(f2); length = s.nextInt(); lines = s.nextInt(); patterns = s.nextInt(); int i = 0;// for indexing dic = new char[lines][length]; System.out.println(s.nextLine()); while (lines-- != 0)// AlienLanguageDictionary is under construction { dic[i++] = s.nextLine().toCharArray(); } AlienLanguageDictionary ald = new AlienLanguageDictionary(dic); int num = 1; while (patterns-- != 0) { String str = s.nextLine(); int d = new AlienLanguageChecker(str, ald).countWords(); pw.println("Case #" + num++ + ": " + d); System.out.println(d); } s.close(); pw.close(); } catch (FileNotFoundException e) { System.out.println("FileNotFoundException "); } } }
package alienLanguage; public class AlienLanguageChecker { AlienLanguageDictionary ald; char[][] pattern;// String inputpattern; public AlienLanguageChecker(String inputpattern, AlienLanguageDictionary ald) { super(); this.inputpattern = inputpattern; this.ald = ald; char ch[] = inputpattern.toCharArray(); pattern = new char[ald.letters][26]; boolean flag = true;// looking for '(' int pi = 0, pj = 0, bi = 0; for (int i = 0; i < ch.length; i++) { if (flag && ch[i] == '(') { flag = false; } else if (!flag && ch[i] == ')') { flag = true; pi++; pj = 0; bi = i; } else if (inputpattern.substring(bi, i).contains("(")) { pattern[pi][pj++] = ch[i]; } else { pj = 0; pattern[pi++][pj] = ch[i]; } } } public int countWords() { int count = 0; for (int i = 0; i < ald.dic.length; i++) { if (Checker(ald.dic[i])) count++; } return count; } public boolean Checker(char ch[]) { int i = 0; boolean flag = true; for (char a[] : pattern) { flag = false; for (char b : a) { if (ch[i] == b) { flag = true; i++; break; } } if (!flag) return false; } return true; } }