Google Code Jam Qualification Round 2009 (2)

広告

A問題: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, 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

コメント

英語を読むのに苦労した。おぼろげに意味はわかるけど細かいところがわからない。いくつか仮説を立てて、サンプル問題と合致するか検証してなんとかルールをつかむ。

3 5 4の最初の3は3文字で構成されますよという意味。次の5は5つ単語が出ますよという意味。そして4は続いて4つパターンが出ますよという意味。カッコつきのパターンは、例えば(ab)ならaかbという意味になる。(zyx)bcは、zbcか、ybcか、xbcとなる。

Case #1は(ab)(bc)(ca)で表現されるパターンにいくつ単語がマッチするか。abcとbcaは作れるので2つが解。

作戦

パターンがカッコつきと、つかないのがあるので統一することにする。例えばパターンabcは(a)(b)(c)と考える。全てのパターンを登録して、1文字ずつ合致するものを調べていく。面倒くさいことを考えずに、ダーティなコードをごりごり書く。

#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <string>

using namespace std;


class pattern_match
{
public:
	pattern_match( string in );
	~pattern_match(){};
	
	bool match( string in );

private:
	int num;
	string character[15];
	
};



pattern_match::pattern_match( string in )
{
	int index = 0;
	
	for( int i = 0; i < in.length(); i++ )
	{
		char c[2];
		c[1] = '';
		c[0] = in[i];
		
		// is alphabet?
		if( 'a' <= c[0] && c[0] <= 'z' || 'A' <= c[0] && c[0] <= 'Z' )
		{
			character[index++] = c;
			continue;
		}
		
		if( c[0] == '(' )
		{
			c[0] = in[++i];
			
			character[index] = "";
			while( c[0] != ')' )
			{
				if( c[0] != ')' )
					character[index].append(c);
				c[0] = in[++i];
			}			
			index++;
			continue;
		}
		
		if( c[0] == '' || c[0] == 0x0a || c[0] == 0x0d )
			break;
		
		printf("Unknown token : %c(%x)", c[0], c[0] );
		exit(EXIT_FAILURE);
	}

//	printf("num = %dn", index);
	num = index;

//	for( int i = 0; i < num; i++ )
//		puts( character[i].c_str() );

}



bool pattern_match::match( string in )
{
	for( int i = 0; i < in.length(); i++ )
	{
		char c[2];
		c[0] = in[i];
		c[1] = '';
		int loc = character[i].find( c );
		if( loc != string::npos )
			continue;
		else 
			return false;
	}
	
	return true;
}




int main()
{
	FILE *input = fopen("input.dat", "rt");
	char line[1024];
	int L, D, N;
	
	/* -------------------------------------------------------------
	 
	------------------------------------------------------------- */
	if( input == NULL )
		return EXIT_FAILURE;

	fgets(line, sizeof(line), input);
	sscanf( line, "%d %d %d", &L, &D, &N);
	printf("L = %d, D = %d, N = %dn", L, D, N);

	/* -------------------------------------------------------------
	 
	 ------------------------------------------------------------- */
	vector words;
	
	puts("");
	puts("word");
	for( int i = 0; i < D; i++ )
	{
		fgets(line, sizeof(line), input);
		
		for( int i = 0; i < sizeof(line); i++ )
			if( line[i] == 'n' )
				line[i] = '';
		
		words.push_back(line);
	}
	
	for( vector::iterator it = words.begin(); it != words.end(); it++ )
	{
		printf("%sn", it->c_str() );
	}
	

	/* -------------------------------------------------------------
	 
	 ------------------------------------------------------------- */
	puts("");
	puts("pattern");
	for( int i = 0; i < N; i++ )
	{
		int count = 0;
		
		fgets(line, sizeof(line), input);
		pattern_match m( line );
		
		for( vector::iterator it = words.begin(); it != words.end(); it++ )
		{
			if( m.match( *it ) == true )
				count++;
		}
		printf("Case #%d: %dn", i + 1, count );
		
	}
	
	puts("");
	return EXIT_SUCCESS;
}

サンプル問題を入れてみてきちんと解を出すことを確かめる。出力のフォーマットも間違いないのを確認して、small問題を解く。correct、やった。largeも解くが、間違ってsmallの回答を渡して怒られる。怒られなかったらアウトだったから助かった。largeも提出して次へ。

問題そのものより、std::stringの挙動とか改行コード、終端文字などで苦労した。C++を使うのは本当に久しぶりだ。