Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ///Link del enunciado http://juez.oia.unsam.edu.ar/#/task/equipo/statement
- #include <iostream>
- #include <vector>
- #include <map>
- #include <fstream>
- using namespace std;
- pair <int, string> best;
- struct Trie{
- map < char, pair <int, Trie> > arbol;
- int nivel = -1;
- };
- void armar(Trie &T, const string &s, const int &p){
- if(p < int(s.size() + 1)){
- T.arbol[s[p]].first++;
- T.nivel = ((p < int(s.size())) ? (p + 1) : p);
- armar(T.arbol[s[p]].second, s, (p + 1));
- }
- }
- void recorrer(Trie &T, const string &s){
- for(auto i:T.arbol){
- int o = (i.second.first * (T.nivel * T.nivel));
- if(o > best.first) best = {o, (s + i.first)};
- recorrer(i.second.second, (s + i.first));
- }
- }
- int main()
- {
- ifstream in("equipo1.in", fstream::in);
- ofstream out("equipo.out", fstream::out);
- int fil, col;
- Trie T;
- in >> col >> fil;
- for(int i = 0; i < fil; i++){
- string s;
- in >> s;
- armar(T, s, 0);
- }
- recorrer(T, "");
- out << best.first << endl << best.second << endl;
- in.close();
- out.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement