MEGChai MEGChai
  • 文章
    • 随笔
    • 笔记
    • 教程
  • 关于
首页 › 数据结构与算法 › 在线评测 › UVaOJ 1593 - Alignment of Code
AOAPC II

UVaOJ 1593 - Alignment of Code

Chai
2021-11-24 0:00:00在线评测阅读 452

问题描述

p1593

原题链接:UVaOJ 1593 - Alignment of Code

相关说明:本题为《算法竞赛入门经典(第2版)》习题 5-1

解法一:模拟

字符串模拟处理的水题,用 stringstream 进行处理会很方便,注意行末不需要补充空格;

C++
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;
}
Python
1
#TODO
AOAPC II UVaOJ
赞赏 赞(0)
订阅
提醒
guest
guest
0 评论
内嵌评论
查看所有评论
  • 0
  • 0
Copyright © 2020-2023 MEGChai.
  • 文章
    • 随笔
    • 笔记
    • 教程
  • 关于
# 生活 # # 心理 # # 编程 # # 音乐 # # 写作 #
Chai
95
文章
4
评论
58
喜欢
wpDiscuz