码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 1523 Decoding Morse Sequences(DP)

时间:2016-02-19 17:17:22      阅读:128      评论:0      收藏:0      [点我收藏+]

标签:

题目链接:点击打开链接

题意:给出一个编码, 给n个单词的字典, 求有多少个字典中单词组成的语句可以编译成所给编码。

思路:答案数很大, 我们不难想到要用动态规划的思想。   用d[i]表示到第i个编码为止的答案数, 那么起点就是d[0] = 1;  我们在第二层循环枚举n个单词的编码, 如果能匹配,就转移过去。

细节参见代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
using namespace std;
typedef long long ll;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int mod = 1000000000 + 7;
const int INF = 1000000000;
const int maxn = 10000 + 10;
int T,n,m,d[maxn];
char table[26][10] = {{".-"},{"-..."},{"-.-."},{"-.."},{"."},{"..-."},{"--."},
{"...."},{".."},{".---"},{"-.-"},{".-.."},{"--"},{"-."},{"---"},{".--."},
{"--.-"},{".-."},{"..."},{"-"},{"..-"},{"...-"},{".--"},{"-..-"},{"-.--"},{"--.."}};
char buf[maxn][105], s[maxn], str[30];
int main() {
    scanf("%d",&T);
    while(T--) {
        scanf("%s%d",s,&n);
        for(int i=0;i<n;i++) {
            scanf("%s",str);
            buf[i][0] = '\0';
            int len = strlen(str);
            for(int j=0;j<len;j++) {
                strcat(buf[i], table[str[j]-'A']);
            }
        }
        int len = strlen(s);
        memset(d, 0, sizeof(d));
        d[0] = 1;
        for(int i=0;i<len;i++) {
            if(d[i]) {
                for(int j=0;j<n;j++) {
                    int a = strlen(buf[j]);
                    if(strncmp(s+i,buf[j],a) == 0) d[i+a] += d[i];
                }
            }
        }
        printf("%d\n",d[len]);
    }
    return 0;
}


HDU 1523 Decoding Morse Sequences(DP)

标签:

原文地址:http://blog.csdn.net/weizhuwyzc000/article/details/50697160

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!