记录编号 350208 评测结果 AAAAAAAAAA
题目名称 情书 最终得分 100
用户昵称 GravatarPhosphorus15 是否通过 通过
代码语言 C++ 运行时间 3.421 s
提交时间 2016-11-15 16:57:43 内存使用 1.14 MiB
显示代码纯文本
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <map>

using std::cin;
using std::cout;
using std::endl;
using std::fill;
using std::vector;
using std::string;

struct Node{
	bool v;
	Node *ch[26],*fail;
	int n;
	Node():n(0),fail(NULL),v(false){for(int x=0;x!=26;x++) ch[x] = NULL;}
};

Node * root = new Node(),* superRoot = new Node();
Node * que[100000];

void init(){
	root->fail = superRoot;
	for(int x=0;x!=26;x++){
		superRoot->ch[x] = root;
	}
	superRoot->n = -1;
}

void insert(const char * s){
	Node * cr  = root;
	int ch = 0;
	while(*s!='\0'){
		ch = (*s) - 'a';
		if(cr->ch[ch]){
			cr = cr->ch[ch];
		}else{
			cr->ch[ch] = new Node();
			cr = cr->ch[ch];
		}
		s++;
	}
	cr->n++;
}

void build(){
	Node * cr;
	int p=0,q=0;
	que[q++] = root;
	while(p not_eq q){
		cr = que[p++];
		for(int x=0;x!=26;x++){
			//cout<<x<<endl;
			if(cr->ch[x]){
				cr->ch[x]->fail = cr->fail->ch[x];
				//cout<<"push into queue "<<cr->ch[x]<<endl;
				que[q++] = cr->ch[x];
			}else{
				cr->ch[x] = cr->fail->ch[x];
			}
		}
	}
}

int query(const char * s){
	Node * cr = root;
	int ans = 0;
	while(*s!='\0'){
		int ch = (*s) - 'a';
		cr = cr->ch[ch];
		for(Node * u = cr;u->n != -1 ;u = u->fail){
			if(!u->v)
				ans += u->n, u->v = true;
		}
		s++;
	}
	return ans;
}

int n;
string tmp;

int run(){
	freopen("lettera.in","r",stdin);
	freopen("lettera.out","w+",stdout);
	cin>>n;
	init();
	for(int x=0;x!=n;x++){
		cin>>tmp;
		insert(tmp.c_str());
	}
	build();
	while(cin>>tmp){
		tmp = tmp.substr(0,tmp.length()-1);
		int i = query(tmp.c_str());
		if(i>=n){
			printf("Yes\n");
		}else{
			printf("No\n");
		}
		for(int x=0;x!=100000;x++){
			if(que[x]){
				que[x]->v = false;
			}else
				break;
		}
	}
	fflush(stdout); // flush the buffer area
}

int rvalue(run());

int main(int argc,char ** argv){
	return 0;
}