hihocoder #1107 : Shortest Proper Prefix

时间限制: 10000ms
单点时限: 1000ms
内存限制: 512MB

描述

Query auto-completion(QAC) is widely used in many search applications. The basic idea is that when you type some string s in the search box several high-frequency queries which have s as a prefix are suggested. We say string s1 has string s2 as a prefix if and only if the first |s2| characters of s1 are the same as s2 where |s2| is the length of s2.

These days Little Hi has been working on a way to improve the QAC performance. He collected N high-frequency queries. We say a string s is a proper prefix if there are no more than 5 collected queries have s as a prefix. A string s is a shortest proper prefix if s is a proper prefix and all the prefixes of s(except for s itself) are not proper prefixes. Little Hi wants to know the number of shortest proper prefixes given N collected queries.

Hint: the 4 shortest proper prefixes for Sample Input are "ab", "bb", "bc" and "be". Empty string "" is not counted as a proper prefix even if N <= 5.

输入

The first line contains N(N <= 10000), the number of collected queries.

The following N lines each contain a query.

Each query contains only lowercase letters 'a'-'z'.

The total length of all queries are no more than 2000000.

Input may contain identical queries. Count them separately when you calculate the number of queries that have some string as a prefix.

输出

Output the number of shortest proper prefixes.

样例输入
12
a
ab
abc
abcde
abcde
abcba
bcd
bcde
bcbbd
bcac
bee
bbb
样例输出
4

开始时想用map,代码敲了一般才想到对于有重复string的情况不好处理,只好中规中矩的按照字典树的方式求解了。


trie,又称前缀树或字典树,是一种有序树,用于保存关联数组,其中的键通常是字符串。与二叉查找树不同,键不是直接保存在节点中,而是由节点在树中的位置决定。一个节点的所有子孙都有相同的前缀,也就是这个节点对应的字符串,而根节点对应空字符串。一般情况下,不是所有的节点都有对应的值,只有叶子节点和部分内部节点所对应的键才有相关的值。

trie解决词频统计和前缀匹配问题。

字典树有如下性质:

1根节点不含字符。

2.从根节点到某一节点,路径上经过的字符连接起来,就是该节点对应的字符串。

3.每个单词的公共前缀作为一个字符节点保存。


#include <iostream> 
#include <string> 
using namespace std;

const int tree_width = 27;

struct trie
{
	int count;
	struct trie *next[tree_width];
	trie()
	{
		count = 0;
		for (int i = 0; i < tree_width; i++)
			next[i] = NULL;
	}
};


void insert(string word,trie *tree)
{
	trie *tmp = tree;
	tmp->count++;
	for (int i = 0; tmp!=NULL&&word[i]; i++)
	{
		if (tmp->next[word[i] - 'a'] == NULL)
		{
			tmp->next[word[i] - 'a'] = new trie;
		}
		tmp = tmp->next[word[i] - 'a'];
		tmp->count++;
	}
}

int ans = 0;
void visit(trie *tree)
{
	trie *tmp = tree;
	if (tmp->count <= 5 && tmp->count > 0)    
	{
		ans++;
		return;
	}
	for (int i = 0; i < tree_width; i++)
	{
		if (tmp->next[i] != NULL)
			visit(tmp->next[i]);
	}
}

int main()
{
	int n;
	cin >> n;
	string s;
	trie *tree=new trie;
	for (int i = 0; i < n; i++)
	{
		cin >> s;
		insert(s,tree);
	}
	visit(tree);
	cout << ans << endl;
	system("pause");
	return 0;
}

找个模板套进去就行(づ ̄ 3 ̄)づ





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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值