A paragraph of text is given, consisting of a series of words, separated from each other by spaces and / or newline characters (tabs are not used). The word may contain punctuation. The text ends with the special word "$$$", which will be the only one on the last line.
For example, in the text
The quick brown-fox jump$ over, the --- lazy dog. $$$ the words are "The", "quick", "brown-fox", "jump$", "over,", "the", "---", "lazy", "dog.".
The last word "$$$" does not need to be considered.
Your program will also be given an integer indicating the width of the window w. You must print the words with the minimum number of spaces between them so that:
- the number of characters in each line did not exceed w (except for the newline character).
- there must be at least one space between two words on one line.
- the leftmost character of the string must not be empty
- the rightmost character of a line should not be empty except for the last line
- spaces should be distributed as evenly as possible between all words in the string. If this cannot be done exactly, then a large portion of the spaces should be located closer to the right edge of the line.
With an output width of 21, the above text would be displayed as follows:
123456789012345678901 The quick brown-fox jump$ over, the --- lazy dog. Input Description:
The first line of the input file contains only the width of the window w. The following lines contain the text of the paragraph. The last line contains a single word "$$$".
It is guaranteed that:
- the word "$$$" is not found inside the paragraph text
- the total length of any two consecutive words, separated by a single space, does not exceed w
- single word length does not exceed 50 characters
- the number of words in a paragraph does not exceed 5000
- window width does not exceed 100.
Output description:
The output file should consist of paragraph text words. There should be no spaces at the beginning and end of the file. Output file db. in the format win1251.
The program formats the text, but when checking hidden tests, it gives an error. What error gives and what tests, it is not known. Maybe I misunderstood the task somewhere or there is a cunning error in the program itself.
Please help me figure it out. 2 weeks I suffer, I just can not understand what the hidden trick is.
#include <iostream> #include <vector> #include <string> #include <sstream> #include <cctype> #include <fstream> using namespace std; string inputSpace(string input, int size) { string rez = "", buf = input, tep = ""; int spaceNeed = size - input.length(); int lineLengyh; bool flagg; if (spaceNeed == 0) { flagg = false; rez = input; } else { flagg = true; } while (flagg) { for (int i = buf.length() - 1; i >= 0; i--) { rez = buf[i] + rez; if (buf[i] == ' ' && buf[i - 1] != ' ' && spaceNeed > 0) { rez = ' ' + rez; spaceNeed--; } lineLengyh = rez.length(); } if (spaceNeed == 0) { flagg = false; } else { buf = rez; rez = ""; } } return rez; } int main() { ifstream innFile("input.txt"); ofstream outFile("output.txt"); string line; stringstream ss; vector<string> vec; int maxLineSize; innFile>>maxLineSize; if (innFile.is_open()) { while (innFile.good()) { getline(innFile, line); ss << line << " "; } } line.empty(); while(ss>> line) { vec.push_back(line); } vec.pop_back(); int lineSize; line.empty(); for(size_t i = 0; i <= vec.size(); i++) { line.clear(); lineSize = 0; while(lineSize < maxLineSize) { lineSize += (int)vec[i].length(); if(lineSize > maxLineSize) { i--; break; } if (i >= vec.size()){ break; } line += vec[i]; lineSize++; if(lineSize >= maxLineSize) break; line.push_back(' '); i++; } if (line[line.length() - 1] == ' ') { line.erase( line.end() - 1 ); } if (i == vec.size()) { outFile << line << endl; } else { outFile << inputSpace(line, maxLineSize) << endl; } } innFile.close(); outFile.close(); return 0; }