求助大佬指针哪里写错了?
查看原帖
求助大佬指针哪里写错了?
922691
Hisy楼主2024/1/26 12:46
#include<iostream>
#include<string>
using namespace std;
struct trie{
	bool word;
	trie *next[10];
};
trie *head=new trie;
inline void insert(string str){
	if(str.empty()){
		return;
	};
	trie *now=head;
	for(int iter=0;iter<str.size();++iter){
		if(now->next[str[iter]-'0']==NULL){
			now->next[str[iter-'0']]=new trie;
			for(int fex=0;fex<10;++fex){
				now->next[str[iter]-'0']->next[fex]=NULL;
				now->next[str[iter]-'0']->word=false;
			};
		};
		now=now->next[str[iter]-'0'];
	};
	now->word-true;
};
inline bool find(string str){
	if(str.empty()){
		return true;
	};
	trie *now=head;
	for(int iter=0;iter<str.size();++iter){
		if(now->next[str[iter]-'0']==NULL){
			return false; 
		}else{
			if(now->next[str[iter]-'0']->word){
				return true;
			};
			now=now->next[str[iter]-'0'];
		};
	};
	return true;
};
inline void remove(trie *dict){
	for(int iter=0;iter<10;++iter){
		if(dict->next[iter]!=NULL){
			remove(dict->next[iter]);
		};
	};
	if(dict!=head){
		delete dict;
	};
};
inline void remake(void){
	for(int iter=0;iter<10;++iter){
		head->next[iter]=NULL;
	};
	head->word=false;
};
inline void work(void){
	bool flag=false;
	remake();
	int maxn;
	cin>>maxn;
	while(maxn--){
		string str;
		cin>>str;
		if(!flag){
			flag=find(str);
		};
		if(flag){
			remove(head);
			cout<<"YES"<<endl;
			return; 
		};
		insert(str);
	};
	remove(head);
	cout<<"NO"<<endl;
	return;
};
int main(int argc,char **argv){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int tot;
	cin>>tot;
	while(tot--){
		work();
	};
	return 0;
};

题解里面也有,正好正常的 TrieTrie 实现不太了,于是就用了指针。

然后,悲哀的音乐响起。

我还没有提交,但是第一个样例就报了 RERE。

求助大佬!

2024/1/26 12:46
加载中...