System Administrators
National Olympiad — National round
Submission name: sysadmin.cpp
Problem summary
Four original character bytes are packed into a 32-bit word, first character most significant. Encoding rotates that word K bits right and prints its bytes as four decimal values separated by dots. Decode every encoded group and concatenate its four original characters.
Input
K, followed by dotted groups until EOF; there is no count.
Output
The decoded message on one line, preserving its spaces.
Constraints
0 ≤ K ≤ 31; at most 1,000,000 groups; each component is 0…255; original length is divisible by four.
Worked example
An independently prepared example for this explanation.
Input
0 67.79.68.69
Output
CODE
Solution analysis
- Pack the four encoded bytes in big-endian order.
- Rotate the unsigned 32-bit word left by K.
- Output its four bytes in the original order.
Why it works
The inverse of a right rotation is a left rotation by the same amount. Unpacking restores the original byte sequence.
Watch out. Handle K=0 separately; shifting a 32-bit value by 32 is undefined.
Commented C++17 solution
An independently written complete program with standard headers.
#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
#include<string>
#include<limits>
#include<numeric>
#include<set>
#include<map>
#include<tuple>
#include<cstdint>
#include<functional>
#include<cmath>
using namespace std;
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int k;
cin>>k;
uint32_t a,b,c,d;
char dot;
while(cin>>a>>dot>>b>>dot>>c>>dot>>d){
uint32_t x=(a<<24)|(b<<16)|(c<<8)|d;
if(k){
x=(x<<k)|(x>>(32-k)); // Invert the circular rotation.
}
for(int shift=24;shift>=0;shift-=8){
cout.put(char((x>>shift)&255));
}
}
cout<<'\n';
}