444 - Encoder and Decoder

All about problems in Volume 4. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

Post Reply
Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Location: Dhaka, Bangladesh
Contact:

Post by Jan » Thu Sep 15, 2005 11:12 pm

Your code is correct. I just increased your array size and got it Accepted.

Use

Code: Select all

......
char input[810]; 
......
You will get Accepted. And delete the code you posted :) .
Ami ekhono shopno dekhi...
HomePage

silvere
New poster
Posts: 5
Joined: Mon Oct 31, 2005 9:45 am

444:why i get COMPILE ERROR?

Post by silvere » Tue Nov 01, 2005 4:59 am

run:gcc -g -o coder coder.c
ok.

but i get CE.WHY?

(forgive my poor english and so many functions;-)

Code: Select all

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int reverse(char* str)
{
	char c;
	int i,j;
	i=0;
	j=strlen(str)-1;
	while(i<j)
	{
		c=str[i];
		str[i]=str[j];
		str[j]=c;
		i++;
		j--;
	}
	return 0;
}

char* encode(char* str,char* res)
{
	int i;
	for(i=0;i<strlen(str) && str[i]!='\n' && str[i]!='\r';i++)
	{
		sprintf(res,"%s%d",res,str[i]);
	}
	reverse(res);
	return res;
}

//察看字符是否在规定的范围内
int judge_char(char c)
{
	if ( (c>='A' && c<='Z') || (c>='a' && c<='z') || c==' ' || c=='!' || c==',' || c=='.' || c==';' || c==':' || c=='?')
		return 0;
	else
		return 1;
}

char* decode(char * str,char* res)
{
	char j[1024]="",k[1024]="";
	int length,i,res_len=0;
	reverse(str);
	strcpy(k,str);
	length=strlen(str);	

	bzero(res,1024);
	for(i=0;i<length ;i++)
	{
		if(!(k[i]<='9' && k[i]>='0'))
		{
			str++;
			continue;
		}
		strncpy(j,str,2);
		j[2]='\0';
		if(judge_char(atoi(j)))
		{
			strncpy(j,str,3);
			j[3]='\0';
			if(judge_char(atoi(j)))
			{
				exit(1);
			}
			i+=2;
			str+=3;
			res_len++;
			sprintf(res,"%s%c",res,atoi(j));
		}
		else
		{
			i++;
			str+=2;
			res_len++;
			sprintf(res,"%s%c",res,atoi(j));
		}
	}
	res[res_len]='\0';
	return res;
}

int include_dig(char *str)
{
    int i;
    for(i=0;i<strlen(str);i++)
    {
	if(str[i]<='9' && str[i]>='0')
	    return 1;
    }
    return 0;
}

int include_char(char *str)
{
    int i;
    for(i=0;i<strlen(str);i++)
	if(!judge_char(str[i]) && str[i]!=' ')
	    return 1;
    return 0;
}

int main()
{
	char res[1024];
	char* str=(char*)malloc(sizeof(char)*1024);
	bzero(res,1024);
	bzero(str,1024);
	size_t len;
	while(getline(&str,&len,stdin) >0)
	{
		if(include_char(str))
			printf("%s\n",encode(str,res));
		else if(include_dig(str))
			printf("%s\n",decode(str,res));
		else
		{
			printf("%s\n",res);
		}
		bzero(res,1024);
		bzero(str,1024);
	}
	free(str);
	return 0;
}

butterfly.

Solaris
Learning poster
Posts: 99
Joined: Sun Apr 06, 2003 5:53 am
Location: Dhaka, Bangladesh
Contact:

Post by Solaris » Tue Nov 01, 2005 6:47 pm

In response to your submission you should get a mail in your specified address (if you had not turned that option off). In that mail the details about the compile error i.e. the error messages are provided. Those emails help me a lot to get rid of my CEs :P

Anyway I have tried to run your code in VC. But it shows the following error messages:
1. error C2065: 'bzero' : undeclared identifier
2. error C2065: 'getline' : undeclared identifier
I think u know what to do :)
Where's the "Any" key?

akayg
New poster
Posts: 2
Joined: Tue Jan 24, 2006 1:54 pm

