題意
給出一堆字串,若一個串是另一個串的字首 ,那麼它們可以連線在一起
問最大的連結長度
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; } /* */