Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.94 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. using ll = long long;
  6.  
  7. struct rule
  8. {
  9. string ip_from;
  10. int mask;
  11. string ip_to;
  12. string left, right;
  13. string password;
  14. };
  15.  
  16. bool match_id (string left, string right, string name)
  17. {
  18. if (left.empty())
  19. return true;
  20. return (int)name.size() == (int)left.size() && left <= name && name <= right;
  21. }
  22. bool match_pass (string pass, string attempt)
  23. {
  24. return pass.empty() || attempt == pass;
  25. }
  26. void parse_ip (string &parse_to, int &mask_to, string from)
  27. {
  28. int pos = find(from.begin(), from.end(), '/') - from.begin();
  29. if (pos == (int)from.size())
  30. from += "/32";
  31. from[pos] = ' ';
  32.  
  33. for (char &ch : from)
  34. {
  35. if (ch == '.')
  36. ch = ' ';
  37. }
  38.  
  39. stringstream str(from);
  40. int ptr[4], mask;
  41. // cerr << from << endl;
  42. assert(str >> ptr[0] >> ptr[1] >> ptr[2] >> ptr[3] >> mask);
  43.  
  44. parse_to.clear();
  45. mask_to = mask;
  46. for (int i = 0; i < 4; i++)
  47. {
  48. for (int j = 7; j >= 0; j--)
  49. parse_to += ((ptr[i] >> j) & 1) + '0';
  50. }
  51. }
  52.  
  53. string to_ip (string what)
  54. {
  55. int ptr[4];
  56. assert((int)what.size() == 32);
  57. for (int i = 0, k = 0; i < 4; i++)
  58. {
  59. ptr[i] = 0;
  60. for (int j = 0; j < 8; j++)
  61. ptr[i] *= 2, ptr[i] += (what[k] - '0'), k++;
  62. }
  63.  
  64. stringstream str;
  65. for (int i = 0; i < 4; i++)
  66. {
  67. str << ptr[i];
  68. if (i + 1 < 4)
  69. str << ".";
  70. }
  71. return str.str();
  72. }
  73.  
  74. void solve(istream &cin = std::cin, ostream &cout = std::cout)
  75. {
  76. vector<vector<rule>> by_port;
  77. vector<int> ports;
  78.  
  79. string s;
  80.  
  81. while (getline(cin, s))
  82. {
  83. if (!s.empty() && s[0] == '-')
  84. break;
  85.  
  86. stringstream str(s);
  87. char type;
  88. str >> type;
  89.  
  90. if (type == 'A')
  91. {
  92. by_port.push_back(vector<rule>());
  93. int port;
  94. str >> port;
  95. ports.push_back(port);
  96. }
  97. else
  98. {
  99. assert(!by_port.empty());
  100. assert(type == 'F');
  101.  
  102. rule cur;
  103. string from;
  104. str >> from;
  105. parse_ip (cur.ip_from, cur.mask, from);
  106.  
  107. char dt;
  108. str >> dt;
  109. assert(dt == 'T');
  110.  
  111. string to;
  112. str >> to;
  113.  
  114. int temp;
  115. parse_ip(cur.ip_to, temp, to);
  116. assert(temp == 32);
  117.  
  118. // cerr << "here: " << str.str() << endl;
  119.  
  120. while (str >> dt)
  121. {
  122. if (dt == 'I')
  123. {
  124. string what, left, right;
  125. str >> what;
  126. const int pos = find(what.begin(), what.end(), '-') - what.begin();
  127. if (pos != (int)what.size())
  128. {
  129. left = what.substr(0, pos);
  130. right = what.substr(pos + 1);
  131. }
  132. else
  133. {
  134. left = right = what;
  135. }
  136.  
  137. cur.left = left;
  138. cur.right = right;
  139. }
  140. else if (dt == 'P')
  141. {
  142. str >> cur.password;
  143. }
  144. else
  145. assert(false);
  146. }
  147.  
  148. by_port.back().push_back(cur);
  149. }
  150. }
  151.  
  152. while (getline(cin, s))
  153. {
  154. stringstream str(s);
  155. string from, name, password;
  156. int port = -1;
  157.  
  158. // cerr << "query : " << s << endl;
  159.  
  160. char dt;
  161. while (str >> dt)
  162. {
  163. if (dt == 'F')
  164. {
  165. string ifrom;
  166. str >> ifrom;
  167. int temp;
  168. parse_ip(from, temp, ifrom);
  169. assert(temp == 32);
  170. }
  171. else if (dt == 'T')
  172. {
  173. str >> port;
  174. }
  175. else if (dt == 'I')
  176. {
  177. str >> name;
  178. }
  179. else if (dt == 'P')
  180. {
  181. str >> password;
  182. }
  183. else
  184. assert(false);
  185. }
  186.  
  187. const int pos = find(ports.begin(), ports.end(), port) - ports.begin();
  188. const string no = "dev/null";
  189. auto f = [&] ()
  190. {
  191. if (pos == (int)ports.size())
  192. return no;
  193.  
  194. for (const auto &r : by_port[pos])
  195. {
  196. if (from.substr(0, r.mask) == r.ip_from.substr(0, r.mask) && match_id(r.left, r.right, name) && match_pass(r.password, password))
  197. return to_ip(r.ip_to);
  198. }
  199.  
  200. return no;
  201. };
  202.  
  203. auto ans = f();
  204. cout << ans << endl;
  205. }
  206. }
  207.  
  208.  
  209. int main()
  210. {
  211. ios_base::sync_with_stdio(false);
  212. cin.tie(nullptr);
  213.  
  214. cout << fixed;
  215.  
  216. #ifdef LOCAL
  217. ifstream fin("../input.txt");
  218.  
  219. solve(fin);
  220.  
  221. cout << setprecision(4) << "clock: " << clock() / (double) CLOCKS_PER_SEC << endl;
  222. #else
  223. cout << setprecision(20);
  224.  
  225. solve();
  226. #endif
  227.  
  228. return 0;
  229. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement