269. Alien Dictionary 另类字典 *HARD*
There is a new alien language which uses the latin alphabet. However, the order among letters are unknown to you. You receive a list of words from the dictionary, where words are sorted lexicographically by the rules of this new language. Derive the order of letters in this language.
For example,
Given the following words in dictionary,
[
"wrt",
"wrf",
"er",
"ett",
"rftt"
]
The correct order is: "wertf"
.
Note:
- You may assume all letters are in lowercase.
- If the order is invalid, return an empty string.
- There may be multiple valid order of letters, return any one of them is fine.
#include<iostream>
#include<string>
#include<vector>
#include<set>
#include<map>
using namespace std; string getOrder(vector<string>& v)
{
int n = v.size(), i, j;
if (n < )
return "";
set<char> st;
for (i = ; i < n; i++)
{
for (j = ; j < v[i].size(); j++)
st.insert(v[i][j]);
}
map<char, set<char>> mp;
for (set<char>::iterator it = st.begin(); it != st.end(); it++)
mp[*it] = set<char>();
string ans = "";
for (i = ; i < n; i++)
{
for (j = ; j < v[i].size() && j < v[i - ].size(); j++)
{
if (v[i][j] != v[i - ][j])
{
mp[v[i][j]].insert(v[i - ][j]);
break;
}
}
}
while (!mp.empty())
{
int num = ;
for (map<char, set<char>>::iterator it = mp.begin(); it != mp.end(); )
{
if ((it->second).empty())
{
char c = it->first;
ans += c;
mp.erase(it++);
num++;
for (map<char, set<char>>::iterator iter = mp.begin(); iter != mp.end(); iter++)
iter->second.erase(c);
}
else
it++;
}
if ( == num)
return "";
}
return ans;
} int main()
{
vector<string> v;
v.push_back("w");
v.push_back("t");
v.push_back("e");
v.push_back("r");
v.push_back("rf");
cout << getOrder(v) << endl;
return ;
}
st:所有出现的字母的集合。
mp:每个字母有一个set,为在这个字母之前的字母的集合。
拓扑排序。
269. Alien Dictionary 另类字典 *HARD*的更多相关文章
- [LeetCode] 269. Alien Dictionary 另类字典
There is a new alien language which uses the latin alphabet. However, the order among letters are un ...
- [LeetCode] Alien Dictionary 另类字典
There is a new alien language which uses the latin alphabet. However, the order among letters are un ...
- [LeetCode] 269. Alien Dictionary 外文字典
There is a new alien language which uses the latin alphabet. However, the order among letters are un ...
- [leetcode]269. Alien Dictionary外星字典
There is a new alien language which uses the latin alphabet. However, the order among letters are un ...
- 269. Alien Dictionary火星语字典(拓扑排序)
[抄题]: There is a new alien language which uses the latin alphabet. However, the order among letters ...
- LeetCode 269. Alien Dictionary
原题链接在这里:https://leetcode.com/problems/alien-dictionary/ 题目: There is a new alien language which uses ...
- 269. Alien Dictionary
题目: There is a new alien language which uses the latin alphabet. However, the order among letters ar ...
- [Locked] Alien Dictionary
Alien Dictionary There is a new alien language which uses the latin alphabet. However, the order amo ...
- 设计一个 硬件 实现的 Dictionary(字典)
Dictionary 就是 字典, 是一种可以根据 Key 来 快速 查找 Value 的 数据结构 . 比如 我们在 C# 里用到的 Dictionary<T>, 在 程序设计 里, 字 ...
随机推荐
- Linux下如何查看高CPU占用率线程
转于:http://www.cnblogs.com/lidabo/p/4738113.html 目录(?)[-] proc文件系统 proccpuinfo文件 procstat文件 procpidst ...
- angular.extend()和 angular.copy()的区别
1.angular.copy angular.copy(source, [destination]); // source: copy的对象. 可以使任意类型, 包括null和undefined. ...
- TRUNCATE TABLE (Transact-SQL)
删除表中的所有行,而不记录单个行删除操作. TRUNCATE TABLE 与没有 WHERE 子句的 DELETE 语句类似:但是,TRUNCATE TABLE 速度更快,使用的系统资源和事务日志资源 ...
- [LeetCode_3] Longest Substring Without Repeating Characters
LeetCode: 3. Longest Substring Without Repeating Characters class Solution { public: int lengthOfLon ...
- Tips collection of iOS development
<转>UITableView当数据很少的时候,去掉多余的cell分割线 在tableView初始化的时候 UIView *v = [[UIViewalloc] initWithFram ...
- 20161106PM-接口
使用网址:http://apistore.baidu.com apikey:1f1d014aee0adeddbe33a6e1f55f7925 Composer GET http://apis.baid ...
- RabbitMQ 开启WEB管理
rabbitmq-plugins 插件管理器 1.开启rabbitmq management - WEB管理插件 # rabbitmq-plugins enable rabbitmq_manage ...
- 未解决的问题,登录163邮箱http://mail.163.com/,用xpath的方式定位密码输入框的时候,总是报找不到该元素
退出的时候出现: xpath定位方法: 注意xpath路径写的太长,如果层级全部写完定位不到,就尝试去掉一些层级
- my.cnf
skip-external-locking skip-name-resolve back_log= key_buffer_size=384M max_allowed_packet=4M thread_ ...
- Openfire Strophe开发中文乱码问题
网站上有很多Openfire Web方案,之前想用Smack 但是jar包支持客户端版本的,还有JDK版本问题 一直没调试成功 估计成功的方法只能拜读源码进行修改了. SparkWeb 官网代码很 ...