fix(parser): handle oversized words in line break algorithm

When a word exceeds the page width, the DP algorithm would leave
dp[i] = MAX_COST, causing a cascade failure where all preceding
words also got MAX_COST. This resulted in each word being placed
on its own line.

Fix: When dp[i] remains MAX_COST after the inner loop, force the
oversized word onto its own line (ans[i] = i) and inherit the cost
from the next word (dp[i] = dp[i+1]) to allow preceding words to
find valid configurations.
This commit is contained in:
Eunchurn Park 2025-12-27 15:41:04 +09:00
parent abe3e6c6db
commit d3848779f9
No known key found for this signature in database
GPG Key ID: 29D94D9C697E3F92

View File

@ -106,6 +106,18 @@ std::vector<size_t> ParsedText::computeLineBreaks(const int pageWidth, const int
ans[i] = j; // j is the index of the last word in this optimal line
}
}
// Handle oversized word: if no valid configuration found, force single-word line
// This prevents cascade failure where one oversized word breaks all preceding words
if (dp[i] == MAX_COST) {
ans[i] = i; // Just this word on its own line
// Inherit cost from next word to allow subsequent words to find valid configurations
if (i + 1 < static_cast<int>(totalWordCount)) {
dp[i] = dp[i + 1];
} else {
dp[i] = 0;
}
}
}
// Stores the index of the word that starts the next line (last_word_index + 1)