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 p 1 = 1; the second words on each line are printed at the minimal possible position p 2, 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 p 3 - 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 p i.
 
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 关于代码对齐的练习 只需要找出每一列最长单词的长度,利用stringstream就可以轻松实现

vjudge 上的题目链接 :https://cn.vjudge.net/problem/POJ-3959;
关键字: vector , stringstream , setw() , string
 #include <iostream>
#include <algorithm>
#include <vector>
#include <sstream>
#include <iomanip>
using namespace std;
const int N = + ;
int max_len[]; //定义为全局列表 数组初始化为0
//记录每一列最长的长度
int main()
{
ios_base::sync_with_stdio(false);
string line;
int line_num = ;
vector<string> tail[N];
while(getline(cin,line)) //开始录入每一行的数据 遇到ctrl+z停止
{
int i=;
string word;
stringstream wordline(line); //word<<line 使用字符串流读取每一行的每一个单词
while(wordline>>word)
{
max_len[i] = max(max_len[i],int(word.length()));
tail[line_num].push_back(word);
++i;
}
++line_num;
}
for(int i = ;i < line_num;i++)
{
for(int j = ;j < tail[i].size(); j++)
{
cout<<setiosflags(ios::left)<<setw(max_len[j]+)<<tail[i][j];
//setiosflags(ios::left) 即控制向左输入
}
cout<<endl;
}
return ;
}

tips: stringstream 很方便,,但也很慢。


UVA 1593 Alignment of Code(紫书习题5-1 字符串流)的更多相关文章

  1. Uva - 1593 - Alignment of Code

    直接用<iomanip>的格式输出,setw设置输出宽度,setiosflags(ios::left)进行左对齐. AC代码: #include <iostream> #inc ...

  2. UVA 1593: Alignment of Code(模拟 Grade D)

    题意: 格式化代码.每个单词对齐,至少隔开一个空格. 思路: 模拟.求出每个单词最大长度,然后按行输出. 代码: #include <cstdio> #include <cstdli ...

  3. UVA 1594 Ducci Sequence(紫书习题5-2 简单模拟题)

    A Ducci sequence is a sequence of n-tuples of integers. Given an n-tuple of integers (a1, a2, · · · ...

  4. 紫书 习题 11-9 UVa 12549 (二分图最小点覆盖)

    用到了二分图的一些性质, 最大匹配数=最小点覆盖 貌似在白书上有讲 还不是很懂, 自己看着别人的博客用网络流写了一遍 反正以后学白书应该会系统学二分图的,紫书上没讲深. 目前就这样吧. #includ ...

  5. 紫书 习题 11-8 UVa 1663 (最大流求二分图最大基数匹配)

    很奇怪, 看到网上用的都是匈牙利算法求最大基数匹配 紫书上压根没讲这个算法, 而是用最大流求的. 难道是因为第一个人用匈牙利算法然后其他所有的博客都是看这个博客的吗? 很有可能-- 回归正题. 题目中 ...

  6. 紫书 习题8-12 UVa 1153(贪心)

    本来以为这道题是考不相交区间, 结果还专门复习了一遍前面写的, 然后发现这道题的区间是不是 固定的, 是在一个范围内"滑动的", 只要右端点不超过截止时间就ok. 然后我就先考虑有 ...

  7. 紫书 习题8-7 UVa 11925(构造法, 不需逆向)

    这道题的意思紫书上是错误的-- 难怪一开始我非常奇怪为什么第二个样例输出的是2, 按照紫书上的意思应该是22 然后就不管了,先写, 然后就WA了. 然后看了https://blog.csdn.net/ ...

  8. UVA 816 Abbott's Revenge 紫书

    紫书的这道题, 作者说是很重要. 但看着题解好长, 加上那段时间有别的事, 磨了几天没有动手. 最后,这道题我打了五遍以上 ,有两次被BUG卡了,找了很久才找到. 思路紫书上有,就缺少输入和边界判断两 ...

  9. UVa 11582 Colossal Fibonacci Numbers! 紫书

    思路是按紫书上说的来. 参考了:https://blog.csdn.net/qwsin/article/details/51834161  的代码: #include <cstdio> # ...

随机推荐

  1. Vim直接打开Tampermonkey网址的方法。

    根据tampermonkey利用@require调用本地脚本的方法,比如我电脑上保存了Tampermonkey脚本a.user.js和它调用的a.js, 想在Vim编辑这两个文件时,都能一键打开网页里 ...

  2. 【转载】#446 - Deciding Between an Abstract Class and an Interface

    An abstract class is a base class that may have some members not implemented in the base class, but ...

  3. vuejs 开发中踩到的坑

    用 v-for 循环式  每个item的值相等的情况下,会影响v-model的双向绑定: Modal 组件开发,主要用slot 标签来实现 <template> <transitio ...

  4. hdu-2582 f(n)---找规律+素数筛法

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2582 题目大意: 给出公式Gcd(n)=gcd(C[n][1],C[n][2],……,C[n][n- ...

  5. Android(java)学习笔记42:Map集合的获取功能

    1. Map集合的获取功能:  package cn.itcast_01; import java.util.Collection; import java.util.HashMap; import ...

  6. hdu 6243,6247

    题意:n只狗,n个笼子,每个笼子只能有一只,求不在自己笼子的狗的数量的期望. 分析:概率是相等的,可以直接用方案数代替,k 不在自己的笼子的方案数是 n!- (n-1)!,这样的k有n个,总的方案数n ...

  7. SqlSugar之DbContext

    创建一个DbContext和DbSet进行使用,我们可以在DbSet中进行扩展我们的方法 //可以直接用SimpleClient也可以扩展一个自个的类 //推荐直接用 SimpleClient //为 ...

  8. 用$(this)选择其下带有class的子元素

    $(this).find('.son').removeClass("disn")

  9. 关于前端token

    主要是一些前端使用的流程: 客户端使用用户名密码登录.服务端收到请求,去验证用户名与密码.验证成功后,服务端会签发一个 Token,把这个 Token 发送给客户端.客户端将收到的Token存储起来. ...

  10. 20.springboot项目部署到linux服务器文件上传临时路径处理问题

    1.前言 把项目部署到服务器上之后,文件上传默认会在/tmp路径中. 之前想了各种解决办法,比如如何更改这个上传路径...... 最后发现不是个好的方法,当然就想到了更好的解决方案. 就是我把上传文件 ...