Luogu2543[AHOI2004]奇怪的字符串 (动态规划 LCS)

Posted bingoyes

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Luogu2543[AHOI2004]奇怪的字符串 (动态规划 LCS)相关的知识,希望对你有一定的参考价值。

04年的省选这么water吗,开个滚动数组算了

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define R(a,b,c) for(register int  a = (b); a <= (c); ++ a)
#define nR(a,b,c) for(register int  a = (b); a >= (c); -- a)
#define Max(a,b) ((a) > (b) ? (a) : (b))
#define Min(a,b) ((a) < (b) ? (a) : (b))
#define Fill(a,b) memset(a, b, sizeof(a))
#define Swap(a,b) a^=b^=a^=b
#define ll long long
#define ON_DEBUG

#ifdef ON_DEBUG

#define D_e_Line printf("\\n\\n----------\\n\\n")
#define D_e(x)  cout << #x << " = " << x << endl
#define Pause() system("pause")

#else

#define D_e_Line ;

#endif

struct ios
    template<typename ATP>ios& operator >> (ATP &x)
        x = 0; int f = 1; char c;
        for(c = getchar(); c < '0' || c > '9'; c = getchar()) if(c == '-')  f = -1;
        while(c >= '0' && c <= '9') x = x * 10 + (c ^ '0'), c = getchar();
        x*= f;
        return *this;
    
io;
using namespace std;

const int N = 10007;

char a[N], b[N];
int f[2][N];

int main()
    
    scanf("%s%s", a, b);
    int lenA = strlen(a), lenB = strlen(b);
    
    R(i,1,lenA)
        R(j,1,lenB)
            if(a[i-1] == b[j-1])
                f[i&1][j] = f[(i-1)&1][j-1] + 1;
            else
                f[i&1][j] = Max(f[(i-1)&1][j], f[i&1][j-1]);
        
        
    printf("%d", f[lenA&1][lenB]);
    return 0;

技术图片

以上是关于Luogu2543[AHOI2004]奇怪的字符串 (动态规划 LCS)的主要内容,如果未能解决你的问题,请参考以下文章

P2548 [AHOI2004]智能探险车

[Luogu 2023] AHOI2009 维护序列

luogu P4248 [AHOI2013]差异

luogu2051 [AHOI2009]中国象棋

[luogu P2054] [AHOI2005]洗牌

[luogu2054 AHOI2005] 洗牌 (数论)