POJ 1432 Decoding Morse Sequences
来源:互联网 发布:买车贷款和全款 知乎 编辑:程序博客网 时间:2024/05/22 03:30
题意
给出一个字母的译码表,给出一个有n个单词的字典,给出一串编码。问再给出字典中有多少种不同的解码方式。
分析
首先编码只有-和.,可以很方便地建立一棵Trie树,还是最熟悉的二叉方式。
然后令opt[i]表示区间[i..len]的方案数
则有opt[i]=sigma{[i..j]组成的单词的个数*opt[j+1]|i<=j
代码
#include<iostream>#include<algorithm>#include<cstring>#include<cstdio>#include<bitset>using namespace std;const int INF=0x3f3f3f3f,MAXN=10000;typedef bitset<MAXN+3> data;const char* morse[26] = { ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."};struct Trie{ int ch[4*20*MAXN+3][2],cnt[4*20*MAXN+3],tot; void init(){ memset(ch,0,sizeof(ch)); memset(cnt,0,sizeof(cnt)); tot=0; } void insert(const data &d,int len){ int cur=0; for(int i=0;i<len;i++){ if(!ch[cur][d[i]])ch[cur][d[i]]=++tot; cur=ch[cur][d[i]]; } cnt[cur]++; }}T;int getmorse(char *s,data &d){ int len=strlen(s),p=0; d=0; for(int i=0;i<len;i++){ int tl=strlen(morse[s[i]-'A']); for(int j=0;j<tl;j++){ if(morse[s[i]-'A'][j]=='.')d[p++]=0; else d[p++]=1; } } return p;}char str[MAXN+3],tmp[MAXN+3];int n,opt[MAXN+3];void input(){ scanf("%s",str); scanf("%d",&n); T.init(); data d; for(int i=1;i<=n;i++){ scanf("%s",tmp); T.insert(d,getmorse(tmp,d)); }}int dp(){ int len=strlen(str); opt[len]=1; for(int i=len-1;i>=0;i--){ opt[i]=0; int cur=0; for(int j=i;j<len;j++){ if(str[j]=='.'){ if(!T.ch[cur][0])break; cur=T.ch[cur][0]; } else{ if(!T.ch[cur][1])break; cur=T.ch[cur][1]; } if(T.cnt[cur]!=0)opt[i]+=T.cnt[cur]*opt[j+1]; } } return opt[0];}int main(){ int T;scanf("%d",&T); while(T--){ input(); printf("%d\n",dp()); } return 0;}
0 0
- poj 1432 Decoding Morse Sequences
- POJ 1432 Decoding Morse Sequences
- [ZOJ 1387] [POJ 1432] Decoding Morse Sequences
- 1432 Decoding Morse Sequences
- 1387 Decoding Morse Sequences
- HDU 1523 Decoding Morse Sequences
- HDU 1523 Decoding Morse Sequences(DP)
- morse
- poj 2141 水题 Message Decoding
- poj 1239 Increasing Sequences
- POJ 2034 Anti-prime Sequences
- poj 2034 Anti-prime Sequences
- POJ 2034 Anti-prime Sequences
- POJ-2034-Anti-prime Sequences
- POJ 2034 Anti-prime Sequences
- poj 2442 sequences(堆排列)
- poj 2034 Anti-prime Sequences
- Increasing Sequences - POJ 1239 dp
- Mac新手操作指南(一)
- 【发布公告】您的应用卡么?Bugly全新功能:卡顿/ANR监控
- 一个感染型的病毒逆向分析
- ZigZag Conversion
- 网络检错MBProssHUD库的使用
- POJ 1432 Decoding Morse Sequences
- hdu1254推箱子
- css基础学习之--inline-block
- K-means算法
- HDU-3074-Multiply game-单点更新
- poj 3468 A Simple Problem with Integers 线段树
- int,bigint,smallint、tinyint、varchar
- POJ 1661 Help Jimmy
- 编程式使用IoC容器