zoukankan      html  css  js  c++  java
  • 紫书第五章训练 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 }
  • 相关阅读:
    Android 自定义标题栏 并进行事件处理
    java synchronized详解
    Java中LinkedList与ArrayList有什么区别
    android动态全屏切换
    java线程机制介绍
    设置导航栏背景和文字属性
    Dictionary的用法
    bundle
    解析Json
    Copy与MutableCopy
  • 原文地址:https://www.cnblogs.com/tzcacm/p/6845960.html
Copyright © 2011-2022 走看看