>传送门<

题意:

给你几组样例,给你两个字符ab,一个长度len,一个长度为len的字符串strstr是字符串s的子串

strs删掉除过ab两字符剩下的子串,现在求s,多种情况输出一种。构造不出来输出-1

思路:

想都想不到的拓扑排序

因为这个str肯定是满足s顺序关系的s的子串,所以依次对str建图,又因为给了好几个子串所以全部建图,如果最后跑完拓扑,得到的字符串长度等于s长度即 可。

所以今后遇到求顺序一定的问题或图,哪怕是字符串都往拓扑排序靠

细节:对于这道题最重要的就是对每一个字符编号

  1. pos = (s[i]-'a')*10000+sum; sum为该字符出现的次数;
  2. ans += (u-1)/10000+'a'; u为编号; 即时出现cc这种情况,编号为20001,20002,但(u-1)/10000+'a结果都是等于c
  3. 强的一批的编号方式

 Code

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn = 1e6 + 5; int n, m, len;
char a, b;
int in[maxn], num[30]; //in表示入度, num表示各字母在原序列中出现的次数
vector<int> e[maxn]; //记录边
string s, ans;
//拓扑排序
bool topsort()
{
queue<int> q;
for(int i = 0; i < 26; i++){
//之所以只取相同字母的第一个,是因为在第一个后面出现的肯定会有入度
if(in[i*10000+1]==0&&num[i]!=0)
q.push(i*10000+1);
}
while(!q.empty()) {
int u=q.front(); q.pop();
ans += (u-1)/10000+'a';
for(int v = 0; v < e[u].size(); v++){
if(--in[e[u][v]]==0)
q.push(e[u][v]);
}
}
if(ans.size()==n) return true;
else return false;
}
int main()
{
cin >> n >> m;
for(int i = 0; i < (m-1)*m/2; i++) {
cin >> a >> b >> len;
if(len==0) continue;
cin >> s;
int pos, pre = -1, na = 0, nb = 0;
for(int i = 0; i < len; i++) {
if(s[i]==a) pos = (s[i]-'a')*10000 + (++na); //对相应的点进行编号
else pos = (s[i]-'a')*10000 + (++nb);
if(pre==-1) pre = pos;
else {
e[pre].push_back(pos); //添加边(即使是重边也没有关系)
in[pos]++;
pre=pos;
}
}
num[a-'a'] = na, num[b-'a'] = nb; //用来记录a, b是否原在序列中出现过
} if(topsort()) cout<<ans<<endl;
else cout<<-1<<endl;
return 0;
}

最新文章

  1. 转载文章(Redis中对key的操作)
  2. Codeforces#262_1002
  3. UVA567
  4. 在Windows和UNIX下利用PHP和LDAP进行身份验证
  5. Map.putAll方法——追加另一个Map对象到当前Map集合(转)
  6. 读&amp;lt;大数据日知录:架构与算法&amp;gt;有感
  7. 问题:Bringing up interface eth0: Device eth0 does not seem to be present,delaying initialization. [FAILED]—— 找不到网卡。
  8. javascript判断浏览器
  9. jquery的节点查询
  10. iOS图片设置圆角
  11. Java之IO流概述和File基本操作
  12. 【RL-TCPnet网络教程】第29章 NTP网络时间协议基础知识
  13. LNMP的配置与优化
  14. ELK安装(ubuntu)
  15. Linux下使用vim命令编辑与修改文本内容
  16. 第8章 传输层(1)_TCP/UDP协议的应用场景
  17. leetcode916
  18. Python爬虫利器:BeautifulSoup库
  19. RabbitMQ入门介绍
  20. Linux 定时器应用【转】

热门文章

  1. shell实现99乘法表
  2. 2021升级版微服务教程7-OpenFeign实战开发和参数调优
  3. vagrant up报错【io.rb:32:in `encode&#39;: &quot;\x95&quot; followed by &quot;\&quot;&quot; on GBK (Encoding::InvalidByteSequenceError)】
  4. Java中的基本数据类型与引用数据类型
  5. [usaco2008 Oct]Pasture Walking 牧场旅行
  6. ElasticSearch Python 基本操作
  7. Eureka详解系列(二)--如何使用Eureka(原生API,无Spring)
  8. MapReduce过程源码分析
  9. 使用Linux服务器来通过网络安装和激活Windows 7 —— 一些基本原理
  10. HTTPS学习(二):原理与实践