zoj 1387 Decoding Morse Sequences

dp[i]表示在给定的密码中,第i位及第i位之前的密码段一共有几种组合
然后在从第i位开始遍历字典,更新dp[i+k]的值(k为字典中某个词的长度)

#include<iostream>
#include<map>
#include<cstring>
#include<string>
#include<vector>
using namespace std;
const int MAX = 10100;

char ch[27][5] = {
	".-", "-...", "-.-.", "-..",
	".", "..-.", "--.", "....",
	"..", ".---", "-.-", ".-..",
	"--", "-.", "---", ".--.",
	"--.-", ".-.", "...", "-",
	"..-", "...-", ".--", "-..-",
	"-.--", "--.."
};

char str[MAX];
char dic[MAX][120];
char diclen[MAX];
int morselen[27];
int dp[MAX];

void solve(int n) {
	int i, len, j, k;
	bool ok;
	len = strlen(&str[0]);
	memset(dp, 0, sizeof(dp));
	dp[0] = 1;
	for (i = 1; i <= len; i++) {
		for (j = 0; j < n; j++) {
			ok = true;
			for (k = 0; dic[j][k]; k++) {
				if (dic[j][k] != str[i + k - 1]) {
					ok = false;
					break;
				}
			}
			if (ok) dp[i - 1 + k] += dp[i - 1];
		}
	}

	printf("%d\n", dp[len]);
}

int main() {
	int t, n, i, j, len;
	scanf("%d", &t);
	while (t--) {
		scanf("%s", &str[0]);
		scanf("%d", &n);
		for (i = 0; i < n; i++) {
			char temp[25];
			scanf("%s", &temp[0]);
			len = 0;
			for (j = 0; temp[j]; ++j) {
				strcpy(&dic[i][len], ch[temp[j] - 'A']);
				len += strlen(ch[temp[j] - 'A']);
			}
			dic[i][len] = '\0';
			diclen[i] = len;
		}

		solve(n);

	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值