| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 500 人关注过本帖
标题:新手,求帮助,程序崩溃,我是大一的 !!
只看楼主 加入收藏
amorvos
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2011-12-28
结帖率:0
收藏
已结贴  问题点数:20 回复次数:1 
新手,求帮助,程序崩溃,我是大一的 !!
题目
Problem D: 一种基于摩尔斯电码密码
Time Limit: 1 Sec  Memory Limit: 16 MB
Submit: 199  Solved: 94
[Submit][Status][Web Board]
Description

Morse code represents characters as variable length sequences of dots and dashes. In practice, characters in a message are delimited by short pauses. The following table shows the Morse code sequences:
A  .-  H  ....  O  ---  V  ...-
B  -...  I  ..  P  .--.  W  .--
C  -.-.  J  .---  Q  --.-  X  -..-
D  -..  K  -.-  R  .-.  Y  -.--
E  .  L  .-..  S  ...  Z  --..
F  ..-.  M  --  T  -      
G  --.  N  -.  U  ..-      


Note that four dot-dash combinations are unassigned. For the purposes of this problem we will assign them as follows (these are not the assignments for actual Morse code):
underscore  ..--  period  ---.
comma  .-.-  question mark  ----


Thus, the message "ACM_GREATER_NY_REGION" is encoded as:
.- -.-. -- ..-- --. .-. . .- - . .-. ..-- -. -.-- ..-- .-. . --. .. --- -.
M.E. Ohaver proposed an encryption scheme based on mutilating Morse code. Her scheme replaces the pauses between letters, necessary because Morse is a variable-length encoding that is not prefix-free, with a string that identifies the number of dots and dashes in each. For example, consider the message ".--.-.--". Without knowing where the pauses should be, this could be "ACM", "ANK", or several other possibilities. If we add length information, however, ".--.-.--242", then the code is unabiguous.
Ohaver's scheme has three steps, the same for encryption and decryption:
1. Convert the text to Morse code without pauses but with a string of numbers to indicate code lengths
2. Reverse the string of numbers
3. Convert the dots and dashes back into to text using the reversed string of numbers as code lengths
As an example, consider the encrypted message "AKADTOF_IBOETATUK_IJN". Converting to Morse code with a length string yields ".--.-.--..----..-...--..-...---.-.--..--.-..--...----.232313442431121334242". Reversing the numbers and decoding yields the original message "ACM_GREATER_NY_REGION".
Input

This problem requires that you implement Ohaver's encoding algorithm. The input will consist of several messages encoded with Ohaver's algorithm. The first line of the input is an integer n that specifies the number of test cases. The following n lines contain one message per line. Each message will use only the twenty-six capital letters, underscores, commas, periods, and question marks. Messages will not exceed 100 characters in length.
Output

For each message in the input, output the line number starting in column one, a colon, a space, and then the decoded message. The output format must be adhered to precisely.
Sample Input
5
AKADTOF_IBOETATUK_IJN
PUEL
QEWOISE.EIVCAEFNRXTBELYTGD.
?EJHUT.TSMYGW?EJHOT
DSU.XFNCJEVE.OE_UJDXNO_YHU?VIDWDHPDJIKXZT?E

Sample Output
1: ACM_GREATER_NY_REGION
2: PERL
3: QUOTH_THE_RAVEN,_NEVERMORE.
4: TO_BE_OR_NOT_TO_BE?
5: THE_QUICK_BROWN_FOX_JUMPS_OVER_THE_LAZY_DOG


