记录编号 553793 评测结果 AAAAAAAAAA
题目名称 [Youdao2010] 有道搜索框 最终得分 100
用户昵称 Gravatarfsdh 是否通过 通过
代码语言 C++ 运行时间 0.278 s
提交时间 2020-08-25 21:56:52 内存使用 14.36 MiB
显示代码纯文本
#include <cstdio>
#include <cstring>
#include <vector>
#include <iostream>
#include <queue>
#include <algorithm>
using namespace std;
const int MAXN = 10010;
char s1[MAXN][41];
priority_queue <int ,vector <int > ,greater <int > > qu;
int sum ;
class node {
	public :
	//	vector <int > c;
		int cnt;
		node *next[26];
	node () {
		cnt = 0;
		memset (next ,0 ,sizeof (next));
	}
};
queue <node * > que;
void insert (node *p ,char *s ,int num) {
	int len = strlen (s);
	for (int q = 0;q < len;++ q) {
		int x = s[q] - 'a';
		if (p -> next[x] == NULL) p -> next[x] = new node ();
	//	p -> c.push_back (num);
		p = p -> next[x];
	}
	p -> cnt = num;
	return ;
}
void dfs (node *p) {
	if (p -> cnt != 0) {
//		printf ("%d\n",p -> cnt);
		sum ++;
		if (sum > 8) return ;
		cout << s1[p -> cnt] << ' ';
		qu.push(p -> cnt);
	}
	for (int q = 0;q < 26;++ q) {
		if (p -> next[q] == NULL) continue ;
		dfs (p -> next[q]);
	}
}
bool query (node *p ,char *s) {
	int len = strlen (s);
	for (int q = 0;q < len;++ q) {
		int x = s[q] - 'a';
		if (p -> next[x] == NULL) return false;
		p = p -> next[x];
	}
	dfs (p);
//			if (p -> cnt != 0) {抱歉啊 BFS 爆零啊 
//		//		printf ("%d\n",p -> cnt);
//				cout << s1[p -> cnt] << ' ';
//			}
//			for (int q = 0;q < 26;++ q) {
//				if (p -> next[q] == NULL) continue ;
//				que.push(p -> next[q]);
//			}
//			while (!que.empty ()) {
//				p = que.front ();
//				que.pop ();
//				if (p -> cnt != 0) {
//		//			printf ("%d\n",p -> cnt);
//					cout << s1[p -> cnt] << ' ';
//				}
//				for (int q = 0;q < 26;++ q) {
//					if (p -> next[q] == NULL) continue ;
//					que.push(p -> next[q]);
//				}
//			}
//			cout <<endl;
//	for (int q = 0;q < p -> c.size ();++ q) {
//		cout << s[p -> c[q]] << ' ';
//	}
	return true;
}
node *root;
int main () {
	freopen ("youdao.in","r",stdin);
	freopen ("youdao.out","w",stdout);
	root = new node ();
	int n;
	
	char ch[41];
	scanf ("%d",&n);
	for (int q = 1;q <= n;++ q) {
		cin >> s1[q];
		insert (root , s1[q] ,q);
	}
	scanf ("%d",&n);
	while (n --) {
		scanf ("%s",ch);
		sum = 0; 
		if (!query (root ,ch)) {
			printf ("%s\n",ch);
		}else {
//			int sum = 0;
//			while (!qu.empty ()) {
//				sum ++;
//				if (sum <= 8)
//				cout << s1[qu.top ()] << ' ' ;
//				qu.pop ();
//			}
			printf ("\n");
		}
	}
	return 0;
}