Advertisement
Guest User

Formando equipo

a guest
May 26th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.17 KB | None | 0 0
  1. ///Link del enunciado http://juez.oia.unsam.edu.ar/#/task/equipo/statement
  2.  
  3. #include <iostream>
  4. #include <vector>
  5. #include <map>
  6. #include <fstream>
  7.  
  8. using namespace std;
  9.  
  10. pair <int, string> best;
  11.  
  12. struct Trie{
  13.     map < char, pair <int, Trie> > arbol;
  14.  
  15.     int nivel = -1;
  16. };
  17.  
  18. void armar(Trie &T, const string &s, const int &p){
  19.     if(p < int(s.size() + 1)){
  20.         T.arbol[s[p]].first++;
  21.  
  22.         T.nivel = ((p < int(s.size())) ? (p + 1) : p);
  23.  
  24.         armar(T.arbol[s[p]].second, s, (p + 1));
  25.     }
  26. }
  27.  
  28. void recorrer(Trie &T, const string &s){
  29.     for(auto i:T.arbol){
  30.         int o = (i.second.first * (T.nivel * T.nivel));
  31.  
  32.         if(o > best.first) best = {o, (s + i.first)};
  33.  
  34.         recorrer(i.second.second, (s + i.first));
  35.     }
  36. }
  37.  
  38. int main()
  39. {
  40.     ifstream in("equipo1.in", fstream::in);
  41.     ofstream out("equipo.out", fstream::out);
  42.  
  43.     int fil, col;
  44.  
  45.     Trie T;
  46.  
  47.     in >> col >> fil;
  48.  
  49.     for(int i = 0; i < fil; i++){
  50.         string s;
  51.  
  52.         in >> s;
  53.  
  54.         armar(T, s, 0);
  55.     }
  56.  
  57.     recorrer(T, "");
  58.  
  59.     out << best.first << endl << best.second << endl;
  60.  
  61.     in.close();
  62.     out.close();
  63.  
  64.     return 0;
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement