2019牛客暑期多校训练营(第五场) subsequence 2(topo)

链接:https://ac.nowcoder.com/acm/contest/885/H
来源:牛客网
 

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 262144K,其他语言524288K
Special Judge, 64bit IO Format: %lld

题目描述

There is a hidden string of length n composed of the first m lowercase English letters. For any two different English letters, we will tell you a subsequence of the hidden string constructed by removing all other letters from the hidden string.

For example, if the hidden string is "apple" and the chosen letters are 'e' and 'p', the resulting subsequence would be "ppe"; if the chosen letters are 'a' and 'x', the resulting subsequence would be "a".

Now, please recover the hidden string. Output -1 if there are no possible hidden string. Output any one if there are multiple possible hidden strings.

输入描述:

The first line contains two integers n and m.

Following are m⋅(m−1)/2m \cdot (m-1)/2m⋅(m−1)/2 groups of two lines. The first line in each group contains a two-letter string composed of c1, c2, and an integer LEN. The second line contains a string composed of letters c1, c2 with length LEN, indicates the resulting subsequence by removing all other letters except c1 and c2 from the hidden string.

* 1≤n≤1041 \le n \le 10^41≤n≤104

* 2≤m≤102 \le m \le 102≤m≤10

* 0≤LEN≤n0 \le LEN \le n0≤LEN≤n

* all unordered pairs of letters in first m small English letters will occur exactly once.

输出描述:

If there is at least one possible hidden string, please output any. Otherwise, output -1.

示例1

输入

复制

3 3
ab 2
ab
bc 2
bc
ca 2
ac

输出

复制

abc

说明

First group tells us that there is one 'a' and one 'b' where 'a' is before 'b', the second group tells us there is one 'b' and one 'c' where 'b' is before 'c'. So the only possible answer is "abc" which satisfies the last group as well.

示例2

输入

复制

3 3
cb 3
bbc
ca 2
ac
ab 3
abb

输出

复制

-1

示例3

输入

复制

4 3
ac 4
cccc
ba 0

cb 4
cccc

输出

复制

cccc

                     不说了~直接建图topo()~~!细节有什么问题主页QQ聊就好

 

#include <vector>
#include <stack>
#include <iostream>
#include <string>
#include <queue>
#include <cstring>
using namespace std;
const int maxn = 260000;
vector<int>G[maxn]; int  out[maxn];
int num[26];
int n, m;
string ans = "";
bool topo() {
	queue<int>q;
	for (int i = 0; i < 26; i++) 
		if (num[i] > 0 && out[i * 10000 + 1] == 0)
			q.push(i * 10000 + 1);
	while (!q.empty()) {
		int u = q.front(); q.pop();
		//cout << u << "\n";
		ans += ((u - 1) / 10000 + 'a');
		for (auto v : G[u]) {
			if (--out[v] == 0) {
				q.push(v);
			}
		}
	}
	return ans.size() == n;
}
int main() {
	memset(num, -1, sizeof num);
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	cin >> n >> m;
	int M = m * (m - 1) / 2;
	for (int i = 0; i < M; i++) {
		char a, b; int len;
		cin >> a >> b >> len;
		if (len == 0)continue;
		string tmp; cin >> tmp;
		int suma = 0, sumb = 0;
		int ori = -1;
		for (int i = 0; i < len; i++) {
			int id;
			if (tmp[i] == a) {
				++suma;
				id = (a - 'a') * 10000 + suma;
			}
			else {
				++sumb;
				id = (b - 'a') * 10000 + sumb;
			}
			if (ori != -1) {
				out[id]++;
				G[ori].push_back(id);
			}
			ori = id;
		}
		if (num[a - 'a'] == -1)num[a - 'a'] = suma;
		if (num[b - 'a'] == -1)num[b - 'a'] = sumb;
	}
	int allnum = 0;
	for (int i = 0; i < m; i++)allnum += num[i];
	if (allnum != n) {
		return 0 * puts("-1");
	}
	if (topo()) 
		cout << ans << "\n";
	else puts("-1");
	cin >> ans;
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值