求。条。
查看原帖
求。条。
800884
Weekoder楼主2024/9/24 15:44

只有。10分。

#include <bits/stdc++.h>

#define int long long

#define debug(x) (cout << #x << " " << x << "\n")

using namespace std;

const int N = 1e6 + 5, M = 30;

int n, m, trie[N][M], tot, cnt;

bool exist[N], flag, vis[N];

void insert(string s) {
    int cur = 0;
    for (int i = 0; i < s.size(); i++) {
        int c = s[i] - 'a';
        if (!trie[cur][c])
            trie[cur][c] = ++tot;
        cur = trie[cur][c];
    }
    exist[cur] = 1;
}

void dfs(string s, int cur, int i, bool f) {
    if (i == s.size() && exist[cur]) {
        if (!f) flag = 1;
        else {
            if (!vis[cur]) vis[cur] = 1, cnt++;
        }
        return ;
    }
    int c = s[i] - 'a';
    if (!f) {
        if (i < s.size()) dfs(s, cur, i + 1, 1);
        for (int i = 0; i < 26; i++) if (trie[cur][i]) {
            dfs(s, trie[cur][i], i, 1);
            if (i != c) dfs(s, trie[cur][i], i + 1, 1);
        }
    }
    if (i >= s.size()) return ;
    if (trie[cur][c]) dfs(s, trie[cur][c], i + 1, f);
}

signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0); 
	cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        string s;
        cin >> s;
        insert(s);
    }
    for (int i = 1; i <= m; i++) {
        flag = cnt = 0;
        memset(vis, 0, sizeof vis);
        string s;
        cin >> s;
        dfs(s, 0, 0, 0);
        if (flag) cout << "-1\n";
        else cout << cnt << "\n";
    }
	return 0;
}
2024/9/24 15:44
加载中...