把电话号码转换成为词典中能够记忆的的单词的组合,找到最短的组合。

我这道题应用到的知识点:

1 Trie数据结构

2 map的应用

3 动态规划法Word Break的知识

4 递归剪枝法

思路:

1 建立Trie字典树。方便查找, 可是字典树不是使用字符来建立的。而是把字符转换成数字。建立一个数字字典树。 然后叶子节点设置一个容器vector<string>装原单词。

2 动态规划建立一个表,记录能够在字典树中找到的字符串的前缀子串

3 假设找到整个串都在字典树中,那么就能够直接返回这个单词。假设无法直接找到。那么就要在表中找到一个前缀子串,然后后面部分在字典树中查找,看是否找到包括这个子串的单词,而且要求找到的单词长度最短。- 这里能够使用剪枝法提高效率。

原题:http://acm.timus.ru/problem.aspx?space=1&num=1002

作者:靖心 - http://blog.csdn.net/kenden23

#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <algorithm>
#include <unordered_map> using namespace std; class PhoneNumber1002_2
{
static const int SIZE = 10;
struct Node
{
vector<string> words;
Node *children[SIZE];
explicit Node () : words()
{
for (int i = 0; i < SIZE; i++)
{
children[i] = NULL;
}
}
}; struct Trie
{
Node *emRoot;
int count;
explicit Trie(int c = 0) : count(c)
{
emRoot = new Node;
}
~Trie()
{
deleteTrie(emRoot);
}
void deleteTrie(Node *root)
{
if (root)
{
for (int i = 0; i < SIZE; i++)
{
deleteTrie(root->children[i]);
}
delete root;
root = NULL;
}
}
}; void insert(Trie *trie, string &keys, string &keyWords)
{
int len = (int)keys.size(); Node *pCrawl = trie->emRoot;
trie->count++; for (int i = 0; i < len; i++)
{
int k = keys[i] - '0';
if (!pCrawl->children[k])
{
pCrawl->children[k] = new Node;
}
pCrawl = pCrawl->children[k];
}
pCrawl->words.push_back(keyWords);
} Node *search(Node *root, string &keys)
{
int len = (int)keys.size(); Node *pCrawl = root;
for (int i = 0; i < len; i++)
{
int k = keys[i] - '0';
if (!pCrawl->children[k])
{
return NULL;//没走全然部keys
}
pCrawl = pCrawl->children[k];
}
return pCrawl;
} void searchLeft(Node *leaf, Node *r, int len, int &prun)
{
if (len >= prun) return; if (leaf->words.size())
{
r = leaf;
prun = len;
return;
} for (int i = 0; i < SIZE; i++)
{
searchLeft(leaf->children[i], r, len+1, prun);
}
} void wordsToKey(string &keys, string &keyWords,
unordered_map<char, char> &umCC)
{
for (int i = 0; i < (int)keyWords.size(); i++)
{
keys.push_back(umCC[keyWords[i]]);
}
} void charsToMap(const string phdig[], unordered_map<char, char> &umCC)
{
for (int i = 0; i < 10; i++)
{
for (int k = 0; k < (int)phdig[i].size(); k++)
{
umCC[phdig[i][k]] = i + '0';
}
}
} string searchComb(Trie *trie, string &num)
{
vector<string> tbl(num.size());
for (int i = 0; i < (int)num.size(); i++)
{
string s = num.substr(0, i+1);
Node *n = search(trie->emRoot, s);
if (n && n->words.size())
{
tbl[i].append(n->words[0]);
continue;//这里错误写成break! 。
}
for (int j = 1; j <= i; j++)
{
if (tbl[j-1].size())
{
s = num.substr(j, i-j+1);
n = search(trie->emRoot, s);
if (n && n->words.size())
{
tbl[i].append(tbl[j-1]);
tbl[i].append(" ");
tbl[i].append(n->words[0]);
break;
}
}
}
} if (tbl.back().size())
{
return tbl.back();
} string ans;
for (int i = 0; i < (int)tbl.size() - 1; i++)
{
if (tbl[i].size())
{
string tmp = tbl[i];
string keys = num.substr(i+1);
Node *n = search(trie->emRoot, keys); if (!n) continue; Node *r = NULL;
int prun = INT_MAX;
searchLeft(n, r, 0, prun); tmp += r->words[0]; if (ans.empty() || tmp.size() < ans.size())
{
ans = tmp;
}
}
}
return ans.empty()? "No solution." : ans;
} //測试函数。不使用解题
void printTrie(Node *n)
{
if (n)
{
for (int i = 0; i < SIZE; i++)
{
printTrie(n->children[i]);
for (int j = 0; j < (int)n->words.size(); j++)
{
cout<<n->words[j]<<endl;
}
}
}
}
public:
PhoneNumber1002_2()
{
const string phdig[10] =
{"oqz","ij","abc","def","gh","kl","mn","prs","tuv","wxy"};
unordered_map<char, char> umCC;
charsToMap(phdig, umCC); int N; string num, keys, keyWords;
while ((cin>>num) && "-1" != num)
{
cin>>N; Trie trie;
while (N--)
{
cin>>keyWords;
wordsToKey(keys, keyWords, umCC); insert(&trie, keys, keyWords); keys.clear();//别忘记清空
} cout<<searchComb(&trie, num)<<endl;
}
}
};

