Monday, February 7, 2011

Facebook Hacker Cup Test Round, Reverser

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.

#include <iostream>
#include <string>
#include <sstream>
using namespace std;
#include <vector>
#include <iterator>
template <typename T, typename S> void copys(const T& c, S& d) { istringstream iss(c); copy(istream_iterator<T>(iss), istream_iterator<T>(),back_inserter<S>(d)); return;}
#define vs vector<string>
#define vi vector<int>

vector<string> words;
string func(string word, int i){
  char temp[51];size_t len;
  if(word.size() <= i)return word;
  else{
    len = (float)word.size()/2.0+0.5;
    len = word.copy(temp,len,word.size()/2);
    temp[len]='\0';
    word.erase(word.begin()+word.size()/2, word.end());
    return func(temp, i) + func(word, i);
  }
}
main(){
  int i, n;
  string s;
  cin>>n;
  getline(cin,s);
  while(n--){
    getline(cin, s);
    copys(s, words);
    for(i=0;i<words.size();i++)
      cout<<(i?" ":"")<<func(words[i], i+1);
    cout<<endl;
    words.clear();
  }
}

No comments:

Post a Comment