/************************************************************* ** ** ** Sample solution for "Around and Around We Go" ** ** Steven J Zeil ** ** 10/08/2015 ** ** ** *************************************************************/ #include #include #include #include #include #include #include #include #include using namespace std; const char filler = '+'; struct Syllable { string text; int startingTime; string::size_type startingCol; Syllable (); }; Syllable::Syllable () : text(""), startingTime(0), startingCol(0) {} struct Line { int startingTime; int lengthTime; int startingSyllableIndex; Line (); }; Line::Line () : startingTime(0), lengthTime(0), startingSyllableIndex(0) {} void readInput (istream& in, int nLines, vector& lines, vector& voice1) { string lineTxt; int time = 0; for (int lineNum = 0; lineNum < nLines; ++lineNum) { getline (in, lineTxt); // discard line terminator Line line; line.startingTime = time; line.startingSyllableIndex = voice1.size(); getline(in, lineTxt); istringstream lineIn (lineTxt); Syllable syllable; while (lineIn >> syllable.text) { voice1.push_back (syllable); } for (unsigned i = line.startingSyllableIndex; i < voice1.size(); ++i) { int t; in >> t; voice1[i].startingTime = time; time += t; line.lengthTime += t; } lines.push_back(line); } } void generateOutput (const Line& line, vector& voice1, vector& voice2) { unsigned i1 = 0; while (i1 < voice1.size() && voice1[i1].startingTime < line.startingTime) ++i1; unsigned i2 = 0; while (i2 < voice2.size() && voice2[i2].startingTime < line.startingTime) ++i2; int stopTime = line.startingTime + line.lengthTime; string line1; string line2; unsigned start1 = i1; unsigned start2 = i2; while (voice1[i1].startingTime < stopTime || voice2[i2].startingTime < stopTime) { Syllable& syl1 = voice1[i1]; Syllable& syl2 = voice2[i2]; if (syl1.startingTime == syl2.startingTime) { // Print a word from each voice int time = syl1.startingTime; string::size_type col = max(line1.size(), line2.size()); if (line1.size() > 0) col = max(col, line1.size()+1); if (line2.size() > 0) col = max(col, line2.size()+1); for (unsigned i = start1; i < i1; ++i) { if (voice1[i].startingTime < syl2.startingTime) { col = max(col, voice1[i].startingCol + time - voice1[i].startingTime); } } for (unsigned i = start2; i < i2; ++i) { if (voice2[i].startingTime < syl1.startingTime) { col = max(col, voice2[i].startingCol + time - voice2[i].startingTime); } } line1 += string(col - line1.size(), filler); line2 += string(col - line2.size(), filler); syl1.startingCol = col; syl2.startingCol = col; line1 += syl1.text; line2 += syl2.text; ++i1; ++i2; } else if (syl1.startingTime < syl2.startingTime) { // Print a word from voice1 int time = syl1.startingTime; string::size_type col = line1.size(); if (line1.size() > 0) col = line1.size()+1; for (unsigned i = start2; i < i2; ++i) { if (voice2[i].startingTime < syl1.startingTime) { col = max(col, voice2[i].startingCol + time - voice2[i].startingTime); } } line1 += string(col - line1.size(), filler); syl1.startingCol = col; line1 += syl1.text; ++i1; } else // if (voice1[i1].startingTime > voice2[i2].startingTime) { // Print a word from voice2 int time = syl2.startingTime; string::size_type col = line2.size(); if (line2.size() > 0) col = line2.size()+1; for (unsigned i = start1; i < i1; ++i) { if (voice1[i].startingTime < syl2.startingTime) { col = max(col, voice1[i].startingCol + time - voice1[i].startingTime); } } line2 += string(col - line2.size(), filler); syl2.startingCol = col; line2 += syl2.text; ++i2; } } cout << line1 << "\n" << line2 << endl; } void processDataset (istream& in, int nLines, int offset) { vector lines; vector voice1; readInput (in, nLines, lines, voice1); vector voice2 = voice1; for (unsigned i = 0; i < voice1.size(); ++i) { voice2[i].startingTime = voice2[i].startingTime + offset; } Syllable sentinal; sentinal.startingTime = 1000000; //lines[nLines-1].startingTime + lines[nLines-1].lengthTime; voice1.push_back(sentinal); voice2.push_back(sentinal); for (unsigned i = 0; i < lines.size(); ++i) { Line line = lines[i]; generateOutput (line, voice1, voice2); } } void solve (istream& in) { int L, N; in >> L >> N; processDataset (in, L, N); } /* * Run the program. Because not all IDEs support easy redirection of * input, if one or more command line parameters are supplied, * they are treated as the name of an input file. Otherwise, read from cin. */ int main(int argc, char** argv) { if (argc > 1) { for (int i = 1; i < argc; ++i) { ifstream input (argv[i]); solve (input); } } else { solve (cin); } return 0; }