289 - A Very Nasty Text Formatter

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

Moderator: Board moderators

Post Reply
Adrian Kuegel
Guru
Posts: 724
Joined: Wed Dec 19, 2001 2:00 am
Location: Germany

289 - A Very Nasty Text Formatter

Post by Adrian Kuegel »

Can someone explain me this problem?
You should do as few hyphenations as possible. However, if it is impossible to justify a line of the text, output it left-justified.
Does it mean, that I should minimize the number of lines that can't be printed with length n, and then minimize the number of hyphenations?
If yes, why the output for last sample test case is not:

Code: Select all

This
is a-
n ex-
ample
of  a
para-
graph
whic-
h  is
pret-
typr-
inte-
d  on
a  r-
ow w-
ith a
leng-
th of
five.
The only way how I can reproduce the sample output is by using greedy, but I got WA when I submitted a program with it. Did someone solve this problem with DP, or is greedy the algorithm that is wanted here?
Adrian Kuegel
Guru
Posts: 724
Joined: Wed Dec 19, 2001 2:00 am
Location: Germany

Post by Adrian Kuegel »

Well, it seems greedy is the wanted solution, at least I got Accepted now.
Here is a test case where my first greedy program failed, perhaps it will help someone else, too:
4 Te s t.

Output is:
Te s
t.
..
A great helper
Posts: 454
Joined: Thu Oct 18, 2001 2:00 am
Location: Hong Kong

Post by .. »

Thanks for your post. :wink:
Once you tell the word "greedy", I suddednly understand what it is asking.
Before that, I have no way to solve it as I even don't know what it requires....
My signature:
  • Please make discussion about the algorithm BRFORE posting source code.
    We can learn much more in discussion than reading source code.
  • I HATE testing account.
  • Don't send me source code for debug.
little joey
Guru
Posts: 1080
Joined: Thu Dec 19, 2002 7:37 pm

Post by little joey »

Yes, thanks.
The sentence "You should do as few hyphenations as possible." is wrong, because the only way to achieve that is DP. But than there would be a better solution for the second example:

Code: Select all

This    is   an<CR> 
example      of<CR>
a     paragraph<CR>
which        is<CR>
prettyprinted<CR>
on a row with a<CR>
length       of<CR>
fifteen.<CR>
which has zero hyphens.
I guess the sentence should read "You should only hyphenate when you are forced to", or something like that, altough it doesn't clear matters that much.
Greed is indeed the method, and the problem can be solved by using a buffer of only n+1 characters.
Not so much a 'nasty' text formatter, but rather a 'nasty' problem description.
Adrian Kuegel
Guru
Posts: 724
Joined: Wed Dec 19, 2001 2:00 am
Location: Germany

Post by Adrian Kuegel »

I just looked at the ps file. It probably shows the original problem description, and there is only one sample test case, and this test case is also possible to reach with DP (minimize number of hyphenations).
I fear that the one who generated the judge input/output made a mistake.
Post Reply

Return to “Volume 2 (200-299)”