UVa-1593 - Alignment of Code

这几天一直在刷《算法竞赛入门经典》,由于是STL章节,所以这篇和下面的几篇都用STL来写。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<string>
 5 #include<vector>
 6 #include<iomanip>
 7 #include<sstream>
 8 using namespace std;
 9 const int maxx=1010;
10 int main()
11 {
12     //freopen("in.txt","r",stdin);
13     //freopen("out.txt","w",stdout);
14     vector<string> v;
15     string s;
16     while(getline(cin,s))
17         v.push_back(s);
18     vector<string> word[maxx];
19     vector<int> maxsz;
20     for(int i=0;i<v.size();i++)
21     {
22         stringstream ss(v[i]);
23         string t;
24         while(ss>>t)
25         {
26             if(word[i].size()>=maxsz.size()) maxsz.push_back(t.size());
27             else maxsz[word[i].size()]=max(maxsz[word[i].size()],(int)t.size());
28             word[i].push_back(t);
29         }
30     }
31     for(int i=0,j;i<v.size();i++)
32     {
33         for(j=0;j<word[i].size()-1;j++)
34             cout<<setw(maxsz[j]+1)<<left<<word[i][j];
35         cout<<word[i][j]<<endl;
36     }
37 }

 

你可能感兴趣的:(code)