-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheasydp.cpp
43 lines (43 loc) · 1.13 KB
/
easydp.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include <algorithm>
#include <stack>
#include <queue>
#include <cstdio>
#include <cstring>
#include <map>
using namespace std;
const int maxn=1005;
int dp[maxn][maxn];
int main()
{
char s1[maxn],s2[maxn];
while(scanf("%s%s",s1,s2)!=EOF)
{
memset(dp,0,sizeof(dp));
int l1=strlen(s1);
int l2=strlen(s2);
for(int i=0;i<l1;i++)
{
for(int j=0;j<l2;j++)
{
if(s1[i]==s2[j])
{
if(i>0&&j>0)
dp[i][j]=dp[i-1][j-1]+1;
else dp[i][j]=1;
}
else
{
if(i==0&&j==0)
dp[i][j]=0;
else if(i>0&&j==0)
dp[i][j]=dp[i-1][j];
else if(i==0&&j>0)
dp[i][j]=dp[i][j-1];
else dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
}
}
}
cout<<dp[l1-1][l2-1]<<endl;
}
}