样例过 WA on #1-5,求条
查看原帖
样例过 WA on #1-5,求条
1235038
Ekin123楼主2025/2/2 22:11
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;

struct Trie
{
	int tr[3000005][60],idx,tag[3000005];
	void insert(string s)
	{
		int len = s.size();
		int pos = 0;
		for(int i = 0;i < len;i++)
		{
			int v = s[i] - '0';
			if(!tr[pos][v]) tr[pos][v] = ++idx;
			pos = tr[pos][v];
			tag[pos]++;
		}
	} 
	int query(string s)
	{
		int len = s.size();
		int pos = 0;
		for(int i = 0;i < len;i++)
		{
			int v = s[i] - '0';
			if(!tr[pos][v]) return 0;
			pos = tr[pos][v];
		}
		return tag[pos];
	}
	void clear()
	{
		for(int i = 0;i <= idx;i++)
            for(int j = 0;j <= 50;j++)
                tr[i][j] = 0;
        for(int i = 0;i <= idx;i++)
            tag[i] = 0;
        idx = 0;
	}
};

Trie t;

int main()
{
	int T;
	cin >> T;
	while(T--)
	{
		t.clear();
		int n,q;
		cin >> n >> q;
		while(n--)
		{
			string s;
			cin >> s;
			t.insert(s);
		}
		while(q--)
		{
			string s;
			cin >> s;
			cout << t.query(s) << endl;
		}
	}
	return 0;
} 
2025/2/2 22:11
加载中...