I have been doing this problem for more than two days. DP is better written, but preprocessing is hard to write. The first time I did not write out, see LRJ, but the result is more and more speechless. But still a little inspired, instead of c[i][j] to indicate the first string to take out the former I, the second string to take out the first J when the number of letters is already started, but it is not finished.
And then because of the memset and time-out, learned that if most of the data range is smaller, still do not use memset initialization. The data range is read first, and then the for loop is initialized. From the time-out of the 3s to ac 0s, this gap is quite big! And then the time got rank 5!
LRJ seems to be done with a scrolling array, his C-array calculation is still with the DP, it is difficult to read .... (I have already abandoned the treatment)
1#include <cstdio>2#include <cstring>3#include <iostream>4 using namespacestd;5 Const intinf=0x3f3f3f3f;6 Const intmaxn=5005;7 CharA[MAXN],B[MAXN];8 intLen1,len2;9 intfirst[255][3],last[255][3];Ten intDP[MAXN][MAXN]; One intC[MAXN][MAXN]; A intMain () - { - intT; thescanf"%d",&T); - for(intKase=1; kase<=t;++Kase) - { -scanf"%s%s", A +1, B +1); +Len1=strlen (A +1), Len2=strlen (b +1); - for(intI='A'; i<='Z'; i++) + { Afirst[i][1]=first[i][2]=inf; atlast[i][1]=last[i][2]=-1; - } - for(intI=0; i<=len1;i++) - for(intj=0; j<=len2;j++) -c[i][j]=dp[i][j]=0; - for(intI=1; i<=len1;i++) in { - if(first[a[i]][1]==inf) first[a[i]][1]=i; tolast[a[i]][1]=i; + } - for(intI=1; i<=len2;i++) the { * if(first[b[i]][2]==inf) first[b[i]][2]=i; $last[b[i]][2]=i;Panax Notoginseng } - for(intI=0; i<=len1;i++) the { + if(i) A { thed[n][0]=c[i-1][0]; + if(first[a[i]][1]==i) c[i][0]++; - if(last[a[i]][1]==i&&last[a[i]][2]==-1) c[i][0]--; $ } $ for(intj=1; j<=len2;j++) - { -c[i][j]=c[i][j-1]; the if(first[b[j]][2]==j&&first[b[j]][1]>i) c[i][j]++; - if(last[b[j]][2]==j&&last[b[j]][1]<=i) c[i][j]--;Wuyi } the } - for(intI=0; i<=len1;i++) Wu for(intj=0; j<=len2;j++) - { About if(i>0&&j>0) Dp[i][j]=min (dp[i-1][j],dp[i][j-1])+C[i][j]; $ Else if(i==0&&j>0) dp[i][j]=dp[i][j-1]+C[i][j]; - Else if(i>0&&j==0) dp[i][j]=dp[i-1][j]+C[i][j]; - } -printf"%d\n", Dp[len1][len2]); A } +}
The efficiency of their own problems have been relatively low, these days busy with the orientation and some students work, time fragmented. However, this semester students work more ...
Uva-1625-color Length