Question

求两个字符串的最长非公共子序列

Solution

分类讨论:

  1. 长度为0,输出-1。

Code

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int>P;
const double eps = 1e-8;
const int NINF = 0xc0c0c0c0;
const int INF  = 0x3f3f3f3f;
const ll  mod  = 1e9 + 7;
const ll  maxn = 1e6 + 5;
const int N = 5e3 + 5;

char a[N],b[N];

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin>>a+1>>b+1;
    int lena=strlen(a+1),lenb=strlen(b+1);
    if(lena>lenb) cout<<lena<<'\n';
    else if(lena<lenb) cout<<lenb<<'\n';
    else if(lena==lenb){
        bool ok=false;
        for(int i=1;i<=lena;i++){
            if(a[i]==b[i]) continue;
            ok=true;
        }
        if(!ok) cout<<-1<<'\n';
        else cout<<lena<<'\n';
    }
    return 0;
}