poj 2945 Find the Clones trie树的简单应用

题意:

给n个长m的字符串,统计他们的出现频率,输出出现1次的有几种,出现2次的有几种...出现n次的有几种。n<=20000,m<=20。

分析:

也可以用排序,map水的,但还是写个trie树也不麻烦,trie树我觉得就是针对字符串的hash表,效率如果数据大点是比暴力解法高很多的,另外写的时候不小心把index定义成char,n<256完全没问题。。调了一个小时也是醉了。

代码:

//poj 2945
//sep9
#include <iostream>
using namespace std;
int ans[20048];
int v[200],index;
char s[32];
struct TRIE
{
	int s[6];
	int cnt;
}a[400010];

void insert(char ss[])
{
	int i=0,h=0;
	while(ss[i]!='\0'){
		if(!a[h].s[v[ss[i]]])
			a[h].s[v[ss[i]]]=++index;
		h=a[h].s[v[ss[i]]];
		++i;
	}
	++a[h].cnt;
}

int main()
{
	int n,m;
	v['A']=0;v['G']=1;v['T']=2;v['C']=3;
	while(scanf("%d%d",&n,&m)==2){
		if(n==0&&m==0)
			break;
		memset(a,0,sizeof(a));
		index=0;
		for(int i=0;i<n;++i){
			scanf("%s",s);
			insert(s);
		}
		memset(ans,0,sizeof(ans));
		for(int i=0;i<=index;++i)
			if(a[i].cnt)
				++ans[a[i].cnt];
		for(int i=1;i<=n;++i)
			printf("%d\n",ans[i]);
	}
	return 0;
} 

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2025-01-14 07:52:23

poj 2945 Find the Clones trie树的简单应用的相关文章

POJ 2945 Find the Clones (Trie树)

Find the Clones Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 7140   Accepted: 2655 Description Doubleville, a small town in Texas, was attacked by the aliens. They have abducted some of the residents and taken them to the a spaceship

POJ 3630 Phone List(trie树的简单应用)

题目链接:http://poj.org/problem?id=3630 题意:给你多个字符串,如果其中任意两个字符串满足一个是另一个的前缀,那么输出NO,否则输出YES 思路:简单的trie树应用,插入的过程中维护到当前节点是不是字符串这个布尔量即可,同时判断是否存在上述情况. code: 1 #include <iostream> 2 #include <cstdio> 3 #include <string> 4 #include <cstring> 5

poj 2503 Babelfish (map,trie 树)

链接:poj 2503 题意:输入 语言A及翻译为语言B的词典,之后再输入语言B的单词,判断是否能从词典中找到, 若能找到,将其翻译为语言A,否则输出"eh". 思路:这题肯定得先将词典对应语言存起来,但是如果直接暴力找输入的单词是否出现过,必然会TLE 因为单词都是一对一的关系,可以用map实现 当然,trie树是用空间换时间,对于字符串的查找,在时间上有着相当的优势,因此也可以用trie树 注:sscanf函数,从一个字符串中读进与指定格式相符的数据. map实现:938MS #i

poj 2418 Hardwood Species (trie 树)

链接:poj 2418 题意:给定一些树的种类名,求每种树所占的百分比,并按字典序输出 分析:实质就是统计每种树的数量n,和所有树的数量m, 百分比就为 n*100./m 由于数据达到一百万,直接用数组查找肯定超时, 可以用trie树,空间换取时间 注:这题树的品种名除了包括大写字母,小写字母和空格外,还有其他字符, 所以要注意trie树的子结点的个数 #include<cstdio> #include<cstdlib> #include<cstring> #inclu

POJ2945 Find the Clones trie树

建一颗$trie$树(当然你哈希也资瓷),边插边更新,看看搜到最底时有多少个字符串,然后更新. #include<cstdio> #include<iostream> #include<algorithm> #include<cstring> #include<cmath> #include<cctype> #include<cstdlib> #include<vector> #include<queue

Trie树的简单描述(需后续总结)

http://www.cnblogs.com/pony1993/archive/2012/07/18/2596730.html 字典树(Trie树) 字典树,又称单词查找树,Trie树,是一种树形结构,典型应用是用于统计,排序和保存大量的字符串,所以经常被搜索引擎系统用于文本词频统计.它的优点是:利用字符串的公共前缀来节约存储空间,最大限度的减少无谓的字符串比较,查询效率比哈希表高. 它有三个基本性质,根节点不包含字符,除根节点外每一个节点都只包含一个字符,从根节点到某一节点,路径上经过的字符连

poj 2945 Find the Clones (map+string,hash思维)

Find the Clones Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 7498   Accepted: 2780 Description Doubleville, a small town in Texas, was attacked by the aliens. They have abducted some of the residents and taken them to the a spaceship

POJ 2945 Find the Clones Hash

题目大意:给出一些字符串,问其中n个一样的有多少. 思路:看discuss里各种神奇的方法啊,什么map啊,什么Trie啊.这题不是一眼Hash么..难道是我想错了? 任意hash方法将所有字符串hash然后排序,之后统计一下相同的有多少就行了,500+MS水过.. PS:明天就是NOIP我这么水真的好( CODE: #include <cstdio> #include <cstring> #include <iostream> #include <algorit

POJ 2945 Find the Clones 水

Find the Clones Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 7524   Accepted: 2789 Description Doubleville, a small town in Texas, was attacked by the aliens. They have abducted some of the residents and taken them to the a spaceship