Page 9 of 11
444 - Encoder and Decoder (runtime Error!)
Posted: Sat Dec 03, 2011 6:33 pm
by rahid
I got runtime error for my code. Please give me some clue why its happened??
implant dentists
Posted: Sun Jan 15, 2012 9:59 pm
by DivinaBack76
I completely agree with everything that was said, well put.
Q 444, Time Limit Exceeded
Posted: Mon Jan 23, 2012 7:10 pm
by anubhavhcm
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);
}
}
Re: Q 444, Time Limit Exceeded
Posted: Tue Jan 24, 2012 2:55 pm
by anubhavhcm
any one plz help!!!

Re: Q 444, Time Limit Exceeded
Posted: Wed Jan 25, 2012 1:37 am
by brianfry713
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().
444 why RE??
Posted: Thu Apr 12, 2012 9:55 am
by cse.mehedi
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;
}
Re: 444 why RE??
Posted: Fri Apr 13, 2012 12:26 am
by brianfry713
main should return int.
Re: 444 why RE??
Posted: Fri Apr 13, 2012 12:30 am
by cse.mehedi
brianfry713 wrote:main should return int.
I used return 1; but again RE!!

Re: 444 why RE??
Posted: Mon Apr 16, 2012 10:05 pm
by brianfry713
change:
void main()
to:
int main()
return 0 not 1.
Re: 444 - Encoder and Decoder (runtime Error!)
Posted: Sat Jul 14, 2012 1:20 pm
by sith
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;
}
}
}
Re: 444 - Encoder and Decoder (runtime Error!)
Posted: Tue Jul 17, 2012 1:03 am
by brianfry713
I believe there is a blank line in the judge's input.
Re: 444 - Encoder and Decoder (runtime Error!)
Posted: Fri Dec 28, 2012 11:33 pm
by sumit saha shawon
why getting wa????????????
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;
}
Re: 444 - Encoder and Decoder (runtime Error!)
Posted: Sat Dec 29, 2012 11:22 pm
by brianfry713
Try input Z
Re: 444 - Encoder and Decoder (runtime Error!)
Posted: Thu Apr 18, 2013 11:14 am
by sirajul
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
Re: 444 (Encoder Decoder) Why W A? Please, please help me
Posted: Thu Nov 21, 2013 7:17 pm
by RAT
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