Timus : 1002. Phone Numbers 题解的更多相关文章

  1. 1002 Phone Numbers 解题报告

    1002. Phone Numbers Time limit: 2.0 secondMemory limit: 64 MB In the present world you frequently me ...

  2. URAL 1002 Phone Numbers(KMP+最短路orDP)

    In the present world you frequently meet a lot of call numbers and they are going to be longer and l ...

  3. CF55D Beautiful numbers 题解

    题目 Volodya is an odd boy and his taste is strange as well. It seems to him that a positive integer n ...

  4. Hdoj 1905.Pseudoprime numbers 题解

    Problem Description Fermat's theorem states that for any prime number p and for any integer a > 1 ...

  5. Hdoj 1058.Humble Numbers 题解

    Problem Description A number whose only prime factors are 2,3,5 or 7 is called a humble number. The ...

  6. [LeetCode] Add Two Numbers题解

    Add Two Numbers: You are given two non-empty linked lists representing two non-negative integers. Th ...

  7. poj 1995 Raising Modulo Numbers 题解

    Raising Modulo Numbers Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 6347   Accepted: ...

  8. Timus 1009. K-based Numbers

    1009. K-based Numbers Time limit: 0.5 secondMemory limit: 64 MB Let’s consider K-based numbers, cont ...

  9. Timus 1712. Cipher Grille 题解

    版权声明:本文作者靖心,靖空间地址:http://blog.csdn.net/kenden23/.未经本作者同意不得转载. https://blog.csdn.net/kenden23/article ...

随机推荐

  1. [转] linux中 参数命令 -- 和 - 的区别

    在 Linux 的 shell 中,我们把 - 和 -- 加上一个字符(字符串)叫做命令行参数. 主流的有下面几种风格Unix 风格参数 前面加单破折线 -BSD 风格参数 前面不加破折线GNU 风格 ...

  2. 目标检测算法SSD在window环境下GPU配置训练自己的数据集

    由于最近想试一下牛掰的目标检测算法SSD.于是乎,自己做了几千张数据(实际只有几百张,利用数据扩充算法比如镜像,噪声,切割,旋转等扩充到了几千张,其实还是很不够).于是在网上找了相关的介绍,自己处理数 ...

  3. 优化脚本性能 Optimizing Script Performance

    This page gives some general hints for improving script performance on iOS. 此页面提供了一些一般的技巧,提高了在iOS上的脚 ...

  4. Tomcat网站根目录的配置

    在</Host>前插入: <Host> … … <Context path="" docBase="E:\Users\Administrat ...

  5. shell if 条件语句实践

    对于if 语法 我们不过多做介绍,这里直接上实例,以开发rsync服务启动脚本为例,先对rsync做个简单介绍 [root@backup ~]# rpm -qa|grep rsync rsync--. ...

  6. javaScript 笔记(4) -- 弹窗 & 计时事件 & cookie

    弹窗 可以在 JavaScript 中创建三种消息框:警告框.确认框.提示框. 警告框:经常用于确保用户可以得到某些信息. 当警告框出现后,用户需要点击确定按钮才能继续进行操作. 语法: window ...

  7. CSS Modules使用方法

    css modules调研 css模块化解决方案 抛弃css (Radium,jsxstyle,react-style) 利用js来管理样式依赖(css Modules) css模块化面临的问题 全局 ...

  8. 【15】vuex2.0 之 modules

    vue 使用的是单一状态树对整个应用的状态进行管理,也就是说,应用中的所有状态都放到store中,如果是一个大型应用,状态非常多, store 就会非常庞大,不太好管理.这时vuex 提供了另外一种方 ...

  9. 【11】 Express安装入门与模版引擎ejs

    前言 Express简介和安装 运行第一个基于express框架的Web 模版引擎 ejs express项目结构 express项目分析 app.set(name,value) app.use([p ...

  10. [AGC008F] Black Radius(树形dp)

    神题啊!! Description 给你一棵有N个节点的树,节点编号为1到N,所有边的长度都为1 "全"对某些节点情有独钟,这些他喜欢的节点的信息会以一个长度为N的字符串s的形式给 ...