//Google Code Jam 2009, Qualification Round A Alien Language
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
//Google Code Jam
public class GoogleCode1 {
    private static MyScanner in;
    private static PrintStream out;
    private static void solve() throws IOException
    {
        L = in.nextInt();
        D = in.nextInt();
        int N = in.nextInt();
        words = new char[D][L];
        for (int i = 0; i < D; i++) {
            words[i] = in.nextString().toCharArray();
        }
        int C = N;
        for (int i = 0; i < C; i++) {
            out.print("Case #" + (i + 1) + ": ");
            solveCase();
        }
    }
    static int L;
    static int D;
    static char[][] words;
    private static void solveCase() throws IOException {
        String pat = in.nextString();
        HashSet<Character>[] token = new HashSet[L];
        boolean isgroup = false;
        int idx = 0;
        for (int i = 0; i < pat.length(); i++) {
            char c = pat.charAt(i);
            if (c == '(') {
                isgroup = true;
                token[idx] = new HashSet<Character>();
            }
            else if (c == ')') {
                isgroup = false;
                idx++;
            }
            else {
                if (isgroup) {
                    token[idx].add(c);
                }
                else {
                    token[idx] = new HashSet<Character>();
                    token[idx].add(c);
                    idx++;
                }
            }
        }
        int cnt = 0;
        for (int i = 0; i < D; i++) {
            boolean valid = true;
            for (int j = 0; j < L; j++) {
                if (!token[j].contains(words[i][j])) {
                    valid = false;
                    break;
                }
            }
            if (valid)
                cnt++;
        }
        out.println(cnt);
    }
    public static void main(String[] args) throws IOException {
        // helpers for input/output
        boolean usingFileForIO = true;
        if (usingFileForIO) {
            // using input.txt and output.txt as I/O
            in = new MyScanner("E:\\zin.txt");
            out = new PrintStream("E:\\zout.txt");
        }
        else {
            in = new MyScanner();
            out = System.out;
        }
        solve();
    }
    // =====================================
    static class MyScanner {
        Scanner inp = null;
        public MyScanner() throws IOException
        {
            inp = new Scanner(System.in);
        }
        public MyScanner(String inputFile) throws IOException {
            inp = new Scanner(new FileInputStream(inputFile));
        }
        public int nextInt() throws IOException {
            return inp.nextInt();
        }
        public long nextLong() throws IOException {
            return inp.nextLong();
        }
        public double nextDouble() throws IOException {
            return inp.nextDouble();
        }
        public String nextString() throws IOException {
            return inp.next();
        }
        public String nextLine() throws IOException {
            return inp.nextLine();
        }
    }
}
No comments:
Post a Comment