博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj 3959 Alignment of Code <vector>“字符串”
阅读量:6637 次
发布时间:2019-06-25

本文共 4090 字,大约阅读时间需要 13 分钟。

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 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 p
2 - 2; the third words on each line are printed at the minimal possible position p
3, 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 herestop: integer; //  ends here   s:  string;   c:   char; // temp

Sample Output

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

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

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

 

补充几点

在vector中查找特定的元素

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

vector
x; x.push_back(123); x.push_back(333); x.push_back(222); vector
::iterator iter; iter = find(x.begin(), x.end(), 222);

 

清空vector

vector
student;student.clear();

 

转载于:https://www.cnblogs.com/farewell-farewell/p/5459462.html

你可能感兴趣的文章
【转】WaitForSingleObject()& ReleaseMutex()的用法
查看>>
加域时,客户机必须启动的服务
查看>>
MY BACKUP JOB COMPLETED BUT HAS EXCEPTIONS OR WARNINGS, WHY?
查看>>
杭电 hdu 2010
查看>>
Uwsgi的安装
查看>>
关于短信的管理:删除、读、等等
查看>>
仿猪八戒首页导航菜单
查看>>
Nginx 漏洞 (CVE-2018-16843,CVE-2018-16844)
查看>>
轻松解决“正在连接”无线的故障
查看>>
直接退出程序
查看>>
poj 2481 Cows【线段树单点更新】
查看>>
32位LINUX下hadoop2.2.0重新编译及安装步骤
查看>>
前端开发学习系列之一:准备工作
查看>>
活动目录中的Get-Aduser这个cmdlets调用的是账户的哪个属性?
查看>>
我的友情链接
查看>>
shell中if的参数说明
查看>>
随笔-java注解,反射,枚举
查看>>
jqm two columns
查看>>
我的友情链接
查看>>
一个例子看懂所有nodejs的官方网络demo
查看>>