I’ve lost the competition!

The first round was so buggy that I was not able to submit anything! BTW, just for fun I have solved some problems of  “Facebook Hacker Cup Test Round“.

Problem: Reverser

You’ve intercepted a series of transmissions encrypted using an interesting and stupid method, which you have managed to decipher. The messages contain only spaces and lowercase English characters, and are encrypted as follows: for all words in a sentence, the ith word (1-based) word is replaced with the word generated by applying the following recursive operation f(word, i):

If the length of word is less than or equal to i,
return word.
Otherwise, return f(right half of word, i) + f(left half of word, i).

If word is of odd length, it is split such that the right side is longer. You’ve decided to have a little fun with whoever is sending the messages, and to broadcast your own messages encrypted in the same style that they are using.
Input
Your input will begin with an integer N, followed by a newline and then N test cases. Each case consists of an unencrypted sentence containing only spaces and lowercase letters, and cases are newline-separated. There will be no leading or trailing spaces in a sentence and there will be at most 1 space character between any otherwise-adjacent characters
Output
Output, for each case and separated by newlines, the contents of the encrypted sentence after applying the encoding method describe above to it. You may ignore traditional capitalization rules and stick to all lowercase letters.
Constraints
5 ≤ N ≤ 25
Sentences will contain no more than 100 characters.
Sample input:
5
this is a short sentence
this encryption is lame
racecar
test problems are pretty easy to solve
q we rty uiop
Sample Output:
siht is a ortsh encesent
siht oniptrycen is lame
racecar
tset msleobpr are ttypre easy to solve
q we rty uiop

The code (C++):

//============================================================================
// Name        : reverse-cpp.cpp
// Author      : Francesco Laurita
// Version     : 1.0
// Copyright   : Francesco Laurita
// Description :
//============================================================================

#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <math.h>
#include <vector>
using namespace std;

string apply(string in, int pos){
	int slen = in.length();
	if (slen <= pos){
		return in;
	}else{
		int mid = ceil(slen / 2);
		string right = in.substr(mid,slen);
		string left  = in.substr(0,mid);
		return apply(right,pos) + apply(left,pos);
	}
}
int main(int argc, char **argv) {

	ifstream fp(argv[1]);
	if (!fp){
		cerr << "Unble to open file!" << endl;
		return 1;
	}
	int nlines;
	fp >> nlines;
	fp.ignore();
	for (int i = 0; i < nlines; i++){
		string line,tok;
		getline(fp,line);
		stringstream ss(line);
		vector<string> tokens;
		int j = 1;
		while (ss >> tok){
			tokens.push_back(apply(tok,j));
			j++;
		}
		for (vector<string>::iterator it = tokens.begin(); it != tokens.end(); ++it){
			cout << *it;
			if (it != tokens.end()-1)
				cout << " ";
		}
		tokens.empty();
		cout << endl;
	}
	fp.close();
	return 0;
}

Code is also available on pastebin: http://pastebin.com/nW2KBiyZ

© 2013 Francesco Laurita' s blog Suffusion theme by Sayontan Sinha