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
User avatar
rio
A great helper
Posts: 385
Joined: Thu Sep 21, 2006 5:01 pm
Location: Kyoto, Japan

Post by rio » Wed Apr 04, 2007 3:18 pm

HINT
Manpage of atoi():
NAME
atoi, atol, atoll, atoq - convert a string to an integer

SYNOPSIS
#include <stdlib.h>

int atoi(const char *nptr);
long atol(const char *nptr);
long long atoll(const char *nptr);
long long atoq(const char *nptr);

DESCRIPTION
The atoi() function converts the initial portion of the string pointed
to by nptr to int.

RED-C0DE
New poster
Posts: 16
Joined: Mon Mar 26, 2007 6:47 pm

Post by RED-C0DE » Thu Apr 05, 2007 11:35 am

Tanks Rio. I got AC :P . I didn't use atoi function and wrote a function to get Numeric Value of a string myself. so I have 1 question again>>:

Is better that we using standard functions of C , C++ or write them ourself?

my answer now is : We should writing functions that we need ourself. and how about U?

ranacse05
New poster
Posts: 37
Joined: Wed Mar 28, 2007 5:08 pm
Location: Rajshahi
Contact:

Runtime Error

Post by ranacse05 » Sun May 06, 2007 7:44 am

Here is my code plz tell me why i getting RE?

Code: Select all

[color=green]#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>

int main()
    {
    int i,j,x,y;
    char a[81];
    while(gets(a))
    {
     int ln=strlen(a);

     if(isdigit(a[0]))
      {
       for(i=ln-2;i>=0;i=i-2)
	    {
	    char b[4]={0};
	    if((a[i]-'0')<3)
	     {
	     b[1]=a[i];
	     b[0]=a[i+1];
	     b[2]=a[i-1];
	     i--;
	     }
	     else
	     {
	     b[1]=a[i];
	     b[0]=a[i+1];
	     }
	     x=atoi(b);
	     printf("%c",x);

	    }
      }
      else
      {
     for(j=ln-1;j>=0;j--)
     { x=a[j];
     for(i=0;;i++)
     { y=x%10;
     //if(y!=0)
      printf("%d",y);
      x=x/10;
      if(x==0)
      break;
      }
     }
       }


     printf("\n");
     }
    return 0;
    }[/color]

I like to solve problems.

sfelixjr
New poster
Posts: 9
Joined: Wed Apr 25, 2007 3:29 pm
Location: Brazil
Contact:

PE

Post by sfelixjr » Fri May 18, 2007 4:21 pm

i got presentation error, but this problem output is very simple, i cant see where my mistake is, does anybody here can help me?

ishtiaq ahmed
Learning poster
Posts: 53
Joined: Sat Jul 29, 2006 7:33 am
Location: (CSE,DU), Dhaka,Bangladesh

444(WA)

Post by ishtiaq ahmed » Wed Jul 18, 2007 3:25 pm

I have tested all the input of the board and and matched the output. Why i am facing WA. Plz help me

Code: Select all

CUT AFTER ACCEPTED
No venture no gain

with best regards
------------------------
ishtiaq ahmed

RC's
Learning poster
Posts: 65
Joined: Fri Jul 13, 2007 3:17 pm

Post by RC's » Tue Jul 24, 2007 8:41 am

I got RE for this problem.. What's wrong with my code ?

Code: Select all

removed
I got AC already
Hm... I wonder why it was runtime error when I declared the array size for input = 85 and output = 255... I increased both of them to 1000 and got AC..
The maximum of spy message is 80, right ? so it's minimum size is 81
so the maximum output should be 80 * 3 = 240, so it's minimum size is 241...

mirage
New poster
Posts: 11
Joined: Sat Jan 19, 2008 9:37 pm

444 Encode Decode Getting Wrong Answer

Post by mirage » Fri Feb 01, 2008 1:11 pm

hi friends,
I m gettin wrong answer for this problem but can't figure out why....
Tried various inputs but works for all of them....
Is thr any special input???
Plz help

here's the code...

Code: Select all

