RE求调(能不能优化?)

P1439 【模板】最长公共子序列

RJSPZ @ 2022-11-29 13:45:53

N=1e5+7过不了

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e4+7;
int f[N][N];
inline int read()
{
    int x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9')
    {
        if(ch=='-')
            f=-1;
        ch=getchar();
    }
    while(ch>='0' && ch<='9')
        x=x*10+ch-'0',ch=getchar();
    return x*f;
}
inline void write(int x)
{
    if(x<0)
        putchar('-'),x=-x;
    if(x>9)
        write(x/10);
    putchar(x%10+'0');
    return;
}
int a[N],b[N];
int n;
int main(){
    n=read();
    for(int i=1;i<=n;i++){
        a[i]=read();
    }
    for(int i=1;i<=n;i++){
        b[i]=read();
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            f[i][j]=max(f[i-1][j],f[i][j-1]);
            if(a[i]==b[j]){
                f[i][j]=max(f[i][j],f[i-1][j-1]+1);
            }
        }
    } 
    write(f[n][n]);
    return 0;
}

by scp020 @ 2022-11-29 14:00:14


|