ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Gym102875H Happy Morse Code(DP)

2020-12-14 21:00:07  阅读:315  来源: 互联网

标签:Gym102875H Code string Morse int update mp include dp


Gym102875H Happy Morse Code(DP)

Description
Little Rabbit and Little Horse recently learned about Morse code and found that just only two symbols of dash and dot can express countless words, for that each letter has a unique dash-dot string correspondence. Little Rabbit and Little Horse get the wrong conclusion because a dash-dot string does not necessarily correspond to a letter, it may also correspond to a number, or it may correspond to a sentence indicating start, interrupt, and end.
Anyway, they plan to use 0 to represent a dot, 1 to represent dash, and binary strings to express their own happy Morse code. They randomly assign a binary string for each letter and made it into a cipher book.
Given a binary string, can Little Rabbit and Little Horse’s cipher book uniquely interpret the meaning of the string? If it is, output happymorsecode; if there is more than one possible interpretation, output puppymousecat and the number of feasible interpretations modulo 128; if it can’t be interpreted at all, output nonono.
Input
The input contains several test cases.
The first line contains a single integer T (1≤T≤105), indicating the number of test cases.
For each test case: the first line contains two integers n (1≤n≤105) and m (1≤m≤26), indicating the length of the given binary string s and the number of letters in the cipher book. For the following m lines, each line contains a unique letter and its binary string correspondence t (1≤|t|≤5), where |t| denotes the length of string t. The last line contains the given binary string s.
It is guaranteed that the sum of n will not exceed 105.
Output
For each test case, output following content in a line: if the cipher book can interpret the unique meaning of the string, output happymorsecode; if the book can interpret more than one meanings of the string, output puppymousecat and the number of feasible interpretation modulo 128; if the string can’t be interpreted at all, output nonono.
input
3
4 2
a 01
b 10
0110
4 4
a 01
b 10
c 01
d 0110
0110
4 2
a 1
b 10
0110
output
happymorsecode
puppymousecat 3
nonono

题意

给定一个长度为 n n n的字符串(下文称其“答案串”)和 m m m个长度不大于 5 5 5的字符串(下文称其“匹配串”)。问这 m m m个字符串有几种方法组成这个长度为 n n n的字符串。

题解

首先用哈希 m p mp mp存入每个匹配串出现的次数。
令 d p [ i ] dp[i] dp[i]表示长度为 i i i的答案串子串被匹配串成功匹配的次数。
写出 d p dp dp方程: d p [ i ] = ( d p [ i ] + d p [ i − j ] ∗ m p [ t e m p ] % M O D ) % M O D dp[i] = (dp[i] + dp[i - j] * mp[temp] \% MOD) \% MOD dp[i]=(dp[i]+dp[i−j]∗mp[temp]%MOD)%MOD
遍历答案串,在遍历过程中每次都枚举长度 [ 1 , 5 ] [1,5] [1,5]的字符串,看其是否存在于匹配串中,若存在则将其累加到 d p [ i ] dp[i] dp[i]中。
由于答案要模 128 128 128,所以要加一些处理记录是否存在进位的情况。
部分细节处理在代码中注释。

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
#define lowbit(x) ((x) & -(x))
#define endl "\n"
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
typedef unsigned long long ull;
const int N = 1e5 + 10, NN = 10, INF = 0x3f3f3f3f, LEN = 20;
const int MOD = 128;
const ull seed = 31;
inline int read() {
    int x = 0, f = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9') {
        if (ch == '-')
            f = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9') {
        x = (x << 1) + (x << 3) + (ch ^ 48);
        ch = getchar();
    }
    return x * f;
}
unordered_map<string, int> mp;
int n, m;
int dp[N];
bool update[N];
string s;
void init() {
    mp.clear();
    memset(dp, 0, sizeof dp);
    memset(update, false, sizeof update);
}
int main() {
    // ios::sync_with_stdio(false);
    // cin.tie(0);
    // cout.tie(0);
    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);
    int T;
    T = read();
    while (T--) {
        init();
        n = read();
        m = read();
        for (int i = 1; i <= m; i++) {
            string temp1, temp2;
            cin >> temp1 >> temp2;
            ++mp[temp2];
        }
        cin >> s;
        s = ' ' + s;
        dp[0] = 1;
        update[0] = false;  // 记录有无进位
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= 5 && j <= i; j++) {
                string temp = s.substr(i - j + 1, j);
                if (mp[temp]) {
                    // 如下三个if判断是否在运算过程中产生了进位
                    if (dp[i - j] * mp[temp] >= 128)
                        update[i] = true;
                    if (dp[i] + dp[i - j] * mp[temp] % MOD >= 128)
                        update[i] = true;
                    if (update[i - j])
                        update[i] = true;
                    dp[i] = (dp[i] + dp[i - j] * mp[temp] % MOD) % MOD;
                }
            }
        }
        if (dp[n] == 0 && update[n] == false) // 无法组成答案串
            printf("nonono\n");
        else if (dp[n] == 1 && update[n] == false) // 组成了答案串并且只有一种情况,没有进位
            printf("happymorsecode\n");
        else
            printf("puppymousecat %d\n", dp[n]);
    }
    return 0;
}

标签:Gym102875H,Code,string,Morse,int,update,mp,include,dp
来源: https://blog.csdn.net/Hc_Soap/article/details/111185112

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有