标签:main sizeof style while freopen 题意 include logs pre
求两个字符串的公共子序列,如“abcd” 与 “becd”的公共子序列是 “bcd”
设两个字符串为 串s 和串t
dp[i][j]:= s1..si和t1...tj对应的LCS长度
那么 dp[i][j] = {
0 , i =0 or j = 0;
dp[i-1][j-1] + 1, i ,j > 0 and si = tj;
max(dp[i-1][j] , dp[i][j-1]), i, j > 0 and si != tj;
#include<bits/stdc++.h>
int N,C;
int w[1010],v[1010];
int dp[1010][1010];
int main()
{
//freopen("1.txt","r",stdin);
int t;
scanf("%d", &t);
while(t--)
{
char a[1003],b[1003];
scanf("%s %s",a,b);
memset(dp,0,sizeof(dp));
int aa = strlen(a);
int bb = strlen(b);
for(int i = 1; i <= aa;i++)
for(int j = 1; j <= bb;j++)
{
if(a[i-1] == b[j-1])
dp[i][j] = dp[i-1][j-1]+1;
else
{
dp[i][j] = std::max(dp[i-1][j],dp[i][j-1]);
}
}
// 打印DP表
for(int i = 0; i <= aa;i++)
{
for(int j = 0; j <= bb;j++)
{
printf("%d ", dp[i][j]);
}
printf("\n");
}
printf("%d\n",dp[aa][bb]);
}
}
标签:main sizeof style while freopen 题意 include logs pre
原文地址:http://www.cnblogs.com/Jadon97/p/6810278.html