444 WA

Post by akayg » Tue Jan 24, 2006 2:00 pm

444
i think my code is correct.
i tested it many cases that i saw in this forum.
but, i got WA.

Code: Select all

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main()
{
	char buf[880];
	char tmp[2];
	char tmp2[3];
	int num;
	int i;
	while (fgets(buf, sizeof(buf), stdin))
	{
		buf[strlen(buf)-1] = '\0';
		/*
		while (buf[strlen(buf)-1] == ' ')
			buf[strlen(buf)-1] = '\0';
		*/
		for (i=strlen(buf)-1; i>=0; i--) {
			if (buf[i] >= 48 && buf[i] <= 57) {
				if (buf[i] == '1') {
					tmp2[0] = buf[i--];
					tmp2[1] = buf[i--];
					tmp2[2] = buf[i];
					num = atoi(tmp2);
				} else {
					tmp[0] = buf[i--];
					tmp[1] = buf[i];
					num = atoi(tmp);
				}
				printf("%c", num);
			} else if ((buf[i] >= 65 && buf[i] <=90) || 
				(buf[i]>=97 && buf[i]<=122) || buf[i] ==32 || buf[i]==33 ||
				buf[i]==44 || buf[i]==46 || buf[i]==58 || buf[i]==59 || buf[i]==63)
			{
				if (buf[i] > 99)
					printf("%d%d%d", buf[i]%10, buf[i]/10%10, buf[i]/10/10);
				else
					printf("%d%d", buf[i]%10, buf[i]/10);
			}
		}
		printf("\n");
	}
	return 0;
}
who has the idea?
thanks for read.
Last edited by akayg on Wed Jan 25, 2006 2:04 am, edited 1 time in total.

chunyi81
A great helper
Posts: 293
Joined: Sat Jun 21, 2003 4:19 am
Location: Singapore

Post by chunyi81 » Tue Jan 24, 2006 4:59 pm

You were very careless.

Read the problem description again:
The following is an ASCII table of the valid characters in a message:


"A" 65 "a" 97 " " 32
"B" 66 "b" 98 "!" 33
. . "," 44
. . "." 46
. . ":" 58
"Y" 89 "y" 121 ";" 59
"Z" 90 "z" 122 "?" 63
You left out the ";" character. (ASCII 59)

akayg
New poster
Posts: 2
Joined: Tue Jan 24, 2006 1:54 pm

that's my mistake.

Post by akayg » Wed Jan 25, 2006 2:09 am

i edit my code.

but, No aceepted.

Psyco
New poster
Posts: 14
Joined: Sat Jan 21, 2006 12:39 pm
Contact:

444 Why WA? I Have to finish!!!!! HELP!!!

Post by Psyco » Fri Jan 27, 2006 3:00 pm

