Results 1 to 6 of 6
Thread: Need help to speed up algorithm
- 04-06-2012, 06:19 PM #1
Member
- Join Date
- Apr 2012
- Posts
- 3
- Rep Power
- 0
Need help to speed up algorithm
Hi. I'm trying to do Sphere Online Judge (SPOJ) - Problem DICTSUB
But I'm getting timeout..So i need to speed up my algorithm. How improve my code or write new better code?
Java Code:import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; class Main { public static void main (String[] args) throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int cases=0; int cases2=0; String slowo=" "; String word=" "; int p=0; cases=Integer.parseInt(br.readLine()); for(int i=0; i<cases; i++) { if(slowo!=null && slowo.length()>0) { slowo=br.readLine(); p=slowo.indexOf(" "); cases2=Integer.parseInt(slowo.substring(0,p)); word=slowo.substring(p+1); for(int k=0; k<cases2; k++) { slowo=br.readLine(); slowo=decode(slowo); int pom=0; int x=0; while(pom!=-1 && x<slowo.length()) { pom=word.indexOf(slowo.charAt(x),pom); x++; } if(pom!=-1) System.out.println("YES"); else System.out.println("NO"); } } System.out.print("\r"); } } public static String decode(String W) { char[] array=W.toCharArray(); StringBuilder ret=new StringBuilder(""); int pom=0; for(int i=0; i<array.length; i++) if(i%2==0) pom=array[i]; else for(int k=0; k<pom; k++) ret.append(array[i]); return ret.toString(); } }
- 04-06-2012, 06:42 PM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,604
- Rep Power
- 5
Re: Need help to speed up algorithm
First, you should define what you mean by 'timeout'. Second, if you wish to speed this up, I recommend profiling your code by timing operations using System.currentTimeMillis - only then will you know which part of your algorithm is the bottleneck so you can focus on that.
- 04-06-2012, 08:08 PM #3
Member
- Join Date
- Apr 2012
- Posts
- 3
- Rep Power
- 0
Re: Need help to speed up algorithm
SPOJ has time limit for each task. Your code has some time to be performed. We don't know what input author prepared for our algorithm.
The slowest part of my code is comparing strings:
I need faster method to compare strings. But how to do that?Java Code:while(pom!=-1 && x<slowo.length()) { pom=word.indexOf(slowo.charAt(x),pom); x++; }Last edited by Norm; 04-07-2012 at 02:03 AM. Reason: fixed code tags
- 04-06-2012, 09:14 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,373
- Blog Entries
- 7
- Rep Power
- 17
Re: Need help to speed up algorithm
How, exactly, is a String from the dictionary encoded? You assume an RLE as <len><char><len><char> ... is the <len> a binary encoded number or do you have to read its character representation?
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 04-07-2012, 12:30 AM #5
Member
- Join Date
- Apr 2012
- Posts
- 3
- Rep Power
- 0
Re: Need help to speed up algorithm
I could encode "normal" Strings to RLE and then compare but I tried this and it was still too slow..Or did you mean something else?
- 04-07-2012, 02:01 AM #6
Re: Need help to speed up algorithm
Cross posted at: Speed Up Algorithm - Java Help - CODECALL
If you don't understand my response, don't ignore it, ask a question.
Similar Threads
-
Java Applet speed?
By Masherbrum in forum New To JavaReplies: 0Last Post: 04-06-2012, 03:11 AM -
Set max upload speed
By Giuseppe_Mazzei in forum New To JavaReplies: 9Last Post: 06-03-2011, 02:28 AM -
Improve the Speed of loop
By venk123 in forum New To JavaReplies: 15Last Post: 02-16-2011, 06:36 AM -
How to speed sql Statements?
By bezudar in forum Advanced JavaReplies: 3Last Post: 11-20-2008, 09:53 AM -
compare speed
By bbq in forum JDBCReplies: 1Last Post: 06-28-2007, 05:34 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks