题意:输入若干行代码,要求各列单词的左边界对其且尽量靠左。单词之间最少要空一格,每个单词不超80个字符,每行不超过180个字符,一共最多1000行。
思路:每行用一个vector存一下,存的时候维护一下每一列单词长度的最大值。
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 1000 + 5;
vector<string> v[MAXN];//每列的单词
int len[200] = {0}, cnt = 0;
int main()
{
string s;
while (getline(cin, s))
{
stringstream ss(s);
string t;
int i = 0;
while (ss >> t)
{
v[cnt].push_back(t);
len[i] = max(len[i], (int)t.length());
i++;
}
cnt++;
}
for (int i = 0; i < cnt; i++)
{
int num = 0;
for(auto it: v[i])
{
cout << it;
int l = len[num] - it.length() + 1;
if (num == v[i].size() - 1) break;
for (int j = 0; j < l; j++)
{
cout << " ";
}
num++;
}
cout << endl;
}
return 0;
}
/*
start: integer; // begins here
stop: integer; // ends here
s: string;
c: char; // temp
*/