还没开始就结束了?
  • 板块学术版
  • 楼主Rainber
  • 当前回复5
  • 已保存回复5
  • 发布时间2024/9/25 20:31
  • 上次更新2024/9/25 22:30:45
查看原帖
还没开始就结束了?
726870
Rainber楼主2024/9/25 20:31

字典树,动态开点。

然后以下代码:

#include<stdio.h>
#include<malloc.h>
#define NODESIZE 212
#define ALPHABETS 52
struct node//size:4*52+4=212
{
	void *next[ALPHABETS];
	int count;
} *root;
int translate(char r)
{
	if(r>='a'&&r<='z')
	{
		return r-'a';
	}
	return r-'A'+26;
}
void free_tree(struct node *root)
{
	if(root!=NULL)
	{
		int i;
		for(i=0;i<ALPHABETS;++i)
		{
			free_tree(root->next[i]);
		}
		free(root);
	}
}
struct node *new_node()
{
	int i;
	struct node *p=malloc(NODESIZE);
	for(i=0;i<ALPHABETS;++i)
	{
		p->next[i]=NULL;
	}
	p->count=0;
	return p;
}
void build(struct node *root,char *s)
{
	struct node *p=root;
	while(*s!='\0')
	{
		p->count++;
		if(p->next[translate(*s)]==NULL)
		{
			p->next[translate(*s)]=new_node();
		}
		p=p->next[translate(*s)];
		s++;
	}
	p->count++;
}
int count(struct node *root,char *s)
{
	struct node *p=root;
	while(*s!='\0')
	{
		if(p->next[translate(*s)]==NULL)
		{
			return 0;
		}
		p=p->next[translate(*s)];
		s++;
	}
	return p->count;
}
char s[3000001];
void fun()
{
	free_tree(root);
	root=new_node();
	printf("%d %d",root,root->count);
	int n,q,i;
	scanf("%d%d",&n,&q);
	for(i=0;i<n;++i)
	{
		scanf("%s",s);
		build(root,s);
	}
	for(i=0;i<q;++i)
	{
		scanf("%s",s);
		printf("%d\n",count(root,s));
	}
}
int main()
{
	fun();
}

在还没输入任何东西的情况下,返回了3221226356。

求助,悬2关。

2024/9/25 20:31
加载中...