1432 Decoding Morse Sequences

Decoding Morse Sequences
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 418 Accepted: 177

Description
Before the digital age, the most common "binary" code for radio communication was the Morse code. In Morse code, symbols are encoded as sequences of short and long pulses (called dots and dashes respectively). The following table reproduces the Morse code for the alphabet, where dots and dashes are represented as ASCII characters "." and "-":

 

Input
The first line of the input contains exactly one positive integer d equal to the number of data sets, 1 <= d <= 20. The data sets follow.

The first line of each data set contains a Morse sequence - a nonempty sequence of at most 10 000 characters "." and "-" with no spaces in between.

The second line contains exactly one integer n, 1 <= n <= 10 000, equal to the number of words in a dictionary. Each of the following n lines contains one dictionary word - a nonempty sequence of at most 20 capital letters from "A" to "Z". No word occurs in the dictionary more than once.

Output
The output should consist of exactly d lines, one line for each data set. Line i should contain one integer equal to the number of distinct phrases into which the Morse sequence from the i-th data set can be parsed. You may assume that this number is at most 2 * 10^9 for every single data set.

Sample Input

 

Sample Output

 

Source
Central Europe 2001

 **********************************************************************************

*************************************************************************************

  • Source Code
    #include<stdio.h>
    #include<string.h>
    char Morse[26][5]={".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};
    int best[10001],n,len;
    char s[10001],dic[10001][30],word[10001][101];
    void init()
    {
    int i,j;
    memset(word,'/0',sizeof(word));
    for(i=0;i<n;i++)
       for(j=0;j<strlen(dic[i]);j++)
        strcat(word[i],Morse[dic[i][j]-'A']);
    memset(best,0,sizeof(best));
    best[0]=1;
    //for(i=0;i<n;i++)
    //    printf("%s/n",word[i]);
    }
    void DP()
    {
    int i,j,k,flag;
    for(i=0;i<=len;i++)
       if(best[i])
        for(j=0;j<n;j++)
        {
         flag=1;
         for(k=0;k<strlen(word[j]);k++)
         {
          if(i+k==len)break;
          if(s[i+k]!=word[j][k]){flag=0;break;}
         }
         if(flag)best[i+strlen(word[j])]+=best[i];
        }
    printf("%d/n",best[len]);
    }
    int main()
    {
    int cas,i;
    scanf("%d",&cas);
    while(cas--)
    {
       scanf("%s%d",s,&n);
       len=strlen(s);
       for(i=0;i<n;i++)
        scanf("%s",dic[i]);
       init();
       DP();
    }
    }
    
    
  • 2

    1
    .---.--.-.-.-.---...-.---.
    6
    AT
    TACK
    TICK
    ATTACK
    DAWN
    DUSK


    Notice that in the absence of pauses between letters there might be multiple interpretations of a Morse sequence. For example, the sequence -.-..-- could be decoded both as CAT or NXT (among others). A human Morse operator would use other context information (such as a language dictionary) to decide the appropriate decoding. But even provided with such dictionary one can obtain multiple phrases from a single Morse sequence.


    Task

    Write a program which for each data set:

    reads a Morse sequence and a list of words (a dictionary),

    computes the number of distinct phrases that can be obtained from the given Morse sequence using words from the dictionary,

    writes the result.

    Notice that we are interested in full matches, i.e. the complete Morse sequence must be matched to words in the dictionary.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xcl119xxcl

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值