//encoder and decoder
#include<iostream>
#include<string>
using namespace std;
int main(){
	string s;
	while(getline(cin,s,'\n')){
		//check category 
		int i=s.length()-1;
		int val=s[i]-'0';
		if(val>=0 && val<=9){
			//this is the encoded one decode it
			i=s.length()-1;
			while(i>0){
				int j=int(s[i--]-'0')*10+int(s[i--]-'0');
				if(j<33) j=j*10+int(s[i--]-'0');
				cout<<char(j);
				if(s[i]==' ') i=0;
			}
		}
		else{
			i=s.length()-1;
			while(i>=0){
				int temp=int(s[i]);
				if(temp<100){
					cout<<temp%10<<int(temp/10);
				}
				else cout<<temp%10<<int(temp/10)%10<<int(temp/100);	
				i=i-1;
			}
		}
		cout<<"\n";
	}
	return(0);	
}
[/code]

mirage
New poster
Posts: 11
Joined: Sat Jan 19, 2008 9:37 pm

Post by mirage » Sun Feb 03, 2008 5:52 pm

hi friends,
I m gettin wrong answer for this problem but can't figure out why....
Tried various inputs but works for all of them....
Is thr any special input???
Plz help

here's the code...

Code: Select all

//encoder and decoder
#include<iostream>
#include<string>
using namespace std;
int main(){
   string s;
   while(getline(cin,s,'\n')){
      //check category
      int i=s.length()-1;
      int val=s[i]-'0';
      if(val>=0 && val<=9){
         //this is the encoded one decode it
         i=s.length()-1;
         while(i>0){
            int j=int(s[i--]-'0')*10+int(s[i--]-'0');
            if(j<33) j=j*10+int(s[i--]-'0');
            cout<<char(j);
            if(s[i]==' ') i=0;
         }
      }
      else{
         i=s.length()-1;
         while(i>=0){
            int temp=int(s[i]);
            if(temp<100){
               cout<<temp%10<<int(temp/10);
            }
            else cout<<temp%10<<int(temp/10)%10<<int(temp/100);   
            i=i-1;
         }
      }
      cout<<"\n";
   }
   return(0);   
}

gvs
New poster
Posts: 6
Joined: Sun Jun 19, 2005 7:39 pm

strange

Post by gvs » Thu Feb 07, 2008 7:58 pm

Hi all,
can anybody help me?
I have tested my code for all the I/O posted on the board, yet the OJ is givin me WA.
It is really frustrating.
Can anybody please help?
maKe wHat yoU waNt...... you'll succeed

AcmNightivy
New poster
Posts: 36
Joined: Tue Dec 04, 2007 10:20 am

Post by AcmNightivy » Sat Feb 09, 2008 11:12 am

my god..this code give the correct ans i found..and i have considered the range of the array..but it change from WA to RE..and till now..I only correct one code..depressed..help..

Code: Select all

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

void Encode (char str[])
{
	int i, loop, count;
	char result[241];
	int temp;

	count = 0;

	for (i = 0; str[i] != '\0'; i++)
	{
		temp = (int)str[i];
		if (temp >= 100)
			loop = 3;
		else
			loop = 2;
				
		if (loop == 2)
		{
			result[count++] = temp / 10 + 48;
			result[count++] = temp % 10 + 48;
		}
		else
		{
			result[count++] = temp / 100 + 48;
			temp = temp % 100;
			result[count++] = temp / 10 + 48;
			result[count++] = temp % 10 + 48;
		}	
	}
	count--;
	while (count >= 0)
		cout<<result[count--];
	cout<<endl;
}

void Translate (char str[])
{
	int i;
	int count;
	
	count = 0;
	for (i = strlen (str) - 1; i >= 0; i--)
	{
		count = count * 10 + str[i] - 48;
		if ((count >= 65 && count<= 122) || count == 32 
			|| count == 33 || count == 44 || count == 46 
			|| count == 58 || count == 59 || count == 63)
		{
			cout<<(char)count;
			count = 0;
		}
	}
	cout<<endl;
}

int main ()
{
	char str[82];
	
	while (gets (str))
	{
		if (str[0] > '9' || str[0] < '0')
			Encode (str);
		else
			Translate (str);
	}

	return 0;
}

murad357
New poster
Posts: 2
Joined: Wed Mar 26, 2008 7:33 pm

444 Wrong Answer (Java)

Post by murad357 » Wed Apr 02, 2008 5:52 pm

Hello Friends,

I know this is a very easy problem. But looks like I am missing something here. I am getting WA for this code, while I have tested all the sample input given in this forum. Any help will be appreciated. (I also tried this with BufferedReader instead of Scanner).

Note: I have two other static method reverse and isDigit which I didn't include in this code

Code: Select all

