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>, 在 程序设计 里, 字 ...
随机推荐
- The Number Off of FFF
X soldiers from the famous “FFF army'' is standing in a line, from left to right. You, as the captai ...
- php编译安装报错:make: *** [sapi/cli/php] Error 1 解决办法
ext/iconv/.libs/iconv.o: In function `php_iconv_stream_filter_ctor':/ext/iconv/iconv.c:2491: undefin ...
- Life is short
相信不少码农曾看过类似“life is short, use Python”等之类略带调侃意味的小段子(譬如我),而其也并非不无道理.每门编程语言都是合理的存在,都有它们的优点,及缺陷. 码农们也大多 ...
- python utf-8 配置
环境:centos6.5,python 2.6 源码文档使用utf-8 #!/usr/bin/python # -*- coding: UTF-8 -*- 字符串默认用utf-8(不用在前面加u了) ...
- table表格,让thead固定,tbody有滚动条,关键是都对齐的纯css写法。
找了好久才找到一篇可以简单粗暴就能用的,所以拿过来算是收藏了.里面有一个css2里的命令是我没用过的也是这里面关键的:table-layout:fixed; 原理很简单,有爱研究的童鞋可以去css官网 ...
- Command调用存储过程小实例
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx. ...
- 。Java中的一些小细节
1.main方法. ------任何一个Java程序都有一个main方法,它是程序的入口. ------当执行 “ java + 类名 “ 这个命令时,JVM就会去加载这个类,并且寻找这个类中的m ...
- 28.zookeeper单机(Standalones模式)和集群搭建笔记
zookeeper单机(Standalones模式)和集群搭建: 前奏: (1).zookeeper也可以在windows下使用,和linux一样可以单机也可以集群,具体就是解压zookeeper-3 ...
- 代码性能优化——task
var t1 = Task.Factory.StartNew(delegate { //代码(查接口.数据库) }); 缺点: 不能使用request( HttpContext.Current.Req ...
- dpkg: 处理归档 /var/cache/apt/archives/软件名 (--unpack)时出错:由于已经达到 MaxReports 限制,没有写入 apport 报告。
一.环境介绍: OS:ubuntu16.04 64bit 二.错误如下: 正准备解包 .../libqt4-script_4%3a4.8.7+dfsg-5ubuntu2_i386.deb ...正在 ...