Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

ft啊,怎么回事?

Posted by hz02ruc at 2003-10-21 17:48:33 on Problem 1051
#include<stdio.h> 
#include<stdlib.h> 
#include<string.h> 
#define M 101 
#define N 6 
struct MorseCode 
{ 
    char code[N]; 
    char ch; 
}list[30]={{".-\0",'A'},{"-...\0",'B'},{"-.-.\0",'C'},{"-..\0",'D'},{".\0",'E'},{"..-.\0",'F'},{"--.\0",'G'}, 
        {"....\0",'H'},{"..\0",'I'},{".---\0",'J'},{"-.-\0",'K'},{".-..",'L'},{"--\0",'M'},{"-.\0",'N'}, 
        {"---\0",'O'},{".--.\0",'P'},{"--.-\0",'Q'},{".-.\0",'R'},{"...\0",'S'},{"-\0",'T'}, 
        {"..-\0",'U'},{"...-\0",'V'},{".--\0",'W'},{"-..-\0",'X'},{"-.--\0",'Y'},{"--..\0",'Z'}, 
        {"..--\0",'_'},{".-.-\0",','},{"---.\0",'.'},{"----\0",'?'}}; 
char *chrtocode(char ch) 
{ 
    char *p=(char *)malloc(N); 
    for(int i=0;i<30;i++) 
        if(ch==list[i].ch) 
        {    strcpy(p,list[i].code); 
            return p; 
        } 
} 
char codetochr(char *p) 
{ 
    for(int i=0;i<30;i++) 
        if(strcmp(p,list[i].code)==0) 
            return list[i].ch; 
} 
void decode(char *code,int casenum) 
{ 
    char str[4*M],result[M],temp[N],numstr[M]; 
    int codelen=strlen(code); 
    char *p; 
    int cur=0; 
    p=chrtocode(code[0]); 
    strcpy(str,p); 
    numstr[cur++]=strlen(p); 
    for(int i=1;i<codelen;i++) 
    { 
        p=chrtocode(code[i]); 
        int len=strlen(p); 
        strcat(str,p); 
        numstr[cur++]=len; 
    } 
    int k=0,n=0; 
    for(int j=cur-1;j>=0;j--) 
    {     
        memset(temp,'\0',N);         //开始用malloc为numstr开辟空间,结果发现
        for(int s=0;s<numstr[j];s++) //numstr[j]的值会变!!(对sample input中最后一组数据)
            temp[s]=str[k++];        //直接定义一个数组就过了,ft!
        result[n++]=codetochr(temp); 
    } 
    result[n]='\0'; 
    printf("%d: %s\n",casenum,result); 
} 
int main() 
{ 
    int testcase,i; 
    char code[M]; 
    scanf("%d",&testcase); 
    for(i=0;i<testcase;i++) 
    { 
    scanf("%s",code); 
    decode(code,i+1); 
    } 
    return 0; 
} 



Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator