Luo Gu P2543 [AHOI2004] strange string

Topic Portal

Problem-solving ideas:

This simple question can find the longest common subsequence, but space is not enough, how to do it?

Not enough space, rolling array rescue

AC Code:

 1 #include<iostream>
 2 #include<cstdio>
 3 
 4 using namespace std;
 5 
 6 string l,l1;
 7 int f[2][10002],m;
 8 
 9 inline int max(int a,int b) {
10     if(a >= b) return a;
11     return b; 
12 }
13 
14 int main() {
15     cin >> l >> l1;
16     for(int i = 0;i < l.length(); i++) {
17         for(int j = 1;j <= l1.length(); j++) {
18             if(l[i] == l1[j-1])
19                 f[m][j] = f[m^1][j-1] + 1;
20             else 
21                 f[m][j] = max(f[m^1][j],f[m][j-1]);
22         }
23         m = m ^ 1;
24     }
25     printf("%d",f[m^1][l1.length()]);
26     return 0;
27 }

 

Guess you like

Origin www.cnblogs.com/lipeiyi520/p/12319495.html