hihoCoder week227 Longest Subsequence

时间:2024-12-21 11:07:32

题目链接 https://hihocoder.com/contest/hiho227/problem/1

题目详解 https://hihocoder.com/discuss/question/5587

#include <bits/stdc++.h>
using namespace std;
#define Max(a,b) ((a>b)?a:b)
const int N = 1e6+;
int n,ans; string s[N], S;
int slen,Slen;
int f[N/][], id[]; void init()
{
for(int i=; i<=; i++)
id[i] = Slen;
for(int i=Slen-; i>=; i--) {
for(int j=; j<; j++) {
f[i][j] = id[j];
}
int x = S[i] - 'a';
id[x] = i;
}
/*
for(int i=0; i<Slen; i++) {
for(int j=0; j<3; j++) {
printf("%d%c",f[i][j],j==2?'\n':' ');
}
}
*/
} void solve(int k)
{
int i=, j=;
while(i == && j < Slen) {
if(S[j] != s[k][i])
j++;
else
i++,j++;
}
j--;
while(i < slen && j < Slen) {
int x= s[k][i] - 'a';
j = f[j][x];
if(j >= Slen) break;
i++;
} if(i >= slen) {
//cout<< k <<" "<< slen<<endl;
ans = Max(ans, slen);
}
return ; } int main()
{
freopen("in.txt","r",stdin);
while(~scanf("%d", &n)) {
ans = ;
for(int i=; i<n; i++)
cin >> s[i];
cin >> S;
Slen = S.length();
init();
for(int i=; i<n; i++) {
slen = s[i].length();
if(slen <= ans || slen > Slen)
continue;
solve(i);
}
printf("%d\n", ans);
}
return ;
}