Description

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.

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 代码超时!!!!!!!!!!!!!!!
#include <iostream>
#include <cstdio>
#include <string>
#include <vector>
#include <sstream>
using namespace std; vector<string> code[]; //1000行单词
int len[];
int main()
{
int n = ; //len中存储一行中每个单词的长度
string line;
while((getline(cin,line)) != NULL){ n++;
stringstream ss(line); //不用这个会累死的 int q = ; //q为每行单词第几个数
string word;
while(ss >> word){
int t = word.length();
if(n == )len[q] = t;
else if(len[q] < t)len[q] = t; //同一列单词中最长的
q++;
code[n].push_back(word);
}
} for(int i = ;i <= n;i++){ int N = code[i].size(); for(int j = ;j < N;j++){ line = code[i][j];
int q = line.length();
cout << code[i][j]; for( int p = ;p <= len[j] - q;p++)printf(" ");
}
putchar('\n');
} // system("pause");
return ;
}

n++的位置不对 导致插入向量的字符串存在问题

#include <iostream>
#include <cstdio>
#include <string>
#include <vector>
#include <sstream>
using namespace std; vector<string> code[]; //1000行单词
int len[];
int main()
{
int n = ; //len中存储一行中每个单词的长度
string line;
while((getline(cin,line)) != NULL){
stringstream ss(line); //不用这个会累死的 int q = ; //q为每行单词第几个数
string word;
while(ss >> word){
int t = word.length();
if(n == )len[q] = t;
else if(len[q] < t)len[q] = t; //同一列单词中最长的
q++;
code[n].push_back(word);
}
n++;
}
for(int i = ;i < n;i++){ int N = code[i].size(); for(int j = ;j < N;j++){ line = code[i][j];
int q = line.length();
cout << code[i][j]; for( int p = ;p <= len[j] - q;p++)printf(" ");
}
putchar('\n');
} // system("pause");
return ;
}

补充几点

在vector中查找特定的元素

用 find 函数,头文件#include <algorithm>

vector<int> x;
x.push_back();
x.push_back();
x.push_back();
vector<int>::iterator iter;
iter = find(x.begin(), x.end(), );

清空vector

vector<int>student;
student.clear();

poj 3959 Alignment of Code <vector>“字符串”的更多相关文章

  1. [刷题]算法竞赛入门经典(第2版) 5-1/UVa1593 - Alignment of Code

    书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 代码:(Accepted,0 ms) //UVa1593 - Alignment of Code #include&l ...

  2. UVA 1593 Alignment of Code(紫书习题5-1 字符串流)

    You are working in a team that writes Incredibly Customizable Programming Codewriter (ICPC) which is ...

  3. POJ 1836 Alignment

    Alignment Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 11450 Accepted: 3647 Descriptio ...

  4. POJ 3174 Alignment of the Planets (暴力求解)

    题意:给定 n 个坐标,问你三个共线的有多少组. 析:这个题真是坑啊,写着 n <= 770,那么一秒时间,三个循环肯定超时啊,我一直不敢写了,换了好几种方法都WA了,也不知道为什么,在比赛时坑 ...

  5. POJ 1208 The Blocks Problem --vector

    http://poj.org/problem?id=1208 晚点仔细看 https://blog.csdn.net/yxz8102/article/details/53098575 #include ...

  6. poj 3415 后缀数组 两个字符串中长度不小于 k 的公共子串的个数

    Common Substrings Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 11469   Accepted: 379 ...

  7. poj 2774 后缀数组 两个字符串的最长公共子串

    Long Long Message Time Limit: 4000MS   Memory Limit: 131072K Total Submissions: 31904   Accepted: 12 ...

  8. POJ 1836 Alignment (双向DP)

    Alignment Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 10804   Accepted: 3464 Descri ...

  9. 代码对齐 (Alignment of Code,ACM/ICPC NEERC 2010,UVa1593)

    题目描述: 解题思路: 输入时提出单个字符串,并用一个数组记录每列最长长度,格式化输出 #include <iostream> #include <algorithm> #in ...

随机推荐

  1. string 与char* char[]之间的转换

    1.首先必须了解,string可以被看成是以字符为元素的一种容器.字符构成序列(字符串).有时候在字符序列中进行遍历,标准的string类提供了STL容器接口.具有一些成员函数比如begin().en ...

  2. C/C++ 中##(两个井号)和#(一个井号)用法

    ##(两个井号)和#(一个井号)都是什么意思 连接符 ##(两个井号) 不知道什么符 #(一个井号) ## 连接符号由两个井号组成,其功能是在带参数的宏定义中将两个子串(token)联接起来,从而形成 ...

  3. irms模拟数据生成及数据分析

    一.数据准备 1.每天生成随机一个文本,每小时向文本中追加2次数据,每次10万条 随机数据生成: 2,32  * * * *  bash /mnt/jediael/irms/signalGenerat ...

  4. Effective Java实作equals() - 就是爱Java

    equals()这个方法,定义在Object class中,这个是所有class的base class,因此所有的class都继承这个方法,默认是比较内存地址,不过Mix需要的是商业规则上的比较,所以 ...

  5. Ubuntu apache 禁止目录浏览

    $ sudo vim /etc/apache2/sites-enabled/000-default 将Options后面Indexes前面加上"-"表示禁止目录浏览: <Di ...

  6. hdu 1811 Rank of Tetris

    http://acm.hdu.edu.cn/showproblem.php?pid=1811 拓扑排序和并差集 #include <cstdio> #include <queue&g ...

  7. spring 4 泛型注入

    最近对系统进行改造,发现在泛型实例初始化的时候,得不到想要的泛型.或者需要强制转换. spring 4 开始支持泛型对象初始化,初始化方法如下: 注:使用配置文件的方法暂时还没有发现,下面是使用jav ...

  8. 认识什么是SEO

    何为SEO? SEO是由英 文Search Engine Optimization缩写而来, 中文意译为“搜索引擎优化”,是指在了解搜索引擎自然排名机制的基础上,对网站进行内部及外部的调整优化,改进网 ...

  9. c、c++知识点

    一. (1)在linux下类似uint8_t这样的文件定义在头文件<stdint.h>里面   (2)截取了stdint.h头文件里的一些常用部分 二.c++中c_str()用法 函数返回 ...

  10. linux之chdir函数解析

    [lingyun@localhost chdir]$ ls chdir.c [lingyun@localhost chdir]$ cat chdir.c  /********************* ...