zoukankan      html  css  js  c++  java
  • Alignment of Code 解题心得

    原题:

    ou 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





    分析:使用 vector <string> 保存每一行的代码就可以了
    我的代码:
     1 #include<iostream>
     2 #include<sstream>
     3 #include<string>
     4 #include<vector>
     5 #include<algorithm>
     6 using namespace std;
     7 
     8 vector <string> line[1100];  //容器
     9 string code, temp;
    10 int MaxCol[180], col = 0, row = 0;   
    11 
    12 
    13 int main()
    14 {
    15     while (getline(cin, code))
    16     {
    17         stringstream ss(code);
    18         while (ss >> temp)
    19         {
    20             MaxCol[col] = max(MaxCol[col], (int)temp.size());    //得出列的最大数
    21             col++;
    22             line[row].push_back(temp);    //加入容器
    23         }
    24         row++;    col = 0;
    25     }
    26     //输出
    27     for (int i = 0; i < row; i++)
    28     {
    29         int j = 0;
    30         for (; j < line[i].size() - 1; j++)
    31         {
    32             cout << line[i][j];
    33             for (int k = 0; k < MaxCol[j] - (line[i][j].size()-1); k++)
    34                 cout << ' ';
    35         }
    36         cout << line[i][j];
    37         cout << endl;
    38     }
    39 
    40     return 0;
    41 }
    42 
    43 
    44 
    45 //最后一列不能有空格!!!!!!!!

    最后要说的是格式问题  每一行的最后一列都不能加空格

  • 相关阅读:
    YZR.Data 事务处理(Tranaction)
    啊Ran讲微信开发(.net) :公众号(服务号)+自定义服务器(OAuth授权登录)
    啊Ran讲微信开发(.net) 目录结构
    啊Ran讲微信开发(.net) :公众号(订阅号)+自定义服务器(自定义菜单)
    js和jQuery的总结
    Javascript重要解析
    IntelliJ-项目配置,解决no artifacts的warnings
    农夫过河问题(java版)
    redis安装常见问题
    idea 项目中 maven 编译出错Fatal error compiling: 无效的目标发行版: 1.8 -> [Help 1] 解决方法
  • 原文地址:https://www.cnblogs.com/shawn-ji/p/4647699.html
Copyright © 2011-2022 走看看