#include<stdio.h>
#include<string.h>
char str[10000]; int i, j, len;
int main()
{
while(gets(str))
{
len=strlen(str);
for(i=len;i>=0;i--)
{
switch(str)
{
case ' ' : printf("23"); str=0; break; case '!' : printf("33"); str=0; break; case ',' : printf("44"); str=0; break; case '.' : printf("64"); str=0; break; case ':' : printf("85"); str=0; break; case ';' : printf("95"); str=0; break; case '?' : printf("36"); str=0; break; case 'a' : printf("79"); str=0; break; case 'b' : printf("89");str=0; break; case 'c' : printf("99");str[i]=0; break; case 'd' : printf("001");str[i]=0; break; case 'e' : printf("101");str[i]=0; break; case 'f' : printf("201");str[i]=0; break; case 'g' : printf("301");str[i]=0; break; case 'h' : printf("401");str[i]=0; break; case 'i' : printf("501");str[i]=0; break; case 'j' : printf("601");str[i]=0; break; case 'k' : printf("701");str[i]=0; break; case 'l' : printf("801");str[i]=0; break; case 'm' : printf("901");str[i]=0; break; case 'n' : printf("011");str[i]=0; break; case 'o' : printf("111");str[i]=0; break; case 'p' : printf("211");str[i]=0; break; case 'q' : printf("311");str[i]=0; break; case 'r' : printf("411");str[i]=0; break; case 's' : printf("511");str[i]=0; break; case 't' : printf("611");str[i]=0; break; case 'u' : printf("711");str[i]=0; break; case 'v' : printf("811");str[i]=0; break; case 'w' : printf("911");str[i]=0; break; case 'x' : printf("021");str[i]=0; break;
case 'y' : printf("121");str[i]=0; break; case 'z' : printf("221");str[i]=0; break; case 'A' : printf("56"); str[i]=0; break; case 'B' : printf("66"); str[i]=0; break; case 'C' : printf("76"); str[i]=0; break; case 'I' : printf("37"); str[i]=0; break;case 'O' : printf("97"); str[i]=0; break;case 'U' : printf("58"); str[i]=0; break; case 'D' : printf("86"); str[i]=0; break; case 'J' : printf("47"); str[i]=0; break;case 'P' : printf("08"); str[i]=0; break;case 'V' : printf("68"); str[i]=0; break; case 'E' : printf("96"); str[i]=0; break; case 'K' : printf("57"); str[i]=0; break;case 'Q' : printf("18"); str[i]=0; break;case 'W' : printf("78"); str[i]=0; break; case 'F' : printf("07"); str[i]=0; break; case 'L' : printf("67"); str[i]=0; break;case 'R' : printf("28"); str[i]=0; break;case 'X' : printf("88"); str[i]=0; break; case 'G' : printf("17"); str[i]=0; break; case 'M' : printf("77"); str[i]=0; break;case 'S' : printf("38"); str[i]=0; break;case 'Y' : printf("98"); str[i]=0; break; case 'H' : printf("27"); str[i]=0; break; case 'N' : printf("87"); str[i]=0; break;case 'T' : printf("48"); str[i]=0; break;case 'Z' : printf("09"); str[i]=0; break;
default :
if(str[i]!=0)
{
if( i>=2 && (str[i]-'0')*100+(str[i-1]-'0')*10+(str[i-2]-'0')>=32 && (str[i]-'0')*100+(str[i-1]-'0')*10+(str[i-2]-'0')<=122 && str[i-1]!=' ' && str[i-2]!=' '){
printf("%c",(str[i]-'0')*100+(str[i-1]-'0')*10+(str[i-2]-'0'));
str[i-2]=str[i-1]=str[i]=0;
i-=2;
}
else if( (str[i-1]-'0')+(str[i]-'0')*10>=32 && i>=1 && str[i]<='9' && str[i]>='0' && str[i-1]<='9' && str[i]>='0' && str[i]!=' ' && str[i-1]!=' ')
{
printf("%c",(str[i-1]-'0')+(str[i]-'0')*10);
str[i]=str[i-1]=0; i--;
}
else printf("%c",str[i]);
}
break;
}

}
printf("\n");
}
return 0;
}

Why WA?
I Want to Error Data.... Plz !!!!!!!!!!!!!!!!!!!!!!!!!!!!!

kolpobilashi
Learning poster
Posts: 54
Joined: Mon Jan 02, 2006 3:06 am
Location: Dhaka,Bangladesh
Contact:

444 RE!!! sumbuddy help meee....

Post by kolpobilashi » Sun Jun 18, 2006 8:53 am

may b my logic is wrong.but the code below gave the correct output for the sample input given in the problem. at 1st i use the "strrev" function in the "decoder" part and got CE and got the following msg

[size=18]04657187_24.c: In function `void decoder(char *)':
04657187_24.c:43: implicit declaration of function `int strrev(...)'[/size]

later i did that manually and got RE and got
[size=12][/size]

"[size=18]Your program has died with signal 11 (SIGSEGV). Meaning:
Invalid memory reference
Before crash, it ran during 0.002 seconds[/size]"
Last edited by kolpobilashi on Mon Jun 19, 2006 5:39 pm, edited 1 time in total.
Sanjana

User avatar
emotional blind
A great helper
Posts: 383
Joined: Mon Oct 18, 2004 8:25 am
Location: Bangladesh
Contact:

Post by emotional blind » Sun Jun 18, 2006 10:53 pm

