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. jdk是什么?jdk1.8安装配置方法

    jdk是什么呢?jdk的是java development kit的缩写,意思是java程序开发的工具包.也可以说jdk是java的sdk. 目前的JDK大致分三个大版本:Java SE:Java P ...

  2. JS 排序:冒泡、 二分搜索 /折半搜索 half-interval search

    冒泡排序:  重复地走访过要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来. var arr = [5,0,-56,900,12]; //大的排序次数 for(var i=0; i& ...

  3. 使用while循环+try-except定位元素

    selenium学习过程中,发现自己遇到的最大的困难不是那些元素的操作,而是元素的定位,有时候明明利用firebug将xpath路径确认好了,但是在定位元素的时候还是会报错,后来在度娘上找到了一个方法 ...

  4. codeforces 611D New Year and Ancient Prophecy

    f[i = 以i结尾][j = 长度为j] = 方案数. f[i][j] = sum{ f[i-j][k] , k < j || (k == j && s(i-j+1,j) &g ...

  5. Python Day 15 递归、匿名函数、内置函数

    阅读目录 内容回顾 生成器的send方法 递归 匿名函数 内置函数 ##内容回顾 #1.带参装饰器 - 自定义 | wraps def wrap(info) def outer1(func): fro ...

  6. solidity开发之windows下配置remix本地环境遇到的问题及解决

    本人按照这个教程配置remix本地环境.[https://cloud.tencent.com/developer/article/1374376] win+R打开管理员终端,在欲配置为本地目录的路径执 ...

  7. POJ 3020 Antenna Placement 【最小边覆盖】

    传送门:http://poj.org/problem?id=3020 Antenna Placement Time Limit: 1000MS   Memory Limit: 65536K Total ...

  8. 树的直径的求法即相关证明【树形DP || DFS】

    学习大佬:树的直径求法及证明 树的直径 定义: 一棵树的直径就是这棵树上存在的最长路径. 给定一棵树,树中每条边都有一个权值,树中两点之间的距离定义为连接两点的路径边权之和.树中最远的两个节点之间的距 ...

  9. Android学习笔记_12_网络通信之从web获取资源数据到Android

    从web获取图片信息,并显示到android的imageView控件. 一.添加网络访问权限. <uses-permission android:name="android.permi ...

  10. 通过sql语句查询出来的结果字段没有到对应实体类时的处理方法

    通过sql语句查询出来的结果字段没有到对应实体类时的处理方法,对于Person类获取用户第一个名字和年龄两个字段,常见的有两种方式: 1.在创建一个与查询结果字段对应的类,然后通过构造函数实现: Qu ...