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.
Example
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
弱小又无助的菜鸡,问不认识的大佬要的代码,万分感谢大佬的帮助,大佬博客链接
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
const int M = 128;
const int X = 122127;
map<string, int> ma;
int dp[N], f[N];
string t, s;
char c;
int main()
{
ios::sync_with_stdio(false);
int T, n, m, i, j;
cin >> T;
while (T--)
{
ma.clear();
memset(dp, 0, sizeof(dp));
memset(f, 0, sizeof(f));
cin >> n >> m;
for (i = 0; i < m; i++)
{
cin >> c >> t;
ma[t]++;
}
cin >> s;
s = ' ' + s;
dp[0] = 1, f[0] = 1;
for (i = 1; i <= n; i++)
{
for (j = 1; j <= 5 && j <= i; j++)
{
t = s.substr(i - j + 1, j);
if (ma[t])
{
dp[i] = (dp[i] + dp[i - j] * ma[t] % M) % M;
f[i] = (f[i] + f[i - j] * ma[t] % X) % X; //类似于哈希,用于判断dp过后的数值
} //是否是因为除于操作变为0或1。
}
}
if (dp[n] == 0 && f[n] == 0)
puts("nonono");
else if (dp[n] == 1 && f[n] == 1)
puts("happymorsecode");
else
printf("puppymousecat %d\n", dp[n]);
}
return 0;
}
本文介绍了一种基于二进制字符串的莫尔斯电码解码算法,通过使用0和1来表示点和划,实现了字母与二进制串间的转换。文章详细解释了如何通过动态规划方法确定给定字符串是否能被唯一解读,并提供了具体实现代码。
425

被折叠的 条评论
为什么被折叠?



