E. Pattern Matching
首先 p [ m t j ] p[mt_j] p[mtj]必须能够匹配所给字符 s j s_j sj,然后把所有能够匹配的 s j s_j sj的其他模板串也找出来,这些必须放在 p [ m t j ] p[mt_j] p[mtj]的后面,典型拓扑排序,连边然后排序即可
#define IO ios::sync_with_stdio(false);cin.tie();cout.tie(0)
#pragma GCC optimize(2)
#include<set>
#include<map>
#include<cmath>
#include<stack>
#include<queue>
#include<bitset>
#include<random>
#include<bitset>
#include<string>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<unordered_map>
#include<unordered_set>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,int> pli;
typedef pair<int,int> pii;
//==========================================================
const int N=200010;
int n,m,k;
map<string,int> mp;
vector<int> E[N]; // 邻接表
int d[N];
int main()
{
//IO;
int T=1;
//cin>>T;
while(T--)
{
cin>>n>>m>>k;
for(int i=1;i<=n;i++)
{
string s;
cin>>s;
mp[s]=i;
}
bool ok=1;
for(int i=1;i<=m;i++)
{
string s;int pos;
cin>>s>>pos;
if(!ok) break;
bool flag=0;
for(int j=0;j<1<<k;j++)
{
string t(s);
for(int l=0;l<k;l++)
if(j>>l&1)
t[l]='_';
if(mp.count(t))
{
if(mp[t]!=pos)
E[pos].push_back(mp[t]),d[mp[t]]++;
else
flag=1;// 该位置必须能够匹配
}
}
if(!flag) ok=0;
}
if(ok)
{
queue<int> q;
for(int i=1;i<=n;i++)
if(!d[i]) q.push(i);
vector<int> ans;
while(q.size())
{
int t=q.front();q.pop();
ans.push_back(t);
for(auto v:E[t])
if(--d[v]==0) q.push(v);
}
if(ans.size()==n)
{
cout<<"YES\n";
for(auto t:ans) cout<<t<<' ';
}
else
cout<<"NO\n";
}
else
cout<<"NO\n";
}
return 0;
}
心路历程:
刚开始理解成把原匹配串重排列,然后一一对应
字符串匹配?直接暴力枚举最多
2
4
=
16
2^4=16
24=16匹配,然后二分匹配随便搞搞?真的语文水平。。。写完发现题意中有一个first匹配,然后发现理解错了,然后······重看一遍又写发现又看错了,然后看了看网上题意解释