Advertisement
avisrivastava254084

Untitled

Oct 26th, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.80 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <vector>
  5. #include <map>
  6. #include "/Users/aviralsrivastava/Desktop/vertica_ServerFault/stdc++.h"
  7.  
  8. using namespace std;
  9.  
  10. // Represents an entry in users.xml
  11. // Some fields omitted due to laziness
  12. struct User
  13. {
  14.     string Id;
  15.     string DisplayName;
  16. };
  17.  
  18. // Represents an entry in posts.xml
  19. // Some fields omitted due to laziness
  20. struct Post
  21. {
  22.     string Id;
  23.     string PostTypeId;
  24.     string OwnerUserId;
  25.     string AcceptedAnswerId;
  26. };
  27.  
  28. string parseFieldFromLine(const string &line, const string &key)
  29. {
  30.     // We're looking for a thing that looks like:
  31.     // [key]="[value]"
  32.     // as part of a larger string.
  33.     // We are given [key], and want to return [value].
  34.  
  35.     // Find the start of the pattern
  36.     string keyPattern = key + "=\"";
  37.     ssize_t idx = line.find(keyPattern);
  38.  
  39.     // No match
  40.     if (idx == -1)
  41.         return "";
  42.  
  43.     // Find the closing quote at the end of the pattern
  44.     size_t start = idx + keyPattern.size();
  45.  
  46.     size_t end = start;
  47.     while (line[end] != '"')
  48.     {
  49.         end++;
  50.     }
  51.  
  52.     // Extract [value] from the overall string and return it
  53.     // We have (start, end); substr() requires,
  54.     // so we must compute, (start, length).
  55.     return line.substr(start, end - start);
  56. }
  57.  
  58. // Keep track of all users
  59. map<string, User> users;
  60.  
  61. void readUsers(const string &filename)
  62. {
  63.     ifstream fin;
  64.     fin.open(filename.c_str());
  65.  
  66.     string line;
  67.     while (getline(fin, line))
  68.     {
  69.         User u;
  70.         u.Id = parseFieldFromLine(line, "Id");
  71.         u.DisplayName = parseFieldFromLine(line, "DisplayName");
  72.         users[u.Id] = u;
  73.     }
  74. }
  75.  
  76. // Keep track of all posts
  77. map<string, Post> posts;
  78.  
  79. void readPosts(const string &filename)
  80. {
  81.     ifstream fin;
  82.     fin.open(filename.c_str());
  83.  
  84.     string line;
  85.     while (getline(fin, line))
  86.     {
  87.         Post p;
  88.         p.Id = parseFieldFromLine(line, "Id");
  89.         p.PostTypeId = parseFieldFromLine(line, "PostTypeId");
  90.         p.OwnerUserId = parseFieldFromLine(line, "OwnerUserId");
  91.         p.AcceptedAnswerId = parseFieldFromLine(line, "AcceptedAnswerId");
  92.         posts[p.Id] = p;
  93.     }
  94. }
  95.  
  96. User findUser(const string &Id)
  97. {
  98.     if (users.find(Id) != users.end())
  99.     {
  100.         return users[Id];
  101.     }
  102.     return User();
  103. }
  104.  
  105. // Some data for the map
  106. struct MapData
  107. {
  108.     string DisplayName;
  109.     int Count;
  110. };
  111.  
  112. Post findPost(const string &Id)
  113. {
  114.     if (posts.find(Id) != posts.end())
  115.     {
  116.         return posts[Id];
  117.     }
  118.     return Post();
  119. }
  120.  
  121. int main(int argv, char **argc)
  122. {
  123.     // Load our data
  124.     readUsers("users-short.xml");
  125.     readPosts("posts-short.xml");
  126.  
  127.     // Calculate the users with the most answers
  128.     map<string, MapData> answers;
  129.  
  130.     for (map<string, Post>::iterator ite = posts.begin(); ite != posts.end(); ite++)
  131.     {
  132.         Post p = ite->second;
  133.         answers[p.OwnerUserId].DisplayName = "";
  134.         if (p.PostTypeId == "2")
  135.         {
  136.             answers[p.OwnerUserId].Count++;
  137.         }
  138.     }
  139.  
  140.     cout << "Top 10 users with the most answers:" << endl;
  141.     for (int i = 0; i < 10; i++)
  142.     {
  143.         string key;
  144.         MapData max_data = {"", 0};
  145.         for (map<string, MapData>::iterator it = answers.begin();
  146.              it != answers.end(); ++it)
  147.         {
  148.             if (it->second.Count >= max_data.Count)
  149.             {
  150.                 key = it->first;
  151.                 max_data = it->second;
  152.             }
  153.         }
  154.  
  155.         answers.erase(key);
  156.         cout << max_data.Count << '\t' << findUser(key).DisplayName << endl;
  157.     }
  158.     // Calculate the users with the most accepted answers
  159.     map<string, MapData> acceptedAnswers;
  160.  
  161.     for (map<string, Post>::iterator ite2 = posts.begin(); ite2 != posts.end(); ite2++)
  162.     {
  163.         Post post = ite2->second;
  164.         if (post.PostTypeId == "1" && post.AcceptedAnswerId != "")
  165.         {
  166.             Post answerPost = findPost(post.AcceptedAnswerId);
  167.             if (answerPost.PostTypeId != "2")
  168.                 continue;
  169.             acceptedAnswers[answerPost.OwnerUserId].DisplayName = "";
  170.             acceptedAnswers[answerPost.OwnerUserId].Count++;
  171.         }
  172.     }
  173.  
  174.     cout << "Top 10 users with the most accepted answers:" << endl;
  175.     for (int i = 0; i < 10; i++)
  176.     {
  177.         string key;
  178.         MapData max_data = {"", 0};
  179.         for (map<string, MapData>::iterator it = acceptedAnswers.begin();
  180.              it != acceptedAnswers.end(); ++it)
  181.         {
  182.             if (it->second.Count >= max_data.Count)
  183.             {
  184.                 key = it->first;
  185.                 max_data = it->second;
  186.             }
  187.         }
  188.  
  189.         acceptedAnswers.erase(key);
  190.         cout << max_data.Count << '\t' << findUser(key).DisplayName << endl;
  191.     }
  192.  
  193.     cout << endl;
  194.  
  195.     return 0;
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement