简单DP,注意有多个测试数据 用DP找出2个字串的最长子序列 ,然后用2个字符串中长度较长的字符串长度减去子序列长度即可 #include <iostream> #include <string> using namespace std; string str1,str2; int DP_LCS(int i,int j){ //DP if(i==-1 || j==-1) return 0; if(str1[i]==str2[j]) return DP_LCS(i-1,j-1)+1; else { if(DP_LCS(i-1,j)>DP_LCS(i,j-1)) return DP_LCS(i-1,j); else return DP_LCS(i,j-1); } } int main(){ int size1,size2,i,j,size; while(cin>>size1>>str1>>size2>>str2){ i=size1-1; j=size2-1; size=size1>=size2?size1:size2; cout<<size-DP_LCS(i,j)<<endl; } return 0; }