【题目链接】
https://www.lydsy.com/JudgeOnline/problem.php?id=1212
【算法】
字典树 + dp
【代码】
#include<bits/stdc++.h> using namespace std; #define MAXL 10000010 #pragma GOC optimize("O2") int i,j,n,m,len,ans,mx; char s[MAXL]; bool ok[MAXL]; struct Trie { int tot; struct Node { int child[26]; bool is_last; } a[310]; inline void insert(char *s) { int i,len,x = 0; len = strlen(s); for (i = 0; i < strlen(s); i++) { if (!a[x].child[s[i]-'a']) a[x].child[s[i]-'a'] = ++tot; x = a[x].child[s[i]-'a']; } a[x].is_last = true; } inline bool check(int l,int r) { int i,x = 0; for (i = l; i <= r; i++) { if (a[x].child[s[i]-'a']) x = a[x].child[s[i]-'a']; else return false; } return a[x].is_last; } } T; int main() { scanf("%d%d",&n,&m); for (i = 1; i <= n; i++) { scanf("%s",&s); mx = max(mx,(int)strlen(s)); T.insert(s); } while (m--) { ans = 0; memset(ok,false,sizeof(ok)); scanf("%s",&s); len = strlen(s); for (i = 0; i < len; i++) { for (j = max(i-mx,-1); j < i; j++) { if ((j == -1 || ok[j]) && T.check(j+1,i)) { ok[i] = true; ans = i + 1; break; } } } printf("%d ",ans); } return 0; }