Typical Trie usage. But please note that it could be any order of input strings.

#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <iostream>
using namespace std; class TrieNode {
public:
// Initialize your data structure here.
TrieNode() {
c = ;
bIsLast = false;
}
TrieNode(char vc) {
c = vc;
bIsLast = false;
}
public:
char c;
bool bIsLast;
unordered_map<char, TrieNode*> childMap;
}; class Trie {
public:
Trie()
{
root = new TrieNode();
} ~Trie()
{
if (root)
{
for(auto kv : root->childMap)
{
delete kv.second;
}
delete root;
}
} // Inserts a word into the trie.
bool insert(string s)
{
TrieNode *r = root;
for (int i = ; i < s.length(); i++)
{
if (r->childMap.find(s[i]) == r->childMap.end())
{
r->childMap.insert(make_pair(s[i], new TrieNode(s[i])));
}
else if (r->childMap[s[i]]->bIsLast)
{
return false;
} r = r->childMap[s[i]];
}
r->bIsLast = true;
return r->childMap.empty();
} private:
TrieNode* root;
}; int main()
{
Trie tr; int n; cin >> n;
vector<string> strs;
while(n --)
{
string str; cin >> str;
if(!tr.insert(str))
{
cout << "BAD SET" << endl;
cout << str << endl;
return ;
}
} cout << "GOOD SET" << endl;
return ;
}

HackerRank "No Prefix Set"的更多相关文章

  1. Spring配置文件标签报错:The prefix "XXX" for element "XXX:XXX" is not bound. .

    例如:The prefix "context" for element "context:annotation-config" is not bound. 这种 ...

  2. [LeetCode] Implement Trie (Prefix Tree) 实现字典树(前缀树)

    Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...

  3. [LeetCode] Longest Common Prefix 最长共同前缀

    Write a function to find the longest common prefix string amongst an array of strings. 这道题让我们求一系列字符串 ...

  4. 1014: [JSOI2008]火星人prefix

    1014: [JSOI2008]火星人prefix Time Limit: 10 Sec Memory Limit: 162 MB Description 火星人最近研究了一种操作:求一个字串两个后缀 ...

  5. [BZOJ1014][JSOI2008]火星人prefix

    [BZOJ1014][JSOI2008]火星人prefix 试题描述 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:madamimadam,我们将这个字符串的各个字 ...

  6. No plugin found for prefix 'mybatis-generator' in the current project

    http://blog.csdn.net/you23hai45/article/details/50792430 1.错误描述 F:\workspaces\Mybatis>mvn mybatis ...

  7. 【leetcode】Longest Common Prefix

    题目简述: Write a function to find the longest common prefix string amongst an array of strings. 解题思路: c ...

  8. LintCode 78:Longest Common Prefix

      public class Solution { /** * @param strs: A list of strings * @return: The longest common prefix ...

  9. [LintCode] Longest Common Prefix 最长共同前缀

    Given k strings, find the longest common prefix (LCP). Have you met this question in a real intervie ...

随机推荐

  1. 用vs2008打开vs2012项目

    1 使用记事本打开*.sln解决方案文件,将Visual Studio 2012改为Visual Studio 2008 将版本号改为9.00 2 打开扩展名为*.csproj的项目文件,修改为 To ...

  2. 怎样知道linux是否安装了mysql

    1. 使用 [root@localhost Desktop]# rpm -qa |grep mysql (-qa查询已经安装的软件) mysql-libs-5.1.71-1.el6.x86_64 结果 ...

  3. #include #import @class 的一些用法区别

    从网上查了一些资料,整理了一下,发现很多都说的比较详尽,下面摘录自网络 说一下#import同class之间的区别 在ios中我们经常会在.h和.m中引入一些类啊等等一般用的是#import来进行声明 ...

  4. 递归神经网络之理解长短期记忆网络(LSTM NetWorks)(转载)

    递归神经网络 人类并不是每时每刻都从头开始思考.正如你阅读这篇文章的时候,你是在理解前面词语的基础上来理解每个词.你不会丢弃所有已知的信息而从头开始思考.你的思想具有持续性. 传统的神经网络不能做到这 ...

  5. 阅读《构建之法》第八、九、十章有感和Sprint总结

    1.阅读<构建之法>读后感 第八章:需求分析 需求分析,我觉得需求分析挺重要的,一个需求分析是指对要解决的问题进行详细的分析,弄清楚问题的要求,包括需要输入什么数据,要得到什么结果,最后应 ...

  6. Fresco最最最简单使用

    http://blog.csdn.net/wa991830558/article/details/46005063

  7. URAL 1205 By the Underground or by Foot?(SPFA)

    By the Underground or by Foot? Time limit: 1.0 secondMemory limit: 64 MB Imagine yourself in a big c ...

  8. 越狱Season 1-Episode 15: By the Skin and the Teeth

    Season 1, Episode 15: By the Skin and the Teeth -Pope: doctor...you can leave. 医生你得离开 -Burrows: It's ...

  9. windows环境下安装python模块大招

    python发展到今天,感觉版本有点控制不住了,同时出现多个版本python2.5,python2.7 python 3 ,同时跨越windows,mac,*inux等多个平台,还有32位,64位等不 ...

  10. python设置字体颜色

    在开发项目过程中,为了方便调试代码,经常会向stdout中输出一些日志,默认的这些日志就直接显示在了终端中.而一般的应用服务器,第三方库,甚至服务器的一些通告也会在终端中显示,这样就搅乱了我们想要的信 ...