Google Code Jam 2009 Qualifying First question----Alien Language

Source: Internet
Author: User

Problem

After years of study, scientists at Google Labs has 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 is exactly D words in this language.

Once the Dictionary of all the words in the alien language is built, the next breakthrough is to discover that the alien S has been transmitting messages to Earth for the past decade. Unfortunately, these signals is weakened due to the distance between our both planets and some of the words May is misinte Rpreted. In order-to-help them decipher these messages, the scientists has asked you to devise a algorithm that would determine th E number of possible interpretations for a given pattern.

A pattern consists of exactly L tokens. Each token was either a single lowercase letter (the scientists was very sure that's the letter) or a group of unique Lowercase letters surrounded by parenthesis (and). For example: (AB) d (DC) means the first letter was either A or B, the second letter was definitely d and the last letter is E Ither d or C. Therefore, the pattern (AB) d (DC) can stand for either one of the These 4 Possibilities:add, ADC, BDD, BDC.

Input

The first line of input contains is 3 integers, L, D and N separated by a space. D lines follow, each containing one word of length L. These is the words that is 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 this all known words provided is unique.

Output

For each test case, output

Case #XK

Where X is the test case number, starting from 1, and K indicates how many words in the alien language m Atch 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

The problem itself is small data set, the character length is only 15, and the number of strings that need to be matched to only 5,000, so the use of the traversal method, that is, in the string set, first pick out all the strings that satisfy the first alphabetical list, and then pick the string to satisfy the second, until finally, Count the last number of strings saved is the result.

Code:

#include <stdio.h>
#include <malloc.h>
#include <string.h>

int main ()
{
file* FP;
File* FpW;
int I, j, K;
Char ptmp[65535];
fp = fopen ("1.txt", "R");

int NL, ND, NN;
FSCANF (FP, "%d%d%d", &NL, &nd, &NN);

char* PData = (char*) malloc (nl*nd*sizeof (char) +1);
int* plistold = (int*) malloc (nd*sizeof (int));
int* plistnew = (int*) malloc (nd*sizeof (int));
for (i=0; i<nd; i++)
{
FSCANF (FP, "%s", PDATA+I*NL);
}
for (i=0; i<nn; i++)
{
int nPos = 0;
int bstop = 0;
FSCANF (FP, "%s", ptmp);
int ncountold = ND;
int ncountnew = 0;
for (j=0; j<ncountold; j + +)
{
PLISTOLD[J] = j;
}

for (j=0; J<strlen (ptmp); j + +)
{
if (ptmp[j]== ' (')
{
bstop = 1;
}
else if (ptmp[j]== ') ')
{
bstop = 0;
memcpy (Plistold, Plistnew, ncountnew*sizeof (int.));
Ncountold = ncountnew;
ncountnew = 0;
npos++;
}
Else
{
for (k=0; k<ncountold; k++)
{
if (Pdata[plistold[k]*nl+npos]==ptmp[j])
{
plistnew[ncountnew++] = plistold[k];
}
}
if (bstop==0)
{
memcpy (Plistold, Plistnew, ncountnew*sizeof (int.));
Ncountold = ncountnew;
ncountnew = 0;
npos++;
}
}
}
printf ("Case #%d:%d/n", i+1, Ncountold);
}
Free (pData);
Free (plistold);
Free (plistnew);
Fclose (FP);
return 0;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.