Google Code Jam在线測试题目--Alien Language

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

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

思路L:对于每个pattern,字典中每个词与它匹配。

代码:

/*2014-10-16
*zhuangweibiao
*/
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
bool match(string target, string pattern)
{
	int index = 0;
	for(int i = 0; i < pattern.length(); ++i)
	{
		if(pattern[i] == ‘(‘)
		{
			bool found = false;
			while(pattern[++i] != ‘)‘)
			{
				if(pattern[i] == target[index])
					found = true;
			}
			if(!found)
				return false;
			index++;
		}
		else
		{
			if(pattern[i] != target[index])
				return false;
			index++;
		}
	}
	return true;
}
int main()
{
	ifstream ifile("A-large-practice.in");
	ofstream ofile("match2.txt");
	int L, D, N;
	string str[5001];
	ifile >> L >> D >> N;
	for(int i = 0; i < D; ++i)
		ifile >> str[i];
	string pattern;
	for(int i = 0; i < N; ++i)
	{
		ifile >> pattern;
		int count = 0;
		for(int j = 0; j < D; ++j)
		{
			if(match(str[j], pattern))
				count++;
		}
		ofile << "Case #" << i + 1 << ": " << count << endl;
	}
	return 0;
}
时间: 2024-10-25 06:33:25

Google Code Jam在线測试题目--Alien Language的相关文章

Google Code Jam 2014 Round2

Google Code Jam Round2 晚上10点开始的,开始google一直上不去,然后开了个vpn就行了. 说说我的情况吧. P1就是某年noip普及组纪念品分组,贪心. p2是说给你一个排列,然后每次可以交换两个相邻的数,使得最后序列变为先上升后下降的样子.(n<=1000) 一开始题目看错了,以为是交换任意两个,然后愣了半天不会,去写后两题暴力了. (话说我Round1Cp1题目也看错了害的我逗了好久) 后来突然发现是adjacent elements...(<论英语学习的重要性

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. Cells number i and i+1 are adjacent, and prisoners in adjacent cells are called "neighbours." A wall with a window separates adjacent cells, and

Google Code Jam 2012 Practice - Store Credit

Problem You receive a credit C at a local store and would like to buy two items. You first walk through the store and create a list L of all available items. From this list you would like to buy two items that add up to the entire value of the credit

Google Code Jam在线测试题目--Alien Language

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

Java学习笔记(5)----使用正则表达式解决Google Code Jam Qualification2009赛题 Alien Language

原题地址: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

【微软2014实习生及秋令营技术类职位在线測试】题目2 : K-th string

时间限制:10000ms 单点时限:1000ms 内存限制:256MB Description Consider a string set that each of them consists of {0, 1} only. All strings in the set have the same number of 0s and 1s. Write a program to find and output the K-th string according to the dictionary

Google Code Jam 2014 Round 2回顾和分析

回顾 比赛开始网络就一直在抽风,不知道宿舍网渣还是有人攻击服务器.刷了n遍n久刷出了题目.提交A小case的时候眼睁睁看着时间过去,却提交不上,这破网.最后A题B题还是解决了,C题扫了一眼,读都没读,就奔D题去了,因为我看到了熟悉的trie这个单词,加之看到小case只有9分,判断小case应该比较容易.前面因为网络浪费了很多时间,加之从未编过trie的程序,只能临时上网翻书去学,最后D小这个可以很容易暴力解的问题也没编完. 最终的rank是13xx,考虑到在这次GCJ之前从未接触过编程竞赛,而

Google Code Jam Round 1A 2015 Problem B. Haircut 二分

Problem You are waiting in a long line to get a haircut at a trendy barber shop. The shop has B barbers on duty, and they are numbered 1 through B. It always takes the kth barber exactly Mk minutes to cut a customer's hair, and a barber can only cut

N球M盒问题 &amp; Password Attacker google code jam

第一个题目本质是个动归dp问题.但是分析dp的方程使用了组合数学中的N求M盒问题. 先来看下N球M盒的经典问题. 给定N个相同的球,放入M个不同的盒子中,要求每个盒子必须非空,求组合数. 假设Xi为第i个盒子中的放入的小球数,则方程 X1+X2+X3+...+Xm=N, 其实抽象成数学问题,就是求这个M元1次方程的正整数解的组数. 插板法分析: N个小球排成一行,插入M-1个板,只能在小球中间插,而且板与板之间必须有球 分析到这里,聪明的大家肯定一看就知道是个N-1个空隙选M-1个来放入板,Cm