算法竞赛入门经典(第2版)习题4-6 莫尔斯电码 Morse Mismatches UVa 508

来源:互联网 发布:prada高仿包淘宝 编辑:程序博客网 时间:2024/04/28 11:34

逻辑如下

1.读入每个字母的Morse编码m[40][10]

2.读入词典word[maxn][11]

3.对照字母将词典翻译为Morse编码词典mword[maxn][61],并计算出每个词的编码长度mlen[maxn]

4.读入编码code[maxn][81]同时计算出编码长度clen[maxn]

5.从Morse编码词典中查找相同长度的编码检验其是否能匹配,对通过检验的mword计数,并将第一个精确匹配上的词序号记录

6.如果存在且只有一个mword[n]能够精确匹配则直接输出,如果存在但有多个mword[n]能够精确匹配则输出第一个匹配上的词,后面加上“!”

7.如果不存在则能够精确匹配的mword[n],则递增变量i,对比长度为clen-i的mword[n]的前clen-i个字符或对比长度为clen+i的前clen个字符,发现能够匹配的词就输出并添加后缀“?”


这题前几次提交总是报“runtime error”,把数组反复调大一点后终于wa了。wa了两天,各种修改,甚至把读取数据的方式从getchar改成了scanf,但一直过不去。

只好一边改错一边搜ac的标程,今天终于搜到了(谢谢作者代号4101)。

修改测试数据后反复对拍,原来自己的程序里有逻辑错误:第七步里的递增变量的长度上限被错误地设置为待解码的代码长度,达到上限后程序会停止继续搜索直接输出”?“。

修改后ac。

以后查错还是得更细致一点,拍脑袋想错误不如认真多读几遍自己的程序,既然UVa都报错了,就别过于相信自己的程序逻辑了。


//#define LOCAL//#define TESTING#include<stdio.h>#include<string.h>#include<ctype.h>#define maxn 1005char c[maxn],m[maxn][15],word[maxn][15],mword[maxn][100],mlen[maxn],code[maxn][100],clen[maxn];int result;int check(char* codes, int len, int wordn, int change){    int finded = 0;    for(int i = 0; i < wordn; i++)        {            if((len == mlen[i]-change) || (len == mlen[i]+change))            {                int lim;                bool eq = true;                if(len > mlen[i]) lim = mlen[i];                else lim = len;                for(int j = 0; j < lim;j++)                {                    if(codes[j] != mword[i][j])                    {                        eq = false;                        break;                    }                }                if(eq)                {                    if(!finded) result = i;                    finded++;                }            }        }    return finded;}int main(){    #ifdef LOCAL    freopen("xt4-6.in","r",stdin);    //freopen("xt4-6.out","w",stdout);    #endif    //读入单个字符编码    char temp,tempc;    int cn = 0,cm,ind = 0;    memset(c,0,sizeof(c));    memset(m,0,sizeof(m));    for(;;)    {        char temps[maxn];        scanf("%s", temps);        if(temps[0] == '*') break;        for(int i = 0; i < strlen(temps); i++)        {            if(isalpha(temps[i]) || isdigit(temps[i]))            {                c[cn] = temps[i];                cn++;                cm = 0;            }            else if(temps[i] == '.' || temps[i] == '-')            {                m[cn-1][cm] = temps[i];                cm ++;            }        }    }    //读入单个字符编码结束    //读入词典    int wordn = 0, wcn = 0, mwn = 0, maxmlen = 0;    bool firstc = true;    memset(word,0,sizeof(word));    memset(mword,0,sizeof(mword));    memset(mlen,0,sizeof(mlen));    for(;;)    {        char temps[maxn];        scanf("%s", temps);        if(temps[0] == '*') break;        for(int i = 0; i < strlen(temps); i++)        {            if(isalpha(temps[i]) || isdigit(temps[i]))            {                word[wordn][wcn] = temps[i];                wcn++;                for(int j = 0; j < strlen(c); j++)                {                    if(temps[i] == c[j])                    {                        for(int k = 0; k < strlen(m[j]);k++)                        {                            mword[wordn][mwn] = m[j][k];                            mwn++;                        }                        break;                    }                }            }        }        mlen[wordn] = mwn;        wordn++;        wcn = 0;        if(maxmlen<mwn) maxmlen = mwn;        mwn = 0;    }    //读入词典结束    //读入待解编码并输出对应结果    int coden = 0, cnn = 0;    firstc = true;    bool firstoutput = true;    memset(code,0,sizeof(code));    memset(clen,0,sizeof(clen));    for(;;)    {        char temps[maxn];        scanf("%s", temps);        if(temps[0] == '*') break;        for(int i = 0; i < strlen(temps); i++)        {            if(temps[i] == '.' || temps[i] == '-')            {                code[coden][cnn] = temps[i];                cnn++;            }        }        //输出code[coden]对应的word[]                int rtype;                rtype = check(code[coden],cnn,wordn,0);                if(rtype == 1) printf("%s\n", word[result]);                else if(rtype > 1) printf("%s!\n", word[result]);                else                {                    for(int i = 1; i < maxmlen; i++)                    {                        rtype = check(code[coden],cnn,wordn,i);                        if(rtype) break;                    }                    printf("%s?\n", word[result]);                }                result = -2;        //输出结束        coden++;        firstoutput = false;        clen[coden]=cnn;        cnn = 0;    }    //读入待解编码并输出对应结果结束    return 0;}


0 0
原创粉丝点击