Find the Clones(字典树之哈希功能)
来源:互联网 发布:四川省委党校网络教育 编辑:程序博客网 时间:2024/05/17 08:03
萌萌哒的链接:http://poj.org/problem?id=2945
题目的意思就是找每一个字符串出现的次数,输出出现1-n次的字符串的个数.
字典树的哈希应用.index记录相同字符串出现的次数,最后dfs查找. 因为题目中只有4个字母,所以dfs总的时间复杂度
为4*字典树的总结点个数.
字典树用链表写挺方便的,就是内存花销太大,比如这道题目如果把内存开成26,就MLE了.
#include <iostream>#include <cstring>#include <cstdio>#include <cmath>#include <set>#include <map>#include <queue>#include <vector>#include <cstdlib>#include <algorithm>#define ls u << 1#define rs u << 1 | 1#define lson l, mid, u << 1#define rson mid + 1, r, u << 1 | 1#define INF 0x3f3f3f3f#define MAX 4using namespace std;typedef long long ll;const int M = 2e4 + 100;const int mod = 2147483647;char s[25];int num[M],d[] = {'A'-'A','C'-'A','T'-'A','G'-'A'};struct Trie { int index; Trie *next[MAX]; Trie() { index = 0; memset(next,0,sizeof(next)); }};void Trie_insert(Trie *tr,int len) { if(s[len]) { int u = s[len] - 'A'; for(int i = 0; i < 4; i++){ if(u == d[i]){ u = i; break; } } if(s[len + 1] == '\0' && tr->next[u]) { tr->next[u]->index++; return ; } if(tr->next[u] == 0) tr->next[u] = new Trie; Trie_insert(tr->next[u],len + 1); } else { tr->index = 1; }}void dfs(Trie *tr) { if(tr->index) num[tr->index]++; for(int u = 0; u < 4; u++) { if(tr->next[u]) dfs(tr->next[u]); }}void deal_Trie(Trie *tr){ if(tr == NULL) return ; for(int u = 0; u < 4; u++){ if(tr->next[u]) dfs(tr->next[u]); } free(tr);}int main() { int n,m; while(~scanf("%d %d",&n,&m) && (n | m)) { Trie *root = new Trie; memset(num,0,sizeof(num)); for(int i = 0; i < n; i++) { scanf("%s",s); Trie_insert(root,0); } dfs(root); for(int i = 1; i <= n; i++) { printf("%d\n",num[i]); } //deal_Trie(root); } return 0;}
0 0
- Find the Clones(字典树之哈希功能)
- POJ 2945 - Find the Clones(字典树)
- Find the Clones(字典树)
- POJ2945 Find the Clones(暴力 & 字典树)
- poj 2945 Find the clones (tire树)
- POJ2945 Find the Clones Tire树
- POJ2945 Find the Clones
- poj3945 Find the Clones
- 2945 Find the Clones //trie
- POJ 2945 Find the Clones
- TOJ 2429 Find the Clones
- poj 2945 Find the Clones
- poj 2945 Find the Clones
- POJ 2945 Find the Clones
- poj 2945 Find the Clones
- POJ 2945 Find the Clones (Trie树 两种姿势)
- poj 2945 Find the Clones trie树的简单应用
- poj_2945 Find the Clones (Trie树 内存分配)
- UIWebView---JavaScriptCore框架介绍
- firefox修改配置文件保存路径 (书签、配置、历史记录等)
- 动态规划 最长公共子序列
- NokiaX NokiaX2 NokiaXL 开发android adb 找不到设备 解决方案
- SDUT 2105 小泉的难题
- Find the Clones(字典树之哈希功能)
- 开通博客的第一天
- C++ set(集)和multiset(多集)
- struts2进阶【1】——环境配置
- poj--2503+字典树入门题
- 在虚拟机,ios开发,Unable to boot the iOS Simulator
- 成功人士的三级龙门
- Apps that include an arm64 are required to include to include both armv7 and armv7s architecture
- win8.1中麒麟版Ubuntu14.10samba服务器配置