UVALive4983 UVa1593 POJ3959 Alignment of Code【字符串流+输入输出+水题】

509 篇文章 9 订阅
281 篇文章 4 订阅

Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 890 Accepted: 319

Description

You are working in a team that writes Incredibly Customizable Programming Codewriter (ICPC) which is basically a text editor with bells and whistles. You are working on a module that takes a piece of code containing some definitions or other tabular information and aligns each column on a fixed vertical position, while keeping the resulting code as short as possible, making sure that only whitespaces that are absolutely required stay in the code. So, that the first words on each line are printed at position p 1 = 1; the second words on each line are printed at the minimal possible position p 2, such that all first words end at or before position p 2 - 2; the third words on each line are printed at the minimal possible position p 3, such that all second words end at or before position p 3 - 2, etc.
For the purpose of this problem, the code consists of multiple lines. Each line consists of one or more words separated by spaces. Each word can contain uppercase and lowercase Latin letters, all ASCII punctuation marks, separators, and other non-whitespace ASCII characters (ASCII codes 33 to 126 inclusive). Whitespace consists of space characters (ASCII code 32).

Input

The input file contains one or more lines of the code up to the end of file. All lines (including the last one) are terminated by a standard end-of-line sequence in the file. Each line contains at least one word, each word is 1 to 80 characters long (inclusive). Words are separated by one or more spaces. Lines of the code can have both leading and trailing spaces. Each line in the input file is at most 180 characters long. There are at most 1000 lines in the input file.

Output

Write to the output file the reformatted, aligned code that consists of the same number of lines, with the same words in the same order, without trailing and leading spaces, separated by one or more spaces such that i-th word on each line starts at the same position p i.

Sample Input

  start:  integer;    // begins here
stop: integer; //  ends here  
 s:  string;   
c:   char; // temp 

Sample Output

start: integer; // begins here
stop:  integer; // ends   here
s:     string;
c:     char;    // temp

Source



Regionals 2010 >> Europe - Northeastern


问题链接UVALive4983 UVa1593 POJ3959 Alignment of Code

问题描述参见上文。

问题分析

  输入有若干行,每行有若干单词,让各行的单词对齐。这应该是一个单词矩阵。

  输出时,需要构造好这个矩阵,可以用向量数组来存储这个矩阵。同时,需要分别对各个列的单词计算其最长的长度。有了这两点,输出就不是问题了。

程序说明

  数组maxlen[]用于存储各个列的单词的最长长度,maxlen[i]=k表示第i列单词的最长为k。

  向量数组words[]用于存储各个行的单词,words[i]中存储第i行的各个单词。

  C++的输出格式控制需要用到库iomanip。

参考链接:(略)


AC的C++语言程序:

/* UVALive4983 UVa1593 POJ3959 Alignment of Code */

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

using namespace std;

const int N = 180;
const int N2 = 1000;

int maxlen[N+1];
vector<string> words[N2];

int main()
{
    string s, t;

    memset(maxlen, 0, sizeof(maxlen));

    int lencount = 0;
    while(getline(cin, s)) {
        stringstream ss(s);

        int i = 0;
        while(ss >> t) {
            maxlen[i] = max((int)t.length(), maxlen[i]);
            words[lencount].push_back(t);
            i++;
        }
        lencount++;
    }

    cout << setiosflags(ios::left);
    for(int i=0; i<lencount; i++) {
        int j;
        for(j=0; j<(int)words[i].size()-1; j++)
            cout << setw(maxlen[j] + 1) << words[i][j];
        cout << words[i][j] << endl;
    }

    return 0;
}



  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值