Hackerrank (1): CodeStorm 2015 > Little Alexey's Tree
来源:互联网 发布:兰格钢铁 知乎 编辑:程序博客网 时间:2024/06/04 18:16
题目:
Problem Statement
Little Alexey was playing with trees while studying powerful tree algorithms.
Recently, he discovered a tree with
If we concatenate all the letters on the path from vertex
For every vertex
Input Format
On the first line, you are given a single positive integer
Constraints
Output Format
Print
Sample Input 1
41 2 a2 3 a3 4 b
Sample Output 1
4 4 4 1
Sample Input 2
91 2 b2 3 a2 4 c1 6 c6 5 d6 7 c1 9 d9 8 e
Sample Output 2
8 4 4 8 8 5 5 5 8
Explanation
Let's look at the first sample. A way between
题解:
通过了22个Case,还有36个Case超时,算法应该是对的。
C++版:
#include <cmath>#include <cstdio>#include <vector>#include <iostream>#include <algorithm>#include <unordered_map>#include <set>#include <stack>using namespace std;struct comparator { bool operator() (const pair<int, char>& a, const pair<int, char>& b) { return a.second >= b.second; }};int main() { /* Enter your code here. Read input from STDIN. Print output to STDOUT */ int numberOfLines; cin >> numberOfLines; if(numberOfLines == 1) { cout << 1 << endl; return 0; } unordered_map<int, set<pair<int, char>, comparator>> tree; for(int i = 0; i < numberOfLines - 1; i++) { int start, end; char c; cin >> start >> end >> c; if(tree.find(start) == tree.end()) { set<pair<int, char>, comparator> edges; edges.insert(pair<int, char>(end, c)); tree.insert(pair<int, set<pair<int, char>, comparator>>(start, edges)); } else { set<pair<int, char>, comparator> s = tree[start]; s.insert(pair<int, char>(end, c)); tree[start].insert(pair<int, char>(end, c)); } if(tree.find(end) == tree.end()) { set<pair<int, char>, comparator> edges; edges.insert(pair<int, char>(start, c)); tree.insert(pair<int, set<pair<int, char>, comparator>>(end, edges)); } else { tree[end].insert(pair<int, char>(start, c)); } } for(int i = 1; i <= numberOfLines; i++) { vector<pair<string, int>> results; stack<pair<pair<int, int>, string>> path; auto iter = tree[i].begin(); string s = ""; char c = iter->second; s += c; do { pair<int, int> p(iter->first, i); path.push(pair<pair<int, int>, string>(p, s)); iter++; } while (iter != tree[i].end() && iter->second == c); while(!path.empty()) { int cur = path.top().first.first; int last = path.top().first.second; string curS = path.top().second; path.pop(); auto iter2 = tree[cur].begin(); if(iter2->first == last) iter2++; if(iter2 == tree[cur].end()) { results.push_back(pair<string, int>(curS, cur)); continue; } char c1 = iter2->second; curS += c1; for(iter2; iter2 != tree[cur].end() && iter2->second == c1; iter2++) { if(iter2->first == last) continue; pair<int, int> p(iter2->first, cur); path.push(pair<pair<int, int>, string>(p, curS)); } } string largestS = ""; int largestI = 0; for(int j = 0; j < results.size(); j++) { if(results[j].first == largestS) { largestI = max(largestI, results[j].second); } else if(results[j].first > largestS) { largestS = results[j].first; largestI = results[j].second; } } cout << largestI << " "; } return 0;}
- Hackerrank (1): CodeStorm 2015 > Little Alexey's Tree
- 【HackerRank】【HourRank 20】Birjik and Nicole's Tree Game
- hihocoder 1381 Little Y's Tree
- Hackerrank-Data structures-Tree
- Hackerrank Fibonacci Numbers Tree
- HackerRank-Tree: Inorder Traversal
- hackerrank Find Digits && Upotain tree
- Cut the tree _ HackerRank
- [hackerrank]Counting on a tree
- HackerRank-Tree: Level Order Traversal
- Hackerrank Recurrent on a Tree
- hackerrank Queen's Attack II
- Little Boy's Problem
- Little's Law
- HackerRank Self Balancing Tree(AVL树)
- [HackerRank University CodeSprint]Counting On a Tree
- HackerRank Even Tree(树dp)
- HackerRank - number-game-on-a-tree
- 反射
- 2015.10.31
- UVa 1400"Ray, Pass me the dishes!"(区间最大连续数组和)
- 数据库批量操作中SqlParameter参数传递的问题
- 一段时间的javascript学习回顾------小模块汇总
- Hackerrank (1): CodeStorm 2015 > Little Alexey's Tree
- 游戏贴图打包工具 - TexturePacker
- 别让大学对不起高三
- Android中listview条目中按钮点击事件的实现
- 隐式图--UVA - 10603 Fill
- Summary Ranges
- 一些课后实践about java
- 以 dpdk应用层包处理程序为例 谈谈 多进程和多线程模型选择时的若干考虑
- 软件版本