Codeforces 1476E E. Pattern Matching(状态压缩+拓扑序列)

题目链接:E. Pattern Matching

题意:给定n个模式串和m个字符串,对于字符串的每一位都,如果模式串为’_‘或和字符串上字母相同,则称该字符串可匹配该模式串,对于每个字符串给定一个mt,要求将模式串重新排序,使得对于每个字符串,从前往后第一个能匹配的模式串为原模式串中下标为mt的模式串

题解:我们可以状态压缩枚举每个字符串对应可行的匹配串,我们可以发现,如果某个字符串和下标为mt的模式串若不匹配那么一定无解,对于可匹配的串,将mt向他们建边,然后跑拓扑序,这样就能保证每个字符串对应mt都排在其他可行串的前面,需要注意的是若跑出的拓扑序大小不为n说明有环即不合题意输出NO

代码注释很详细,不懂可以看代码注释:

#include<iostream>
#include<stack>
#include<list>
#include<set>
#include<vector>
#include<algorithm>
#include<math.h>
#include<numeric>
#include<map>
#include<cstring>
#include<queue>
#include<iomanip>
#include<cmath>
#include<queue>
#include <bitset>
#include<unordered_map>
	#ifndef local
	#define endl '\n'
#endif 
#define mkp make_pair
using namespace std;
using std::bitset;
typedef long long ll;
typedef long double ld;
const int inf=0x3f3f3f3f;
const ll MAXN=2e6+10;
const ll N=2e5+100;
const ll mod=1e9+7;
const ll hash_p1=1610612741;
const ll hash_p2=805306457;
const ll hash_p3=402653189;
//-----------------------------------------------------------------------------------------------------------------*/
// ll head[MAXN],net[MAXN],to[MAXN],edge[MAXN]/*流量*/,cost[MAXN]//费用;
/* 
void add(ll u,ll v,ll w,ll s){
	to[++cnt]=v;net[cnt]=head[u];edge[cnt]=w;cost[cnt]=s;head[u]=cnt;
	to[++cnt]=u;net[cnt]=head[v];edge[cnt]=0;cost[cnt]=-s;head[v]=cnt;
}
struct elemt{
	int p,v;
};
-----------------------------------
求[1,MAXN]组合式和逆元 
ll mi(ll a,ll b){
	ll res=1;
	while(b){
		if(b%2){
			res=res*a%mod;
		}	
		a=a*a%mod;
		b/=2;
	}
	return res;
}
ll fac[MAXN+10],inv[MAXN+10]
ll C(int m,int n){//组合式C(m,n); 
	if(!n){
		return 1;
	}
	return fac[m]*(inv[n]*inv[m-n]%mod)%mod;
}
fac[0]=1;inv[0]=1;
for(ll i=1;i<=MAXN;i++){
	fac[i]=(fac[i-1]*i)%mod;
	inv[i]=mi(fac[i],mod-2);
}
---------------------------------
 unordered_map<int,int>mp;
//优先队列默认小顶堆 , greater<int> --小顶堆  less<int> --大顶堆  
priority_queue<elemt,vector<elemt>,comp>q;
struct comp{
	public:
		bool operator()(elemt v1,elemt v2){
			return v1.v<v2.v;
		}
};
	set<int>::iterator it=st.begin();
*/
//emplace_back()  等于push_back(),但效率更高,传输pair时emplace_back(i,j)==push_back({i,j}) 
// vector<vector<int>>edge; 二维虚拟储存坐标 
//-----------------------------------------------------------------------------------------------------------------*/
  map<string,int>mp; 
  //emplace_back()
vector<int>edge[N];
int n,m,k;
int d[N];//拓扑中记录入度数
void add(int u,int v){
	d[v]++;
	edge[u].emplace_back(v);
}
vector<int>ans;
void tuopu(){//输出拓扑序(拓扑序:无环时保证每个u->v的边中u排在v前面)
	queue<int>q;
	for(int i=1;i<=n;i++){
		if(!d[i]){
			q.push(i);
		}
	}
	while(q.size()){
		int f=q.front();
		q.pop();
		ans.emplace_back(f);//将路径存到ans中
		for(int i=0;i<edge[f].size();i++){
			int tmp=edge[f][i];
			d[tmp]--;
			if(!d[tmp]){
				q.push(tmp);
			}
		}
	}
}
int main(){
/*cout<<setiosflags(ios::fixed)<<setprecision(8)<<ans<<endl;//输出ans(float)格式控制为8位小数(不含整数部分)*/
/*cout<<setprecision(8)<<ans<<endl;//输出ans(float)格式控制为8位小数(含整数部分)*/
	ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);//同步流
	cin>>n>>m>>k;
	for(int i=1;i<=n;i++){
		string s;
		cin>>s;
		mp[s]=i;
	}
	for(int i=1;i<=m;i++){
		string s;
		int mt;
		bool f=0;//判断可匹配的串中有无下标为mt的
		cin>>s>>mt;
		for(int i=0;i<(1<<k);i++){//状压,枚举可匹配的串
			string ss=s;
			for(int j=0;j<k;j++){
				if(i&(1<<j)){
					ss[j]='_';
				}
			}
			if(!mp.count(ss)){
				continue;
			}
			else{
				int tmp=mp[ss];
				if(tmp==mt){
					f=1;
				}
				else{
					add(mt,tmp);//因为要求新路径中mt排在最前面,所以拓扑建边是把排在mt后面的边都指向mt,
					//这样就能保证输出的拓扑序中mt始终是先输出的
				}
			}
		}
		if(!f){//可匹配的串中无下标为mt的,不合题意直接结束
			cout<<"NO"<<endl;
			return 0;
		}
	}
	tuopu();//输出拓扑路径
	if(ans.size()==n){
		cout<<"YES"<<endl;
		for(int i=0;i<ans.size();i++){
			cout<<ans[i]<<" ";
		}
	}
	else{//拓扑序大小不为n,说明存在环,即不合法
		cout<<"NO"<<endl;
	}
	return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值