zoukankan      html  css  js  c++  java
  • codeforces 1476 E. Pattern Matching (trie + 拓扑排序)

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

    题目大意:

    给定 (n) 个模式串和 (m) 个匹配串,其中模式串由小写字母和通配符'_'组成,匹配串由小写字母组成,
    同时,为每个匹配串指定一个(mj), 要求重新排列模式串顺序以后,按顺序匹配时,匹配串第一个匹配到的模式串为原顺序中的第 (mj) 个模式串,
    求重新排列后的顺序

    题解:

    编号为 (mj) 的模式串要在其他能匹配到的模式串前面,就形成了偏序关系,所以考虑拓扑排序,从 (mj) 向该字符串能匹配到的其他模式串连有向边即可
    最后,如果有环,则不存在方案
    同时,如果字符串和 (mj) 无法匹配也不存在方案

    #include<bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    
    const int maxn = 100010;
    
    int n, m, k, rt = 0, tot = 0;
    int a[maxn], d[maxn];
    vector<int> topo;
    char p[maxn][5], s[maxn][5];
    
    vector<int> g[maxn];
    
    struct Trie{
    	int son[30];
    	int val;
    	
    	Trie(){
    		memset(son, 0, sizeof(son));
    		val = -1;
    	}
    }t[maxn << 2];
    
    void insert(int x){
    	int cur = rt;
    	for(int i = 0 ; i < k ; ++i){
    		int c = p[x][i] - 'a';
    		if(p[x][i] == '_') c = 27;
    		if(!t[cur].son[c]){
    			t[cur].son[c] = ++tot;
    		}
    		cur = t[cur].son[c];
    	}
    	t[cur].val = x;
    }
    
    void query(int cur, int x, int dep){
    	if(t[cur].val != -1) {
    		if(a[x] != t[cur].val){
    			g[a[x]].push_back(t[cur].val);
    			++d[t[cur].val];
    		}
    		return;
    	}
    	int c = s[x][dep] - 'a';
    	if(t[cur].son[c]) {
    		query(t[cur].son[c], x, dep + 1);
    	}
    	if(t[cur].son[27]){
    		query(t[cur].son[27], x, dep + 1);
    	}
    	return;
    }
    
    queue<int> q;
    
    void topsort(){
    	while(!q.empty()){
    		int u = q.front(); q.pop();
    		for(auto i : g[u]){
    			int v = i;
    			--d[v];
    			if(!d[v]){
    				q.push(v); 
    				topo.push_back(v);
    			}
    		}
    	}
    } 
    
    ll read(){ ll s = 0, f = 1; char ch = getchar(); while(ch < '0' || ch > '9'){ if(ch == '-') f = -1; ch = getchar(); } while(ch >= '0' && ch <= '9'){ s = s * 10 + ch - '0'; ch = getchar(); } return s * f; }
    
    int main(){
    	n = read(), m = read(), k = read();
    	
    	for(int i = 1 ; i <= n ; ++i) scanf("%s", p[i]);
    	for(int i = 1 ; i <= m ; ++i) {
    		scanf("%s", s[i]);
    		a[i] = read();
    	}
    	
    	for(int i = 1 ; i <= n ; ++i) insert(i);
    	
    	for(int i = 1 ; i <= m ; ++i){
    		for(int j = 0 ; j < k ; ++j){
    			if(s[i][j] != p[a[i]][j] && p[a[i]][j] != '_'){
    				printf("NO
    ");
    				return 0;
    			}
    		}
    		query(rt, i, 0);
    	}
    	
    	for(int i = 1 ; i <= n ; ++i){
    		if(!d[i]) {
    			topo.push_back(i); 
    			q.push(i);
    		}
    	}
    	
    	topsort();
    	
    	if(topo.size() < n){
    		printf("NO
    ");
    	} else{
    		printf("YES
    ");
    		for(auto i : topo){
    			printf("%d ", i);
    		} printf("
    ");
    	}
    	
    	return 0;
    }
    
  • 相关阅读:
    技术收集
    Entity Framework的扩展库
    暂时收集
    php 处理高并发的思路
    nginx缓存优先级(缓存问题者必看)
    mysql5.5主从配置
    php源码编译常见错误解决方案
    今天开始要改变模式了
    nrpe 在ubuntu上安装遇到的问题
    zendstudio 10下载汉化
  • 原文地址:https://www.cnblogs.com/tuchen/p/14359326.html
Copyright © 2011-2022 走看看