微软笔试题-Shortest Proper Prefix

<pre name="code" class="cpp">#include<iostream>
#include <algorithm>
#include <queue>
#include <string>
#include <string.h>
using namespace std;
#define CHILD_NUM 2
#define MAXNODE 2000
#define MAXLINE 2000
int p = 0;
struct TrieNode
{
	TrieNode *childs[CHILD_NUM];
	int count;
};
TrieNode nodes[MAXNODE];

void insertNode(TrieNode *root, char *str)
{
	TrieNode * pivot = root;
	while (*str)
	{
		if (pivot->childs[*str - 'a'] == NULL)
		{
			pivot->childs[*str - 'a'] = &nodes[p++];
			pivot->childs[*str - 'a']->count = 0;
		}
		pivot = pivot->childs[*str - 'a'];
		pivot->count++;
		str++;
	}
}
//bool hasChild(TrieNode *r){
//	bool hasChild = false;
//	for (int i = 0; i < 26; i++)
//	{
//		if (r->childs[i] != nullptr)
//		{
//			hasChild = true;
//			break;
//		}
//	}
//	return hasChild;
//}



//void dfs(TrieNode *r,string s)
//{
//	if (!hasChild(r))
//	{
//		cout << s << endl;
//		return;
//	}
//	for (int i = 0; i < 26; i++)
//	{
//		if (r->childs[i] != nullptr)
//		{
//			s.push_back('a'+i);
//			dfs(r->childs[i], s);
//			s.pop_back();
//		}
//	}
//}
int ans = 0;
void dfs2(TrieNode *r, string s)
{
	for (int i = 0; i < 26; i++)
	{
		TrieNode * child = r->childs[i];
		if ( child != nullptr)
		{
			s.push_back('a' + i);
			if (child->count <= 5)
			{
				//cout << s << endl;
				ans++;
				s.pop_back();
				continue;
			}
			else
			{
				dfs2(r->childs[i], s);
				s.pop_back();
			}
		}
	}
}





int main(void)
{
	char ar[10];
	scanf_s("%s", ar);

	int n = 0;
	scanf_s("%d", &n); 
	char strs[100][1000] = {0};
	for (int i = 0; i < n; ++i)
	{
		scanf_s("%s", &strs[i]);
		fflush(stdin);
	}
	memset(nodes, 0, sizeof(nodes));
	//char *strs[] = {"a","ab","abc","abcde","abcde","abcba","bcd","bcde","bcbbd","bcac","bee","bbb"};
	TrieNode *r = new TrieNode();
	for (int i = 0; i < n; i++)
	{
		insertNode(r, strs[i]);
	}
	//printTree(r);
	dfs2(r, string(""));
	cout << ans;
	return 0;
}


//#include<iostream>
//using namespace std;
//const int Max = 1002;
//const int branchNum = 26;
//
//struct tree_node{
//	int count;   // 记录用到这个节点的单词数量,如果=1,则证明其为这个单词唯一的节点。
//	tree_node *next[branchNum];
//}root, node[20 * Max];
//int p = 0;
//
//void insert(char *word){
//	tree_node *location = &root;
//	while (*word){
//		if (location->next[*word - 'a'] == NULL){
//			node[p].count = 0;
//			location->next[*word - 'a'] = &node[p++];
//		}
//		location = location->next[*word - 'a'];
//		location->count++;
//		word++;
//	}
//}
//
//void search(char *word){
//	tree_node *location = &root;
//	while (*word && location){
//		if (location->count == 1) break;
//		printf("%c", *word);
//		location = location->next[*word - 'a'];
//		word++;
//	}
//	printf("\n");
//}
//
//int main(){
//	char word[Max][21];
//	int i, k = 0;
//	while (scanf_s("%s", word[k]) != EOF){
//		insert(word[k]);
//		k++;
//	}
//	for (i = 0; i < k; i++){
//		printf("%s ", word[i]);
//		search(word[i]);
//	}
//	return 0;
//}



                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值