求助
  • 板块灌水区
  • 楼主Kali_linux
  • 当前回复0
  • 已保存回复0
  • 发布时间2023/4/28 22:55
  • 上次更新2023/10/23 17:19:00
查看原帖
求助
393199
Kali_linux楼主2023/4/28 22:55
#include<bits/stdc++.h>
struct dot;
dot* new_dot;
struct dot
{
	int num;
	dot *son[10], *father;
	bool flag;
	dot(){}
	dot(int num, dot *father)
	{
		this -> num = num;
		this -> father = father;
		for(int i = 0; i < 10; i++)
			son[i] = NULL;
	}
	dot* getdot(int n)
	{
		if(son[n] == NULL)
		{
			new_dot = new dot(n, this);
			son[n] = new_dot;
		}
		return son[n];
	}
};
struct tire_tree
{
	dot root;
	tire_tree()
	{
		root.father = &root;
		root.flag = false;
		root.num = -1;
		for(int i = 0; i < 10; i++)
			root.son[i] = NULL;
	}
	void inst(int word)
	{
		int t = word, l = 1;
		while(t /= 10) l++;
		int *wordlist = new int[l];
		for(int i = l - 1; i > -1; i-- && (word /= 10))
			wordlist[i] = word % 10;
		dot *cur = &root;
		for(int i = 0; i < l; i++)
			cur = cur -> getdot(wordlist[i]);
		cur -> flag = true;
	}
	bool search(int word)
	{
		int t = word, l = 1;
		while(t /= 10) l++;
		int *wordlist = new int[l];
		for(int i = l - 1; i > -1; i-- && (word /= 10))
			wordlist[i] = word % 10;
		dot *cur = &root;
		for(int i = 0; i < l; i++)
			if(cur -> son[wordlist[i]] != NULL)
				cur = cur -> getdot(wordlist[i]);
			else
				return false;
		if(cur -> flag)
			return true;
		return false;
	}
	bool startwith(int word)
	{
		int t = word, l = 1;
		while(t /= 10) l++;
		int *wordlist = new int[l];
		for(int i = l - 1; i > -1; i-- && (word /= 10))	wordlist[i] = word % 10;
		dot *cur = &root;
		for(int i = 0; i < l; i++)
			if(cur -> son[wordlist[i]] != NULL)
				cur = cur -> getdot(wordlist[i]);
			else
				return false;
		cur = cur -> father;
		while(cur -> father != cur)
		{
			if(cur -> flag)
				return true;
		    cur = cur -> father;
		}
        return false;
	}
};
int main(void)
{
	int T;
	scanf("%d", &T);
	while(T--)
	{
		int n;
		scanf("%d", &n);
		int *nums = new int[n];
		tire_tree tree;
		for(int i = 0; i < n; i++)
		{
			scanf("%d", nums + i);
			tree.inst(nums[i]);
		}
		bool t = false;
		for(int i = 0; i < n; i++)
			if(tree.startwith(nums[i]))
			{
				t = true;
				printf("NO\n");
				break;
			}
		if(!t)
			printf("YES\n");
	}
}

题目 内存超限,运行时错误

2023/4/28 22:55
加载中...