美文网首页
UVA 1593(Alignment of Code)

UVA 1593(Alignment of Code)

作者: Gaolex | 来源:发表于2016-05-13 23:56 被阅读239次
UVA 1593
#include <iostream>
#include <algorithm>
#include <sstream>
#include <vector>
#include <string>
#include <map>

using namespace std;

int main()
{
    vector < vector<string> > txt;//所有的输入
    string hang,word;//每一行的字母,单个字母
    int col=0, row = 0;//row 输入的行数数
    map<int, int> column;//每列的最长单词长度

    //把输入的文本保存到txt,对于所有的列,找出该列最长的单词的长度
    while ( getline(cin,hang) )
    {
        stringstream hangin(hang);
        vector <string> temprow;//临时行
        int now=1;//标识读到当前是改行第几个单词(处于第几列)
        while (hangin>>word)
        {
            temprow.push_back(word);
            column[now] = max(column[now], (int)word.size());
            now++;
        }
        txt.push_back(temprow);
    }
    //输出答案
    for (int i = 0; i < txt.size(); i++)
    {
        for (int j = 0; j < txt[i].size(); j++)
        {
            cout << txt[i][j];
            for (int k = column[j+1]- txt[i][j].size();k>=0; k--)
            {
                cout << ' ';
            }
            cout << ' ';
        }
        cout << '\n';
    }
    system("pause");
    return 0;
    
}

运行结果:

运行结果

相关文章

网友评论

      本文标题:UVA 1593(Alignment of Code)

      本文链接:https://www.haomeiwen.com/subject/jmymrttx.html