Alien Language

The task

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 

1 ≤ L ≤ 15
1 ≤ D ≤ 5000
1 ≤ N ≤ 500

Input

3 5 4
abc
bca
dac
dbc
cba
(ab)(bc)(ca)
abc
(abc)(abc)(abc)
(zyx)bc

Output

Case #1: 2
Case #2: 1
Case #3: 3
Case #4: 0

Thinking

Very nice description of task, but the idea itself is simpier a bit. Test rows are looking very similar to just regex. The only difference is that for regex we need to split variadic results by pipe.
Look,
(abc) in terms of regex will be represented as (a|b|c)
other parts are the same.
So the easiest way is to turn input tests into regex format.
I would write it as follows, in Golang

Code

package algos

import (
"bufio"
"bytes"
"errors"
"fmt"
"log"
"regexp"
"strconv"
"strings"
)

const (
min  = 1
maxL = 15
maxD = 5000
maxN = 500
)

//Solve solves this task
func Solve() {
input := `3 5 4
abc
bca
dac
dbc
cba
(ab)(bc)(ca)
abc
(abc)(abc)(abc)
(zyx)bc`
scanner := bufio.NewScanner(strings.NewReader(input))
var l, d, n, dInd, nInd int
var samples, tests []string
var err error

// read input and init data
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if line == "" {
continue
}

if l == 0 && d == 0 && n == 0 {
nums := strings.Split(line, " ")
if len(nums) != 3 {
log.Println("Wrong input numbers L D N in first line")
break
}

l, err = parseNumCheckLimits(nums[0], min, maxL)
if err != nil {
log.Fatal(err)
}
d, err = parseNumCheckLimits(nums[1], min, maxD)
if err != nil {
log.Fatal(err)
}
n, err = parseNumCheckLimits(nums[2], min, maxN)
if err != nil {
log.Fatal(err)
}
samples = make([]string, d, d)
tests = make([]string, n, n)
} else if dInd < d {
samples[dInd] = line
dInd++
} else if nInd < n {
tests[nInd] = line
nInd++
}
}
log.Println("Input parsed:", l, d, n, samples, tests)

// execute tests
for i, test := range tests {
executeTest(i+1, l, test, samples)
}
}

func parseNumCheckLimits(d string, min, max int) (int, error) {
dParsed, err := strconv.Atoi(d)
if err != nil {
return -1, errors.New("Wrong input for " + d)
}
if dParsed < min || dParsed > max {
return -1, fmt.Errorf("%d is out of bounds, must be in range [%d %d] inclusive", dParsed, min, max)
}
return dParsed, nil
}

func executeTest(i, l int, test string, samples []string) {
var matches int
rx, _ := regexp.Compile(turnTestIntoRegex(test))
for _, sample := range samples {
if rx.MatchString(sample) {
matches++
}
}
log.Printf("Case #%d: %d", i, matches)
}

func turnTestIntoRegex(input string) string {
var buffer bytes.Buffer
var insideGroup bool
var prevRune rune
for _, s := range input {
if s == '(' {
insideGroup = true
} else if s == ')' {
insideGroup = false
} else if insideGroup && prevRune != '(' {
buffer.WriteString("|")
}
buffer.WriteString(string(s))
prevRune = s
}
return buffer.String()
}

Popular Posts