848 - Fmt

All about problems in Volume 8. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

evandrix
New poster
Posts: 8
Joined: Wed Oct 03, 2007 11:07 am

Re: 848 - FMT

Post by evandrix »

hey anyone can outline a general strategy to solve this problem? i've tried and encountered numerous WA's so far...

i have also compiled the following input test case data and used the UVA toolkit (uva.xgd.dk) to generate the output, but i'm not sure if it's correct to produce the AC solution, can anyone verify?

any help provided is appreciated. thanks.

INPUT: http://docs.google.com/Doc?id=dmj9fv9_4gs3wbqgc
OUTPUT: http://docs.google.com/Doc?id=dmj9fv9_5dh6rk2dx

evandrix
New poster
Posts: 8
Joined: Wed Oct 03, 2007 11:07 am

Re: 848 - FMT

Post by evandrix »

Code: Select all

import java.io.*;
import java.util.*;
class Main implements Runnable{
    static String ReadLn(int maxLength){  // utility function to read from stdin,
                                          // Provided by Programming-challenges, edit for style only
        byte line[] = new byte [maxLength];
        int length = 0;
        int input = -1;
        try{
            while (length < maxLength){//Read untill maxlength
                input = System.in.read();
                if ((input < 0) || (input == '\n')) break; //or untill end of line ninput
                line [length++] += input;
            }

            if ((input < 0) && (length == 0)) return null;  // eof
            return new String(line, 0, length);
        }catch (IOException e){
            return null;
        }
    }

    public static void main(String args[])  // entry point from OS
    {
        Main myWork = new Main();  // Construct the bootloader
        myWork.run();            // execute
    }

    public void run() {
        new myStuff().run();
    }
}
class myStuff implements Runnable{
  /** The maximum column width */
  public static final int COLWIDTH = 72;
  /** The file that we read and format */
  BufferedReader in;
    public void run(){
	  String w, f; int col=0,lineNo=0;
    while ((w = Main.ReadLn(1000)) != null) {
	  lineNo++;

		if (w.length() == 0) { // null line
		System.out.println(); // end current line
		if (col > 0) {
		  System.out.println(); // output blank line
		  col = 0;
		}
		continue;
		}
		
		// otherwise it's text, so format it.
		StringTokenizer st = new StringTokenizer(w);
		int tokenNo=0;
		String prevWord="", ss=w;
		while (st.hasMoreTokens()) {
		f = st.nextToken();
		tokenNo++;

		if (tokenNo==1)	//print spaces before 1st word
		{
			System.out.print( w.substring(0,w.indexOf(f)) );
			col += w.indexOf(f);
			prevWord=f;
		}
		else {
			//print spaces between words - able to handle multiple indexOf's
			ss = ss.substring ( (ss.indexOf(prevWord)+prevWord.length()) );
			System.out.print( ss.substring(0,ss.indexOf(f)) );
			col += ss.indexOf(f);
			prevWord=f;
		}

		if (col + f.length() > COLWIDTH) {
			System.out.println();		//print newline & reset col before printing word f
			col = 0;
		}
			System.out.print(f); //+" ");
			col += f.length(); // +1;
		}	//end while/line
		
		//convert line break into space
		if (col < COLWIDTH) {
			System.out.print(' '); 
			col++;
		}
    }	//end while readline
	
    if (col > 0) System.out.println();
    }
}

Post Reply

Return to “Volume 8 (800-899)”