题意:找LCS
思路:裸题 n*m的写法,我的写法好像比较奇怪。。。用一个ci保存s2第i位可以做为s1的公共子序列的最大值,s1的每一位遍历s2,遍历的时候记录前面出现过的ci的最大值,ci一定是一个连序的上升序列,我的好像不是正经的LCS的算法,改天还是要学习一个的
AC代码:
#include "iostream" #include "string.h" #include "stack" #include "queue" #include "string" #include "vector" #include "set" #include "map" #include "algorithm" #include "stdio.h" #include "math.h" #define ll long long #define bug(x) cout<<x<<" "<<"UUUUU"<<endl; #define mem(a) memset(a,0,sizeof(a)) #define mp(x,y) make_pair(x,y) #define pb(x) push_back(x) using namespace std; const long long INF = 1e18+1LL; const int inf = 1e9+1e8; const int N=1e5+100; const ll mod=1e9+7; char s1[1005],s2[1005]; int dp[1005],c[1005],ans; int main(){ ios::sync_with_stdio(false),cin.tie(0),cout.tie(0); while(cin>>s1+1>>s2+1){ ans=0,mem(dp),mem(c); int l1=strlen(s1+1), l2=strlen(s2+1); for(int i=1; i<=l1; ++i){ int p=0; for(int j=1; j<=l2; ++j){ int t=c[j]; if(s1[i]==s2[j]){ dp[i]=max(dp[i],p+1); c[j]=max(c[j],dp[i]); } if(t==p+1) p++; } ans=max(ans,dp[i]); //for(int j=1; j<=l2; ++j) cout<<c[j]<<" ";cout<<endl; } cout<<ans<<endl; } return 0; }
时间: 2024-12-09 22:48:20