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

rahid
New poster
Posts: 9
Joined: Tue Oct 13, 2009 7:13 am
Location: Asian University of Bangladesh
Contact:

444 - Encoder and Decoder (runtime Error!)

Post by rahid » Sat Dec 03, 2011 6:33 pm

I got runtime error for my code. Please give me some clue why its happened??

Code: Select all

Code Deleted After Solved!! :)

DivinaBack76
New poster
Posts: 1
Joined: Sun Jan 15, 2012 9:58 pm
Location: Bosnia Herzegovina
Contact:

implant dentists

Post by DivinaBack76 » Sun Jan 15, 2012 9:59 pm

I completely agree with everything that was said, well put.

anubhavhcm
New poster
Posts: 2
Joined: Mon Jan 23, 2012 6:59 pm

Q 444, Time Limit Exceeded

Post by anubhavhcm » Mon Jan 23, 2012 7:10 pm

I am getting "time limit exceeded". Plz help how can i reduce the runtime??? :(


#include<iostream>
#include<cstring>
#include<cstdlib>
using namespace std;

void decode(char msg[])
{
int len=strlen(msg),n;
char m;

for (int i=len-1;i>=0;i--)
{
n=0;
if (msg=='1')
{
n=(msg-'0')*100 + (msg[i-1]-'0')*10 + (msg[i-2]-'0');
i-=2;

}
else
{
n=(msg-'0')*10 + (msg[i-1]-'0');
i--;
}
m=n;
cout<<m;
}
cout<<endl;
}

void encode(char msg[])
{
int l=strlen(msg),n;

for (int i=l-1;i>=0;i--)
{
n=msg;
if (n/100==0)
cout<<n%10<<n/10;
else
cout<<n%10<<(n/10)%10<<n/100;
}
cout<<endl;
}

int main()
{
char msg[85];
cin.getline(msg,80);

while(!cin.eof())
{
if (atoi(msg))
{
decode(msg);
}
else
{
encode(msg);
}

cin.getline(msg,80);
}
}

anubhavhcm
New poster
Posts: 2
Joined: Mon Jan 23, 2012 6:59 pm

Re: Q 444, Time Limit Exceeded

Post by anubhavhcm » Tue Jan 24, 2012 2:55 pm

any one plz help!!! :(

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: Q 444, Time Limit Exceeded

Post by brianfry713 » Wed Jan 25, 2012 1:37 am

scanf and printf are faster than cin and cout. I used getchar() to read in each line and then I already had the length of the string, eliminating strlen() will speed up your code. You can check if the first character is a digit or not instead of calling atoi().
Check input and AC output for thousands of problems on uDebug!

cse.mehedi
New poster
Posts: 36
Joined: Sun Mar 18, 2012 8:18 am

444 why RE??

Post by cse.mehedi » Thu Apr 12, 2012 9:55 am

Hele me any one!!! :(
it shows "This means that the execution of your program didn't finish properly. Remember to always terminate your code with the exit code 0."

Code: Select all

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

int rev(int n)
{
    int x=0,q=0,s=0,t=0;
    s=n;
    if(s>99)
    {
        x=100;
        while(s!=0)
        {
            q=s%10;
            s/=10;
            t=t+(q*x);
            x/=10;
        }
    }
    else
    {
        x=10;
        while(s!=0)
        {
            q=s%10;
            s/=10;
            t=t+(q*x);
            x/=10;
        }
    }
    return t;
}

void disOutput(char str[81])
{
    int n=1,i,s=0;
    for(i=strlen(str)-1;i>=0;i--)
    {
        if((int)str[i]>=48 && (int)str[i]<=57)
        {
            s+=((int)str[i]-48)*n;
            n*=10;
            if(s>=32)
            {
               printf("%c",(char)rev(s));
               s=0,n=1;
            }
        }
        else
        {
            printf("%d",rev((int)str[i]));
        }
    }
    printf("\n");
    return 0;
}

void main()
{
    char str[81];
    while(gets(str))
    disOutput(str);
    return 0;
}


brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 444 why RE??

Post by brianfry713 » Fri Apr 13, 2012 12:26 am

main should return int.
Check input and AC output for thousands of problems on uDebug!

cse.mehedi
New poster
Posts: 36
Joined: Sun Mar 18, 2012 8:18 am

Re: 444 why RE??

Post by cse.mehedi » Fri Apr 13, 2012 12:30 am

brianfry713 wrote:main should return int.
I used return 1; but again RE!! :(

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 444 why RE??

Post by brianfry713 » Mon Apr 16, 2012 10:05 pm

change:
void main()
to:
int main()

return 0 not 1.
Check input and AC output for thousands of problems on uDebug!

sith
Learning poster
Posts: 72
Joined: Sat May 19, 2012 7:46 pm

Re: 444 - Encoder and Decoder (runtime Error!)

Post by sith » Sat Jul 14, 2012 1:20 pm

Hello
I've got Runtime error
Why????

Code: Select all

import java.io.*;
import java.util.HashMap;
import java.util.Map;

class Main {
    public static void main(String[] args) {
        Map<Character, Integer> codes = new HashMap<Character, Integer>();
        Map<Integer, Character> decodes = new HashMap<Integer, Character>();

        for (int i = 'A'; i <= 'Z'; i++) {
            codes.put((char) i, i - 'A' + 65);
            decodes.put(i - 'A' + 65, (char) i);
        }
        for (int i = 'a'; i <= 'z'; i++) {
            codes.put((char) i, i - 'a' + 97);
            decodes.put(i - 'a' + 97, (char) i);
        }

        codes.put(' ', 32);
        codes.put('!', 33);
        codes.put(',', 44);
        codes.put('.', 46);
        codes.put(':', 58);
        codes.put(';', 59);
        codes.put('?', 63);

        decodes.put(32, ' ');
        decodes.put(33, '!');
        decodes.put(44, ',');
        decodes.put(46, '.');
        decodes.put(58, ':');
        decodes.put(59, ';');
        decodes.put(63, '?');

        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));

        try {
            String line;
            while ((line = reader.readLine()) != null) {
                StringBuilder result = new StringBuilder();
                if (Character.isLetter(line.charAt(0))) {
                    encode(line, result, codes);
                } else {
                    decode(line, result, decodes);
                }
                writer.write(result.append('\n').toString());
            }
            writer.flush();
        } catch (IOException e) {
        }
    }

    private static void encode(String line, StringBuilder result, Map<Character, Integer> codes) {
        char[] lineArray = line.toCharArray();
        reverse(lineArray);
        for (Character c : lineArray) {
            char[] chars = codes.get(c).toString().toCharArray();
            reverse(chars);
            for (char aChar : chars) {
                result.append(aChar);
            }
        }
    }

    private static void reverse(char[] chars) {

        for (int i = 0; i < chars.length / 2; i++) {
            char tmp = chars[i];
            int switchIntdex = chars.length - 1 - i;
            chars[i] = chars[switchIntdex];
            chars[switchIntdex] = tmp;
        }
    }

    private static void decode(String line, StringBuilder result, Map<Integer, Character> codes) {
        int startIndex = 0;
        int endIndex = 1;

        char[] chars = line.toCharArray();
        reverse(chars);

        while (endIndex < chars.length) {
            if (chars[startIndex] == '1') {
                endIndex++;
            }
            result.append(codes.get(Integer.valueOf(new String(chars, startIndex, endIndex-startIndex+1))));
            startIndex = endIndex + 1;
            endIndex = startIndex + 1;

        }
    }
}

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 444 - Encoder and Decoder (runtime Error!)

Post by brianfry713 » Tue Jul 17, 2012 1:03 am

I believe there is a blank line in the judge's input.
Check input and AC output for thousands of problems on uDebug!

sumit saha shawon
New poster
Posts: 19
Joined: Tue Jun 26, 2012 9:19 pm

Re: 444 - Encoder and Decoder (runtime Error!)

Post by sumit saha shawon » Fri Dec 28, 2012 11:33 pm

why getting wa???????????? :o

code:

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<vector>
using namespace std;
char mes[10000];
int func(int n)
{
int a=0;
while(n)
{
a=a*10+(n%10);
n=n/10;

}
return a;
}
int main()
{

while(gets(mes))
{
vector<char>ans;
vector<int>line;
int len=strlen(mes),n;
if(mes[0]-48>=0&&mes[0]-48<=10)
{
for(int i=len-1; i>=0; i--)
{
if(mes-'0'>=3)
{
n=(mes-'0')*10+mes[i-1]-'0';
i--;
ans.push_back(n);
}
else if(mes-'0'==1)
{
n=((mes-'0')*100)+((mes[i-1]-'0')*10)+(mes[i-2]-'0');
i-=2;
ans.push_back(n);

}

}
for(int i=0; i<ans.size(); i++)
cout<<ans;
cout<<endl;
}
else
{
for(int i=len-1; i>=0; i--)
{
n=mes;
n=func(n);
line.push_back(n);
}
for(int i=0; i<line.size(); i++)
cout<<line;
cout<<endl;

}


}
return 0;
}

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 444 - Encoder and Decoder (runtime Error!)

Post by brianfry713 » Sat Dec 29, 2012 11:22 pm

Try input Z
Check input and AC output for thousands of problems on uDebug!

sirajul
New poster
Posts: 6
Joined: Sat Mar 23, 2013 12:07 am

Re: 444 - Encoder and Decoder (runtime Error!)

Post by sirajul » Thu Apr 18, 2013 11:14 am

Code: Select all

got acc
you can try these I/O

Code: Select all

aefd
z?.: ;e
001201
abc
67798999





Have a Nice Day !
 ! , . : ; ?
!,.:;?
abcd
001998979
output for the inputs

Code: Select all

00120110179
1019523856436221
fd
998979
cbaL





332312179862310199501872379231018117927
362395238523642344233323
369585644433
001998979
abcd

RAT
New poster
Posts: 1
Joined: Thu Nov 21, 2013 7:12 pm

Re: 444 (Encoder Decoder) Why W A? Please, please help me

Post by RAT » Thu Nov 21, 2013 7:17 pm

try this input:

Code: Select all

00120110179
1019523856436221
fd
998979
cbaL





332312179862310199501872379231018117927
362395238523642344233323
369585644433
001998979
abcd
output will be:

Code: Select all

aefd
z?.: ;e
001201
abc
67798999





Have a Nice Day !
 ! , . : ; ?
!,.:;?
abcd
001998979

Post Reply

Return to “Volume 4 (400-499)”