HackerRank "No Prefix Set"
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"的更多相关文章
- Spring配置文件标签报错:The prefix "XXX" for element "XXX:XXX" is not bound. .
例如:The prefix "context" for element "context:annotation-config" is not bound. 这种 ...
- [LeetCode] Implement Trie (Prefix Tree) 实现字典树(前缀树)
Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...
- [LeetCode] Longest Common Prefix 最长共同前缀
Write a function to find the longest common prefix string amongst an array of strings. 这道题让我们求一系列字符串 ...
- 1014: [JSOI2008]火星人prefix
1014: [JSOI2008]火星人prefix Time Limit: 10 Sec Memory Limit: 162 MB Description 火星人最近研究了一种操作:求一个字串两个后缀 ...
- [BZOJ1014][JSOI2008]火星人prefix
[BZOJ1014][JSOI2008]火星人prefix 试题描述 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:madamimadam,我们将这个字符串的各个字 ...
- 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 ...
- 【leetcode】Longest Common Prefix
题目简述: Write a function to find the longest common prefix string amongst an array of strings. 解题思路: c ...
- LintCode 78:Longest Common Prefix
public class Solution { /** * @param strs: A list of strings * @return: The longest common prefix ...
- [LintCode] Longest Common Prefix 最长共同前缀
Given k strings, find the longest common prefix (LCP). Have you met this question in a real intervie ...
随机推荐
- 使用ingress qdisc和ifb进行qos
ifb The Intermediate Functional Block device is the successor to the IMQ iptables module that was ...
- 关于KVM的几篇细节文档
1. Qemu Study http://lists.gnu.org/archive/html/qemu-devel/2011-04/pdfhC5rVdz7U8.pdf http://handboo ...
- hdu 2795 线段树(二维问题一维化)
Billboard Time Limit: 20000/8000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- Apache使用mod_deflate模块压缩页面优化传输速度
可以写为一行,也可以写多行,默认为gzip SetOutputFilter DEFLATE # Restrict compression to these MIME types AddOutputFi ...
- px em rem在WEB前端开发中的区别
我们都知道基于像素的字体大小所用的单位是px,但是随着响应式设计的不断火热,基于相对字体大小的单位em变开始流行起来.当然,rem也在Web前端开发人员讨论如何更好设置字体大小的讨论话题之列.是不是需 ...
- js调用百度地图API创建地图
技术交流群:233513714 <html xmlns="http://www.w3.org/1999/xhtml"><head runat="serv ...
- 被IP代理网站屏蔽了,真是跪了
被IP代理网站http://www.xicidaili.com/nn/屏蔽了,真是跪了 T T
- Sublime Text3快捷方式与使用技巧
Sublime Text 3 快捷键精华版 Ctrl+Shift+P: 打开命令面板Ctrl+P: 搜索项目中的文件Ctrl+G: 跳转到第几行Ctrl+W: 关闭当前打开文件Ctrl+Shift ...
- 安卓应用运营知识:VersionCode和VersionName
本文转载于:http://blog.sina.com.cn/s/blog_580a227a0101bdzb.html 小米应用商店运营了大概半年了,过程中有喜有泪,我们在运营过程中,发现有的运营和商务 ...
- CLREX
KernelBase.dll!RaiseException() Unknown > coreclr.dll!`RaiseTheExceptionInternalOnly'::`81'::__Bo ...