大家帮忙看看为什么不用memset了还是答案错误
查看原帖
大家帮忙看看为什么不用memset了还是答案错误
668320
fufuQAQ楼主2022/10/25 10:15
//这题只要判断出这个字符串是否存在即可 
#include <iostream>
#include <cstring>
using namespace std;
const int N = 1e5 + 10;
const int M = 3*1e6+10;

int cnt[M];
int trie[M][60], tot = 1;//初始化 
char str[M];

void insert(char* str)//插入一个字符串 
{
	int len = strlen(str), p = 1;
	for (int k = 0; k < len; k++){
		int ch = str[k] - 'a';
		if (trie[p][ch] == 0)
			trie[p][ch] = ++tot;//这个地方放得是位置 
		p = trie[p][ch];
		cnt[p]++; 
	}
}

int search(char* str)//检索字符串是否存在 
{
	int len = strlen(str), p = 1;
	for (int k = 0; k < len; k++)
	{
		p = trie[p][str[k] - 'a'];
		if (p == 0) 
			return false;
	}
	return cnt[p];
}

int main(void)
{
	int t;
	cin>>t;
	while(t--)
	{
    for (int i = 0; i <= tot; i++)
      for (int j = 0; j <= 'z'; j++)
        trie[i][j] = 0;
    for (int i = 0; i <= tot; i++)
        cnt[i] = 0;
		tot=1;
		int n, m;
		cin >> n >> m;
		for (int i = 0; i < n; i++)
		{
			cin >> str;
			insert(str);
		}
		for (int i = 0; i < m; i++)
		{
			cin >> str;
			cout << search(str) << endl;
		}
    }
	return 0;
}

前四个点是错的

2022/10/25 10:15
加载中...