Arranging the Appetizers
|
All submissions for this problem are available.
The Chef has prepared the appetizers in the shapes of letters to spell a special message for the guests. There are n appetizers numbered from 0 to n-1 such that if the appetizers are arrayed in this order, they will display the message. The Chef plans to display them in this order on a table that can be viewed by all guests as they enter. The appetizers will only be served once all guests are seated.
The appetizers are not necessarily finished in the same order as they are numbered. So, when an appetizer is finished the Chef will write the number on a piece of paper and place it beside the appetizer on a counter between the kitchen and the restaurant. A server will retrieve this appetizer and place it in the proper location according to the number written beside it.
The Chef has a penchant for binary numbers. The number of appetizers created is a power of 2, say n = 2k. Furthermore, he has written the number of the appetizer in binary with exactly k bits. That is, binary numbers with fewer than k bits are padded on the left with zeros so they are written with exactly k bits.
Unfortunately, this has unforseen complications. A binary number still "looks" binary when it is written upside down. For example, the binary number "0101" looks like "1010" when read upside down and the binary number "110" looks like "011" (the Chef uses simple vertical lines to denote a 1 bit). The Chef didn't realize that the servers would read the numbers upside down so he doesn't rotate the paper when he places it on the counter. Thus, when the server picks up an appetizer they place it the location indexed by the binary number when it is read upside down.
You are given the message the chef intended to display and you are to display the message that will be displayed after the servers move all appetizers to their locations based on the binary numbers they read.
Input
The first line consists of a single integer T ≤ 25 indicating the number of test cases to follow. Each test case consists of a single line beginning with an integer 1 ≤ k ≤ 16 followed by a string of precisely 2kcharacters. The integer and the string are separated by a single space. The string has no spaces and is composed only of lower case letters from `a` to `z`.
Output
For each test case you are to output the scrambled message on a single line.
Example
Input: 2 2 chef 4 enjoyourapplepie Output: cehf eayejpuinpopolre
The question was basically a cakewalk if done in traditional ways. However the only challenge of this
question was to find the reverse of a binary digit. And to it was a constrained fixed that if the the length
of the binary representation of the digit is less than k then extra number of zeroes are appended. So
suppose if number=3 and number of digits =5. the 011 would be appended to make 00011 & the reversed bit is 11000(24). Thus we use the following steps to reverse the number in binary (bit manipulation)
int y=0;
for(int i=0;i<k;i++)
{
y=y<<1|(num&1);
num>>2;
}
return y;
a dry run of this thing will elaborate the idea clearly.....
code goes here
#include<iostream>
#include<stdio.h>
#include<string.h>
char str[1<<16+1];
int reverse(int x, int k)
{
int y=0;
for(int i=0;i<k;i++)
{
y=(y<<1 |(x&1));
x=x>>1;
}
return y;
}
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;cin>>str;
for(int i=0;i<1<<n;i++)
{
int j=reverse(i,n);
if(i<j)
{
char temp=str[j];
str[j]=str[i];
str[i]=temp;
}
}
cout<<str<<endl;
}
return 0;
}
No comments:
Post a Comment