[code jam 2009试题分析]Qualification Round - Alien Language
来源:互联网 发布:矩阵制度的直销公司 编辑:程序博客网 时间:2024/05/16 12:22
题目如下:
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, 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
Large dataset
1 ≤ L ≤ 15
1 ≤ D ≤ 5000
1 ≤ N ≤ 500
Sample
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
这道题本身没有什么难度,唯一需要注意的就是速度了。
我的解法是找到每一个位(token)对应的单词的集合,然后将所有的集合做交集运算,最终结果集合的元素数量就是所求的结果。
为了快速找到每一个位所对应的单词集合,先便利单词的列表,为每一位建立一个从字母到单词集合的索引。
Index是一个包含了L个map容器的序列,每一个map容器对应一个位。每个map容器包含了从字母x到该位置的字母为x的映射。
这样从模式到单词可以简单的变换成集合操作。
如模式(ab)b(ac)对应的就是
wordSet = (index[0][a] | index[0][b]) & index[1][b] & (index[2][a] | index[2][c])
其中”|”是并集操作,”&”是交集操作。
Len(wordSet)就是要求的结果。
具体代码如下:
- [code jam 2009试题分析]Qualification Round - Alien Language
- [Code Jam] Alien Language
- [Code jam 2010]Qualification Round题目分析
- 2009 Google Code Jam Qualification Round 题解
- Google Code Jam 2009预选赛第一题----Alien Language
- Google code jam: Problem A. Alien Language
- Google Code Jam Notes - Alien Language - Java
- Google Code Jam 2011 Qualification Round ProblemA
- Google Code Jam 2012 Qualification Round
- Google Code Jam Qualification Round 2012
- Google Code Jam在线测试题目--Alien Language
- Reverse-GOOGLE CODE JAM AFICA 2010 Qualification Round
- T9 Spelling-GOOGLE CODE JAM AFICA 2010 Qualification Round
- Store Credit-GOOGLE CODE JAM AFICA 2010 Qualification Round
- ACM-GOOGLE CODE JAM 2011 Qualification Round 2011(总结)
- Google Code Jam 2010 Qualification Round 2012 Problem B && C
- Google Code Jam 2014 Qualification Round 2014 A B D
- Google Code Jam 2014-Qualification Round-Problem C. Minesweeper Master
- flex3 视频播放器
- JavaScript去除空格的三种方法 (trim)
- RAC知识更新之-RAC下重做日志组(摘自文平书)
- win32 Application 模板 建立 MFC程序
- SQL安装问题--无法获取ASPNET账户的系统账户信息
- [code jam 2009试题分析]Qualification Round - Alien Language
- windows ce / windows mobile
- MySQL Partition
- RAC知识更新之-RAC下管理归档日志+修改VIP(摘自文平书)
- Android Activity 祥解
- Sample socket communication program
- dos 命令 大全
- RAC知识更新之-管理ASM+检索ocr信息+禁用RAC自动启动(摘自文平书)
- 某些人