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()]);
}
}
本文介绍了一道关于摩尔斯电码解码的问题,任务是编写程序来解析给定的摩尔斯电码序列,并计算出可以形成的完整词汇数量。通过暴力匹配的方法实现了题目要求。
255

被折叠的 条评论
为什么被折叠?



