Dang_Quan_10_Tin

DELROW (HSGHN L9 2020-2021)

Dec 19th, 2021
898
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.60 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <algorithm>
  4.  
  5. using namespace std;
  6.  
  7. constexpr int N = 5e3 + 5;
  8. string s[N];
  9. int m, n, id[N], p[N], cnt[N];
  10.  
  11. void Read()
  12. {
  13.     cin >> m >> n;
  14.  
  15.     for (int i = 1; i <= m; ++i)
  16.     {
  17.         string v;
  18.         cin >> v;
  19.  
  20.         for (int j = 0; j < n; ++j)
  21.             s[j + 1].push_back(v[j]);
  22.     }
  23. }
  24.  
  25. // Counting Sort
  26. void Sort()
  27. {
  28.     for (int i = m - 1; ~i; --i)
  29.     {
  30.         for (int j = 0; j < 26; ++j)
  31.             cnt[j] = 0;
  32.  
  33.         for (int j = 1; j <= n; ++j)
  34.             ++cnt[s[id[j]][i] - 'a'];
  35.  
  36.         for (int j = 1; j < 26; ++j)
  37.             cnt[j] += cnt[j - 1];
  38.  
  39.         for (int j = n; j; --j)
  40.             p[cnt[s[id[j]][i] - 'a']--] = id[j];
  41.  
  42.         swap(id, p);
  43.     }
  44.     for (int i = 1; i <= n; ++i)
  45.         p[id[i]] = i;
  46.  
  47.     for (int i = 1; i <= n; ++i)
  48.         while (p[i] != i)
  49.         {
  50.             swap(s[p[i]], s[i]);
  51.             swap(p[p[i]], p[i]);
  52.         }
  53. }
  54.  
  55. void Solve()
  56. {
  57.     for (int i = 1; i <= n; ++i)
  58.     {
  59.         reverse(s[i].begin(), s[i].end());
  60.         id[i] = i;
  61.     }
  62.  
  63.     /// O(n * m) sort
  64.     Sort();
  65.     /// O(n * m * log(m)) sort
  66.     // sort(s + 1, s + n + 1);
  67.  
  68.     int ans(m);
  69.  
  70.     for (int i = 1; i < n; ++i)
  71.     {
  72.         int j = 0;
  73.         while (j < m && s[i][j] == s[i + 1][j])
  74.             ++j;
  75.         ans = min(ans, m - j - 1);
  76.     }
  77.  
  78.     cout << ans;
  79. }
  80.  
  81. int32_t main()
  82. {
  83.     ios::sync_with_stdio(0);
  84.     cin.tie(0);
  85.     cout.tie(0);
  86.     freopen("DELROW.INP", "r", stdin);
  87.     freopen("DELROW.OUT", "w", stdout);
  88.  
  89.     Read();
  90.     Solve();
  91. }
  92.  
Advertisement
Add Comment
Please, Sign In to add comment