Advertisement
mr_dot_convict

String and LIS ICPC

Aug 7th, 2021
1,415
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.90 KB | None | 0 0
  1. #include     <bits/stdc++.h>
  2. #ifndef      CONVICTION
  3. #define      debug(x...)
  4. #endif
  5. #define IOS  ios_base::sync_with_stdio(false); cin.tie (nullptr)
  6. #define PREC cout.precision (10); cout << fixed
  7. #define F    first
  8. #define S    second
  9. #define int  long long
  10. #define ff   long double
  11. using namespace std;
  12. //Don’t practice until you get it right. Practice until you can’t get it wrong
  13.  
  14. void preproc() { }
  15.  
  16. int n, q;
  17. int prevdp[26][26], nextdp[26][26], seen[26], state[26][26];
  18. const int D = 52;
  19.  
  20. // reference : https://codeforces.com/blog/entry/80195
  21. struct Matrix {
  22.    int a[D][D];
  23.    Matrix() {
  24.       for (int i = 0; i < D; ++i)
  25.          for (int j = 0; j < D; ++j)
  26.             a[i][j] = -1;
  27.    }
  28.  
  29.    Matrix operator *(Matrix other) {
  30.       Matrix res = Matrix();
  31.       for (int i = 0; i < D; ++i)
  32.          for (int j = 0; j < D; ++j)
  33.             for (int k = 0; k < D; ++k)
  34.                if ( a[i][j] != -1 and other.a[j][k] != -1 )
  35.                   res.a[i][k] = max(res.a[i][k], a[i][j] + other.a[j][k]);
  36.  
  37.       return res;
  38.    }
  39. };
  40.  
  41. Matrix expo_power(Matrix a, int k) {
  42.    Matrix res = Matrix();
  43.    for (int i = 0; i < D; ++i)
  44.       res.a[i][i] = 1;
  45.    while(k) {
  46.       if(k % 2)
  47.          res = res * a;
  48.       a = a * a;
  49.       k /= 2;
  50.    }
  51.    return res;
  52. }
  53.  
  54. void solve()
  55. {
  56.    cin >> n >> q;
  57.  
  58.    memset(state, -1, sizeof(state));
  59.  
  60.    string s;
  61.    for (int i = 0; i < n; ++i) {
  62.       cin >> s;
  63.       memset(prevdp, -1, sizeof(prevdp));
  64.       memset(nextdp, -1, sizeof(nextdp));
  65.       memset(seen, 0, sizeof(seen));
  66.  
  67.       prevdp[s[0] - 'a'][s[0] - 'a'] = 1;
  68.  
  69.       for (int j = 1; j < (int)s.size(); ++j) {
  70.          int fx = s[j]-'a';
  71.  
  72.          nextdp[fx][fx] = 1;
  73.          for (int start = 0; start <= fx; ++start)  // 26
  74.             for (int y = 0; y <= fx; ++y)
  75.                if ( prevdp[start][y] != -1 )
  76.                   nextdp[start][fx] = max(prevdp[start][fx], prevdp[start][y] + 1);
  77.  
  78.          for (int x = 0; x < 26; ++x)
  79.             for (int y = 0; y < 26; ++y)
  80.                prevdp[x][y] = nextdp[x][y];
  81.       }
  82.  
  83.       for (int x = 0; x < 26; ++x)
  84.          for (int y = x; y < 26; ++y)
  85.             state[x][y] = max(state[x][y], prevdp[x][y]);
  86.    }
  87.  
  88.    Matrix init = Matrix();
  89.    for (int x = 0; x < 26; ++x)
  90.       for (int y = x; y < 26; ++y)
  91.          init.a[2*x][2*y] = state[x][y];
  92.  
  93.  
  94.    for (int x = 0; x < 26; ++x) {
  95.       init.a[2*x][2*x+1] = 0; // v-> v'
  96.       init.a[2*x + 1][2*x + 1] = 0; // self loop v' <-> v'
  97.    }
  98.  
  99.    Matrix res = expo_power(init, q+1);
  100.    int ans = 0;
  101.    for (int x = 0; x < 26; ++x)
  102.       for (int y = x; y < 26; ++y)
  103.          ans = max({ans, res.a[2*x][2*y + 1]});
  104.  
  105.    cout << ans << '\n';
  106. }
  107.  
  108. signed main()
  109. {
  110.    IOS; PREC;
  111.    preproc();
  112.  
  113.    int tc = 1;
  114.    cin >> tc;
  115.    for (int Tt = 1; Tt <= tc; ++Tt) {
  116.       // cout << "Case #" << Tt << ": ";
  117.       solve();
  118.    }
  119.    return EXIT_SUCCESS;
  120. }
  121.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement