Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- string s;
- int N;
- int dp[55][55];
- int cost[55][55];
- int main() {
- // freopen("input.txt", "r", stdin);
- cin >> s;
- N = s.size();
- memset(dp, 127, sizeof(dp));
- dp[1][1] = 1;
- cost[1][1] = 1;
- for(int i = 2; i <= N; i++) {
- for(int m = i; m > 0; m--) {
- int sz = i+1-m;
- cost[i][m] = sz;
- if(sz%2 == 0) {
- int mid = sz/2;
- bool same = 1;
- for(int j = m-1; j < (m-1)+mid; j++) {
- if(s[j] != s[j+mid]) {
- same = 0;
- break;
- }
- }
- if(same) cost[i][m] = cost[i-mid][m] + 1;
- }
- if(m > 1) {
- for(int m2 = m-1; m2 > 0; m2--) {
- dp[i][m] = min(dp[i][m], dp[m-1][m2] + cost[i][m] + (m2 != 1));
- }
- } else {
- dp[i][m] = min(dp[i][m], cost[i][m]);
- }
- }
- }
- // for(int i = 0; i <= N; i++) {
- // for(int m = 0; m <= N; m++) {
- // printf("%02d ", (dp[i][m] < 1<<20 ? dp[i][m] : -1));
- // }
- // printf("\n");
- // }
- int sol = 1<<30;
- for(int m = 1; m <= N; m++) {
- sol = min(sol, dp[N][m]);
- }
- cout << sol << "\n";
- return 0;
- }
- // 01234
- // xyzz
- /*
- int mid = (i+1-m)/2;
- bool same = 1;
- for(int j = m; j < m+mid; j++) {
- if(s[j] != s[j+mid]) {
- same = 0;
- break;
- }
- }
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement