记录编号 154116 评测结果 AAAAAAAAAA
题目名称 [HNOI 2004] L语言 最终得分 100
用户昵称 Gravatarztx 是否通过 通过
代码语言 C++ 运行时间 0.779 s
提交时间 2015-03-21 17:14:58 内存使用 5.55 MiB
显示代码纯文本
/****************************************\
* Author : ztx
* Title  : [bzoj] 1212: [HNOI2004]L语言
* ALG    : Trie树
* CMT    :
* Time   :
\****************************************/

#include <cstdio>
#define Rep(i,l,r) for(i=(l);i<=(r);i++)
#define Rev(i,r,l) for(i=(r);i>=(l);i--)
int CH , NEG ;
template <typename TP>
inline void read(TP& ret) {
    ret = NEG = 0 ; while (CH=getchar() , CH<'!') ;
    if (CH == '-') NEG = true , CH = getchar() ;
    while (ret = ret*10+CH-'0' , CH=getchar() , CH>'!') ;
    if (NEG) ret = -ret ;
}
/*
template <typename TP>
inline void reads(TP& ret) {
	while (ret=getchar() , ret<'!') ;
	while (CH=getchar() , CH>'!') ;
}*/

#include <cstring>

#define  maxn  1100000LL

struct Trie {
	bool ok ;
	Trie *ch[26] ;
	Trie() { ok = false ; int i ; Rep (i,0,25) ch[i] = NULL ; }
} ;

int n , m , ans ;

int word[maxn] = {0} ;
bool f[maxn] = {0} ;

Trie *root = new Trie , *o ;

inline void reads() {
	word[0] = 0 ;
	while (CH=getchar() , CH<'!') ;
	while (word[++word[0]]=CH-'a' , CH=getchar() , CH>'!') ;
}

int main() {
int i , j ;
	#define READ
	#ifdef  READ
		freopen("language.in" ,"r",stdin ) ;
		freopen("language.out","w",stdout) ;
	#endif
	scanf("%d%d", &n , &m) ;
	Rep (i,1,n) {
		reads() ;
		o = root ;
		Rep (j,1,word[0]) {
			if (o->ch[word[j]] == NULL) o->ch[word[j]] = new Trie ;
			o = o->ch[word[j]] ;
			if (j == word[0]) o->ok = true ;
		}
	}
	while (m --> 0) {
		reads() ;
		f[0] = true ;
		Rep (i,0,word[0]) if (f[i]) {
			ans = i ;
			f[i] = false ;
			o = root ;
			Rep (j,i+1,word[0]) {
				if (o->ch[word[j]] == NULL) break ;
				o = o->ch[word[j]] ;
				if (o->ok) f[j] = true ;
			}
		}
		printf("%d\n", ans) ;
	}
	#ifdef  READ
		fclose(stdin) ; fclose(stdout) ;
	#else
		getchar() ; getchar() ;
	#endif
	return 0 ;
}