May be gcc does not support strrev() function:

User avatar
emotional blind
A great helper
Posts: 383
Joined: Mon Oct 18, 2004 8:25 am
Location: Bangladesh
Contact:

Post by emotional blind » Sun Jun 18, 2006 11:23 pm

problem description

Code: Select all

spy's message is at most 80 characters long,
but encoded messege may be 3*80=240 character long at maximum, isnt it?


So take array size 241 to avoid run time error.

kolpobilashi
Learning poster
Posts: 54
Joined: Mon Jan 02, 2006 3:06 am
Location: Dhaka,Bangladesh
Contact:

Post by kolpobilashi » Mon Jun 19, 2006 4:50 am

thanx a lot. i have done wat u told,increased the array size upto "244"
but this time got WA :(

wat's wrong in my code? :cry:
Sanjana

User avatar
emotional blind
A great helper
Posts: 383
Joined: Mon Oct 18, 2004 8:25 am
Location: Bangladesh
Contact:

Post by emotional blind » Mon Jun 19, 2006 4:19 pm

check your revDigit() function

what should be the reverse of 100,
1 or 001
think about it

User avatar
emotional blind
A great helper
Posts: 383
Joined: Mon Oct 18, 2004 8:25 am
Location: Bangladesh
Contact:

Post by emotional blind » Mon Jun 19, 2006 4:59 pm

before

Code: Select all

return (rev);
add this

Code: Select all

if(len==3){
	if(rev<10)printf("00");
	else if(rev<100)printf("0");
}
else if(len==2){
	if(rev<10)printf("0");
}

another thing
it is a good practice to give your code in code(as I did),
otherwise it is very difficult to read it


and dont forget to remove your code after get ACCEPTED

kolpobilashi
Learning poster
Posts: 54
Joined: Mon Jan 02, 2006 3:06 am
Location: Dhaka,Bangladesh
Contact:

Post by kolpobilashi » Mon Jun 19, 2006 5:41 pm

thanx a lot Arif bhai. atlast i got AC. thnx 4 all the suggestions.:D
Sanjana

bongssi
New poster
Posts: 14
Joined: Mon Jul 31, 2006 10:35 am

444 WA, plz help me!!

Post by bongssi » Sun Aug 06, 2006 12:46 pm

I don't know why it's wrong. I have tried many test cases, but it seeems to work correctly. Please find bugs in this program!!


#include <stdio.h>
#include <string.h>

char result[1000], input[1000];
int result_top, str_length;

int power(int m, int n){ /* returns m^n */

int i, result=1;
for(i=0; i<n; i++)
result *= m;

return result;
}

void init_result(char result[1000]){

int i;
for(i=0; i<1000; i++)
result = 0;
}

void decode(char input[1000], char result[1000]){

int i;
char code;
for(i=str_length-1; i>=0; ){

code = 0;
while(!(code>=32 && code<=122)){

code *= 10;
code += input[i--] - '0';
}

result[result_top++] = code;
}
}

void incode(char input[1000], char result[1000]){

int i, digits;
char code, tmp;
for(i=0; i<str_length; i++){

code = input;
digits = 0;
tmp = code;

do{ /*get number of digits*/
tmp/=10;
digits++;
}while(tmp > 0);

tmp = code;

while(digits >= 1){

result[result_top++] = (tmp / power(10, digits-1) + '0');

tmp %= power(10, digits-1);

digits--;
}
}
}

int main(void){

int is_decode, i;
while(scanf("%[^\n]", input) == 1){

init_result(result);
str_length = strlen(input);
if(str_length > 80) str_length = 80;

result_top = 0;
if(input[0] >= '0' && input[0] <= '9') is_decode = 1;
else is_decode = 0; /*incode*/

if(is_decode){ /*decoding*/

decode(input, result);

for(i=0; i<result_top; i++)
printf("%c", result);

printf("\n");
}

/*incoding*/

else{

incode(input, result);

for(i=result_top-1; i>=0; i--)
printf("%c", result);

printf("\n");
}

fflush(stdin);
}

return 0;
}

Post Reply

Return to “Volume 4 (400-499)”