题目如下:
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)就是要求的结果。
具体代码如下:
import sys
f = open(sys.argv[1])
L, D, N = [int(i) for i in f.readline().split()]
index = [{} for i in range(L)]
for i in range(D):
word = f.readline().strip()
offset = 0
for l in word:
if l not in index[offset]:
index[offset][l] = set()
index[offset][l].add(word)
offset += 1
for i in range(1, N+1):
testcase = f.readline().strip()
state = False
offset = 0
result = None
for l in testcase:
if l == '(':
state = True
s = set()
elif l == ')':
state = False
offset += 1
if result is None:
result = s
else:
result &= s
del s
else:
wordset = index[offset].get(l, set())
if state :
s |= wordset
else:
if not wordset: #wordset is an empty set
print("Case #%d: 0" % i)
break
if result is None:
result = wordset.copy()
else:
result &= wordset
if not result:
print("Case #%d: 0" % i)
break
offset += 1
else:
print("Case #%d: %d" % (i, len(result)))
f.close()
分享到:
相关推荐
自己写的源码,希望各位看官指出不足之处,好交流进步
《Code Jam 2021资格赛:编程挑战解析》 Code Jam是由Google主办的一项全球性的编程竞赛,每年都会吸引众多编程爱好者参与。2021年的资格赛中,参赛者们面临了一系列精心设计的算法问题,旨在测试他们的逻辑思维、...
Google Code Jam通常包含多个 rounds,从资格赛(Qualification Round)开始,然后是初赛(Round 1)、复赛(Round 2)和决赛(World Finals)。每个round都会提供几个编程题目,参赛者需要在限定的时间内提交他们的...
2020-1 Bluetooth Qualification Program - Test Case Reference List(TCRLs)_2020-01-GATT-Audio_2020-BLP1.1-DTP-PAMP-ATP-CTES-MICP-CCP-TBS-MCP-MCS.zip.zip
2020-1 Bluetooth Qualification Program - Test Case Reference List(TCRLs)_2020-01-GATT-Audio_2020-BLP1.1-DTP-PAMP-ATP-CTES-MICP-CCP-TBS-MCP-MCS.zip.7z
ZVEI PCN-Delta-Qualification-Matrix-ZVEI-5_0_16.xlsm
- `Google-Hash-Code-2020-Online-Qualification-Round-master`:这是项目的主要根目录,包含所有与比赛相关的源代码和资源文件。 - `src`:源代码目录,可能包含Java类和其他编程语言的源文件。 - `main/java`:...
5. **失效模式和效应分析**:识别潜在的故障模式,分析其可能的原因和影响,以采取预防措施。 6. **应力筛选**:在产品制造过程中应用应力筛选,如高温老化测试,以提前暴露潜在的问题。 7. **兼容性和互换性**:...
- **数据收集与分析**:阐述了如何收集和分析工艺数据,以确保工艺符合标准。 - **质量控制**:强调了质量管理系统的重要性,包括工艺监控、统计过程控制(SPC)和持续改进。 - **认证流程**:详细介绍了从初步评估...
DO-178C对应的工具鉴定标准,DO-330.DO-330 Software Tool Qualification Considerations Mar 2012
应力测试过程通常包括以下步骤:测试计划定义,环境室设置,应力测试执行,性能评估,数据分析和报告。通过应力测试驱动的鉴定,可以验证IC在极端工作条件下的可靠性,并帮助制造商和设计者了解和改善其产品以适应...
Stress-Test-Driven Qualification of Integrated Circuits
总结来说,"AEC - Q100 Rev H - QTP Template Qualification Test Plan - 完整英文电子版(4页)"是一个关于汽车电子组件可靠性测试的详细规划文档,它指导着制造商如何按照AEC-Q100 Rev H标准进行组件的测试和验证...
Qualification Rounds - 2015: B (Python) - 2016: A, C (C++) - 2017: A, B (C++) 3. Numerical Analysis Algorithms --- C - Newton-Raphson Root Finding Method - Simpson's Integration - Trapezoidal ...
完整英文版 IEC 61730-1:2023 Photovoltaic (PV) module safety qualification - Part 1:Requirements for construction(光伏(PV)组件安全资质第1部分:施工要求)。 IEC 61730-1:2023 规定并描述了光伏 (PV) ...
这份QTP(Qualification Test Plan)模板是用于指导制造商按照AEC-Q104标准来设计和执行器件的资格验证测试计划。在5页的完整英文电子版文档中,我们可以预期涵盖以下几个关键知识点: 1. **标准介绍**:AEC-Q104...
2015年CS游戏资格赛该目录包含 DCI 获得 2015 CS Games 资格所需的所有详细信息。介绍如果您接受它,您的任务将是开发一个比赛日程管理器。 每年,资格赛的组织者都会花费大量时间来尝试制定对所有参与者有吸引力的...
完整英文电子版 AEC - Q102 Rev A CDC Template Certificate of Design, Construction and Qualification (CDC 模板设计、实施和资格证书 )。
JEDEC JEP172A:2015(R2020) Discontinuing Use of the Machine Model for Device ESD Qualification JEDEC JEP172A:2015(R2020)是 JEDEC 固态技术协会发布的一项标准,标题为 Discontinuing Use of the Machine...
JEDEC JESD47K-2018 Stress-Test-Driven Qualification of Integrated Circuits JEDEC(Joint Electron Devices Engineering Council,联合电子设备工程委员会)是一家非营利性技术标准组织,旨在推动半导体和电子...