uvaoj-1593:alignment of code

来源:互联网 发布:视频社交软件 编辑:程序博客网 时间:2024/05/18 08:36

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 herestop: integer; //  ends here   s:  string;   c:   char; // temp

Sample Output 

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


题解:刘汝佳135页练习题5-1;考察string和sstream之类的东西,一道模拟题;

题目的意思大概就是在每行输入一定数量的字符串,字符串之间用空格隔开,然后让每一列的字符串之间左对齐//幸好不是右对齐;

我的思路就是定义一个一维数组用来存string们,然后建立一个一维数组来存每一列字符串的字符数的最大值,然后直接打印就行了,需要注意的就是每一行最后一个单词后边没有空格;


code:

#include <string>
#include <iostream>
#include <cstdio>
#include <vector>
#include <sstream>
#include <algorithm>
using namespace std;
const int maxn=105000;

int main()
{
    string s[maxn];
    string str;
    int numb[maxn]={0};
    int dig[maxn]={0};
    int cnt=0;
    int num=0;
    while(getline(cin,str))
    {
        stringstream ss(str);
        string stri;
        int num_col=0;
        while(ss >> stri)
        {
            s[cnt]=stri;
            int len=(int)stri.size();
            numb[num_col]=max(numb[num_col],len);
            num_col++;
            cnt++;
        }
        dig[num++]=num_col;
    }
    cnt=0;
    for(int i=0; i<num; i++)
    {
        for(int j=0; j<dig[i]; j++)
        {
            cout<<s[cnt];
            cnt++;
            if(j==dig[i]-1) break;
            int len=(int)s[cnt-1].size();
            for(int k=0; k<numb[j]-len+1; k++)
                cout<<' ';

        }
        cout<<endl;
    }
    return 0;
}


笔记:

一道简单题被卡很久对我来说几乎是家常便饭了,做出来以后感觉很简单,没做出来真的是虐心啊;

这道题首先是最后的细节,UVA经常因为空格来让题目WA,这个要注意;

另外s.size()的时候需要强制转换,看起来是个好习惯,记下来;

最主要的是string的意义,string是一个类似int的类型,而不是char s[100]这样的字符型数组,string s[100]的意思就是一个一维数组;

关于string贴上链接:

string类型详解;

http://blog.csdn.net/nefu2015214119/article/details/50631150;

给刘汝佳跪了,继续去钻研。。。

2 0
原创粉丝点击