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>, 在 程序设计 里, 字 ...
随机推荐
- java 创建string对象机制 字符串缓冲池 字符串拼接机制
对于创建String对象的机制,在这一过程中涉及的东西还是值得探究一番的. 首先看通过new String对象和直接赋值的方式有什么区别,看如下代码: public static void main( ...
- json,pickle
json 将python基本数据类型转换成字符串形式 import json dict = {'k1':'v1'} result = json.dumps(dict) print(result,ty ...
- 逗比的wifi开关
笔记本会出现网卡开机不能用的现象.具体表现为:网卡没有禁用,但是搜索不到无线信号.适配器选项框里面选中无线网卡,然后诊断这个链接提示启用无线功能.然后我点击应用此修复就能搜索到无线信号了.问题是,电脑 ...
- IOS UITableview代理方法总结
tableview的datasource代理 @required的两个数据源方法 1.返回每个 session 中 cell 的个数 - (NSInteger)tableView:(UITableVi ...
- WebStorm 11、PhpStorm 10免费激活(不需要注册码)
之前分享的WebStorm9.WebStrom10.PhpStorm9注册码生成网站已经被站长关了,比较遗憾!http://my.oschina.net/ximidao/blog/486353 现在官 ...
- [转载] SQL获取所有数据库名、表名、储存过程以及参数列表
查询一个数据库中所有表字段属性的sql语句 1.获取所有用户名: SELECT name FROM Sysusers where status='2' and islogin='1' is ...
- MySQL为什么需要一个主键
主键 表中每一行都应该有可以唯一标识自己的一列(或一组列). 一个顾客可以使用顾客编号列,而订单可以使用订单ID,雇员可以使用雇员ID 或 雇员社会保险号. 主键(primary key) 一列(或一 ...
- easyUI 操作
<a href="javascript:void(0)" onclick="locationUrl()">点击 自动加链接</a> fu ...
- 一看便知_linux安装redis和调试
rpm包下载地址: http://vault.centos.org/6.3/os/x86_64/Packages/1.进入目录,解压文件 # tar -zxvf redis-3 ...
- yum安装nginx
1.在/etc/yum.repos.d/目录下创建一个源配置文件ngxin.repo: cd /etc/yum.repos.d/ vim nginx.repo 填写如下内容: [nginx] name ...