10分求调
查看原帖
10分求调
946432
xudabai2009楼主2025/1/19 19:06

用字典树写的,想当板子练 只对了最后一个点

#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <algorithm>
#define gc getchar()
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N = 1e3 + 5;
ll n, m, cnt[N];
struct node
{
    int son[26];
    int num = 0;
} t[N][N];
void insert(int no, string s)
{
    int num = 0, len = s.size();
    for (int i = 0; i < len; i++)
    {
        int ch = s[i] - 'a';
        if (t[no][num].son[ch] == 0)
            t[no][num].son[ch] = ++cnt[no];
        num = t[no][num].son[ch];
        if (i == len - 1)
            t[no][num].num++;
    }
}
void query(string s)
{
    int len = s.size();
    for (int no = 1; no <= n; no++)
    {
        int num = 0;
        for (int i = 0; i < len; i++)
        {
            int ch = s[i] - 'a';
            if (t[no][num].son[ch] == 0){
                break;
            }
            num = t[no][num].son[ch];
            if (t[no][num].num && i == len-1)
            {
                cout<<no<<" ";
                break;
            }
            
        }
    }
    //if(outa) cout<<" ";
    cout<<endl;
    return ;
}
int main()
{
    ios::sync_with_stdio(0);
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        int l;
        cin >> l;
        for (int j = 1; j <= l; j++)
        {
            string s;
            cin >> s;
            insert(i, s);
        }
    }
    cin >> m;
    for (int i = 1; i <= m; i++)
    {
        string s;
        cin >> s;
        query(s);
    }
    return 0;
}

2025/1/19 19:06
加载中...