The_Law

Untitled

Jan 12th, 2017
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.89 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. const int ID = 7;
  6.  
  7. vector<int> z;
  8.  
  9. void zfunc(string s)
  10. {
  11.     for (int i = 1, l = 0, r = 0; i < s.size(); ++i)
  12.     {
  13.         if (i <= r)
  14.             z[i] = min(z[i - l], r - i + 1);
  15.  
  16.         while (i + z[i] < s.size() && s[i + z[i]] == s[z[i]])
  17.             ++z[i];
  18.  
  19.         if (i + z[i] - 1 > r)
  20.             r = i + z[i] - 1, l = i;
  21.     }
  22. }
  23.  
  24. int main()
  25. {
  26. //    freopen("twi.in", "r", stdin);
  27.  
  28.     int m;
  29.     cin >> m;
  30.  
  31.     string s;
  32.     getline(cin, s);
  33.     getline(cin, s);
  34.  
  35.     string t = "http://!";
  36.     t += s;
  37.  
  38.     int n = t.size();
  39.     z.resize(n);
  40.  
  41.     zfunc(t);
  42.  
  43.     string ans = "";
  44.     int k = max((int)s.size() - m, 0);
  45.  
  46.     for (int i = ID + 1; i < n; ++i)
  47.     {
  48.         if (z[i] >= ID && t[i + z[i]] != ' ' && k && (i == ID + 1 || (i > ID + 1) && t[i - 1] == ' '))
  49.         {
  50.             int it = i;
  51.             int cnt = 0;
  52.  
  53.             bool badbitch = false;
  54.  
  55.             while(it < n && t[it] != ' ')
  56.             {
  57.                 bool small = ('a' <= t[it] && t[it] <= 'z');
  58.                 bool fig = ('0' <= t[it] && t[it] <= '9');
  59.                 bool dot = (t[it] == '.');
  60.                 bool slash = (t[it] == '/');
  61.                 bool tdots = (t[it] == ':');
  62.  
  63.                 if (!(small || fig || dot || slash || tdots))
  64.                     badbitch = true;
  65.  
  66.                 ++it, ++cnt;
  67.             }
  68.  
  69.             if (badbitch)
  70.             {
  71.                 ans += t[i];
  72.                 continue;
  73.             }
  74.  
  75.             int add = cnt - 3 - min(cnt, k);
  76.             k -= min(cnt - 3, k);
  77.  
  78.             for (int j = i; j < i + add; ++j)
  79.                 ans += t[j];
  80.  
  81.             ans += "...";
  82.  
  83.             i = it - 1;
  84.         }
  85.         else
  86.             ans += t[i];
  87.     }
  88.  
  89.     cout << ans << endl;
  90.     cout << max((int)ans.size() - m, 0) << endl;
  91.  
  92.     return 0;
  93. }
Advertisement
Add Comment
Please, Sign In to add comment