PAT程序设计考题——甲级1045 (最长公共子序列) C++实现

来源:互联网 发布:ubuntu mate 树莓派 编辑:程序博客网 时间:2024/05/29 15:13

to PAT

#include<iostream>
#include<algorithm>
#include<map>
#include<string>
#include<vector>
const int maxn=10010;
const int maxc=210;
using namespace std;
int main(){int color;
scanf("%d",&color);
 int k;
 int n;
 int aorder[10010],border[10010];
  scanf("%d",&n);
 for(int i=1;i<=n;i++){//please think the question why we store the vector from 1
 scanf("%d",&border[i]);
}
 scanf("%d",&k);
 for(int i=1;i<=k;i++)
 scanf("%d",&aorder[i]);
vector<int> temp(n+1);
vector<vector<int> > lcs(k+1,temp);
for(int i=0;i<=k;i++)
lcs[i][0]=0;
for(int i=0;i<=n;i++)
lcs[0][i]=0;
for(int i=1;i<=k;i++)
 for(int j=1;j<=n;j++)
 {    int max1=max(lcs[i-1][j],lcs[i][j-1]);
  if(aorder[i]==border[j])
  {lcs[i][j]=max1+1;
  }
  else{
   lcs[i][j]=max1;
  }
 }
 cout<<lcs[k][n];
 return 0;
}

阅读全文
0 0
原创粉丝点击