1387 Decoding Morse Sequences

Decoding Morse Sequences

Time Limit: 10 Seconds       Memory Limit: 32768 KB

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 "-":


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.


Input

The rst 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

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


Sample Output

2


Source: Central Europe 2001


题意:给你摩尔斯电码 , 给你一串字符串s和n串单词 ,查询这串字符串中能截取几串单词;

思路:暴力匹配


AC:

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
typedef long long LL;
using namespace std;
int main()
{
    string Morse[100];
    Morse[0]=".-",Morse[1]="-...",Morse[2]="-.-.",Morse[3]="-..",Morse[4]=".",Morse[5]="..-.",Morse[6]="--.",
    Morse[7]="....",Morse[8]="..",Morse[9]=".---",Morse[10]="-.-",Morse[11]=".-..",Morse[12]="--",Morse[13]="-.",
    Morse[14]="---",Morse[15]=".--.",Morse[16]="--.-",Morse[17]=".-.",Morse[18]="...",Morse[19]="-",Morse[20]="..-",
    Morse[21]="...-",Morse[22]=".--",Morse[23]="-..-",Morse[24]="-.--",Morse[25]="--..";

    int t;
    scanf("%d",&t);
    while(t--)
    {
        int f[10010]={0};
        string L;
        string cmp[10010];
        int len[10010]={0};
        cin>>L;
        int n;scanf("%d",&n);
        for(int i=0;i<n;i++)
        {
            string in;
            cin>>in;
            cmp[i]="";
            for(int j=0;j<in.length();j++)
            {
                cmp[i]+=Morse[in[j]-'A'];
            }
            len[i]=cmp[i].length();
        }
        memset(f,0,sizeof(f));
        f[0]=1;
        for(int i=0;i<L.length();i++)
        {
            if(f[i])
                for(int j=0;j<n;j++)
                {
			string x=L.substr(i,len[j]);
			if(x==cmp[j])
				f[i+len[j]]+=f[i];
		}
        }
        printf("%d\n",f[L.length()]);
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值