458 - The 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

letrunghieu
New poster
Posts: 1
Joined: Wed Aug 27, 2014 1:56 pm

Why time limit?

Post by letrunghieu » Wed Aug 27, 2014 2:03 pm

Code: Select all

#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
	string s;
	ifstream InFile("INPUT.inp");
	//InFile.open("INPUT.inp",ios::in);
	while(1)
	{
		if(InFile.eof())
		{
			//InFile.close();
			return 0;
		}
		getline(InFile, s);
		int len = s.length();
		for(int i = 0;i<len;i++)
			cout << char(int(s[i]) -7); 
		cout << endl;
	}
	return 0;
}

lighted
Guru
Posts: 585
Joined: Wed Jun 11, 2014 9:56 pm
Location: Kyrgyzstan, Bishkek

Re: 458 - The Decoder

Post by lighted » Wed Aug 27, 2014 6:46 pm

Terminating condition InFile.eof() never occurs, so you have infinite loop.

Don't read from file. Read from standart input. Look here
http://ideone.com/GaA0b7
A person who sees the good in things has good thoughts. And he who has good thoughts receives pleasure from life... Bediuzzaman

Shihab
New poster
Posts: 33
Joined: Thu Jun 13, 2013 1:19 pm

Re: 458 - The Decoder

Post by Shihab » Sun Aug 31, 2014 8:51 pm

did u print a newline?

Astaldo
New poster
Posts: 1
Joined: Mon Oct 06, 2014 8:52 pm

Re: 458 - The Decoder

Post by Astaldo » Mon Oct 06, 2014 8:56 pm

To all those who are trying this problem in Java... it was a huge headache:

Code: Select all

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

/**
 * UVa Online Judge: Problem 458 (Accepted). 
 * 
 * @author Astaldo
 */

public class Main {
	
	/**
	 * Start of the program. Gets input encoded messages and decode them.
	 * 
	 * @param args -> not used.
	 */
	
	public static void main (String args []){
		
		DataInputStream input = new DataInputStream(System.in);
		DataOutputStream output = new DataOutputStream(System.out);
		
		String line = read(input);
		
		while (!line.isEmpty()){
			
			try {
				
				output.writeBytes(decode(line) + "\n");
				output.flush();
			
			} catch (IOException ioe) {}
			
			line = read(input);
			
			if (line == null)
				line = "";				
			
		}
	}
	
	/**
	 * Reads an input line.
	 * 
	 * @param input -> DataInputStream.
	 * @return the line read or a EOF if not found input line.
	 */
	
	@SuppressWarnings("deprecation")
	public static String read (DataInputStream input){
		
		String line = null;
		
		try {
			
			line = input.readLine();
		
		} catch (IOException ioe){}
		
		return line;
		
	}
	
	/**
	 * Decodes a given message calculating the char by char sum with the encode integer
	 * (in this case, -7).
	 * 
	 * @param msg -> String to decode.
	 * @return the decoded message.
	 */
	
	public static String decode (String msg){
		
		String aux = "";
		int encode = -7;
		char message [] = msg.toCharArray();
		
		for (int i = 0; i < msg.length(); i++)
			message[i] += encode;
		
		for (int i = 0; i < msg.length(); i++)
			aux += message[i];
		
		return aux;
		
	}
}
Good luck!

murtuza
New poster
Posts: 1
Joined: Mon Oct 27, 2014 8:07 pm

Re: 458 - The Decoder

Post by murtuza » Mon Oct 27, 2014 8:16 pm

why TL please help me

Code: Select all

import java.util.*;
public class Main {

    public static void main(String[] args) {
        Scanner in=new Scanner(System.in);
       while(in.hasNext()){
           String s;
           s=in.nextLine();
           for(int i=0;i<s.length();i++)
           {
               //char t=(char)((int)s.charAt(i)-7);
               
               System.out.printf("%c",s.charAt(i)-7);
           }
           System.out.println();
       }
    }
    
}


lighted
Guru
Posts: 585
Joined: Wed Jun 11, 2014 9:56 pm
Location: Kyrgyzstan, Bishkek

Re: 458 - The Decoder

Post by lighted » Tue Oct 28, 2014 11:39 am

I modified your code and now it gives WA. :-? http://ideone.com/yphgE8
A person who sees the good in things has good thoughts. And he who has good thoughts receives pleasure from life... Bediuzzaman

lighted
Guru
Posts: 585
Joined: Wed Jun 11, 2014 9:56 pm
Location: Kyrgyzstan, Bishkek

Re: 458 - The Decoder

Post by lighted » Tue Oct 28, 2014 1:17 pm

This link seems to be useful. http://www.chyernobog.org/blog/2013/02/ ... uva-judge/

I sent you PM.
A person who sees the good in things has good thoughts. And he who has good thoughts receives pleasure from life... Bediuzzaman

xrenon
New poster
Posts: 10
Joined: Tue Sep 23, 2014 4:11 am

delete

Post by xrenon » Wed Nov 12, 2014 11:38 am

delete
Last edited by xrenon on Thu Nov 13, 2014 4:33 am, edited 1 time in total.

lighted
Guru
Posts: 585
Joined: Wed Jun 11, 2014 9:56 pm
Location: Kyrgyzstan, Bishkek

Re: 458 - The Decoder

Post by lighted » Wed Nov 12, 2014 5:15 pm

https://codercodesit.wordpress.com/tag/458/

Finally i managed to get ACCEPTED in Java using BufferedReader. :lol: :lol: :lol:

For all who still get WA in Java with BufferedReader. http://ideone.com/84DCXo

Also you face this problem with 10194, 10197 and other uva problems.
http://acm.uva.es/board/viewtopic.php?f ... 30#p140856
Last edited by lighted on Sat Jan 03, 2015 1:55 pm, edited 1 time in total.
A person who sees the good in things has good thoughts. And he who has good thoughts receives pleasure from life... Bediuzzaman

xrenon
New poster
Posts: 10
Joined: Tue Sep 23, 2014 4:11 am

Re: 458 - The Decoder

Post by xrenon » Thu Nov 13, 2014 4:33 am

DELETED
Last edited by xrenon on Mon Nov 17, 2014 3:58 am, edited 1 time in total.

lighted
Guru
Posts: 585
Joined: Wed Jun 11, 2014 9:56 pm
Location: Kyrgyzstan, Bishkek

Re: 458 - The Decoder

Post by lighted » Thu Nov 13, 2014 11:01 am

xrenon! Try to read links above before posting. I posted those links especially for people having troubles in accepting java code like you. :)
A person who sees the good in things has good thoughts. And he who has good thoughts receives pleasure from life... Bediuzzaman

astandrik
New poster
Posts: 2
Joined: Wed Aug 24, 2016 10:21 pm

Re: 458 - The Decoder

Post by astandrik » Mon Aug 29, 2016 6:30 pm

Anyone pls help. Always getting WA

Code: Select all

import sys
f = sys.stdin
 
c = f.read(1)
while c != '':
    o = ord(c)
    if o == 10 or o == 13:
        sys.stdout.write(c)
    elif o < 135:
        sys.stdout.write(chr(o - 7))
    c = f.read(1)
sys.stdout.write('\n')
exit(0)

Post Reply

Return to “Volume 4 (400-499)”