zoukankan      html  css  js  c++  java
  • POJ 1432 Decoding Morse Sequences (DP)

    Decoding Morse Sequences

    题目链接:

    http://acm.hust.edu.cn/vjudge/contest/129783#problem/D

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

    Source

    2016-HUST-线下组队赛-3
    ##题意:
    ##题解:
    ##代码: ``` cpp #include #include #include #include #include #include #include #include #include #include #include #define LL long long #define eps 1e-8 #define maxn 10010 #define mod 100000007 #define inf 0x3f3f3f3f3f3f3f3f #define mid(a,b) ((a+b)>>1) #define IN freopen("in.txt","r",stdin); using namespace std;

    string Morse[26] = {{".-"},{"-..."},{"-.-."},{"-.."},{"."},{"..-."},{"--."},
    {"...."},{".."},{".---"},{"-.-"},{".-.."},{"--"},{"-."},{"---"},{".--."},
    {"--.-"},{".-."},{"..."},{"-"},{"..-"},{"...-"},{".--"},{"-..-"},{"-.--"},{"--.."}
    };

    int n;
    char text[maxn];
    int dp[maxn];
    map<string, int> mp;

    int main(int argc, char const *argv[])
    {
    //IN;

    int t; cin >> t;
    while(t--)
    {
        scanf("%s", text+1);
        scanf("%d", &n);
        mp.clear();
        for(int i=1; i<=n; i++) {
            char word[25];
            scanf("%s", word);
            string cur;
            for(int i=0; word[i]; i++) {
                cur += Morse[word[i] - 'A'];
            }
            mp[cur]++;
        }
    
        int len = strlen(text+1);
        memset(dp, 0, sizeof(dp));
        dp[0] = 1;
        for(int i=1; i<=len; i++) {
            for(int j=max(0,i-85); j<i; j++) if(dp[j]){
                char cur[100] = {0};
                for(int k=j+1; k<=i; k++) {
                    cur[k-j-1] = text[k];
                }
    
                dp[i] += dp[j] * mp[cur];
            }
        }
    
        printf("%d
    ", dp[len]);;
    }
    
    return 0;
    

    }

  • 相关阅读:
    mui---子页面主动调用父页面的方法
    宝塔使用FTP的问题
    css---颜色过渡渐变
    mui---开发直播APP
    mui---计算缓存大小及清除缓存
    mui---自定义页面打开的方向
    mui---取消掉默认加载框
    mui+回复弹出软键盘
    还不错的MUI技术文档
    mui---父页面跳子页面刷新子页面
  • 原文地址:https://www.cnblogs.com/Sunshine-tcf/p/5805181.html
Copyright © 2011-2022 走看看