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. R编程感悟

    虽然大学阶段曾经学过C++, matlab等编程,但是真的几乎完全还给了老师- 所以,我一直将R 作为自己真正学习的第一门语言.我从2012年初在来美国的第二个rotation中开始接触到了R.当时不 ...

  2. ZOJ Problem Set - 3643 Keep Deleting

    题目大意: 给出a和b串,a是b串的子串,如果b串有连续的a串,那么就将b串的a串删除,问删除多少次: 题目分析: 打比赛的时候没敲出来,后来想到用栈的思想去模拟就行,网上还有用KMP+栈去做的,没有 ...

  3. UVa 10795 - A Different Task

    题目大意:给出n,表示说有n个大小不同的盘子,然后再给出每个盘子的初始位置和目标位置,要求计算出最少的步数使得每个盘子都移动到它的目标位置. 分析:  首先找最大不在目标柱子上的盘子K,因为如果最大的 ...

  4. raido 赋值第一次成功,然后就赋值不显示

    $("#id").attr("checked",true); //显示出现问题,第一次成功 $("#id").prop("chec ...

  5. Socket实现异步通信

    private static void RecVing(IAsyncResult Result) {     //通过 result 获取socket.在这个socket上你启动了BeginAccep ...

  6. python 批量爬取代理ip

    import urllib.request import re import time import random def getResponse(url): req = urllib.request ...

  7. 4-3 yum命令

    1.常用yum命令 <1>查询 yum list #查询所有可用软件包列表(以 包名 - 版本 - yum源所在名称 格式显示) yum search 关键字 #搜索服务器上所有和关键字相 ...

  8. 改造dede 后台会员目录

    可以在后台目录(默认 dede),里面的inc->inc_menu.php 文件里进行添加或更改,实现功能!

  9. Analyzing The Papers Behind Facebook's Computer Vision Approach

    Analyzing The Papers Behind Facebook's Computer Vision Approach Introduction You know that company c ...

  10. WaitAny, WaitAll 和 SignalAndWait

    除了Set 和 WaitOne方法外,在类WaitHandle中还有一些用来创建复杂的同步过程的静态方法. WaitAny, WaitAll 和 SignalAndWait使跨多个可能为不同类型的等待 ...