【UVA-1593】Alignment of Code【紫书】

【UVA-1593】Alignment of Code【紫书】

问题链接:https://vjudge.net/problem/UVA-1593

Solution:

开数组,记录每行同一列单词的最大长度,用这个来补空格数量就可以了。

#include <iostream>
#include <string>
#include <sstream>
#include <vector>

using namespace std;

const int LIM = 80;
int cnt[LIM];
int len = 0;
vector<vector<string> > data;

inline int maxi(int a, int b) {
    return a > b ? a : b;
}

int main(void) {
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    string str, tmp;
    for (int i = 0; getline(cin, str); i++) {
        stringstream ss;
        ss << str;
        data.push_back(vector<string>());
        for (int j = 0; ss >> tmp; j++) {
            data[i].push_back(tmp);
            if (data[i][j].size() > cnt[j]) cnt[j] = data[i][j].size();
            if (j > len) len = j;
        }
    }
    for (int i = 0; i < data.size(); i++) {
        for (int j = 0; j < data[i].size(); j++) {
            cout << data[i][j];
            if (j != data[i].size() - 1)
                for (int k = data[i][j].size(); k <= cnt[j]; k++) cout << ' ';
        }
        cout << "\n";
    }
    return 0;
}