题意
给出一堆字符串,若一个串是另一个串的前缀 ,那么它们可以连接在一起
问最大的链接长度
Sol
LIS沙比提其实是做完了才看出是LIS
#include<cstdio> #include<algorithm> #include<cstring> #include<iostream> #define LL long long // #define int long long using namespace std; const int MAXN = 2001, INF = 1e9 + 7, mod = 998244353; inline int read() { char c = getchar(); int x = 0, f = 1; while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();} while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar(); return x * f; } int N; string s[MAXN]; int f[MAXN]; bool suf(string a, string b) { int cur = 0; for(int i = 0; i < a.length(); i++) { if(a[i] != b[cur]) return 0; cur++; } return 1; } main() { cin >> N; for(int i = 1; i <= N; i++) cin >> s[i]; int ans = 0; for(int i = 1; i <= N; i++) { f[i] = 1; for(int j = 1; j < i; j++) { if(suf(s[j], s[i])) f[i] = max(f[i], f[j] + 1); } ans = max(ans, f[i]); } printf("%d", ans); return 0; } /* */