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. 整理单机安装RabbitMQ,留着备用

    一.安装erlang 1.安装erlang依赖: yum install build-essential openssl openssl-devel unixODBC unixODBC-devel m ...

  2. 教你轻松计算AOE网关键路径(转)

    原文链接:http://blog.csdn.net/wang379275614/article/details/13990163 本次结合系统分析师-运筹方法-网络规划技术-关键路径章节,对原文链接描 ...

  3. 复旦大学2014--2015学年第二学期(14级)高等代数II期末考试第八大题解答

    八.(本题10分)  设 $A,B$ 为 $n$ 阶半正定实对称阵, 求证: $AB$ 可对角化. 分析  证明分成两个步骤: 第一步, 将 $A,B$ 中的某一个简化为合同标准形来考虑问题, 这是矩 ...

  4. javax.crypto.BadPaddingException: Given final block not properly padded

    一.报错 写了一个加密方法,在Windows上运行没有问题,在Linux上运行时提示如下错误: javax.crypto.BadPaddingException: Given final block ...

  5. Toad for Oracle Format Option

    FmtPlus.opt [Qp5FormatterOptions]  Version=252  AssignFavorWrapping=0  AssignAlignment=2  CaseAllowH ...

  6. MySQL的数据类型

  7. Solr搭建大数据查询平台

    参考文章:http://www.freebuf.com/articles/database/100423.html 对上面链接的补充: solr-5.5.0版本已被删除,新url:http://mir ...

  8. Mifare系列3-卡的能源和数据传递(转)

    文/闫鑫原创转载请注明出处http://blog.csdn.net/yxstars/article/details/38080175 在MIFARE卡中,能量和数据通过天线传输,卡中天线为几匝线圈,直 ...

  9. Analyzer中进行货币转换

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  10. springmvc接收JSON类型的数据

    1.在使用AJAX传递JSON数据的时候要将contentType的类型设置为"application/json",否则的话会提示415错误 2.传递的data需要时JSON类型的 ...