字典树

https://vjudge.net/problem/UVALive-3942/origin

const int maxn = 2e6 + 7;
const int mod = 20071027;

int n, t, m;

int dp[maxn], nex[maxn][26], cnt, vis[maxn];
char s[maxn];

void insert(char *s) {
    int l = strlen(s), p = 0;
    for (int i = 0, c; i < l; i++) {
        c = s[i] - 'a';
        if (!nex[p][c]) nex[p][c] = ++cnt;
        p = nex[p][c];
    }
    vis[p]++;
}

ll find(char *s, int pl) {
    if (pl == n)
        return 1;
    if (dp[pl] != -1)
        return dp[pl];
    int p = 0, res = 0;
    for (int i = pl; i < n; i++) {
        int c = s[i] - 'a';
        if (!nex[p][c]) break;
        p = nex[p][c];
        if (vis[p]) {
            res += find(s, i + 1);
            res %= mod;
        }
    }
    dp[pl] = res % mod;
    return res % mod;
}

void solve() {
    int id = 1;
    while (scanf("%s", s) != EOF) {
        memset(dp, -1, sizeof dp);
        memset(nex, 0, sizeof nex);
        memset(vis, 0, sizeof vis);
        n = strlen(s);
        scanf("%d", &t);
        char ss[101];
        for (int i = 1; i <= t; i++) {
//            cin >> ss;
            scanf("%s", ss);
            insert(ss);
        }
        cout << "Case " << id++ << ": " << find(s, 0) << endl;
    }
}
我看见 你