POJ 1051 P,MTHBGWB(map模拟)

本文详细介绍了如何使用映射表将字母转换为摩斯码,并实现从摩斯码到字母的逆向转换。通过遍历映射表,我们可以轻松地进行编码和解码操作。

用map记录字母到摩斯码,摩斯码到字母,摩斯码的长度的映射。


代码:

//
//  main.cpp
//  1051 P,MTHBGWB
//
//  Created by Baoli1100 on 15/4/5.
//  Copyright (c) 2015年 Baoli1100. All rights reserved.
//

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#include <map>

map <char,string> h1;
map <string,char> h2;
map <char,int> len;
string res;
void init(){
    h1['A']=".-";
    h1['B']="-...";
    h1['C']="-.-.";
    h1['D']="-..";
    h1['E']=".";
    h1['F']="..-.";
    h1['G']="--.";
    h1['H']="....";
    h1['I']="..";
    h1['J']=".---";
    h1['K']="-.-";
    h1['L']=".-..";
    h1['M']="--";
    h1['N']="-.";
    h1['O']="---";
    h1['P']=".--.";
    h1['Q']="--.-";
    h1['R']=".-.";
    h1['S']="...";
    h1['T']="-";
    h1['U']="..-";
    h1['V']="...-";
    h1['W']=".--";
    h1['X']="-..-";
    h1['Y']="-.--";
    h1['Z']="--..";
    h1['_']="..--";h2["..--"]='_';len['_']=4;
    h1['.']="---.";h2["---."]='.';len['.']=4;
    h1[',']=".-.-";h2[".-.-"]=',';len[',']=4;
    h1['?']="----";h2["----"]='?';len['?']=4;
    for(int i=0;i<26;i++){
        string k=h1[i+'A'];
        h2[k]=i+'A';
        len[i+'A']=k.length();
    }
}



int main(){
    init();
    int T;
    scanf("%d",&T);
    int kase=1;
    while(T--){
        char s[105];
        scanf("%s",s);
        res="";
        int l=strlen(s);
        int num[105];
        for(int i=0;i<l;i++){
            res+=h1[s[i]];
            num[i]=len[s[i]];
        }
        printf("%d: ",kase++);
        int pos=0;
        for(int i=l-1;i>=0;i--){
            string k=res.substr(pos,num[i]);
            printf("%c",h2[k]);
            pos+=num[i];
        }
        printf("\n");
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值