• 紫书第五章训练 uva 1593 Alignment of Code by crq


    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 p1 = 1; the second words on each line are printed at the minimal possible position p2, such that all first words end at or before position p2 - 2; the third words on each line are printed at the minimal possible position p3, such that all second words end at or before position p3 - 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 pi.

    Note for the Sample:
    The `$ sqcup$' character in the example below denotes a space character in the actual files (ASCII code 32).

    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

    题意分析:将多行字符串中的每一列对齐(这里每行的第一个单词算第一列,第二个单词算第二列...),因为总共不超过1000行,但每行的列数并不相同,因此用一个vector数组来表示:

    vector<string> vec[1000]

    通过stringstream字符串流分隔出每一行的单词存入vec数组。同时定义:

    vector<string> res;

    用来存储每一行格式化后的结果。

    不断从res中找出最长的一串,并根据该长度对其它行进行空格填充后再拼接下一个单词。直到所有单词拼接完成。

    AC代码:

     1 #include <iostream>
     2 #include <string>
     3 #include <vector>
     4 #include <sstream>
     5 using namespace std;
     6 
     7 int main()
     8 {
     9 //    freopen("d:\data1.in","r",stdin);
    10     vector<string> vec[1000], res;
    11     int num = 0, max=0;
    12     string str, str2;
    13     while(getline(cin, str))
    14     {
    15         stringstream ss(str);
    16         while(ss>>str2)
    17         {
    18             vec[num].push_back(str2);
    19         }
    20         if(vec[num].size()>max)
    21             max = vec[num].size();
    22         num++;
    23         res.push_back("");
    24     }
    25     for(int i=0;i<max;i++)
    26     {
    27         if(i==0)
    28         {
    29             for(int j=0;j<res.size();j++)
    30             {
    31                 res[j] += vec[j][0];
    32             }
    33             continue;
    34         }
    35         int m = 0;
    36         for(int j=1;j<res.size();j++)
    37         {
    38             if(res[j].length()>res[m].length())
    39                 m = j;
    40         }
    41         int x = res[m].length();
    42         for(int j=0;j<res.size();j++)
    43         {
    44             if(i<vec[j].size())
    45             {
    46                 string s(x-res[j].length()+1, ' ');
    47                 res[j] += s;
    48                 res[j] += vec[j][i];
    49             }
    50         }
    51     }
    52     for(int i=0;i<num;i++)
    53     {
    54         cout<<res[i]<<endl;
    55     }
    56     return 0;
    57 }
  • 相关阅读:
    JS是单线程的吗?
    JQuery $ $.extend(),$.fn和$.fn.extend javaScript对象、DOM对象和jQuery对象及转换 工具方法(utility)
    JavaScript 操作符 变量
    WEB组件 开发 (未完成 413)
    CSS传统布局之布局模型
    JavaScript 作用域 匿名函数 模仿块级作用域(私有作用域)
    JQuery常用API 核心 效果 JQueryHTML 遍历 Event事件
    JavaScript 属性类型(数据属性 访问器属性)
    CSS居中问题:块级元素和行级元素在水平方向以及垂直方向的居中问题
    javascript 深入浅出 (未完成417)
  • 原文地址:https://www.cnblogs.com/tzcacm/p/6845960.html
Copyright © 2020-2023  润新知