题意:给你n个规定长度的单词,问你其中出现了1次的单词,出现两次的单词...出现n次单词分别有多少个。

当然这题map也能过,但是这里介绍字典树的做法。

首相对于n个单词存入树中,当然建树过程中遇到一样的单词就把那个单词最后一个结点的计数++就行。否则为这个单词是第一次建立,计数为1。

count[i]数组用来存放出现i次的字符串出现的次数。

Travel函数用来递归统计每个单词,将不同出现次数的数字记录到arr数组中,最后打印count数组即可

 #include <iostream>
#include <string>
#include <cstring>
#include <fstream>
#include <map>
#include <algorithm>
#include <sstream>
#include <cstdio>
using namespace std; const int LetterCount = ;//最大长度 struct Node
{
Node* next[LetterCount];//结点的下一个指针
int count;
bool end;//标记是否为最后一个单词
Node();
}; Node::Node()
{
for (int i = ; i < LetterCount; i++)
{
next[i] = NULL;
}
count = ;
end = false;
} class Trie
{
protected:
Node* root;
void GreateRoot();
void Distroy(const Node* root);
void Travel(Node* root, int* arr);
public:
Trie();
~Trie();
int Insert(char* word);
int Query(char* word);
void Travel(int* arr, int n);
}; Trie::Trie()
{
root = NULL;
GreateRoot();
} Trie::~Trie()
{
Distroy(root);
} void Trie::GreateRoot()
{
if (!root)//根结点为false
{
root = new Node();//建根
for (int i = ; i < LetterCount; i++)
{
root->next[i] = NULL;
}
}
} void Trie::Distroy(const Node* root)
{
if (!root)
{
return;
}
for (int i = ; i < LetterCount; i++)
{
if (root->next[i] != NULL)
{
Distroy(root->next[i]);
}
}
delete[] root;
} int Trie::Insert(char* word)
{
Node* p = root;//根结点为root
int length = strlen(word);//计算长度
for (int i = ; i < length; i++)
{
int index = word[i] - 'A';
if (!(p->next[index]))//当前没有那个字母
{
Node* q = new Node();
q->end = false;
for (int j = ; j < LetterCount; j++)
{
q->next[j] = NULL;
}
p->next[index] = q;
}
p = p->next[index];
}
if (p->end)
{
p->count++;
}
else
{
p->end = true;
p->count = ;
}
return p->count;
} int Trie::Query(char* word)
{
Node* p = root;
bool found = true;
int length = strlen(word);
for (int i = ; i < length; i++)
{
int index = word[i] - 'A';
p = p->next[index];
if (!p)//p为false
{
found = false;//没找到
break;
}
}
if (!found || !p->end)//没找到或已经是结束标记
{
return ;
}
return p->count;//否则返回计数
} void Trie::Travel(Node* root, int* arr)
{
if (!root)
{
return;
}
if (root->end)//表示为最后一个词
{
arr[root->count]++;
return;
}
for (int i = ; i < LetterCount; i++)
{
Travel(root->next[i], arr);//递归计算
}
} void Trie::Travel(int* arr, int n)
{
for (int i = ; i < n; i++)
{
arr[i] = ;
}
Travel(root, arr);
} class FindTheClones
{
protected:
int n;
int* count;
Trie tree;
public:
FindTheClones(int n);
~FindTheClones();
void Insert(char* word);
void Travel();
void Output() const;
}; FindTheClones::FindTheClones(int n)
{
this->n = n;
count = new int[n + ];
memset(count, , sizeof(int) * (n + ));
} FindTheClones::~FindTheClones()
{
delete[] count;
} void FindTheClones::Insert(char* word)
{
tree.Insert(word);
} void FindTheClones::Travel()
{
tree.Travel(count, n + );
} void FindTheClones::Output() const
{
for (int i = ; i < n + ; i++)
{
printf("%d\n", count[i]);
}
} int main()
{
int n = , m = ;
while (scanf("%d%d", &n, &m))
{
if (n <= )
{
break;
}
char str[];
FindTheClones obj(n);
for (int i = ; i < n; i++)
{
scanf("%s", str);
obj.Insert(str);
}
obj.Travel();
obj.Output();
}
return ;
}

POJ2945(Find the Clones)--字典树,map的更多相关文章

  1. I: Carryon的字符串排序(字典树/map映射)

    2297: Carryon的字符串 Time Limit: C/C++ 1 s      Java/Python 3 s      Memory Limit: 128 MB      Accepted ...

  2. POJ 1002 487-3279(字典树/map映射)

    487-3279 Time Limit: 2000MS        Memory Limit: 65536K Total Submissions: 309257        Accepted: 5 ...

  3. poj1002 字典树+map+查询单词出现次数

    487-3279 Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 309235   Accepted: 55223 Descr ...

  4. ACM学习历程—HDU 4287 Intelligent IME(字典树 || map)

    Description We all use cell phone today. And we must be familiar with the intelligent English input ...

  5. ZOJ 3674 Search in the Wiki(字典树 + map + vector)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4917 题意:每一个单词都一些tips单词. 先输入n个单词和他们的t ...

  6. POJ2945 Find the Clones trie树

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

  7. hdoj 1251 字典树||map

    统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others)Total Submi ...

  8. 字典树+map

    Problem Description Carryon最近喜欢上了一些奇奇怪怪的字符,字符都是英文小写字母,但奇怪的是a可能比b小,也可能比b大,好奇怪.与此同时,他拿到了好多的字符串,可是看着很不顺 ...

  9. HDU1251 统计难题(字典树|map

    Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀). Input输入数据的第一部分 ...

随机推荐

  1. 阶段5 3.微服务项目【学成在线】_day18 用户授权_04-方法授权-方法授权实现

    2.3 方法授权实现 2.3.1资源服务添加授权控制 1.要想在资源服务使用方法授权,首先在资源服务配置授权控制 1)添加spring-cloud-starter-oauth2依赖. 2)拷贝授权配置 ...

  2. SpringCloud学习成长之路二 服务客户端(rest+ribbon)

    在微服务架构中,业务都会被拆分成一个独立的服务,服务与服务的通讯是基于http restful的. Spring cloud有两种服务调用方式,一种是ribbon+restTemplate,另一种是f ...

  3. sql 获取本周周一和周日

    版本1.0(获取周日存在问题,请勿使用,仅用于引以为戒) 存在问题,获取周日的时候,当当前时间正好是周日,会获取下一周的周日,而非本周周日. ,)),) ),, ,)),) 版本2.0 看到版本1.0 ...

  4. (四)UML之顺序图(时序图)

    一.概念 顺序图是交互图的一种形式,它显示对象沿生命线发展,对象之间随时间的交互表示为从源生命线指向目标生命线的消息.顺序图能很好地显示那些对象与其它那些对象通信,什么消息触发了这些通信,顺序图不能很 ...

  5. 基于JAVA JWT 实现OATUH TOKEN验证

    什么是jwt? 最详细的是官网:https://jwt.io/ 这里以java的ssm框架为例,集成jwt. 1.pom.xml 导入jwt的包 <!-- jwt --> <!-- ...

  6. iOS推送证书上传(转)

    iOS 推送证书制作(JAVA/PHP) 在使用Java或者PHP制作iOS推送服务器的时候,需要自己从开发者网站上导出的aps_developer_identity证书和Apple Developm ...

  7. 表格组件---bootstrapTable

    bootstrapTable中文官方网站http://bootstrap-table.wenzhixin.net.cn1.文件引用 //1.引用Jquery <script src=" ...

  8. wordpress通过$wpdb获取一个分类下所有的文章

    在wordpress程序根目录下新建一个php文件,粘贴下面的代码 如下面的代码注释,修改$CID这个分类id,就可以获取这个分类下的文章了.这个查询需要联合三个表wp_posts.wp_term_r ...

  9. Egret入门学习日记 --- 第十八篇(书中 8.5~8.7 节 内容)

    第十八篇(书中 8.5~8.7 节 内容) 其实语法篇,我感觉没必要写录入到日记里. 我也犹豫了好久,到底要不要录入. 这样,我先读一遍语法篇的所有内容,我觉得值得留下的,我就录入日记里. 不然像昨天 ...

  10. vscode常用插件小结

    工欲善其事,必先利其器. 个人用过的代码编辑器有sublime,webstrom,vscode,H5builder.综合比较下来还是更倾向于vscode. sublime是一款轻量级的编辑器,优点是启 ...