转载自 Livedream

YBT1396

#include<iostream>
#include<map>
#include<queue>
#include<vector>
using namespace std;
/*
无解的情况:
1. 能确定的字母种类少于目标单词的字母种类,比如能确定abc,但目标单词中含有e
2. 拓扑排序的结果不唯一,也就是每次发现的入度为0的结点不唯一
3. 拓扑排序有环
*/
string s[5005], t, std_s;
map<char, char> Map;
vector<char> v[125];
int in[125], tot;
bool vis[125];
char ans[27]; string get_dict() //获取图中的字符的字典序 字符串
{
string tmp = ""; //标准字典序串
for(char c = 'a'; c <= 'z'; c++)
{
if(vis[c] == true)
{
tmp += c;
}
}
return tmp; //标准字符串
} bool is_enough() //判断结点是否足够
{
for(int i = 0; i < t.size(); i++)
{
if(vis[t[i]] == false) //目标单词t中存在字符t[i],而建的图中没有
return false;
}
return true;
} bool topo_sort()
{
queue<char> q;
int cnt = 0;
for(char c = 'a'; c <= 'z'; c++)
{
if(vis[c] == true && in[c] == 0)
{
q.push(c);
cnt++;
if(cnt > 1)
return false;
}
}
while(q.empty() == false)
{
char cur = q.front();
q.pop();
ans[++tot] = cur; //存拓扑排序结果
int cnt = 0; //记录拆掉cur结点后有多少结点的入度减为0
for(int i = 0; i < v[cur].size(); i++)
{
char next = v[cur][i];
in[next]--;
if(in[next] == 0)
{
cnt++; //统计入度为0的点
q.push(next);
}
}
if(cnt > 1) //入度为0的点多于1个,无解
return false;
}
std_s = get_dict(); //获取图中的节点数
if(tot < std_s.size()) //有环
return false;
return true;
}
void print()
{
for(int i = 1; i <= tot; i++)
{
Map[ans[i]] = std_s[i-1]; //ans和std_s建立对应关系
}
for(int i = 0; i < t.size(); i++)
{
cout << Map[t[i]];
}
return ;
}
int main()
{
int k;
cin >> k;
for(int i = 1; i <= k; i++)
{
cin >> s[i];
if(i == 1)
continue;
for(int j = 0; j < min(s[i].size(), s[i-1].size()); j++)
{
char c1 = s[i-1][j], c2 = s[i][j];
if(c1 != c2)
{
v[c1].push_back(c2);
in[c2]++; //入度
vis[c1] = vis[c2] = true;
break; //只找第一对不相同的字符
}
}
}
cin >> t; //目标字符串
if(is_enough() == false || topo_sort() == false)
{
cout << 0;
return 0;
}
print();
return 0;
}

SRX_Test_2_key的更多相关文章

随机推荐

  1. router-link 使用精确匹配

    本来不想写router 规则匹配的问题,有一个笨球问,顺带写一下, 先配置一下路由 export default new Router({ routes: [ { path: '/', name: ' ...

  2. 支持向量机SVM基本问题

    1.SVM的原理是什么? SVM是一种二类分类模型.它的基本模型是在特征空间中寻找间隔最大化的分离超平面的线性分类器.(间隔最大是它有别于感知机) 试图寻找一个超平面来对样本分割,把样本中的正例和反例 ...

  3. redis client原理分析

    代码库地址:https://github.com/garyburd/redigo 1:连接池 2:发送命令 3:解析结果 1:连接池 连接池结构体如下: type Pool struct { // D ...

  4. EasyExcel使用心得

    最近项目中用到了阿里easyExcel做导入导出功能 下面是我写的一个工具类,带泛型的.拿来即用,有需求的小伙伴可以看看. 同时也欢迎大佬提出修改意见. 一.首先先写一个生成Excel表头的DTO类, ...

  5. Ubuntu Kylin 部署 .Net Core 应用程序

    前几日在头条上看到了 优麒麟(https://www.ubuntukylin.com/) ,出于好奇,就下载安装玩玩,整体感觉不错.当然这不是重点,重点是要在它上面部署的.Net Core 应用程序. ...

  6. Qt混合Python开发技术:Python介绍、混合过程和Demo

    前言   Qt中混合Python开发,可调用Python命令与脚本.   Python   Python是一种跨平台的计算机程序设计语言. 是一个高层次的结合了解释性.编译性.互动性和面向对象的脚本语 ...

  7. eclipse关于新建工程找不到二进制文件的解决方法

    eclipse新建工程后先构建项目 然后右键工程,选择属性,选择c/c++ Build,选择Tool chain editor.中间的Current Toolchain改为Mingw Gcc.然后选择 ...

  8. 5 Post和Get

    5 Post和Get GET和POST有什么区别?及为什么网上的多数答案都是错的 知乎回答 get: RFC 2616 - Hypertext Transfer Protocol -- HTTP/1. ...

  9. 《精通Spring4.x企业应用开发实战》第三章

    这一章节主要介绍SpringBoot的使用,也是学习的重点内容,之后就打算用SpringBoot来写后台,所以提前看一下还是很有必要的. 3.SpringBoot概况 3.1.1SpringBoot发 ...

  10. svn“Previous operation has not finished; run 'cleanup' if it was interrupted“报错的解决方案

    今天SVN提交代码遇到"Previous operation has not finished; run 'cleanup' if it was interrupted"报错,&q ...