POJ 1002 487-3279 Trie解读
这个问题的解决方法是多种多样的。如本文所用,Trie为了解决这个问题。
它也可用于hash表。map等解决方案,由于输入是特定7数字,因此,你应该能够解决。
如本文所用,Trie不是非常快。最后,我主要是由于直接输出导线,遍历整个Trie速度相当慢。
思路:
1 使用insert函数建立Trie。主要添加一个叶子节点的信息。记录当前有多少个反复的字符串
2 遍历就是依据叶子节点的信息决定是否须要输出。
#include <stdio.h>
#include <iostream>
#include <string>
using namespace std; const int ARR_SIZE = 10; struct Node
{
int n;
Node **alpha;
Node() : n(0)
{
alpha = new Node*[ARR_SIZE];
for (int i = 0; i < ARR_SIZE; i++) alpha[i] = NULL;
}
~Node()
{
for (int i = 0; i < ARR_SIZE; i++)
{
if (alpha[i]) delete alpha[i];
alpha[i] = NULL;
}
delete alpha;
alpha = NULL;
n = 0;
}
}; Node *Trie; void insertTrie(string &s)
{
Node *pCrawl = Trie;
for (unsigned i = 0; i < s.size(); i++)
{
int id = s[i] - '0';
if (pCrawl->alpha[id] == NULL)
{
pCrawl->alpha[id] = new Node;
}
pCrawl = pCrawl->alpha[id];
}
pCrawl->n++; //添加一个字符串数量
} inline char charToNum(char a)
{
switch (a)
{
case 'A': case 'B': case 'C':
return '2';
case 'D': case 'E': case 'F':
return '3';
case 'G': case 'H': case 'I':
return '4';
case 'J': case 'K': case 'L':
return '5';
case 'M': case 'N': case 'O':
return '6';
case 'P': case 'R': case 'S':
return '7';
case 'T': case 'U': case 'V':
return '8';
case 'W': case 'X': case 'Y':
return '9';
}
return char('9'+1); //There is no mapping for Q or Z.uppercase letters (excluding Q and Z)
} char lookUpTable[26]; void initLookUpTable()
{
for (int i = 0; i < 26; i++)
{
lookUpTable[i] = charToNum(char(i+'A'));
}
} void strToNum(string &s)
{
int j = -1;
for (unsigned i = 0; i < s.size(); i++)
{
if (s[i] != '-')
{
s[++j] = s[i];//错误:s[j++] = s[i];j已经变化,以下还是用j
if ('A' <= s[j] && s[j] <= 'Z')
{
s[j] = lookUpTable[s[j]-'A'];
}
}
}
s.resize(j+1);
} bool flag;
void printRepeat(Node *r, string &path)
{
if (r->n >= 1)
{
if (r->n == 1) return ;
flag = true;
unsigned i = 0;
for ( ; i < 3; i++) putchar(path[i]);
putchar('-');
for ( ; i < 7; i++) putchar(path[i]);
putchar(' ');
printf("%d\n", r->n);
return ;
}
for (int i = 0; i < ARR_SIZE; i++)//in ascending lexicographical order
{
path += char(i+'0');
if (r->alpha[i]) printRepeat(r->alpha[i], path);
path.erase(path.size()-1);
}
} int main()
{
initLookUpTable(); int N;
scanf("%d", &N);
getchar();
string s;
char chs[100];
Trie = new Node;
while (N--)
{
gets(chs);
s = chs;
strToNum(s);
insertTrie(s);
}
flag = false;
string path;
printRepeat(Trie, path);
if (!flag) puts("No duplicates.");
delete Trie;
return 0;
}
版权声明:笔者心脏靖。景空间地址:http://blog.csdn.net/kenden23/,可能不会在未经作者同意转载。
POJ 1002 487-3279 Trie解读的更多相关文章
- 字符串专题:map POJ 1002
第一次用到是在‘校内赛总结’扫地那道题里面,大同小异 map<string,int>str 可以专用做做字符串的匹配之类的处理 string donser; str [donser]++ ...
- POJ 1002 487-3279
A - 487-3279 Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit ...
- [POJ 1002] 487-3279 C++解题报告
487-3279 Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 228365 Accepted: 39826 D ...
- 开篇,UVA 755 && POJ 1002 487--3279 (Trie + DFS / sort)
博客第一篇写在11月1号,果然die die die die die alone~ 一道不太难的题,白书里被放到排序这一节,半年前用快排A过一次,但是现在做的时候发现可以用字典树加深搜,于是乐呵呵的开 ...
- [POJ] #1002# 487-3279 : 桶排序/字典树(Trie树)/快速排序
一. 题目 487-3279 Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 274040 Accepted: 48891 ...
- poj 2513 Colored Sticks (trie 树)
链接:poj 2513 题意:给定一些木棒.木棒两端都涂上颜色,不同木棒相接的一边必须是 同样的颜色.求能否将木棒首尾相接.连成一条直线. 分析:能够用欧拉路的思想来解,将木棒的每一端都看成一个结点 ...
- Poj 1002 487-3279(二叉搜索树)
题目链接:http://poj.org/problem?id=1002 思路分析:先对输入字符进行处理,转换为标准形式:插入标准形式的电话号码到查找树中,若有相同号码计数器增加1,再中序遍历查找树. ...
- POJ 2001 Shortest Prefixes (Trie)
题目链接:POJ 2001 Description A prefix of a string is a substring starting at the beginning of the given ...
- poj 2513 Colored Sticks trie树+欧拉图+并查集
点击打开链接 Colored Sticks Time Limit: 5000MS Memory Limit: 128000K Total Submissions: 27955 Accepted ...
随机推荐
- mojo 关闭utf8
[root@wx03 ~]# cat test.pl use Mojolicious::Lite; use JSON qw/encode_json decode_json/; use Encode; ...
- hdu 5183(hash)
传送门:Negative and Positive (NP) 题意:给定一个数组(a0,a1,a2,⋯an−1)和一个整数K, 请来判断一下是否存在二元组(i,j)(0≤i≤j<n)使得 NP− ...
- ruby语言仅仅是昙花一现
Ruby语言本身存在非常久了,在国内一直没火过.非常多人仅仅是知道有这样的语言,会的人少之又少.不论什么一种语言坚持十来年的发展,变得越来越好,一定有它不平常的地方.不能任意的去比較语言本身的好与坏. ...
- 注册Dev的帮助文件
Download the CHM files from… Code: https://www.devexpress.com/Support/Documentation/download.xml?pla ...
- Lisp的永恒之道 好文
http://www.cnblogs.com/weidagang2046/archive/2012/06/03/tao_of_lisp.html
- copy算法
copy------强化效率无所不用其极 copy(first,last,result)算法可将输入区间[first,last)内的元素拷贝到输出区间[result,result+(last-f ...
- 编程算法 - 萨鲁曼的军队(Saruman's Army) 代码(C)
萨鲁曼的军队(Saruman's Army) 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 直线上有N个点, 每个点, 其距离为R以内的区域里 ...
- xcode 改动整个项目名
目标为:将项目名XCD4改成xcd5. 先上结果图:==> 1.在左側的导航区域点击两次项目名,两次间隔时间略微长些,项目名会变成可编辑状态. 将名称改动为xcd5后按enter键弹出一个对话框 ...
- 【CSS3】transform-origin原点旋转
忙乱, 点 -moz-transform-origin: 0 0; -webkit-transform-origin:0 0; -o-transform-origin:0 0; 以右上角给原点 -mo ...
- c++中volatile详解
1. 为什么用volatile? C/C++ 中的 volatile 关键字和 const 对应,用来修饰变量,通常用于建立语言级别的 memory barrier.这是 BS 在 "The ...