您的位置:首页 > 编程语言

代码对齐 - uva1593 - streamstring的用法

2018-03-15 20:49 429 查看
You are working in a team that writes Incredibly Customizable Programming Codewriter (ICPC) whichis basically a text editor with bells and whistles. You are working on a module that takes a piece ofcode containing some definitions or other tabular information and aligns each column on a fixed verticalposition, while keeping the resulting code as short as possible, making sure that only whitespaces thatare absolutely required stay in the code. So, that the first words on each line are printed at positionp1 = 1; the second words on each line are printed at the minimal possible position p2, such that all firstwords end at or before position p2 − 2; the third words on each line are printed at the minimal possibleposition 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 ormore words separated by spaces. Each word can contain uppercase and lowercase Latin letters, allASCII punctuation marks, separators, and other non-whitespace ASCII characters (ASCII codes 33 to126 inclusive). Whitespace consists of space characters (ASCII code 32).InputThe input file contains one or more lines of the code up to the end of file. All lines (including the lastone) 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 ofthe code can have both leading and trailing spaces. Each line in the input file is at most 180 characterslong. There are at most 1000 lines in the input file.OutputWrite to the output file the reformatted, aligned code that consists of the same number of lines, withthe same words in the same order, without trailing and leading spaces, separated by one or more spacessuch 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 code32).Sample Input␣␣start:␣␣integer;␣␣␣␣//␣begins␣herestop:␣integer;␣//␣␣ends␣here␣s:␣␣string;c:␣␣␣char;␣//␣tempSample Outputstart:␣integer;␣//␣begins␣herestop:␣␣integer;␣//␣ends␣␣␣heres:␣␣␣␣␣string;c:␣␣␣␣␣char;␣␣␣␣//␣temp
分析:
4000

整行的读入用getline()函数
getline()的原型是istream& getline(istream &is, string &str, char delim)
istream &is 表示一个输入流(如cin);
string &str表示把从输入流的字符串放在这个字符串中;
char delim 表示遇到这个字符串时停止读入,在不设置的情况下系统默认的字符串为'\n'
streamstring的用法
用法一:
瞎**输入一行数,可以以空格分割得到单个的字符串#include<iostream>
#include<cstdio>
#include<sstream>

using namespace std;
int main(){
string str,s;
while(getline(cin,str)){
stringstream tran(str);
while(tran>>s){
cout<<s<<endl;
}
}
}
/*
输入:
Apple 8dfjk;
输出:
Apple
8dfjk;
*/
用法二
用于转换#include<iostream>
#include<cstdio>
#include<sstream>

using namespace std;
int main(){
//int->string
int a=112,b=113;
string result;
stringstream ss;
ss<<a;//像流中传值
ss>>result;//向result里写值
cout<<result<<endl;

//string->int
string s;
int n;
s="123";
stringstream tt;
tt<<s;
tt>>n;
cout<<n<<endl;
}
/*
输出:
112
123
*/
用法三
stringstream不会主动释放内存,但若要在程序中用同一个流,反复写大量的数据,将会造成大量的内存消耗,所以要适时地清除一下缓冲(stream.str(""))#include <cstdlib>
#include<iostream>
#include<sstream>
using namespace std;
int main()
{
stringstream ss;
string s;
ss<<"shanghai no1 school";
ss>>s;
cout<<ss.str()<<endl;
cout<<"size of stream = "<<ss.str().length()<<endl;
cout<<"s: "<<s<<endl;
ss.str("");
cout<<"size of stream = "<<ss.str().length()<<endl;
}
/*
输出:
shanghai no1 school
size of stream = 19
s: shanghai
size of stream = 0
*/贴上AC代码:#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
#include<sstream>

using namespace std;

vector<string> a[1300];
int max_len[200];

void print(string x,int n){
int len=x.size();
for(int i=0;i<len;i++){
cout<<x[i];
}
while(len<n){
cout<<' ';
}
}

int main(){
string str,s;
int len;
int row=0,col=0;
while(getline(cin,str)){
stringstream tran(str);
while(tran>>s){
len=s.size();
max_len[col]=max(max_len[col],len);
col++;
a[row].push_back(s);
}
row++;col=0;
}
int i,j;
for(i=0;i<row;i++){
for(j=0;j<a[i].size()-1;j++){
print(a[i][j],max_len[j]);
}
cout<<a[i][j]<<endl;
}
}之前写的代码错了,但是不知道为啥->_->先贴上……
错误代码:#include<iostream>
#include<cstdio>
#include<vector>
#include<string>
#include<algorithm>

using namespace std;
vector<string> a[1003];

void print(string s,int t){
cout<<s;
int len=s.length();
while(len<=t){
cout<<' ';
len++;
}
}

int main(){
string s;
int k=0;
char c;
while(cin>>s){
a[k].push_back(s);
c=getchar();
if(c=='\n'){
k++;
}
}

int Mcol=0;int x;
for(int i=0;i<k;i++){//求出来最多有几列
x=a[i].size();
Mcol=max(Mcol,x);
}
int l[180]={0};
int y;
string str;
for(int i=0;i<Mcol;i++){//列
for(int j=0;j<k;j++){//行
if(i>=a[j].size())continue;
str=a[j][i];
y=str.length();
l[i]=max(l[i],y);
}
}
int j;
for(int i=0;i<k;i++){
for(j=0;j<a[i].size()-1;j++){
print(a[i][j],l[j]);
}
cout<<a[i][j]<<endl;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: