Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.59 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. string s;
  5. int N;
  6. int dp[55][55];
  7. int cost[55][55];
  8.  
  9. int main() {
  10. //    freopen("input.txt", "r", stdin);
  11.     cin >> s;
  12.     N = s.size();
  13.  
  14.     memset(dp, 127, sizeof(dp));
  15.  
  16.     dp[1][1] = 1;
  17.     cost[1][1] = 1;
  18.  
  19.     for(int i = 2; i <= N; i++) {
  20.         for(int m = i; m > 0; m--) {
  21.             int sz = i+1-m;
  22.             cost[i][m] = sz;
  23.  
  24.             if(sz%2 == 0) {
  25.                 int mid = sz/2;
  26.                 bool same = 1;
  27.                 for(int j = m-1; j < (m-1)+mid; j++) {
  28.                     if(s[j] != s[j+mid]) {
  29.                         same = 0;
  30.                         break;
  31.                     }
  32.                 }
  33.  
  34.                 if(same) cost[i][m] = cost[i-mid][m] + 1;
  35.             }
  36.             if(m > 1) {
  37.                 for(int m2 = m-1; m2 > 0; m2--) {
  38.                     dp[i][m] = min(dp[i][m], dp[m-1][m2] + cost[i][m] + (m2 != 1));
  39.                 }
  40.             } else {
  41.                 dp[i][m] = min(dp[i][m], cost[i][m]);
  42.             }
  43.         }
  44.     }
  45. //    for(int i = 0; i <= N; i++) {
  46. //        for(int m = 0; m <= N; m++) {
  47. //            printf("%02d ", (dp[i][m] < 1<<20 ? dp[i][m] : -1));
  48. //        }
  49. //        printf("\n");
  50. //    }
  51.     int sol = 1<<30;
  52.     for(int m = 1; m <= N; m++) {
  53.         sol = min(sol, dp[N][m]);
  54.     }
  55.  
  56.     cout << sol << "\n";
  57.  
  58.     return 0;
  59. }
  60.  
  61. // 01234
  62. // xyzz
  63. /*
  64.     int mid = (i+1-m)/2;
  65.     bool same = 1;
  66.     for(int j = m; j < m+mid; j++) {
  67.         if(s[j] != s[j+mid]) {
  68.             same = 0;
  69.             break;
  70.         }
  71.     }
  72. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement