Description
The heart of the beauty of all people have, GG is no exception. So GG has a passionate love for the perfect string. The perfect string in GG's eyes is an incredibly attractive 01-strand. This substring has its charm, reverse it and flip it back, and it's exactly the same as it was. That's why GG loves it. But not all 01 strings are perfect strings, so GG is determined to transform 01 strings, making all the 01 strings a perfect string. But the transformation of 01 string is a huge project, GG is too busy, he also poor t a 01 string not modified, he needs your help. And you just have to tell it to add at least a few ' 0 ', ' 1 ' characters to make the 01 strings a perfect string.
Input
There is a T set of data input. (t<=100)
Each set of data has only two rows, the first line is a positive integer n (1<=n<=1000), the next line is a 01 string, and the length is n.
Output
Output a row of results for each set of data
Sample Input2410013111Sample Output atHINT
The main idea: judging the longest common substring and then the total minus is, because the longest match will not move, because as long as the remaining one of the group has a number of matching the remaining group is also matched, the specific group of data will know, symmetrical.
Then the oldest string algorithm is counted on the line.
#include <cstdio>#include<cstring>#include<algorithm>using namespacestd;Chars[1100];intdp[1100][1100];inta[1100],b[1100];intMain () {intn,t; scanf ("%d",&T); while(t--) {scanf ("%d",&N); scanf ("%s", s); intk = N-1; for(inti =0; i < n; i++) {A[i]= S[i]-'0'; B[k--] =1-A[i]; } memset (DP,0,sizeof(DP)); for(inti =0; I < n; i++){ for(intj =0; J < N; J + +){ if(A[i] = =B[j]) dp[i+1][j+1] = dp[i][j]+1; Elsedp[i+1][j+1] = max (dp[i+1][j],dp[i][j+1]); }} printf ("%d\n", N-Dp[n][n]); } return 0;}
View Code
Guang Gong School race--lcs--Perfect string