#C1105. Justify Text Without Breaking Words

    ID: 40323 Type: Default 1000ms 256MiB

Justify Text Without Breaking Words

Justify Text Without Breaking Words

You are given a series of test cases. In each test case, you are provided with an integer W (the maximum width allowed) and a line of text. Your task is to determine if the provided text can be wrapped into lines where every line does not exceed the width W and no word is split across two lines.

Formally, for a given test case, let \(W\) be the maximum allowed width and let the text consist of words separated by spaces. A text is considered justified if for every word \(w\) in the text, the following condition holds:

[ \text{if } |w| > W, \text{ then the text cannot be justified.} ]

Otherwise, using a greedy approach, words are added to a line until adding another word (with a preceding space if needed) would exceed \(W\). When that happens, a new line is started. If all words are placed without breaking any word, the answer is YES; otherwise, it is NO.

Examples:

  • For \(W=5\) and text "hello", the answer is YES.
  • For \(W=4\) and text "hello", the answer is NO because the word has 5 characters which exceeds 4.

inputFormat

The input is read from stdin and contains multiple test cases. The first line contains an integer \(T\) representing the number of test cases. For each test case, the first line contains an integer \(W\) (the maximum allowed width), and the second line contains a string representing the text.

Input Format:

T
W1
text_line1
W2
text_line2
... 

outputFormat

For each test case, print YES if the text can be justified without breaking any word under the constraint \(W\), otherwise print NO. Each result should be printed on a new line to stdout.

## sample
1
5
hello
YES

</p>