aboutsummaryrefslogtreecommitdiff
path: root/challenge-046/ulrich-rieke/cpp/ch-1.cpp
blob: 36603cfc5917927d18e9c7404212b7ccce509fcf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <vector>
#include <algorithm>
#include <string>
#include <utility>
#include <map>
#include <iostream>

std::string decode ( const std::vector<std::string> & encoded ) {
  int len = encoded[0].length( ) ;
  std::string decoded ;
  for ( int i = 0 ; i < len ; i++ ) {
      std::map<std::string, int> frequencies ;
      for ( const std::string & word : encoded ) {
    frequencies[ word.substr( i , 1 ) ]++ ;
      }
      std::vector<std::pair<std::string , int> > howmany (
        frequencies.begin( ) , frequencies.end( ) ) ;
      decoded.append( std::max_element( howmany.begin( ) , howmany.end( ) ,
          []( auto & a, auto & b ) { return a.second < b.second ; } )->first) ;
  }
  return decoded ;
}

int main( ) {
  std::vector<std::string> theCodes { "Hxl4!" , "ce-lo" , "ze6lg" , "HWlvR" ,
      "q9m#o" } ;
  std::cout << decode( theCodes ) << std::endl ;
  return 0 ;
}