指针做法52分 WA求调
查看原帖
指针做法52分 WA求调
507348
__vector__楼主2022/6/15 21:33

最近上了洛谷网校的数据结构课,然后老师用的指针做法做这道题,然而我不会指针。。。。。临时去学了下指针来做这道题,WA 了。
由于本蒟蒻不熟悉指针,不知道是不是哪个地方指针写挂了,来问一下

#include <bits/stdc++.h>
using namespace std;
namespace Main
{
	const int maxn=3e6+5;
	char str[maxn];
	struct Node
	{
		unordered_map<char,Node*> ch;
//		Node() : cnt(0){};
		int cnt;
	};
	Node* trie;
	int T,n,q;
	void main()
	{
		scanf("%d",&T);
		while(T--)
		{
			trie=new Node();
			scanf("%d%d",&n,&q);
			for(int i=1;i<=n;i++)
			{
				Node* nd=trie;
				scanf("%s",str);
				int len=strlen(str);
				for(int j=0;j<len;j++)
				{
					char c=str[j];
					nd=(nd->ch[c] ? nd->ch[c] : nd->ch[c]=new Node);
					nd->cnt++;
				}
			}
			for(int i=1;i<=q;i++)
			{
				scanf("%s",str);
				int ans=0;
				Node* nd=trie;
				int len=strlen(str);
				bool flag=0;
				for(int j=0;j<len;j++)
				{
					char c=str[j];
					if(!nd->ch[c])
					{
						flag=1;
						break;
					}
					nd=(nd->ch[c]);
				}
				if(flag)
				{
					printf("0\n");
				}
				else printf("%d\n",nd->cnt);
			}
		}
	}
}
int main()
{
	Main::main();
	return 0;
}
2022/6/15 21:33
加载中...