C 最长非公共子序列 题解
最长非公共子序列
http://www.nowcoder.com/questionTerminal/de449fda714f43d6a7fbc74aa0d3d1cd
给你两个字符串 和
求出满足是其中一个串的子序列但不是另一个串的子序列的串的最长长度。
首先如果那么答案显然为
因为
和
相等,所以
的子串都是
的子串,反之亦然。
否则,答案一定是 因为
所以把
和
中较长的串拿出来
它一定不是另一个串的子串
复杂度
#include <bits/stdc++.h> using namespace std; string S,T; int main(){ cin >> S >> T; if (S == T){ cout << -1 << '\n'; return 0; } cout << max(S.size(),T.size()) << '\n'; return 0; }