文章

60

粉丝

361

获赞

41

访问

498.0k

头像
打卡
P1098 中山大学2018年机试
发布于2021年1月20日 16:40
阅读数 9.2k

教程代码

//1161
//通不过的代码,主要是creat_tree部分,写法不一样
#include<iostream>
#include<string>
#include<cstring>
#include<stack>
#include<vector>
#include <queue>
#include <functional>
using namespace std;
const int maxn=26;
typedef struct TrieNode{
	int nCount;
	struct TrieNode *next[maxn];
}Trie;
Trie root;
void inital()
{
	for(int i=0;i<maxn;i++)
		root.next [i]=NULL;
}

void creat_tree(string str)
{
	int len=str.size();
	Trie *p=&root,*q;
	for(int i=0;i<len;i++)
	{
		int k=str[i]-'a';
		if(p->next[k]==NULL)
		{
			q=(Trie*)malloc(sizeof(root));
			q->nCount =1;
			for(int j=0;j<maxn;j++)
				q->next[j]=NULL;
			p->next[k] =q;
			p=p->next[k];		
		}
		else
		{
			p->next[k]->nCount ++;
			p=p->next [k];
		}
	}
}

int sum;
//计算叶子结点个数
void couttree()
{
	queue <Trie*>q;
	q.push(&root);
	while(!q.empty())
	{
		Trie *p=q.front();
		q.pop();
		int son=0;...
登录查看完整内容


登录后发布评论

1 条评论
可为
2022年2月23日 21:24

大神代码牛皮!

赞(0)