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. linux定时

    linux怎样启动定时任务 crontab -e进入以后的,定时任务写入 */1 * * * * /usr/bin/python /root/lianxi/time_1.py ,每一分钟定时执行tim ...

  2. (实用篇)PHP页面跳转到另一个页面的方法总结

    一.用HTTP头信息  也就是用PHP的header函数.PHP里的header函数的作用就是向浏览器发出由HTTP协议规定的本来应该通过WEB服务器的控制指令,例如声明返回信息的类型("C ...

  3. java读取文件夹下所有文件并替换文件每一行中指定的字符串

    import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.I ...

  4. C++ Vector 用法总结

    1, 头文件#include <vector> using namespace std; 2,定义与初始化 一般结合模板来使用 vector <Elem>           ...

  5. Java--接口和类集框架

    一.接口 接口是静态常量和抽象方法的集合.也就是说,接口中只能有静态常量和抽象方法. public interface Pet { public static final int A = 10; pu ...

  6. codeforces 192 c

    link: http://codeforces.com/contest/330/problem/C broute force but you must be careful about some tr ...

  7. CentOS6.3 Hostname设定修改

    今天装了个CentOS6.3恩,基本上是参照百度文库上面的文章来安装的 http://wenku.baidu.com/link?url=K0tqotryKLFHhJGicx1nC-hsgfWhL3OI ...

  8. RViz 实时观测机器人建立导航2D封闭空间地图过程 (SLAM) ----27

    原创博客:转载请表明出处:http://www.cnblogs.com/zxouxuewei/ ROS提供了非常强大的图形化模拟环境 RViz,这个 RViz 能做的事情非常多.今天我们学习一下如何使 ...

  9. Eclipse中web-inf和meta-inf文件夹的信息

    http://www.cnblogs.com/chinafine/archive/2010/06/13/1757514.html WEB-INF     /WEB-INF/web.xml        ...

  10. Python简史

    Python简史 作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! Python是我喜欢的语言,简洁,优美,容易使用.前两天,我很激 ...