1 / 5

Word Wrap

Word Wrap. HKOI 2005 Junior Q3. Problem. Word wrap – a common feature of text editors / text processors Given an stream of words, place line breaks appropriately such that each line has the “maximum possible” number of words that do not exceed the limit of W words per line. How to do it?.

alongi
Download Presentation

Word Wrap

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Word Wrap HKOI 2005 Junior Q3

  2. Problem • Word wrap – a common feature of text editors / text processors • Given an stream of words, place line breaks appropriately such that each line has the “maximum possible” number of words that do not exceed the limit of W words per line.

  3. How to do it? • For every word, we need to consider whether to put a word on the current line, or on the next line • (We can also observe that the arrangement of lines in the input does not matter) • So we need to convert the lines of the input into words

  4. Implementation • Suppose we read a line from input each time • Split the line into words • We can choose to immediately output the words, or we can choose to store the words in an array to be used later • During output, consider the length l of the “current” line. If (l + 1 + length(next word) > W), then go on to the next line.

  5. Notes • For C++, you can efficiently read in just a “word” by cin >> a, where a is a string • For pascal, functions like “pos” and “copy” are useful. You may also choose to handle the string as an array of characters. • The size of the problem is quite small, solutions should be in linear time, runtime is not a major concern. • A problem that assesses basic data processing and string manipulation of contestants.

More Related