首页 > ACM题库 > HDU-杭电 > HDU 3753-Alignment of Code-字符串-[解题报告]HOJ
2015
04-10

HDU 3753-Alignment of Code-字符串-[解题报告]HOJ

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

题意: 就是让你把一些字符串相应的单词对齐输出。呵呵,学到了一些stl的东西、

可是,时间为什么过得这么快呢?

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 <iomanip>//header
#include <cmath>
#include <sstream>//header
#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;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

参考:http://blog.csdn.net/julyana_lin/article/details/7980429