Alignment of Code 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:␣␣␣␣␣string;
c:␣␣␣␣␣char;␣␣␣␣//␣temp
HINT
题目的解决思路很清晰,就是录入和输出。录入的方式采用的是读取整行然后赋给stringstream对单词进行分段。分段的该过程中要对每一列的最大长度进行计算。输出就按照正常输出就行,然后后面计算空格的个数。
注意:每一行最后一个单词后面直接换行,不要输出空格!!!
Accepted
#include<iostream>
#include<vector>
#include<cstring>
#include <algorithm>
#include<queue>
#include<sstream>
using namespace std;
int main()
{
queue<string>str[1010];
int maxlen[200] = { 0 };
char arr[200] = { 0 };
memset(arr, ' ', 200);
int num = 0;
string s;
char ss[200];
while (cin.getline(ss,200))
{
int i = 0;
stringstream sss(ss);
while (sss >> s)
{
str[num].push(s);
if (s.length() > maxlen[i++])maxlen[i-1] = s.length();
}
num++;
}
for (int i = 0;i < num;i++)
{
int j = 0;
while (!str[i].empty())
{
cout << str[i].front();
if (str[i].size() != 1)
{
arr[maxlen[j] - str[i].front().length()] = '\0';
cout << arr;
arr[maxlen[j++] - str[i].front().length()] = ' ';
}
str[i].pop();
if (str[i].size())cout << " ";
else {
cout << endl;break;
}
}
}
}
Alignment of Code UVA - 1593的更多相关文章
- [刷题]算法竞赛入门经典(第2版) 5-1/UVa1593 - Alignment of Code
书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 代码:(Accepted,0 ms) //UVa1593 - Alignment of Code #include&l ...
- UVA 1593 Alignment of Code(紫书习题5-1 字符串流)
You are working in a team that writes Incredibly Customizable Programming Codewriter (ICPC) which is ...
- UVa 1593 (水题 STL) Alignment of Code
话说STL的I/O流用的还真不多,就着这道题熟练一下. 用了两个新函数: cout << std::setw(width[j]); 这个是设置输出宽度的,但是默认是在右侧补充空格 所 ...
- Uva - 1593 - Alignment of Code
直接用<iomanip>的格式输出,setw设置输出宽度,setiosflags(ios::left)进行左对齐. AC代码: #include <iostream> #inc ...
- UVA 1593: Alignment of Code(模拟 Grade D)
题意: 格式化代码.每个单词对齐,至少隔开一个空格. 思路: 模拟.求出每个单词最大长度,然后按行输出. 代码: #include <cstdio> #include <cstdli ...
- 【习题5-1 UVA - 1593】Alignment of Code
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 模拟题,每一列都选最长的那个字符串,然后后面加一个空格就好. 这个作为场宽. 模拟输出就好. [代码] #include <b ...
- UVa 1593代码对齐
原题链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- poj 3959 Alignment of Code <vector>“字符串”
Description You are working in a team that writes Incredibly Customizable Programming Codewriter (IC ...
- A - Alignment of Code(推荐)
You are working in a team that writes Incredibly Customizable Programming Codewriter (ICPC) which is ...
随机推荐
- 数据处理_HIVE增量ETL的一种方式
适用场景: 贴源层主表历史数据过大,ETL不涉及历史数据对比或聚合 处理流程: 1.确定一个业务主键字段或物理主键字段 2.确定一个可以判断增量数据范围的字段,这取决于具体的业务场景,一般选用记录的创 ...
- KSM概念学习
KSM: Kernel SamePage Merging 内核同页合并 简介 KSM允许内核在两个或多个进程(包括虚拟客户机)之间共享完全相同的内存页. KSM让内核扫描检查正在运行中的程序,并比较他 ...
- 【Notes_2】现代图形学入门——向量与线性代数
向量与线性代数 点乘和叉乘 Dot Multiplication 点乘在图形学的应用 (1) 求两个向量之间的夹角: $$\cos(\theta) = \frac{(\vec{a} \cdot \ve ...
- .NET gRPC 核心功能初体验,附Demo源码
gRPC是高性能的RPC框架, 有效地用于服务通信(不管是数据中心内部还是跨数据中心). 由Google开源,目前是一个Cloud Native Computing Foundation(CNCF)孵 ...
- Hexo的详细搭建过程——小白的血泪经历QAQ
Hexo的详细搭建过程 环境要求: node.js git 这里提供Centos8.2下的安装过程: dnf module list nodejs dnf module install nodejs: ...
- 《C++ Primer》笔记 第2章 变量和基本类型
如果你的数值超过了int表示范围,选用long long 如果你需要使用一个不大的整数,那么明确指定它的类型是signed char或者unsigned char 执行浮点数运算选用double 当一 ...
- js mysql 时间日期比较
js代码 1 var date1 = '2017/2/13'; 2 //var date1 = new Date().toLocaleDateString(); 3 var date2 = '2017 ...
- 【转载】关于grad_tensors的解惑
转载:https://www.cnblogs.com/marsggbo/p/11549631.html 平常都是无脑使用backward,每次看到别人的代码里使用诸如autograd.grad这种方法 ...
- 设计模式系列之原型模式(Prototype Pattern)——对象的克隆
说明:设计模式系列文章是读刘伟所著<设计模式的艺术之道(软件开发人员内功修炼之道)>一书的阅读笔记.个人感觉这本书讲的不错,有兴趣推荐读一读.详细内容也可以看看此书作者的博客https:/ ...
- vue 弹窗禁止底层滚动
原因:底层视图高度超出百分百,加入弹窗后再苹果浏览器隐藏上下栏的情况下遮罩层没有完全遮住底层. 处理:打开弹窗后禁止底层滚动调用stop事件,关闭则开启底层滚动调用move事件. let mo=fun ...