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 ...
随机推荐
- ReentrantLock获取锁方式解读(转)
原文地址:http://www.zhihu.com/question/36771163 (一) lock()方法获取锁.如果该锁没有被另一个线程保持,则获取该锁并立即返回,将锁的保持计数设置为 1.如 ...
- dedecms 按照栏目指定的id排序
方法: 1.打开include/taglib/channelartlist.lib.php,找到大约78行,把 代码如下(一定要注意表名一致): $dsql->SetQuery("SE ...
- (进阶篇)浅谈COOKIE和SESSION关系和区别
COOKIE介绍 cookie 常用于识别用户.cookie 是服务器留在用户计算机中的小文件.每当相同的计算机通过浏览器请求页面时,它同时会发送 cookie.通过 PHP,您能够创建并取回 coo ...
- ZOJ 1007 Numerical Summation of a Series
原题链接 题目大意:x的取值从0.000到2.000,输出每个x对应的y(x)的值 解法:参考了这篇日志http://www.cnblogs.com/godhand/archive/2010/04/2 ...
- JQuery onload、ready概念介绍及使用方法
页面加载完成有两种事件,一是ready,表示文档结构已经加载完成,onload,ready概念容易混淆,下面为大家详细介绍下 页面加载完成有两种事件,一是ready,表示文档结构已经加载完成(不包 ...
- iframe session过期跳转到登陆页面
在login.jsp中添加js: if(window !=top){ top.location.href=location.href; } <script type="text/jav ...
- iframe与frameset(转载)
frameset 在一个页面中设置一个或多个框架 不能嵌套在body标签里 frameset 它称为框架标记,是用来告知HTML文件是框架模式,并且设定可视窗口怎么分割 fram ...
- 第四课,T语言运算符(版本5.0)
TC综合开发工具里支持了丰富的运算符,这样也要求大家对运算符的知识必须了解清楚,否则出现错误还不知道问题所在下面就为大家说说运算符的优先级与各个运算符含义 注意: 优先级代表同一表达式中运算符的运算顺 ...
- c#部分---又见循环。。
小时候背的诗,现在竟然这样玩. 又是循环,可见其无处不在的特性.就像“春眠不觉晓,处处蚊子咬.”
- 最小二乘法 java
import java.util.ArrayList; import java.util.Collection; import org.apache.commons.math3.optim.Point ...