Trie 树玄学假优化 90pts 求卡常
查看原帖
Trie 树玄学假优化 90pts 求卡常
422684
M1saka16I72楼主2024/4/1 22:27
#include <bits/stdc++.h>
#define pii pair<int,int>
#define pil pair<int,long long>
#define pll pair<long long,long long>
#define mp_ make_pair
#define pb_ push_back
#define pob_ pop_back
#define fst first
#define snd second
#define debug cout<<"********\n";
// #define reset(x,y) memset(x,y,sizeof(x))
#define fi(x) freopen(x,"r",stdin)
#define fo(x) freopen(x,"w",stdout)
#define iosf ios::sync_with_stdio(0);cin.tie(0);
#define prec(x) cout<<setprecision(x);
#define prec0(x) cout<<fixed<<setprecision(x);
#define s(x,y) x[y-1]
#define Misaka16172 sb
#define kamisako femboy

using ll = long long;
using ld = long double;
using db = double;
using ull = unsigned long long;

const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ull mod1 = 1e9+7,mod9 = 998244353;

using namespace std;

template <class T>
inline void read(T &x){
	x = 0;T w = 1;
	char c = 0;
	while(c<'0' || c>'9'){
		if(c=='-')	w = -1;
		c = getchar();
	}
	while(c>='0' && c<='9'){
		x = ((x<<3)+(x<<1))+c-'0';
		c = getchar();
	}
	x = x*w;
}

constexpr int L = 405,C = 26,T = 2e6+5;

struct Trie{
	int nxt[L*L*C][C],cnt = 0;
	bool exist[L*L*C];
	char ch[L*L*C];
	void insert(string s){
		int p = 0;
		for(auto c:s){
			if(!nxt[p][c-'a']){cnt++;ch[cnt] = c;nxt[p][c-'a'] = cnt;}
			p = nxt[p][c-'a'];
		}
		exist[p] = 1;
	}
}Trie1;

string text;
int maxd = 0;
int vis[T];

int n,m;
int c = 0;

void dfs(int p,int d){
	c++;
	if(c>T)	return;
	if(Trie1.exist[p]){
		maxd = max(maxd,d);
		if(Trie1.nxt[p][text[d]-'a'])	dfs(Trie1.nxt[p][text[d]-'a'],d+1);
		if(c>T/3 && d<text.length()/4)	return;
		p = 0;
	}
	if(d==text.length())	return;
	if(Trie1.nxt[p][text[d]-'a'])	dfs(Trie1.nxt[p][text[d]-'a'],d+1);
}

void solve(){
	// fi("P2292.in");
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		string s;cin>>s;
		Trie1.insert(s);
	}
	for(int i=1;i<=m;i++){
		maxd = 0;
		cin>>text;
		c = 0;
		dfs(0,0);
		cout<<maxd<<"\n";
	}
}

int main(){
    iosf
	int t = 1;
//	read(t);
	for(int tt=1;tt<=t;tt++){
		solve();
	}
    return 0;
}
2024/4/1 22:27
加载中...