poj-Decoding Morse Sequences(动态规划)
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(动态规划)的更多相关文章
- POJ 1432 Decoding Morse Sequences (DP)
Decoding Morse Sequences 题目链接: http://acm.hust.edu.cn/vjudge/contest/129783#problem/D Description Be ...
- POJ 1239 Increasing Sequences 动态规划
题目链接: http://poj.org/problem?id=1239 Increasing Sequences Time Limit: 1000MSMemory Limit: 10000K 问题描 ...
- HDU 1523 Decoding Morse Sequences
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1523 此题大意为 给你一串摩尔斯密码 再给你一个字典(下面单词本) 用下面的单词组合成给你的摩尔斯密 ...
- poj 1776 Task Sequences
http://poj.org/problem?id=1776 题意: 有一个机器要完成N个作业, 给你一个N*N的矩阵, M[i][j]=1,表示完成第i个作业后不用重启机器,继续去完成第j个作业 M ...
- [POJ 3211] Washing Clothes (动态规划)
题目链接:http://poj.org/problem?id=3211 题意:有M件衣服,每种衣服有一种颜色,一共有N种颜色.现在两个人洗衣服,规则是必须把这一种颜色的衣服全部洗完才能去洗下一种颜色的 ...
- POJ 1661 Help Jimmy -- 动态规划
题目地址:http://poj.org/problem?id=1661 Description "Help Jimmy" 是在下图所示的场景上完成的游戏. 场景中包括多个长度和高度 ...
- POJ 1276 Cash Machine -- 动态规划(背包问题)
题目地址:http://poj.org/problem?id=1276 Description A Bank plans to install a machine for cash withdrawa ...
- POJ 1170 Shopping Offers -- 动态规划(虐心的六重循环啊!!!)
题目地址:http://poj.org/problem?id=1170 Description In a shop each kind of product has a price. For exam ...
- HOJ 2133&POJ 2964 Tourist(动态规划)
Tourist Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1503 Accepted: 617 Description A ...
随机推荐
- 云小课 | 需求任务还未分解,该咋整!项目管理Scrum项目工作分解的心酸谁能知?
温馨提醒:本文约3000字,需要阅读5分钟,共分为8个部分,建议分段阅读! 软件开发过程中,从产品概念形成到产品规划.往往要做详细的需求分析和项目规划等,因此,选对一款项目管理工具对开发者就显得尤为重 ...
- Oracle数据泵常用命令
导读:expdp和impdp是oracle数据库之间移动数据的工具,本文简单总结了数据泵的常用命令,希望对大家有帮助. 前言 expdp和impdp是oracle数据库之间移动数据的工具.expd ...
- jmeter跨线程使用token
项目的接口测试,今早所有接口都不通了,查看原因是登录接口地址变了..... 原来的方式是每个线程中都写了登陆接口来获取token,但是因为登陆接口地址改变的原因,要改好多个登陆接口,所以就想把toke ...
- windows 上的MySQL默认字符集设置踩过的坑
前言: 前几天刚买了新电脑,装上MySQL有几天了,今天没事试了一下,发现默认字符集没有修改,还是默认的latin1,折腾了大半天,终于搞好了. 这是我成功设置后的结果图: 命令式直接在MySQL界面 ...
- 深入LUA脚本语言,让你彻底明白调试原理
这是道哥的第008篇原创 一.前言 上篇文章我们聊了gdb的底层调试机制,明白了gdb是利用操作系统提供的系统信号来调试目标程序的.很多朋友私下留言了,看到能帮助到大家,我心里还是很开心的,其实这也是 ...
- Numpy的学习6-深浅赋值(copy&deep copy)
# = 的赋值方式会带有关联性 import numpy as np a = np.arange(4) # array([0, 1, 2, 3]) b = a c = a d = b # 改变a的第一 ...
- Azure Service Bus(一)入门简介
一,引言 今天开始学习新的内容 Azure Service Bus(服务总线),其实也叫 "云消息服务",和 RabbitMQ,KafKa的一样都是作为消息通信服务,但是它们直接还 ...
- Python进阶——什么是上下文管理器?
在 Python 开发中,我们经常会使用到 with 语法块,例如在读写文件时,保证文件描述符的正确关闭,避免资源泄露问题. 你有没有思考过, with 背后是如何实现的?我们常常听到的上下文管理器究 ...
- Redis入门指导
前言 本文提供全网最完整的Redis入门指导教程,下面我们从下载Redis安装包开始,一步一步的学习使用. 下载Redis 官网提供的Redis安装包是服务于Linux的,而我们需要在Window下使 ...
- Python代码打包成exe可执行程序
首先,打包成exe可执行程序是针对windows平台来说的. 目前比较主流的打包工具就是pyinstaller. 参考:Using PyInstaller 首先安装pyinstaller: pip i ...