[color=#ff0000]我的代码
[/color]




# include <string.h>
# include <stdio.h>
# define MAX 150
struct type
{
    char str [100];
    char let;
    int len;
}   letter[30]= {{".-",'A',2},{"-...",'B',4},{"-.-.",'C',4},{"-..",'D',3},{".",'E',1},{"..-.",'F',4},{"--.",'G',3},{"....",'H',4},{"..",'I',2},{".---",'J',4}, {"-.-",'K',3},{".-..",'L',4},{"--",'M',2},{"-.",'N',2},{"---",'O',3},{".--.",'P',4},{"--.-",'Q',4},{".-.",'R',3},{"...",'S',3},{"-",'T',1},{"..-",'U',3},{"...-",'V',4},{".--",'W',3},{"-..-",'X',4},{"-.--",'Y',4},{"--..",'Z',4},{"..--",'_',4},{".-.-",',',4},{"---.",'.',4},{"----",'?',4}};
int main ()
{
    int i,j,n,m,cha,v,z=0,y=0,s=0;
    int  lang    [MAX];
    char message [MAX];
    char yiciyi  [300];
    char e[5];
    scanf ("%d",&n);
    for ( i = 0 ; i < n ; i ++)
    {
        scanf ("%s",message);
        for ( j = 0 ; message [j] != '\0' ; j++ )
        {
            for  ( cha = 1 ; cha <= 30 ; cha++ )
            {
                if ( message [j]== letter[cha].let)
                {
                    y = j;
                    lang[j]=letter[cha].len;
                    strcat(yiciyi,letter[cha].str);
                    break;
                }
            }
        }
        for ( j = y ; j >= 0 ; j-- )
        {

            m = lang [j];
            for ( v = z; v < m + z; v++ )
            {

                e[s++]=yiciyi[v];
            }
            for  ( cha = 1 ; cha <= 30 ; cha++ )
            {
                if ( e == letter[cha].str)
                {
                    printf ("%c",letter[cha].let);
                }
                memset(e,0,100);
            }
            z = m + 1 ;
            s = 0;

        }
    }
}



[ 本帖最后由 amorvos 于 2011-12-28 16:27 编辑 ]
搜索更多相关主题的帖子: represents following practice message 
2011-12-28 15:52
beyondyf
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
等 级:贵宾
威 望:103
帖 子:3282
专家分:12654
注 册:2008-1-21
收藏
得分:20 
没多看,基本上看到你的输出格式就不对。
北大1051是这题,我是根据编码规则建立了一套新的映射表来完成这一任务的。下面是AC代码。
程序代码:
#include<stdio.h>
#define MAX_SIZE    200
unsigned char EC[] = {
/*0*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/*1*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/*2*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xa4, 0, 0x74, 0,
/*3*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xf4,
/*4*/ 0, 0x22, 0x14, 0x54, 0x13, 0x01, 0x44, 0x33, 0x04, 0x02, 0xe4, 0x53, 0x24, 0x32, 0x12, 0x73,
/*5*/ 0x64, 0xb4, 0x23, 0x03, 0x11, 0x43, 0x84, 0x63, 0x94, 0xd4, 0x34, 0, 0, 0, 0, 0xc4
};
char DC[] = {
/*0*/ 0, 'E', 'I', 'S', 'H', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/*1*/ 0, 'T', 'A', 'U', 'V', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/*2*/ 0, 0, 'N', 'R', 'F', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/*3*/ 0, 0, 'M', 'W', '_', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/*4*/ 0, 0, 0, 'D', 'L', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/*5*/ 0, 0, 0, 'K', ',', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/*6*/ 0, 0, 0, 'G', 'P', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/*7*/ 0, 0, 0, 'O', 'J', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/*8*/ 0, 0, 0, 0, 'B', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/*9*/ 0, 0, 0, 0, 'X', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/*a*/ 0, 0, 0, 0, 'C', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/*b*/ 0, 0, 0, 0, 'Y', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/*c*/ 0, 0, 0, 0, 'Z', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/*d*/ 0, 0, 0, 0, 'Q', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/*e*/ 0, 0, 0, 0, '.', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/*f*/ 0, 0, 0, 0, '?', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
void decode(char * encode_string, char * decode_string)
{
    int c[MAX_SIZE * 4], n[MAX_SIZE], i, j, tc, tn;
    for(i = j = 0; *encode_string != '\0'; encode_string++)
    {
        tc = EC[*encode_string] >> 4;
        tn = EC[*encode_string] & 0xf;
        n[i++] = tn;
        while(tn--){ c[j++] = tc & 1; tc >>= 1;}
    }
    for(j = 0; --i >= 0;)
    {
        for(tc = 0, tn = n[i]; tn--; tc = (tc << 1) + c[j++]);
        *(decode_string++) = DC[(tc << 4) + n[i]];
    }
    *decode_string = '\0';
}
int main()
{
    char encode_string[MAX_SIZE + 1], decode_string[MAX_SIZE + 1];
    int i, n;
    scanf("%d", &n);
    for(i = 1; i <= n; i++)
    {
        scanf("%s", encode_string);
        decode(encode_string, decode_string);
        printf("%d: %s\n", i, decode_string);
    }
    return 0;
}

重剑无锋,大巧不工
2011-12-28 20:21
快速回复:新手,求帮助,程序崩溃,我是大一的 !!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019708 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved