Miyago147852

UVa 12526

Mar 3rd, 2022
909
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.22 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define IO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
  3. #define endl "\n"
  4. #define endll "\n\n"
  5. #define ll long long
  6. #define pb push_back
  7.  
  8. using namespace std;
  9.  
  10. struct trie {
  11.     int cnt = 0;
  12.     map<char, trie*> next;
  13. };
  14. trie* root = new trie;
  15.  
  16. int solve(trie* root) {
  17.     int res = 0;
  18.     if (root->next.size()>1) {
  19.         res += root->cnt;
  20.         for (auto e:root->next){
  21.             if (e.first == '.') res--;
  22.         }
  23.     }
  24.     for (auto e:root->next) res+=solve(e.second);
  25.     return res;
  26. }
  27.  
  28. void buildTrie(string s){
  29.     trie* cur = root;
  30.     for (auto c: s) {
  31.         cur->cnt++;
  32.         if (cur->next[c]!=nullptr) cur->next[c] = new trie;
  33.         cur = cur->next[c];
  34.     }
  35. }
  36.  
  37. signed main() {
  38.     IO;
  39.     #ifdef DEBUG
  40.         freopen("p.in", "r", stdin);
  41.         freopen("p.out", "w", stdout);
  42.     #endif
  43.     string s;
  44.     int n, res;
  45.     while (cin>>n) {
  46.         res = 0;
  47.         vector<string> vs;
  48.         for (auto i=0; i<n; i++){
  49.             cin>>s;
  50.             s += '.';
  51.             vs.pb(s);
  52.             buildTrie(s);
  53.         }
  54.         res = solve(root) + (root->next.size()==1)*n;
  55.         printf("%0.2f\n", 1.0*res/n);
  56.     }
  57.    
  58.     return EXIT_SUCCESS;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment