POJ - 1002 487-3279 (Trie树)
2014-08-01 10:30
417 查看
题目:http://poj.org/problem?id=1002
题意:
n行号码,数字+大写字母+“-”,按照题意转化为纯数字的标准号码后,统计重复出现的号码,号码按顺序输出
分析:
输入后,转化成标准号码,可以用Trie树减少储存空间,最后深度优先搜索,把出现次数大于1的输出即可
代码:
题意:
n行号码,数字+大写字母+“-”,按照题意转化为纯数字的标准号码后,统计重复出现的号码,号码按顺序输出
分析:
输入后,转化成标准号码,可以用Trie树减少储存空间,最后深度优先搜索,把出现次数大于1的输出即可
代码:
#include <stdio.h> #include <iostream> #include <string.h> #include <string> #include <math.h> #include <algorithm> #include <queue> #include <stack> #include <vector> #include <map> using namespace std; #define MAX 1000+10 #define MIN -1e+10 #define INF 0x7f7f7f7f int t, n, m; struct Trie { int id; // 出现次数 Trie *child[11]; // 叶子节点 Trie() { memset(child, 0, sizeof(child)); id = 0; } } root, tries[100000+10]; // 根节点,以及备用叶子节点 int t_num; // 叶子节点数目 int d[] = {2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 9, 9, 9, 9}; // 字母映射数字 void insert(char str[]) // 插入号码 { Trie *x = &root; int p; for(int i = 0; str[i]; i++) { if(str[i] == '-') continue; if(str[i] >='0' && str[i] <='9') p = str[i] - '0'; else if(str[i]>='A' && str[i] <='Z') p = d[str[i] - 'A']; if(x->child[p] == NULL) x->child[p] = &tries[t_num++];// 等价于 <span style="font-family: Arial, Helvetica, sans-serif;">x->child[p] = </span><span style="font-family: Arial, Helvetica, sans-serif;">new Trie;</span> x = x->child[p]; } x->id ++; } int flag; // 是否有重复号码 void output(Trie *x, int num[], int len) // 递归输出重复号码 { if(len >= 7) { if(x->id == 1) return ; for(int i = 0; i<len; i++) { printf("%d", num[i]); if(i == 2) printf("-"); } printf(" %d\n", x->id); flag = 1; return ; } for(int i =0; i<10; i++) { if(x->child[i] == NULL) continue; num[len] = i; output(x->child[i], num, len+1); } } int main() { int i, j, k; freopen("a.txt", "r", stdin); char str[1000]; while(~scanf("%d", &n)) { cin.get(); for(i = 0; i<n; i++) { gets(str); insert(str); } int num[100]; flag = 0; output(&root, num, 0); if(!flag) printf("No duplicates.\n"); } return 0; }
相关文章推荐
- POJ 1002 487-3279
- C -- POJ 1002 487-3279
- POJ1002 487-3279
- 开篇,UVA 755 && POJ 1002 487--3279 (Trie + DFS / sort)
- POJ水题1002 487-3279
- POJ 1002 487-3279
- poj1002-487-3279
- POJ 1002 487-3279 Trie题解
- POJ 1002求487-3279解题报告——【PKU ACM】
- POJ--1002--487-3279
- POJ 1002 487-3279 G++
- POJ1002:487-3279 神奇的TLE
- POJ1002 487-3279 字符串 C语言
- poj 1002 487-3279
- 一个字典树问题--电话号码转化问题(POJ 1002 487-3279)
- poj 1002 487-3279 ((含易错点、测试数据) 简单模拟 )
- POJ1002 SDUT1001 487-3279 ACM算法设计
- 快速切题 poj 1002 487-3279 按规则处理 模拟 难度:0
- poj1002 487-3279
- POJ1002--487-3279