2015
04-10

# 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 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).

The input begins with an integer T. The next T blocks each represents a case. Each case contains one or more lines of the code up to the end of case mark-a single ‘@’ in a single line. All lines 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 for each case.

The input begins with an integer T. The next T blocks each represents a case. Each case contains one or more lines of the code up to the end of case mark-a single ‘@’ in a single line. All lines 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 for each case.

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

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

http://acm.hdu.edu.cn/showproblem.php?pid=3753

getline(cin,tmp)

istringstream strin(tmp);

while(strin >> tmp)

cout << setiosflags(ios :: left);

cout << setw(len[j] + 1) << str[i][j];

/*
Pro: 0

Sol:

date:
*/
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <queue>
#include <set>
#include <vector>
//istringstream对象可以绑定一行字符串，然后以空格为分隔符把该行分隔开来。
using namespace std;
string tmp,str[1111][190];
int len[190],T,num[1111];
int main(){
scanf("%d",&T);
while(T --){
memset(len,0,sizeof(len));
memset(num,0,sizeof(num));
int nNum = 0;
while(getline(cin,tmp) && tmp != "@"){
if(tmp == "") continue; //empty
istringstream strin(tmp);
int j = 0;
nNum ++;
while(strin >> tmp){
str[nNum][++ j] = tmp;
if(tmp.size() > len[j]) len[j]= tmp.size();
}
num[nNum] = j;
}
cout << setiosflags(ios :: left);
for(int i = 1; i <= nNum; i ++){
for(int j = 1; j < num[i]; j ++){
cout << setw(len[j] + 1) << str[i][j];
}
cout << str[i][num[i]] << endl;
}
}
return 0;
}