https://code.google.com/codejam/contest/90101/dashboard#s=p0

Problem

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, LD 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

  1. 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

Large dataset

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

Sample

Input 
 
Output 
 
3 5 4
abc
bca
dac
dbc
cba
(ab)(bc)(ca)
abc
(abc)(abc)(abc)
(zyx)bc
Case #1: 2
Case #2: 1
Case #3: 3
Case #4: 0

Solution:

  1. int solve(int L, int D, vector<string>dictionary, string pattern)
  2. {
  3.  
  4. int matches = ;
  5.  
  6. for (int di = ; di < dictionary.size(); di++) {
  7. string dw = dictionary.at(di);
  8.  
  9. bool bracket = false;
  10. bool inbracket_match = false;
  11. bool word_match = true;
  12. int cci = ;
  13. for (int i = ; i < pattern.length(); i++) {
  14. char chr = pattern[i];
  15. if (chr == '(') {
  16. bracket = true;
  17. } else if (chr == ')') {
  18. bracket = false;
  19. if (!inbracket_match) {
  20. word_match = false;
  21. break;
  22. }
  23. inbracket_match = false;
  24. cci++;
  25. } else {
  26. if (bracket) {
  27. if (dw[cci] == chr) {
  28. inbracket_match = true;
  29. }
  30. } else {
  31. if (dw[cci] != chr) {
  32. word_match = false;
  33. break;
  34. }
  35. cci++;
  36. }
  37. }
  38. }
  39.  
  40. if (word_match) {
  41. matches++;
  42. }
  43.  
  44. }
  45.  
  46. return matches;
  47.  
  48. }
  49.  
  50. int main() {
  51. freopen("in.in", "r", stdin);
  52. freopen("out.out", "w", stdout);
  53.  
  54. int L, D;
  55. scanf("%d\n", &L);
  56. scanf("%d\n", &D);
  57.  
  58. int CN;
  59. scanf("%d\n", &CN);
  60. if (!CN) {
  61. cerr << "Check input!" << endl;
  62. exit();
  63. }
  64.  
  65. vector<string> dict;
  66. for (int di = ; di < D; di++) {
  67. char *word = new char[L];
  68. scanf("%s\n", word);
  69. string strword = string(word, L);
  70. dict.push_back(strword);
  71. }
  72.  
  73. for (int case_i = ; case_i <= CN; case_i++) {
  74. string pattern;
  75. cin >> pattern;
  76.  
  77. auto result = solve(L, D, dict, pattern);
  78. printf("Case #%d: %d\n", case_i, result);
  79. }
  80.  
  81. fclose(stdin);
  82. fclose(stdout);
  83. return ;
  84. }

Google Code Jam 2009 Qualification Round Problem A. Alien Language的更多相关文章

  1. Google Code Jam 2009 Qualification Round Problem C. Welcome to Code Jam

    本题的 Large dataset 本人尚未解决. https://code.google.com/codejam/contest/90101/dashboard#s=p2 Problem So yo ...

  2. Google Code Jam 2009 Qualification Round Problem B. Watersheds

    https://code.google.com/codejam/contest/90101/dashboard#s=p1 Problem Geologists sometimes divide an ...

  3. [C++]Infinite House of Pancakes——Google Code Jam 2015 Qualification Round

    Problem It’s opening night at the opera, and your friend is the prima donna (the lead female singer) ...

  4. [C++]Standing Ovation——Google Code Jam 2015 Qualification Round

    Problem It’s opening night at the opera, and your friend is the prima donna (the lead female singer) ...

  5. Google Code Jam 2014 资格赛:Problem B. Cookie Clicker Alpha

    Introduction Cookie Clicker is a Javascript game by Orteil, where players click on a picture of a gi ...

  6. Google Code Jam 2014 资格赛:Problem D. Deceitful War

    This problem is the hardest problem to understand in this round. If you are new to Code Jam, you sho ...

  7. [刷题]Google Code Jam 2017 - Round1 C Problem A. Ample Syrup

    https://code.google.com/codejam/contest/3274486/dashboard Problem The kitchen at the Infinite House ...

  8. Google Code Jam 2009, Round 1C C. Bribe the Prisoners (记忆化dp)

    Problem In a kingdom there are prison cells (numbered 1 to P) built to form a straight line segment. ...

  9. Google Code Jam 2014 资格赛:Problem C. Minesweeper Master

    Problem Minesweeper is a computer game that became popular in the 1980s, and is still included in so ...

随机推荐

  1. 42、和为S的两个数字

    一.题目 输入一个递增排序的数组和一个数字S,在数组中查找两个数,是的他们的和正好是S,如果有多对数字的和等于S,输出两个数的乘积最小的. 二.解法 import java.util.ArrayLis ...

  2. FPGA与CPLD的概念及其区别

    一.FPGA与CPLD的基本概念 1.CPLD CPLD主要是由可编程逻辑宏单元(LMC,Logic Macro Cell)围绕中心的可编程互连矩阵单元组成,其中LMC逻辑结构较复杂,并具有复杂的I/ ...

  3. juery下拉刷新,div加载更多元素并添加点击事件(二)

    buffer.append("<div class='col-xs-3 "+companyId+"' style='padding-left: 10px; padd ...

  4. C语言实现int转换string

    #include <stdio.h> #include <stdlib.h> #include <string.h> int string2int(const ch ...

  5. LightOJ 1369 Answering Queries(找规律)

    题目链接:https://vjudge.net/contest/28079#problem/P 题目大意:给你数组A[]以及如下所示的函数f: long long f( int A[], int n  ...

  6. Ceph 时钟偏移问题 clock skew detected 解决方案--- 部署内网NTP服务

    告警:HEALTH_WARN clock skew detected on mon.ip-10-25-195-6; 8 requests are blocked > 32 sec; Monito ...

  7. Android项目包命名规则是怎样的?

    com.example.app.activity | Activity 类com.example.app.widget | 自定义的小UIcom.example.app.db | 数据库相关操作com ...

  8. [实战]MVC5+EF6+MySql企业网盘实战(15)——逻辑重构2

    写在前面 上篇文章修改文件上传的逻辑,这篇修改下文件下载的逻辑. 系列文章 [EF]vs15+ef6+mysql code first方式 [实战]MVC5+EF6+MySql企业网盘实战(1) [实 ...

  9. PyQt5点击按钮产生新窗体

    import sys from PyQt5.QtWidgets import QApplication,QWidget from form1 import Ui_Form1 from form2 im ...

  10. docker-compose RabbitMQ与Nodejs接收端同时运行时的错误

    首先讲一下背景: 我现在在开发的一个项目,需要运行RabbitMQ和Nodejs接收端(amqplib库),但是在Nodejs接收端运行时,无法连接至RabbitMQ端,经常提示说 connect E ...