Skip to content

Instantly share code, notes, and snippets.

@junfenglx
Created March 2, 2015 17:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save junfenglx/e7840f9aa9faf022f56e to your computer and use it in GitHub Desktop.
Save junfenglx/e7840f9aa9faf022f56e to your computer and use it in GitHub Desktop.
#include <iostream>
#include <vector>
#include <string>
void print(const std::string &s, size_t i) {
size_t len = s.length();
size_t space_count = i < len ? i : len;
for(size_t j = 0; j < space_count; ++j)
std::cout << " ";
if (space_count < len)
std::cout << s.c_str() + i;
}
int main()
{
using namespace std;
vector<string> words;
size_t max_len = 0;
while(true)
{
string s;
cin >> s;
if (!cin)
break;
string t(s);
if (max_len < t.length())
max_len = t.length();
words.push_back(t);
}
for (size_t i=0; i < max_len; ++i)
{
vector<string>::const_iterator start = words.begin();
print(*start, i);
for(start = start + 1; start != words.end(); ++start)
{
cout << " ";
print(*start, i);
}
cout << endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment