voidInsert(constchar *s, int length){ // 插入 int pos = 0; // pos = root maxlength = std::max(maxlength, length); for (int i = 0; i < length; ++i) { int ins = s[i] - 'a'; if (tree[pos].linkson[ins] == 0) { // insert tree[pos].linkson[ins] = ++cnt; ++tree[pos].size; } pos = tree[pos].linkson[ins]; } tree[pos].last = true; } shortFind(constchar *s, int l, int r){ int pos = 0; // pos = root for (int i = l; i <= r; ++i) { int que = s[i] - 'a'; if (tree[pos].linkson[que] == 0) return0; pos = tree[pos].linkson[que]; // 这里也是和上边插入一模一样 } return tree[pos].last; } } T;
int n, m; bool dp[MAXCHAR];
intmain(){ IMPROVE_IO(); cin >> n >> m; std::string s; for (int i = 1; i <= n; ++i) { cin >> s; T.Insert(s.c_str(), (int) s.size()); } for (int i = 1; i <= m; ++i) { cin >> s; memset(dp, 0, sizeof dp); int ans = 0; int len = (int) s.size(); for (int r = 0; r < len; ++r) { // 枚举右端点 for (int l = std::max(r - maxlength, -1); l <= r; ++l) { // 枚举左端点 if ((l == -1 || dp[l]) && T.Find(s.c_str(), l + 1, r)) { dp[r] = true; ans = r + 1; break; } } } cout << ans << endl; } return0; }