Advertisement
Saleh127

UVA 11239 / Map - Implementation

Mar 26th, 2022
1,542
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.83 KB | None | 0 0
  1. /***
  2.  created: 2022-03-26-23.24.25
  3. ***/
  4.  
  5. #include <bits/stdc++.h>
  6. using namespace std;
  7. #define ll long long
  8. #define test int tt; cin>>tt; for(int cs=1;cs<=tt;cs++)
  9. #define get_lost_idiot return 0
  10. #define nl '\n'
  11.  
  12. map<string,ll>cnt;
  13.  
  14. bool cmp(pair<ll,string>a,pair<ll,string>b)
  15. {
  16.     if(a.first==b.first) return a.second<b.second;
  17.     return a.first>b.first;
  18. }
  19.  
  20. int main()
  21. {
  22.     ios_base::sync_with_stdio(0);
  23.     cin.tie(0);
  24.     cout.tie(0);
  25.  
  26.     string a,b,last;
  27.  
  28.  
  29.     while(getline(cin,a))
  30.     {
  31.         if(a=="0") break;
  32.         last="";
  33.  
  34.  
  35.         vector<string>p;
  36.         map<string,ll>check;
  37.         map<string,string>parent;
  38.  
  39.  
  40.         while(1)
  41.         {
  42.             if(a=="1") break;
  43.  
  44.             if(a[0]>='A' && a[0]<='Z')
  45.             {
  46.                 if(a!=last)
  47.                 {
  48.                     p.push_back(a);
  49.                 }
  50.                 last=a;
  51.             }
  52.             else
  53.             {
  54.                 if(check[a]==0)
  55.                 {
  56.                     if(parent[a]!="")
  57.                     {
  58.                         if(parent[a]!=last)
  59.                         {
  60.                             check[a]=1;
  61.                             b=parent[a];
  62.                             cnt[b]--;
  63.                         }
  64.                     }
  65.                     else
  66.                     {
  67.                         parent[a]=last;
  68.                         cnt[last]++;
  69.                     }
  70.                 }
  71.             }
  72.             getline(cin,a);
  73.         }
  74.  
  75.         vector<pair<ll,string>>ans;
  76.  
  77.         for(auto d:p)
  78.         {
  79.             ans.push_back({cnt[d],d});
  80.         }
  81.  
  82.         sort(ans.begin(),ans.end(),cmp);
  83.  
  84.         for(auto d:ans)
  85.         {
  86.             cout<<d.second<<" "<<d.first<<nl;
  87.         }
  88.  
  89.         cnt.clear();
  90.     }
  91.  
  92.     get_lost_idiot;
  93. }
  94.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement