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

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

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

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

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

  1. #include <iostream>
  2. #include <string>
  3. #include <cstring>
  4. #include <fstream>
  5. #include <map>
  6. #include <algorithm>
  7. #include <sstream>
  8. #include <cstdio>
  9. using namespace std;
  10.  
  11. const int LetterCount = ;//最大长度
  12.  
  13. struct Node
  14. {
  15. Node* next[LetterCount];//结点的下一个指针
  16. int count;
  17. bool end;//标记是否为最后一个单词
  18. Node();
  19. };
  20.  
  21. Node::Node()
  22. {
  23. for (int i = ; i < LetterCount; i++)
  24. {
  25. next[i] = NULL;
  26. }
  27. count = ;
  28. end = false;
  29. }
  30.  
  31. class Trie
  32. {
  33. protected:
  34. Node* root;
  35. void GreateRoot();
  36. void Distroy(const Node* root);
  37. void Travel(Node* root, int* arr);
  38. public:
  39. Trie();
  40. ~Trie();
  41. int Insert(char* word);
  42. int Query(char* word);
  43. void Travel(int* arr, int n);
  44. };
  45.  
  46. Trie::Trie()
  47. {
  48. root = NULL;
  49. GreateRoot();
  50. }
  51.  
  52. Trie::~Trie()
  53. {
  54. Distroy(root);
  55. }
  56.  
  57. void Trie::GreateRoot()
  58. {
  59. if (!root)//根结点为false
  60. {
  61. root = new Node();//建根
  62. for (int i = ; i < LetterCount; i++)
  63. {
  64. root->next[i] = NULL;
  65. }
  66. }
  67. }
  68.  
  69. void Trie::Distroy(const Node* root)
  70. {
  71. if (!root)
  72. {
  73. return;
  74. }
  75. for (int i = ; i < LetterCount; i++)
  76. {
  77. if (root->next[i] != NULL)
  78. {
  79. Distroy(root->next[i]);
  80. }
  81. }
  82. delete[] root;
  83. }
  84.  
  85. int Trie::Insert(char* word)
  86. {
  87. Node* p = root;//根结点为root
  88. int length = strlen(word);//计算长度
  89. for (int i = ; i < length; i++)
  90. {
  91. int index = word[i] - 'A';
  92. if (!(p->next[index]))//当前没有那个字母
  93. {
  94. Node* q = new Node();
  95. q->end = false;
  96. for (int j = ; j < LetterCount; j++)
  97. {
  98. q->next[j] = NULL;
  99. }
  100. p->next[index] = q;
  101. }
  102. p = p->next[index];
  103. }
  104. if (p->end)
  105. {
  106. p->count++;
  107. }
  108. else
  109. {
  110. p->end = true;
  111. p->count = ;
  112. }
  113. return p->count;
  114. }
  115.  
  116. int Trie::Query(char* word)
  117. {
  118. Node* p = root;
  119. bool found = true;
  120. int length = strlen(word);
  121. for (int i = ; i < length; i++)
  122. {
  123. int index = word[i] - 'A';
  124. p = p->next[index];
  125. if (!p)//p为false
  126. {
  127. found = false;//没找到
  128. break;
  129. }
  130. }
  131. if (!found || !p->end)//没找到或已经是结束标记
  132. {
  133. return ;
  134. }
  135. return p->count;//否则返回计数
  136. }
  137.  
  138. void Trie::Travel(Node* root, int* arr)
  139. {
  140. if (!root)
  141. {
  142. return;
  143. }
  144. if (root->end)//表示为最后一个词
  145. {
  146. arr[root->count]++;
  147. return;
  148. }
  149. for (int i = ; i < LetterCount; i++)
  150. {
  151. Travel(root->next[i], arr);//递归计算
  152. }
  153. }
  154.  
  155. void Trie::Travel(int* arr, int n)
  156. {
  157. for (int i = ; i < n; i++)
  158. {
  159. arr[i] = ;
  160. }
  161. Travel(root, arr);
  162. }
  163.  
  164. class FindTheClones
  165. {
  166. protected:
  167. int n;
  168. int* count;
  169. Trie tree;
  170. public:
  171. FindTheClones(int n);
  172. ~FindTheClones();
  173. void Insert(char* word);
  174. void Travel();
  175. void Output() const;
  176. };
  177.  
  178. FindTheClones::FindTheClones(int n)
  179. {
  180. this->n = n;
  181. count = new int[n + ];
  182. memset(count, , sizeof(int) * (n + ));
  183. }
  184.  
  185. FindTheClones::~FindTheClones()
  186. {
  187. delete[] count;
  188. }
  189.  
  190. void FindTheClones::Insert(char* word)
  191. {
  192. tree.Insert(word);
  193. }
  194.  
  195. void FindTheClones::Travel()
  196. {
  197. tree.Travel(count, n + );
  198. }
  199.  
  200. void FindTheClones::Output() const
  201. {
  202. for (int i = ; i < n + ; i++)
  203. {
  204. printf("%d\n", count[i]);
  205. }
  206. }
  207.  
  208. int main()
  209. {
  210. int n = , m = ;
  211. while (scanf("%d%d", &n, &m))
  212. {
  213. if (n <= )
  214. {
  215. break;
  216. }
  217. char str[];
  218. FindTheClones obj(n);
  219. for (int i = ; i < n; i++)
  220. {
  221. scanf("%s", str);
  222. obj.Insert(str);
  223. }
  224. obj.Travel();
  225. obj.Output();
  226. }
  227. return ;
  228. }

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. osg::GraphicsContext::WindowingSystemInterface Screen Resolution(屏幕分辨率)

    unsigned int width, height; //获取系统分辨率 osg::GraphicsContext::WindowingSystemInterface *wsInterface = ...

  2. osgText::Text osg字体

    #ifdef _WIN32 #include <Windows.h> #endif // _WIN32 #include<iostream> #include <osgV ...

  3. float和int转换

    http://blog.sina.com.cn/s/blog_5c6f79380101bbrd.html https://blog.csdn.net/ganxingming/article/detai ...

  4. Microsoft Visual Studio(VS)启动报安装过程中无法运行

    开机启动VS提示无法运行,很可能VS正在更新,可以等待几分钟更新完成,再次运行VS. 也可以把更新进程结束,进程名:VSIXAutoUpdate.exe

  5. Vue-cli安装,创建Vue项目

    1.安装脚手架: npm install -g vue-cli 2.查看vue: vue 3.查看可以使用的模板: vue list 4.创建项目sell: vue init webpack sell ...

  6. python判断命令执行成功

    if os.system('lss') !=0: print 'Without the command'

  7. elk收集tomcat日志

    1.elk收集tomcat普通日志: 只在logstash节点增加如下文件,重启logstash即可: cat >>/home/logstash-6.3.0/config/tomcat_t ...

  8. Vue项目过程中遇到的小问题

    1.给router-link添加点击事件 <router-link to="" @click.native=""></router-link& ...

  9. 如何找到linux centos7 中 redis.conf

    我们假设redis正在运行,但是我们找不带redis的配置文件redis.conf. 正确的示范: (1)systemctl status redis ● redis.service - LSB: s ...

  10. vue 强制组件重新渲染

    参考链接:https://blog.csdn.net/zyx1303031629/article/details/86656785