Decoding the message UVA solution 11220

This is an easy problem from uva judge http://uva.onlinejudge.org/external/112/11220.html

Ad hoc problem

How I did it

  • Read the number of cases.
  • Read a line at a time with getline.
  • Filter the words using an sstream(stringstream) object and save each word in a vector<string>.
  • Traverse the vector and develop the output word.
  • Save the word in a output vector.
  • When I find an empty line I print the output vector content.

Hints

  • Cases T  (1 ≤ T ≤ 30). Total lines per case N (1 ≤ N ≤ 100 ). Each line has  M words (1 ≤ M ≤ 30).
  • At least we will have one word in the output.
  • Print a blank line BETWEEN cases. In the last case you must not print a blank line.

Code

#include <iostream>
#include <cstdio>
#include <vector>
#include <sstream>
using namespace std;
int main(){
int casos, index, flag = 0, answer = 1;
string cad, output,word, input;
stringstream ss;
scanf("%d",&casos);
getline(cin, cad);
getline(cin,cad);//Empty line
while(casos--){

    vector<string> result;
    while(getline(cin,cad))
    {
        vector<string> vec;
        if(cad.size() == 0)
           break;

        ss<<cad;
        while(ss>>input)
              vec.push_back(input);

        index = 0;
        output = "";

        for(int x=0; x<vec.size(); x++)
        {
            word = vec[x];
            if(index<word.size())
            {
             output += word[index];
             index++;
            }
        }
        result.push_back(output);
        ss.clear();
    }
    if(!flag)
       flag = 1;
    else
        printf("\n");
    printf("Case #%d:\n",answer++);
    for(int h=0; h<result.size(); h++)
        printf("%s\n", result[h].c_str());
}
return 0;
}

Leave a comment