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. 分布式Session共享(一):tomcat+redis实现session共享

    一.前言 本文主要测试redis实现session共享的实现方式,不讨论如何让nginx参与实现负载均衡等. 二.环境配置 本测试在Window下进行 name version port Tomcat ...

  2. JVM调优基础

    一.JVM调优基本流程 1.划分应用程序的系统需求优先级 2.选择JVM部署模式:单JVM.多JVM 3.选择JVM运行模式 4.调优应用程序内存使用 5.调优应用程序延迟 6.调优应用程序吞吐量 二 ...

  3. jquery navi

    /// <reference path="../global.js" /> /********************************************* ...

  4. mac下升级ruby环境版本

    在ios开发中会经常使用到cocoapods来管理第三方框架,在安装cocoapods的时候会涉及到ruby环境,有时候会因为版本过低会导致安装失败,本文主要讲一下如何升级ruby环境 安装rvm,r ...

  5. 最短路--hdu2544

    最短路 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  6. [bzoj 1001][Beijing2006]狼抓兔子 (最小割+对偶图+最短路)

    Description 现在小朋友们最喜欢的"喜羊羊与灰太狼",话说灰太狼抓羊不到,但抓兔子还是比较在行的, 而且现在的兔子还比较笨,它们只有两个窝,现在你做为狼王,面对下面这样一 ...

  7. iOS高仿城觅-感谢大神分享

    项目展示,由于没有数据,所以所有的cell显示的都是我自己写的数据 抽屉 首页部分效果 首页效果 部分效果 发现 消息 搜索 设置 模糊效果 代码注释展示 代码注释展示 还有很多细节就不一一展示了,大 ...

  8. Android使用自带JSONObject解析JSON数据

    import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import android ...

  9. mysql 中文配置(转)

    Dos下连接mysql后,运行一下几项就可以插入中文了: SET character_set_client = gbk; SET character_set_connection = gbk; SET ...

  10. jsp验证码 (通过单击验证码或超链接换验证码)

    #code.jsp <%@ page language="java" import="java.util.*" import="java.awt ...