问题描述
p1593原题链接:UVaOJ 1593 - Alignment of Code
相关说明:本题为《算法竞赛入门经典(第2版)》习题 5-1
解法一:模拟
字符串模拟处理的水题,用 stringstream 进行处理会很方便,注意行末不需要补充空格;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
#include <bits/stdc++.h> using namespace std; int main() { string line, word; vector<int> max_len(200, 0); vector<vector<string>> lines; while (getline(cin, line)) { stringstream ss(line); vector<string> line_words; int column_idx = 0; while (ss >> word) { line_words.push_back(word); max_len[column_idx] = max(max_len[column_idx], static_cast<int>(word.length())); column_idx += 1; } lines.push_back(line_words); } for (auto& line : lines) { for (int i = 0; i < line.size(); ++i) cout << line[i] << (i == line.size() - 1 ? "" // note this case : string(max_len[i] - line[i].length() + 1, ' ')); cout << endl; } return 0; } |
1 |
#TODO |