(拓扑排序)cf Educational Codeforces Round 103 (Rated for Div. 2) E. Pattern Matching

题目:
https://codeforces.com/contest/1476/problem/E

You are given n patterns p1,p2,…,pn and m strings s1,s2,…,sm. Each pattern pi consists of k characters that are either lowercase Latin letters or wildcard characters (denoted by underscores). All patterns are pairwise distinct. Each string sj consists of k lowercase Latin letters.

A string a matches a pattern b if for each i from 1 to k either bi is a wildcard character or bi=ai.

You are asked to rearrange the patterns in such a way that the first pattern the j-th string matches is p[mtj]. You are allowed to leave the order of the patterns unchanged.

Can you perform such a rearrangement? If you can, then print any valid order.

Input

The first line contains three integers n, m and k (1≤n,m≤105, 1≤k≤4) — the number of patterns, the number of strings and the length of each pattern and string.

Each of the next n lines contains a pattern — k characters that are either lowercase Latin letters or underscores. All patterns are pairwise distinct.

Each of the next m lines contains a string — k lowercase Latin letters, and an integer mt (1≤mt≤n) — the index of the first pattern the corresponding string should match.

Output

Print “NO” if there is no way to rearrange the patterns in such a way that the first pattern that the j-th string matches is p[mtj].

Otherwise, print “YES” in the first line. The second line should contain n distinct integers from 1 to n — the order of the patterns. If there are multiple answers, print any of them.

Input

5 3 4
_b_d
__b_
aaaa
ab__
_bcd
abcd 4
abba 2
dbcd 5

Output

YES
3 2 4 5 1 

Input

1 1 3
__c
cba 1

Output

NO

Input

2 2 2
a_
_b
ab 1
ab 2

Output

NO

Note

The order of patterns after the rearrangement in the first example is the following:

aaaa
__b_
ab__
_bcd
_b_d 

Thus, the first string matches patterns ab__, bcd, b_d in that order, the first of them is ab, that is indeed p[4]. The second string matches b and ab_, the first of them is _b, that is p[2]. The last string matches _bcd and _b_d, the first of them is _bcd, that is p[5].

The answer to that test is not unique, other valid orders also exist.

In the second example cba doesn’t match __c, thus, no valid order exists.

In the third example the order (a_, _b) makes both strings match pattern 1 first and the order (b, a) makes both strings match pattern 2 first. Thus, there is no order that produces the result 1 and 2.

题意:
搞了很久才懂,给你n个模式串和m个字符串,要求你重新排序模式串,使得每个字符串从模式串1 到 模式串n 匹配,匹配到的第一个模式串的坐标是这个字符串的 mt

思路:拓扑排序 + 哈希
因为数据比较大,暴力模拟可能会TLE(两个for循环),因为字符串只有26个小写字母(abcd…yz)和下划线 _,所有我们可以把每个模式串哈希成一个27进制的整数(27以上的进制都可以),
如果输入的字符串与对应的模式串不匹配,输出NO
否则,把字符串中的字母换成 _ (一个有 2 ^ k 种可能),计算每种可能的哈希并查找对应的模式串,如果有就记录起来(这个比两个for循环快多了)
每个字符串查找完后就进行拓扑排序

代码

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int MAXN = 1e5 + 10;
const int MAXC = 5;
char pattern[MAXN][MAXC]; 
struct node{
	char s[MAXC];
	int mt;
}str[MAXN];
int idx[MAXN*10], ti[MAXN], ans[MAXN];   //每个模式串哈希后对应的坐标,每个字符串的入度,答案的顺序
vector<int> a[MAXN];  //记录每个字符串除了模式串mt 还有模式串与它成功匹配的坐标
int main(){
//    freopen("_in.txt", "r", stdin);
//    freopen("_out1.txt", "w", stdout);
	int n, m, k, final, cnt;
	bool ac = true;
	bool pipei;
	scanf("%d%d%d", &n, &m, &k);
	for (int i = 1; i <= n; i++){
		scanf(" %s", pattern[i]);
		cnt = 0;
		for (int j = 0; j < k; j++){
			cnt *= 27;
			if (pattern[i][j] == '_')
				cnt += 26;
			else
				cnt = cnt + pattern[i][j] - 'a';
		}
		idx[cnt] = i;
	}
	for (int i = 0; i < m; i++){
		scanf(" %s%d", str[i].s, &str[i].mt);
		for (int j = 0; j < k; j++)
			if (pattern[str[i].mt][j] == '_' || pattern[str[i].mt][j] == str[i].s[j])  //匹配 
				continue;
			else{
				ac = false;
				break;
			}
	}
	if (!ac){
		printf("NO");
		return 0;
	}
	final = 1 << k;
	for (int i = 0; i < m; i++){
		for (int j = 0; j < final; j++){  //把字符串s中的字母换成 _ 
			cnt = 0;
			for (int x = 0; x < k; x++){
				cnt *= 27;
				if (j >> x & 1)
					cnt += 26;
				else
					cnt = cnt + str[i].s[x] - 'a';
			}
			if (idx[cnt] != 0 && idx[cnt] != str[i].mt){  //如果成功匹配并且不是模式串mt 就记录下来
				a[str[i].mt].push_back(idx[cnt]);
				ti[idx[cnt]]++;
			}
		}
	}
	queue<int> q;  //拓扑排序
	cnt = 0;
	for (int i = 1; i <= n; i++)
		if (ti[i] == 0)  //入度为0的点进队
			q.push(i);
	while (!q.empty()){
		int temp = q.front();
		q.pop();
		ans[++cnt] = temp;
		int Size = a[temp].size();
		for (int i = 0; i < Size; i++)
			if (--ti[a[temp][i]] == 0)  //入度为0的点进队
				q.push(a[temp][i]);
	}
	if (cnt == n){
		printf("YES\n%d", ans[1]);
		for (int i = 2; i <= n; i++)
			printf(" %d", ans[i]);
	}
	else
		printf("NO");
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值