Google Code Jam 2009预选赛第一题----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

这个题目本身数据集不大, 字符长度只有15,而需要匹配的字符串个数最多只有5000个, 所以采用的是遍历的方法, 也就是在字符串集中, 先挑出满足第一个字母列表的所有字符串,然后再在这些字符串中挑选满足第二个的,直到最后,统计一下最后省下的字符串个数就是结果了.

 

代码:

#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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值