标签:style blog http color strong os
1 Ab3bd
2
解题:很狠狠狠经典的dp题目啊。。。。叫什么来着?最长公共子序列。。。。。长度-最长公共子序列就是我们要的结果啊。。。。
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <vector> 6 #include <climits> 7 #include <algorithm> 8 #include <cmath> 9 #define LL long long 10 using namespace std; 11 char a[1100],b[1100]; 12 int dp[2][1100]; 13 int main(){ 14 int ks,i,len,j; 15 scanf("%d",&ks); 16 while(ks--){ 17 scanf("%s",a); 18 len = strlen(a); 19 for(i = 0; i < len; i++) 20 b[len-i-1] = a[i]; 21 b[len] = ‘\0‘; 22 memset(dp,0,sizeof(dp)); 23 for(i = 1; i <= len; i++){ 24 for(j = 1; j <= len; j++){ 25 if(a[i-1] == b[j-1]){ 26 dp[i%2][j] = dp[(i-1)%2][j-1]+1; 27 }else dp[i%2][j] = max(dp[(i-1)%2][j],dp[i%2][j-1]); 28 } 29 } 30 printf("%d\n",len-dp[(i-1)%2][j-1]); 31 } 32 return 0; 33 }
标签:style blog http color strong os
原文地址:http://www.cnblogs.com/crackpotisback/p/3847333.html