给定一个字符串str和每行的宽度为L ,任务是对齐字符串,使得每行对齐文本的长度为L并借助空格 (‘ ‘) 并且最后一行应该左对齐。
例子:
Input: str = “GeeksforGeek is the best computer science portal for geeks.”, L = 16
Output:
GeeksforGeek__is
the_________best
computer_science
portal_______for
geeks.__________
Input: str = “The quick brown fox jumps over the lazy dog.”, L = 11
Output:
The___quick
brown___fox
jumped_over
the____lazy
dogs.______
The symbol _ denotes a space.
方法:
在知道该行中的完整单词集之前,无法计算每行中单词之间的空格数。我们将逐行解决这个问题。
- 将给定的文本拆分为单词
- 首先选择每行可以插入的单词,包括每个单词之间的空格。为此,包含的单词之间有一个空格的长度之和必须小于或等于L 。
- 现在计算使每条线的长度为L所需的空格数并均匀分布这些空格。
- 对下一组单词重复上述步骤。
- 对于最后一行,必须在末尾分配空格,因为最后一行必须左对齐。
下面是上述方法的实现:
// C++ program to Justify the given Text
// according to the given width of each line
#include "bits/stdc++.h"
using namespace std;
// Function to join the words
// with spaces spread evenly
string JoinALineWithSpace(
vector& words,
int start, int end,
int num_spaces)
{
// Number of words in current line
int num_words_curr_line
= end - start + 1;
// String to store the justified text
string line;
for (int i = start; i < end; i++) {
line += words[i];
--num_words_curr_line;
// Count number of current space needed
int num_curr_space
= ceil((double)(num_spaces)
/ num_words_curr_line);
// Insert spaces in string line
line.append(num_curr_space, ' ');
// Delete the spaces inserted in line
num_spaces -= num_curr_space;
}
// Insert word to string line
line += words[end];
line.append(num_spaces, ' ');
// Return justified text
return line;
}
// Function that justify the words of
// sentence of length of line L
vector JustifyText(
vector& words,
int L)
{
int curr_line_start = 0;
int num_words_curr_line = 0;
int curr_line_length = 0;
// To store the justified text
vector result;
// Traversing the words array
for (int i = 0; i < words.size(); i++) {
// curr_line_start is the first word
// in the current line, and i is
// used to identify the last word
++num_words_curr_line;
int lookahead_line_length
= curr_line_length
+ words[i].size()
+ (num_words_curr_line - 1);
// If by including the words length becomes L,
// then that set of words is justified
// and add the justified text to result
if (lookahead_line_length == L) {
// Justify the set of words
string ans
= JoinALineWithSpace(
words,
curr_line_start,
i,
i - curr_line_start);
// Store the justified text in result
result.emplace_back(ans);
// Start the current line
// with next index
curr_line_start = i + 1;
// num of words in the current line
// and current line length set to 0
num_words_curr_line = 0;
curr_line_length = 0;
}
// If by including the words such that
// length of words becomes greater than L,
// then hat set is justified with
// one less word and add the
// justified text to result
else if (lookahead_line_length > L) {
// Justify the set of words
string ans
= JoinALineWithSpace(
words,
curr_line_start,
i - 1,
L - curr_line_length);
// Store the justified text in result
result.emplace_back(ans);
// Current line set to current word
curr_line_start = i;
// Number of words set to 1
num_words_curr_line = 1;
// Current line length set
// to current word length
curr_line_length = words[i].size();
}
// If length is less than L then,
// add the word to current line length
else {
curr_line_length
+= words[i].size();
}
}
// Last line is to be left-aligned
if (num_words_curr_line > 0) {
string line
= JoinALineWithSpace(
words,
curr_line_start,
words.size() - 1,
num_words_curr_line - 1);
line.append(
L - curr_line_length
- (num_words_curr_line - 1),
' ');
// Insert the last line
// left-aligned to result
result.emplace_back(line);
}
// Return result
return result;
}
// Function to insert words
// of sentence
vector splitWords(string str)
{
vector words;
string a = "";
for (int i = 0; str[i]; i++) {
// Add char to string a
// to get the word
if (str[i] != ' ') {
a += str[i];
}
// If a space occurs
// split the words and
// add it to vector
else {
words.push_back(a);
a = "";
}
}
// Push_back the last word
// of sentence
words.push_back(a);
// Return the vector of
// words extracted from
// string
return words;
}
// Function to print justified text
void printJustifiedText(vector& result)
{
for (auto& it : result) {
cout << it << endl;
}
}
// Function to call the justification
void justifyTheText(string str, int L)
{
vector words;
// Inserting words from
// given string
words = splitWords(str);
// Function call to
// justify the text
vector result
= JustifyText(words, L);
// Print the justified
// text
printJustifiedText(result);
}
// Driver Code
int main()
{
string str
= "GeeksforGeek is the best"
" computer science portal"
" for geeks.";
int L = 16;
justifyTheText(str, L);
return 0;
}
输出:
GeeksforGeek is
the best
computer science
portal for
geeks.
时间复杂度: O(N),其中 N =字符串长度
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live