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:

  1. You may assume all letters are in lowercase.
  2. If the order is invalid, return an empty string.
  3. 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*的更多相关文章

  1. [LeetCode] 269. Alien Dictionary 另类字典

    There is a new alien language which uses the latin alphabet. However, the order among letters are un ...

  2. [LeetCode] Alien Dictionary 另类字典

    There is a new alien language which uses the latin alphabet. However, the order among letters are un ...

  3. [LeetCode] 269. Alien Dictionary 外文字典

    There is a new alien language which uses the latin alphabet. However, the order among letters are un ...

  4. [leetcode]269. Alien Dictionary外星字典

    There is a new alien language which uses the latin alphabet. However, the order among letters are un ...

  5. 269. Alien Dictionary火星语字典(拓扑排序)

    [抄题]: There is a new alien language which uses the latin alphabet. However, the order among letters ...

  6. LeetCode 269. Alien Dictionary

    原题链接在这里:https://leetcode.com/problems/alien-dictionary/ 题目: There is a new alien language which uses ...

  7. 269. Alien Dictionary

    题目: There is a new alien language which uses the latin alphabet. However, the order among letters ar ...

  8. [Locked] Alien Dictionary

    Alien Dictionary There is a new alien language which uses the latin alphabet. However, the order amo ...

  9. 设计一个 硬件 实现的 Dictionary(字典)

    Dictionary 就是 字典, 是一种可以根据 Key 来 快速 查找 Value 的 数据结构 . 比如 我们在 C# 里用到的 Dictionary<T>, 在 程序设计 里, 字 ...

随机推荐

  1. Weblogic的架构

    WebLogic管理控制台  WebLogic服务器提供了一个健壮的基于Web 的工具——管理控制台,它是执行上述任务的主要工具.通过管理控制台,你可以访问WebLogic管理服务.管理服务实现了 S ...

  2. 【日常小记】linux中强大且常用命令:find、grep【转】

    转自:http://www.cnblogs.com/skynet/archive/2010/12/25/1916873.html 在linux下面工作,有些命令能够大大提高效率.本文就向大家介绍fin ...

  3. AC6102 开发板千兆以太网UDP传输实验

    AC6102 开发板千兆以太网UDP传输实验 在芯航线AC6102开发板上,设计了一路GMII接口的千兆以太网电路,通过该以太网电路,用户可以将FPGA采集或运算得到的数据传递给其他设备如PC或服务器 ...

  4. C语言中关于POW在不同状态下四舍五入的解决方法

    这是今天作业中的一个代码: #include <stdio.h>#include<math.h>int main(){ printf("请输入一个整数:") ...

  5. MySQL免安装数据库配置-Windows8.1

    1. 解压Mysql压缩包,将包中my-default.ini复制一份,改名为my.ini,添加一下配置信息到my.ini: [client] port=3306 default-character- ...

  6. 自定义一个可复用的BaseAdapter

    1.我们一点点开始改: 首先我们自定义BaseAdapter,等下我们就要对他进行升级改造 /** * Created by Jay on 2015/9/21 0021. */ public clas ...

  7. GDUFE-OJ 1359校庆素数 埃氏筛法

    Problem Description: 包含33的素数称为校庆素数. 她想知道在L和R之间(包含L和R)有多少个校庆素数. 比如 2333 就是一个校庆素数. Input: 输入的第一行包括一个T( ...

  8. Fragment全解析系列(一):那些年踩过的坑

    开始之前 最新版知乎,单Activity多Fragment的架构,响应可以说非常"丝滑",非要说缺点的话,就是没有转场动画,并且转场会有类似闪屏现象.我猜测可能和Fragment转 ...

  9. Radmin Server-3.5 完美绿色破解版(x32 x64通用) 第三版 + 单文件制作方法

    Radmin Server v3.5 汉化破解绿色版(x32 x64通用) 第三版 下载链接: https://pan.baidu.com/s/1qYVcSQo 2016年7月8日更新第三版1.修复在 ...

  10. ubuntu 14.04 vsftpd安装问题

    sudo apt-get install vsftpd; 打开允许访问用户名 local_enable=YES write_enable=YES chroot_list_file=/etc/vsftp ...