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 `‘ 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 //最后一列不能有空格!!!!!!!!

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

时间: 2024-10-17 22:54:25

Alignment of Code 解题心得的相关文章

UVa 1593 Alignment of Code(字符串)

题意  按要求对齐代码 字符串流的应用 #include <bits/stdc++.h> using namespace std; const int N = 1005, M = 200; string s[N][M], line; int cw[M], cn[N]; int main() { int r = 0, c = 0; while(getline(cin, line)) { stringstream ss(line); while(ss >> s[r][c]) { if(

第四章学习小结 串的模式匹配 解题心得体会

串的模式匹配 解题心得体会 关于串,模式匹配是其一个很重要的问题.针对这个问题,书上讲了两种模式匹配的算法,即BF算法和KMP算法,下面针对这两种算法的实现谈谈我的心得. 一.BF算法的探索 [错误代码1] #include<iostream> #include<string.h> using namespace std; typedef struct{ char ch[1000002]; int length; }SString; void Index_BF(SString S,

poj 3959 Alignment of Code

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 ot

括号配对问题——解题心得

Description You are given a string consisting of parentheses () and []. A string of this type is said to be correct: (a)if it is the empty string (b)if A and B are correct, AB is correct, (c)if A is correct, (A ) and [A ] is correct. Write a program

wechall.net/stegano 解题心得

最近迷上了 www.wechall.net 网站,里面都是些与计算机相关的题目挑战.题目又分很多类型,例如:加密与解密.隐写术.网络攻防.趣味编程.数学逻辑等.题目有的简单,有的很难,需要一些知识和技巧.与其他题目挑战的网站不同的是,在其他类似性质的网站注册的用户可以绑定到 WeChall 网站,然后 WeChall 提供排名信息,而且也分得很细,什么按总分全球排名.什么在自己国家的排名.什么解答某种语言网站题目的排名等.可以从解题的人数判断题目的难易程度,有兴趣的朋友可以去注册,解题中也能学到

LeetCode: Gray Code 解题报告

Gray CodeThe gray code is a binary numeral system where two successive values differ in only one bit. Given a non-negative integer n representing the total number of bits in the code, print the sequence of gray code. A gray code sequence must begin w

A - Alignment of Code(推荐)

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

Ducci Sequence 解题心得

原题贴上 A Ducci sequence is a sequence of n-tuples of integers. Given an n-tuple of integers (a1, a2, ... , an), the next n-tuple in the sequence is formed by taking the absolute differences of neighboring integers: ( a1, a2, ... , an)  (| a1 - a2|,| a2

HDU 4627 The Unsolvable Problem 解题心得

原题: Description There are many unsolvable problem in the world.It could be about one or about zero.But this time it is about bigger number. Given an integer n(2 <= n <= 10 9).We should find a pair of positive integer a, b so that a + b = n and [a, b