hihoCoder week227 Longest Subsequence

题目链接 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+10;
int n,ans; string s[N], S;
int slen,Slen;
int f[N/10][30], id[30];

void init() 
{
    for(int i=0; i<=26; i++) 
        id[i] = Slen;
    for(int i=Slen-1; i>=0; i--) {
        for(int j=0; j<26; 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=0, j=0;
    while(i == 0 && 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 = 0;
        for(int i=0; i<n; i++)
            cin >> s[i];
        cin >> S;
        Slen = S.length();
        init();
        for(int i=0; i<n; i++) {
            slen = s[i].length();
            if(slen <= ans || slen > Slen) 
                continue;
            solve(i);
        }
        printf("%d\n", ans);
    }
    return 0;
}

 

posted @ 2018-11-06 10:59  Draymonder  阅读(158)  评论(0编辑  收藏  举报