public static void main (String args[]) throws IOException   // entry point from OS
    {   
    	Scanner in = new Scanner (System.in);
        try 
        {
        	while (in.hasNext()) 
        	{
        		String str = in.nextLine();
        		String temp="",tmp="";
        		if (str == null) // end of file reached
        			break;
        		else
        		{
        			if (isDigit(str.charAt(0)))
                	{
                		temp = reverse(str);
                		for (int i=0; i< temp.length();)
                		{
                			String t= temp.substring(i);
                			if (t.charAt(0) == '1')
                			{
                				i+=3;
                				char chr = (char) Integer.parseInt(t.substring(0,3));
                				tmp+=chr;
                			}
                			else
                			{
                				i+=2;
                				char chr = (char) Integer.parseInt(t.substring(0,2));
                				tmp+=chr;
                			}
                		}
                		System.out.println(tmp);
                	}
                	else
                	{
                		for (int i=0; i<str.length();i++)
                		{
                			int car = str.charAt(i);
                			temp+=car;
                		}
                		temp = reverse(temp);
                		System.out.println(temp);
                	}	
        		} // end try
        	}// end while
        }
        catch (Exception e)
        {
        	System.exit(0);
        }
    }// End Main

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

Re: 444 Wrong Answer (Java)

Post by Jan » Sun Apr 06, 2008 11:11 am

Search the board first. Don't open a new thread if there is one already.
Ami ekhono shopno dekhi...
HomePage

zyxw
New poster
Posts: 24
Joined: Sat Mar 22, 2008 5:49 am
Location: Chennai
Contact:

444 - WA

Post by zyxw » Wed May 21, 2008 6:31 pm

I cant find where i'm wrong :o

i have checked all the sample i/p in this thread and the following threads:
http://online-judge.uva.es/board/viewto ... =7844#wrap
http://online-judge.uva.es/board/viewto ... 66b4e#wrap

i get exactly same o/p as mentioned in above threads...
so i present my code here...

Code: Select all

#include<iostream>      // acm-444
#include<vector>
#include<cctype>
using namespace std;
#define pb push_back
#define sz size()

void encode(string s)
{
 string ans="";
 for(int i=s.sz-1; i>=0; i--)
 {
  char* t1=new char;
  int a = (int) s[i];
  sprintf(t1,"%d",a);
  
  string t="";
  for( int j=0; t1[j]!='\0'; j++ )
  {
   t+=t1[j];
  }
  reverse(t.begin(),t.end());
  ans+=t;
 }
 cout<<ans<<endl;
}



void decode(string s)
{
 string ans="";
 
 for(int i=s.sz-1; i>=0; )
 {
  string t="";
  t+= s[i];
  t+= s[i-1];
  int a=0;
  sscanf(t.c_str(),"%d",&a);
    
  if( (a>=65 && a<=90) || (a>=97 && a<=122) || (a==32) || (a==33) || (a==44) || (a==46) || (a==58) || (a==59) || (a==63) )
  {
   i-=2;
   ans+= (char) a ;
  }
  else
  {
   t+=s[i-2];
   i-=3;
   a=0;
   sscanf(t.c_str(),"%d",&a);
   ans+= (char) a ;
  }
 }
 cout<<ans<<endl;
}



int main()
{
 string s;
 while( !cin.eof() )
 {
  getline(cin,s);
  if(s=="")
   printf("\n");
  else if( isalpha(s[0]) || (s[0]==' ') || (s[0]=='!') || (s[0]==',') || (s[0]=='.') || (s[0]==':') || (s[0]==';') || (s[0]=='?') )
   encode(s);
  else
   decode(s);
 }
}
Somebody pls help :roll:
I am not totally useless, because I can still be used as a bad example :P

hasib_bd
New poster
Posts: 14
Joined: Wed Apr 30, 2008 12:39 pm

Re: Why #444 WA?

Post by hasib_bd » Sat May 31, 2008 4:08 pm

Following considerations may help to avoid WA for this problem.

1. Consider the array size at least to hold 80 * 3(if the ASCII is >= 100) + 1(Null char)= 2401 characters.
2. Consider the reverse of 100 (for d) as 001 and same for others ending with 0.
3. Consider that there may be empty line in the Input. For those empty line you must also print an empty line. (I was getting WA because i wasn't printing anything when there was an empty line in the input.

Hope it might help.

palash
New poster
Posts: 4
Joined: Wed Apr 16, 2008 8:49 pm

Re: Why #444 WA?

Post by palash » Mon Jun 16, 2008 9:15 pm

pls help, every time i got WA with my code. here is my code

Code: Select all

At last Acc

Post Reply

Return to “Volume 4 (400-499)”