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. hdu Repositoryti

    算法:字典树 题意:给你一些字符串,然后会有一些询问,输出询问在给定的字符串中出现了多少次(字串也是): 例如 add,子串有:a ,d,d,ad ,dd,add:你会发现子串d出现了两次,那么怎么办 ...

  2. SQL 标准中的四种隔离级别

    READ UNCOMMITED(未提交读) 在RERAD UNCOMMITED级别,事务中的修改,即使没有提交,对其他事务也都是可见的.事务可以读取未提交的数据,这也成为脏读(Dirty Read). ...

  3. 【7】使用css/js/html模板来实现一个注册、登录和管理的功能

    分支:auth static添加文件 css文件夹: app.css    自定义css样式[*] bootstrap.min.cs    bootstrap样式 compomemts文件夹: 插件用 ...

  4. IOS 文件管理 2

    IOS开发-文件管理(二) 五.Plist文件 String方式添加               NSString *path = [NSHomeDirectory( )  stringByAppen ...

  5. Django的 select_related 和 prefetch_related 函数对 QuerySet 查询的优化(三)

    4.一些实例 如果我们想要获得所有家乡是湖北的人,最无脑的做法是先获得湖北省,再获得湖北的所有城市,最后获得故乡是这个城市的人.就像这样: 1 2 3 4 5 >>> hb = Pr ...

  6. 用UseMiddleware扩展方法注册中间件类

    用UseMiddleware扩展方法注册中间件类 .NET Core中间件的注册和管道的构建(2)---- 用UseMiddleware扩展方法注册中间件类 0x00 为什么要引入扩展方法 有的中间件 ...

  7. heritrix 3.2.0 -- 环境搭建

    heritrix作为一个比较经典的开源爬虫,写这篇文章目的是因为,3.X之后的heritrix的介绍以及配置的文章比较少了. heritrix 3.x 以后使用maven 2配置jar包引用,但是总是 ...

  8. hadoop执行hbase插入表操作,出错:Stack trace: ExitCodeException exitCode=1:(xjl456852原创)

    在执行hbase和mapreduce融合时,将hdfs上的文本文件插入到hbase中,我没有使用"胖包"(胖包就是将项目依赖的jar包放入项目打包后的lib目录中),而是直接将hb ...

  9. pyqt 自定义例子学习

    # -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' import sys from PyQt4.QtCore impor ...

  10. SpringMVC(二)——流程控制

    SpringMVC主要就是用来做流程控制的,这篇博客总结一下如何在流程控制添加Interceptor(拦截器),如何将进行流程Mapping映射解析,如何编写Controller(控制器). 一,首先 ...