这个问题的解决方法是多种多样的。如本文所用,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解读的更多相关文章

  1. 字符串专题:map POJ 1002

    第一次用到是在‘校内赛总结’扫地那道题里面,大同小异 map<string,int>str 可以专用做做字符串的匹配之类的处理 string donser; str [donser]++ ...

  2. POJ 1002 487-3279

    A - 487-3279 Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit ...

  3. [POJ 1002] 487-3279 C++解题报告

        487-3279 Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 228365   Accepted: 39826 D ...

  4. 开篇,UVA 755 && POJ 1002 487--3279 (Trie + DFS / sort)

    博客第一篇写在11月1号,果然die die die die die alone~ 一道不太难的题,白书里被放到排序这一节,半年前用快排A过一次,但是现在做的时候发现可以用字典树加深搜,于是乐呵呵的开 ...

  5. [POJ] #1002# 487-3279 : 桶排序/字典树(Trie树)/快速排序

    一. 题目 487-3279 Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 274040   Accepted: 48891 ...

  6. poj 2513 Colored Sticks (trie 树)

    链接:poj 2513 题意:给定一些木棒.木棒两端都涂上颜色,不同木棒相接的一边必须是 同样的颜色.求能否将木棒首尾相接.连成一条直线. 分析:能够用欧拉路的思想来解,将木棒的每一端都看成一个结点 ...

  7. Poj 1002 487-3279(二叉搜索树)

    题目链接:http://poj.org/problem?id=1002 思路分析:先对输入字符进行处理,转换为标准形式:插入标准形式的电话号码到查找树中,若有相同号码计数器增加1,再中序遍历查找树. ...

  8. POJ 2001 Shortest Prefixes (Trie)

    题目链接:POJ 2001 Description A prefix of a string is a substring starting at the beginning of the given ...

  9. poj 2513 Colored Sticks trie树+欧拉图+并查集

    点击打开链接 Colored Sticks Time Limit: 5000MS   Memory Limit: 128000K Total Submissions: 27955   Accepted ...

随机推荐

  1. JDWP

    JPDA(Java Platform Debugger Architecture) 是 Java 平台调试体系结构的缩写,通过 JPDA 提供的 API,开发人员可以方便灵活的搭建 Java 调试应用 ...

  2. JPA的Embeddable注解

    来源于http://zjsword2000.blog.163.com/blog/static/4583983320083184844734/ 在hibernate中实现自定义类型,只要实现UserTy ...

  3. hdu1573-X问题

    http://acm.hdu.edu.cn/showproblem.php?pid=1573 中国剩余定理 #include<iostream> #include<cstdio> ...

  4. 系统变量file.encoding对Java的运行影响有多大?(转)good

    这个话题来自: Nutz的issue 361 在考虑这个issue时, 我一直倾向于使用系统变量file.encoding来改变JVM的默认编码. 今天,我想到, 这个系统变量,对JVM的影响到底有多 ...

  5. 在Window和Linux下使用Zthread库

    ZThread库是一个开源的跨平台高级面向对象的线性和sycnchronization 库,以运行POSIX 和Win32 系统中的C++程序. ZThread库的主页:http://zthread. ...

  6. Disqus – About Disqus

    Disqus – About Disqus   Disqus is a free service that enables great online communities. As the web's ...

  7. html中的table在android端显示

    转载请注明出处:http://blog.csdn.net/u012338845/article/details/46773245 開始都是用Html.fromHtml(source).来显示html的 ...

  8. Java NIO实战之聊天室

    在工作之余花了两个星期看完了<Java NIO>.整体来说这本书把NIO写的非常具体,没有过多的废话,讲的都是重点,仅仅是翻译的中文版看的确实吃力.英文水平太低也没办法,总算也坚持看完了. ...

  9. oracle检查点队列与增量检查点【转载】

    oracle检查点队列与增量检查点 今天是2013-09-04,这几天一直心里安顿不下来,今天还好了,可以自己安静的学习一下oracle,在此记录一下学习笔记.这篇文章我不知道在那转载的,一直都留在我 ...

  10. liGDX life_cycle (生命周期)

    本文章翻译自libGDX官方wiki,,转载请注明出处:http://blog.csdn.net/kent_todo/article/details/37940489 libGDX官方网址:http: ...