-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1022.cpp
44 lines (44 loc) · 1.35 KB
/
1022.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
44
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
int m;
cin >> m;
for (int i = 0;i < m;i++)
{
char first[1000] = {};
char second[1000] = {};
int dp[1000][1000] = {};
cin >> first >> second;
int lfirst = strlen(first);
int lsecond = strlen(second);
int ptrf = 0, ptrs = 0;
for (ptrf = 0;ptrf < lfirst;ptrf++)
{
for (ptrs = 0;ptrs < lsecond;ptrs++)
{
if (first[ptrf] == second[ptrs])
{
if (ptrf != 0 && ptrs != 0)
dp[ptrf][ptrs] = dp[ptrf - 1][ptrs - 1] + 1;
else
dp[ptrf][ptrs] = 1;
}
else
{
if (ptrf == 0 && ptrs == 0)
dp[ptrf][ptrs] = 0;
else if (ptrf == 0)
dp[ptrf][ptrs] = dp[ptrf][ptrs - 1];
else if (ptrs == 0)
dp[ptrf][ptrs] = dp[ptrf - 1][ptrs];
else
dp[ptrf][ptrs] = max(dp[ptrf - 1][ptrs], dp[ptrf][ptrs - 1]);
}
//cout << ptrf << " " << ptrs << " " << dp[ptrf][ptrs] << endl;
}
}
cout << dp[lfirst - 1][lsecond - 1] << endl;
}
}