Advertisement
AlejandroGY

Untitled

Mar 5th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.15 KB | None | 0 0
  1. #include <iostream>
  2. #include <array>
  3. #include <string>
  4. #include <algorithm>
  5.  
  6. int indice(char c) {
  7.     return (c == 'A' ? 0 : (c == 'C' ? 1 : (c == 'G' ? 2 : 3)));
  8. }
  9.  
  10. std::array<int, 4> calcula(std::string temp, int ini, int fin) {
  11.     std::array<int, 4> res = { };
  12.     for (int i = ini; i < fin; ++i) {
  13.         res[indice(temp[i])] += 1;
  14.     }
  15.     return res;
  16. }
  17.  
  18. int main( ) {
  19.     std::ios_base::sync_with_stdio(false);
  20.     std::cin.tie(nullptr);
  21.  
  22.     int t;
  23.     std::cin >> t;
  24.     for (int ti = 0; ti < t; ++ti) {
  25.         int n, k;
  26.         std::cin >> n >> k;
  27.         n += 1;
  28.  
  29.         std::string temp;
  30.         std::array<int, 4> prefijo[n], sufijo[n];
  31.         for (int i = 0; i < n; ++i) {
  32.             std::cin >> temp;
  33.             prefijo[i] = calcula(temp, 0, k);
  34.             sufijo[i] = calcula(temp, temp.size( ) - k, temp.size( ));
  35.         }
  36.  
  37.         int memo[2][n];
  38.         auto act = memo[0][0], ant = memo[1][0];
  39.         for (int i = n; i >= 1; --i, std::swap(act, ant)) {
  40.             for (int p = 0; p < i; ++p) {
  41.                 if (i == n) {
  42.                     act[i] = 0;
  43.                 } else {
  44.                     int res = ant[p];
  45.                     if (sufijo[p] == prefijo[i]) {
  46.                         res = std::max(res, 1 + ant[i]);
  47.                     }
  48.                     act[i] = res;
  49.                 }
  50.             }
  51.         }
  52.  
  53.         std::cout << ant[0] << std::endl;
  54.     }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement