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

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

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

Sample Output

2

解题思路:

字典配对问题。就是把输入的各个字符串先转化为"..--"等形式,然后对于给定的字符串,如题中的".---.--.-.-.-.---...-.---.",从第0位开始和下面的候选字符串开始配对。采用dp的思想,dp[i]表示前i个已经配对的情况数。如果dp[i]不为0,表示当前的情况可以由之前的情况转化。

# include<stdio.h>
# include<string.h>
# define N 10050 char cod[26][10] = {{".-"},{"-..."},{"-.-."},{"-.."},{"."},{"..-."},{"--."},
{"...."},{".."},{".---"},{"-.-"},{".-.."},{"--"},{"-."},{"---"},{".--."},
{"--.-"},{".-."},{"..."},{"-"},{"..-"},{"...-"},{".--"},{"-..-"},{"-.--"},{"--.."}}; char final[N][N];//用来存储转化后的字符串
int dp[N];
int main()
{
int t;
while(scanf("%d",&t)!=EOF)
{
// printf("t %d",t);
while(t--)
{
char str[N];
scanf("%s",str);
int Lens=strlen(str);
int n,i,j;
scanf("%d",&n);
for(i=0;i<n;i++)
{
char tmp[N];
scanf("%s",tmp);
//final[i]需要清零
final[i][0]='\0';
int len=strlen(tmp);
for(j=0;j<len;j++)
{
strcat(final[i],cod[tmp[j]-'A']);
}
}
memset(dp,0,sizeof(dp));
dp[0]=1; for(i=0;i<Lens;i++)
{
if(dp[i]!=0)
{
for(j=0;j<n;j++)
{
int tmpL=strlen(final[j]);
if(strncmp(str+i,final[j],tmpL)==0)//很巧妙
dp[i+tmpL]+=dp[i];
}
}
} printf("%d\n",dp[Lens]);
}
}
return 0;
}

poj-Decoding Morse Sequences(动态规划)的更多相关文章

  1. POJ 1432 Decoding Morse Sequences (DP)

    Decoding Morse Sequences 题目链接: http://acm.hust.edu.cn/vjudge/contest/129783#problem/D Description Be ...

  2. POJ 1239 Increasing Sequences 动态规划

    题目链接: http://poj.org/problem?id=1239 Increasing Sequences Time Limit: 1000MSMemory Limit: 10000K 问题描 ...

  3. HDU 1523 Decoding Morse Sequences

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1523 此题大意为 给你一串摩尔斯密码  再给你一个字典(下面单词本) 用下面的单词组合成给你的摩尔斯密 ...

  4. poj 1776 Task Sequences

    http://poj.org/problem?id=1776 题意: 有一个机器要完成N个作业, 给你一个N*N的矩阵, M[i][j]=1,表示完成第i个作业后不用重启机器,继续去完成第j个作业 M ...

  5. [POJ 3211] Washing Clothes (动态规划)

    题目链接:http://poj.org/problem?id=3211 题意:有M件衣服,每种衣服有一种颜色,一共有N种颜色.现在两个人洗衣服,规则是必须把这一种颜色的衣服全部洗完才能去洗下一种颜色的 ...

  6. POJ 1661 Help Jimmy -- 动态规划

    题目地址:http://poj.org/problem?id=1661 Description "Help Jimmy" 是在下图所示的场景上完成的游戏. 场景中包括多个长度和高度 ...

  7. POJ 1276 Cash Machine -- 动态规划(背包问题)

    题目地址:http://poj.org/problem?id=1276 Description A Bank plans to install a machine for cash withdrawa ...

  8. POJ 1170 Shopping Offers -- 动态规划(虐心的六重循环啊!!!)

    题目地址:http://poj.org/problem?id=1170 Description In a shop each kind of product has a price. For exam ...

  9. HOJ 2133&POJ 2964 Tourist(动态规划)

    Tourist Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1503 Accepted: 617 Description A ...

随机推荐

  1. Day4 dict和set

    dict  -- dictionary    一组key的集合,包含key与value的对应.        Python内置的字典,在其他语言中称为map,使用key-value存储,具有极快的查找 ...

  2. PHP代码审计学习-PHP-Audit-Labs-day2

    filter_var()函数 filter_var() 函数通过指定的过滤器过滤一个变量.如果成功,则返回被过滤的数据.如果失败,则返回 FALSE. filter_var(variable, fil ...

  3. WindowsPhone8.1 开发-- 二维码扫描

    随着 WinRT 8.1 API 的发布,Windows 8.1 和 Windows Phone 8.1 (基于 WinRT) 应用程序的开发模型经历了戏剧性的收敛性.与一些特定于平台的考虑,我们现在 ...

  4. Django中ORM的使用

    Django中ORM的使用 ORM orm(object-relation-mapping)对象关系映射,即用对象来表示关系数据库中的表: 类 --> 表, 对象-->一行数据 对象的属性 ...

  5. Python 中的运算符重载

    本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,如有问题请及时联系我们以作处理 一种运算符对于不同类型的对象,有不同的使用方式.例如, + 用于整型对象,表示两个数相加:用于字符串 ...

  6. 搜索引擎优化(SEO)解决方案

      搜索引擎优化(SEO)解决方案 在此之前,希望大家能重新审视搜索引擎,通俗来讲就是我们日常所用的百度.谷歌.搜狗.雅虎等.磨刀不误砍柴工,知己知彼,百战不殆! 一.搜索引擎是什么? 搜索引擎(Se ...

  7. Excel 多/整列(多/整行)移位操作

    步骤1:创建测试数据 步骤2:把B列和C列进行移位操作(整列移位操作,多列移位操作方法一样) 选中B列,鼠标放到B列边缘地带,直到鼠标显示带有四个箭头方向为止,点击键盘shift键进行拖拽,拖拽时显示 ...

  8. 超详细的第一个Servlet程序

    Servlet的第一个程序! 首先查看官方文档,来编写我们的第一段代码 1.先启动Tomcat,确保我们能够正常访问. 2.http://localhost:8080/examples/     查看 ...

  9. umi-request 统一异常处理实践

    首发于语雀文档 前言 本人在工作中用到了 umi-request,百度谷歌搜了一遍,感觉都没找到超过 3 篇合适且含代码的文章,因此只能自行实践总结了. umi-request 有点不同 umi-re ...

  10. [LeetCode]Path Sum系列

    1.二叉树路径求指定和,需要注意的是由于有负数,所以即使发现大于目标值也不能返回false,而且返回true的条件有两个,到叶节点且等于sum,缺一不可 public boolean hasPathS ...