这个问题的解决方法是多种多样的。如本文所用,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. Oracle 执行计划了的rows概念

    alter session set statistics_level=all; select t1.* from t1,t2 where t1.id=t2.id and t1.id<3; sel ...

  2. Oracle ACL(Access Control List)

    在oralce 11g中假如你想获取server的ip或者hostname,执行如下语句 SELECT utl_inaddr.get_host_address FROM dual;  //获取IP S ...

  3. VirtualBox集群建立和网络配置

    安装 1. 安装 安装Oracle VM VirtualBox之后,新建一个虚拟机,制定好内存等信息,开始安装操作系统,这里安装ubuntu-12.04.2-desktop-i386版本. 2. 拷贝 ...

  4. 关于Thread类中三个interrupt方法的研究与学习(转)

    先看三个方法原型: public void interrupt(): public boolean isInterrupted(): public static boolean interrupted ...

  5. Pyhon安装media模块

    都是教科书惹的祸,它没有说清楚.media看着很标准,其实不是python自带的库.需要安装第三方软件后才能用. 在这里http://pythonhosted.org/PyGraphics/insta ...

  6. SQL查询语句联系

    建立四个表,分别是学生表,课程表,成绩表和教师信息表 插入信息: 题目: 1. 查询Student表中的所有记录的Sname.Ssex和Class列 select Sname,Ssex,Class f ...

  7. (读书笔记).NET大局观-.NET语言(1)

    通用语言运行时 通用语言运行时被明确设计为支持多种语言,一般而言,建立于CLR之上的语言可以获得共同的良好处理.通过一个宏大的核心语义集,CLR还界定了一个以它为基础的典型编程语言的大体部分.例如对于 ...

  8. Redis最有用的中文资源,你值得拥有

    只是为了记录资源地址,最好直接访问doc.redisfans.com更美观 Redis 命令参考 本文档是 Redis Command Reference 和 Redis Documentation ...

  9. 你不知道的JavaScript上卷笔记

    你不知道的JavaScript上卷笔记 前言 You don't know JavaScript是github上一个系列文章   初看到这一标题的时候,感觉怎么老外也搞标题党,用这种冲突性比较强的题目 ...

  10. tomcat实现多端口、多域名访问

    tomcat实现多端口访问 tomcat可以实现:多个端口访问同一个web应用.不同的应用通过不同的域名进行访问. 本文介绍的都是只启动一个tomcat服务的情况下,当然,实现这些功能也可以通过启动多 ...