uva-1593题解

题干

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 ‘⊔’ 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: char; // temp

思路

总而言之,题目就是让我们把每个单词在列上对齐即可。对于所有单词,我们考虑用一个 vector allword[MANX]来储存,然后记录每一列的单词最大是多少,即列数,最后每个单词的宽度按照该列最大宽度输出就可以达到题目效果

语法清单

setw

setw 是设置输出格式长度
头文件里面,setw(n)对下一个输出进行n宽度输出
可以和left,setfill(ch)搭配使用
<<left<<setw(n)左对齐输出n(默认是右对齐)
<<setfill('#')<<setw(n)<<arr;

stringstream ss(line)

stringstream

1.介绍

用于处理字符串的输入输出流。从string里面读入

c++里面,流是数据从一个对象到另一个对象的流动。数据输入输出通过IO流实现,cin/cout是istream,ostream创建的对象(cin,cout是抽象实现,不能创建和拷贝,但是可以引用,istream & _cin=cin对,istream _cin=cin和istream _cin都错。 而stringstream不是抽象类,可以直接创建)

2.创建(一般来说创建名为ss)

a.直接创建

string arr("hello world");

stringstream ss;

ss<< arr;

cout <<ss.str()<<endl;

b.初始化创建

stringsream ss("hello world");

3.输出

调用ss.str();不能直接输出ss

4.修改流

ss.str("fuck world")

5.常见用法

string line,wold;

while(getline(cin,line)){

stringstream ss(line)

while(ss>>word)...

}

题解代码

#include<iostream>
#include<sstream>
#include<vector>
#include<string>
#include<cmath>
#include<iomanip>
using namespace std;
//<bits/stdc++.h>万能头文件包含以上所有库
const int MAXN=1010;
vector <string> allword[MAXN];
//allword二维string数组储存所有单词,以供输出
int main(){
    size_t maxlength[200]={0};
//.size()方法返回类型是size_t,故这里也是size_t
    string line,word;
    int linecnt=0;
//记录行数
    while(getline(cin,line)){
//getline返回流状态istream&,如果没有成功输入,由于重载了operator bool,istream&可以转为0;
        int i=0;
        stringstream ss(line);
//用line初始化stringstream对象ss
        while(ss>>word){
//用word逐个读取每行里面的每个单词
            maxlength[i]=max(maxlength[i],word.length());
            i++;
            allword[linecnt].push_back(word);
//把word放入vector里面                   
        }
    linecnt++;
    }
    for(int i=0;i<linecnt;i++){
        for(int j=0;j<allword[i].size();j++){
            if(j<allword[i].size()-1)
            cout<<left<<setw(maxlength[j]+1)<<allword[i][j];
//这一步是题目要求,结尾不能有空格,所以最后一个单词的输出应该是maxlength,单独讨论
            else 
            cout<<left<<allword[i][j];
        }
        cout<<endl;
    }
    return 0;
}
posted @ 2025-07-24 16:15  hardestnut  阅读(9)  评论(0)    收藏  举报