If you can share your code it will be easier for us to try to break it. Also if you like, I can describe my approach which worked.

**EDIT:** I guess @vsp4 has found the flaw.

My approach uses dp[i][j][k], which represents the cost of building the string upto the first i characters such that after it is built, the substring S[j..k] is on the clipboard. Additionally there is a dp_{min}[i] which is also the cost of building the string upto the first i characters, but regardless of what ends up on the clipboard. Initially all values are set to +\infty.

If some substring S[j..i] is pasted to extend the string from S[1..j-1] to S[1..i], we can set dp[i][j][i] = dp[j-1][j][i] + 1. Here we face a problem, that at j-1 we could not have possibly known that some substring in S[1..j] will match with S[j..i]. To overcome this, some preprocessing can be done to find the first occurrence of each substring, and only bother about those. I used Z-algorithm on every substring S[i..N] : 1 \le i \le N to generate a table, and from that we can generate another table f where f[i][j] is the starting index of first occurrence of the substring S[i..j].

As I was saying, we can paste some substring S[j..i] to get S[1..i]. Then we find x = f[j][i], y=x+i-j, which are the bounds of the first occurrence of S[j..i]. We can now set dp[i][x][y] = dp[j-1][x][y] + 1.

Alternately, if S[i] is typed and appended to S[i-1], all dp[i][j][k] can be reduced if possible to dp[i-1][j][k]+1, since we add one character but don’t touch the clipboard.

Clearly, dp_{min}[i] = \min\{dp_{min}[i-1]+1, \min\{dp[i][j][k] \text{ for all } j,k \}\}

The last step is to iterate over all subarrays S[j..k] in S[1..i], and reduce each dp[i][j][k] to dp_{min}[i]+1 if they can be reduced, since we can always copy any substring in the current string to the clipboard after it is built.

In the end, dp_{min}[N] will be the answer.

The complexity is \mathcal{O}(|S|^3) for both the main algorithm and the preprocessing.

You can see my code for more details.