习题4-6 莫尔斯电码

原题:莫尔斯电码

1、此英文原题大致意思是给定一些莫尔斯编码,给定一些已知字典,给定一些编码,求解这些编码的对应原文。

如果可以精确匹配,则直接输出原单词,如果有多个可精确匹配的结果,则输出匹配结果里字典序最小的单词并在末位加上“!”;

如果无法精确匹配,则可以在编码的末尾增加或删除一些字符后匹配单词(增删应尽量小,就是找到增删最少的模糊匹配),无论增删后匹配一个还是多个,最后都要加上“?”;

如果有多个模糊匹配结果(增删数相等),则输出字典序最小的匹配结果;

如果无精确匹配也无模糊匹配结果,则输出整个给定字典里字典序最小的那个单词。

2、此题重点在输入输出数据与格式。

3、数据一一对应如何实现,考虑采用结构体


#include <stdio.h>

#include <string.h>

#define INF 999

#define maxn 1000+10

int ccount;
int wcount;
int mcount;
int p[100+10];
struct morsecode{
    char name;
    char namecode[20];
}code[50];
struct contextword{
    char context[20];
    char contextcode[100];
}word[100+10];
struct morseword{
    char contextcode[100];
    char context[20];
}morse[maxn];
void readcode(){
    int i;
    char t[10];
    ccount=0;
    while(scanf("%s",t)&&t[0]!='*'){
        code[ccount].name=t[0];
        scanf("%s",code[ccount].namecode);
        ccount++;
    }
}
void printcode(){
    int i;
    for(i=0;i<ccount;i++){
        printf("%c %s\n",code[i].name,code[i].namecode);
    }
}
void readword(){
    char t[20];
    int i,j;
    int len;
    wcount=0;
    while(scanf("%s",t)&&t[0]!='*'){
        strcpy(word[wcount].context,t);
        len=strlen(t);
        for(i=0;i<len;i++){
            for(j=0;j<ccount;j++){
                if(t[i]==code[j].name){
                    strcat(word[wcount].contextcode,code[j].namecode);
                    break;
                }
            }
        }
        wcount++;
    }

void printcontext(){
    int i;
    for(i=0;i<wcount;i++){
        printf("%s %s\n",word[i].context,word[i].contextcode);
    }
}
void readmorse(){
    int i,j,k;
    int min;
    char t[100];
    int fcount;
    int mlen,wlen;
    mcount=0;
    while(scanf("%s",t)&&t[0]!='*'){
        strcpy(morse[mcount].contextcode,t);
        fcount=0;
        for(i=0;i<wcount;i++){
            if(strcmp(word[i].contextcode,t)==0){
                fcount++;
                if(fcount==1)
                    strcpy(morse[mcount].context,word[i].context);
                else{
                    strcat(morse[mcount].context,"!"); 
                    break;
                }
            }
        }
        if(fcount==0){
            memset(p,0,sizeof(p));
            mlen=strlen(t);
            for(i=0;i<wcount;i++){
                k=0;
                wlen=strlen(word[i].contextcode);
                for(j=0;j<mlen;j++){
                    if(j<=wlen){
                        if(word[i].contextcode[j]==morse[mcount].contextcode[j])
                            k++;
                    }else
                        break;
                }
                if(k==mlen)
                    p[i]=wlen-mlen;
                else if(k<mlen&&k==wlen)
                    p[i]=mlen-wlen;
                else
                    p[i]=INF;        
            }
            
            min=p[0];
            j=0;
            for(i=0;i<wcount;i++){
                if(p[i]<min){
                    min=p[i];
                    j=i;
                }
            }
            strcpy(morse[mcount].context,word[j].context);
            strcat(morse[mcount].context,"?"); 
        }
        mcount++;
    }

void printword(){
    int i=0;
    for(i=0;i<mcount;i++)
        printf("%s\n",morse[i].context);
}
int main(){
    readcode();
    readword();
    readmorse();
    printword();
    return 0;
}

猜你喜欢

转载自blog.csdn.net/jxufe_acmer/article/details/80331052
4-6
今日推荐