hdu 2846 简单字典树


题意:给出一些短文,然后一些个搜索串,问有多少个文章包含这个搜索串


解法:     字典树,对于长度为n的短文,要向字典树插入n次,第一次为[1,n],第二次[2,n],...第n次[n,n]。

每个结点要标记有多少短文经过此结点(用一个标记记录上一次的短文)。


Repository

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 5419    Accepted Submission(s): 1819


Problem Description
When you go shopping, you can search in repository for avalible merchandises by the computers and internet. First you give the search system a name about something, then the system responds with the results. Now you are given a lot merchandise names in repository and some queries, and required to simulate the process.
 

Input
There is only one case. First there is an integer P (1<=P<=10000)representing the number of the merchanidse names in the repository. The next P lines each contain a string (it's length isn't beyond 20,and all the letters are lowercase).Then there is an integer Q(1<=Q<=100000) representing the number of the queries. The next Q lines each contains a string(the same limitation as foregoing descriptions) as the searching condition.
 

Output
For each query, you just output the number of the merchandises, whose names contain the search string as their substrings.
 

Sample Input
  
  
20 ad ae af ag ah ai aj ak al ads add ade adf adg adh adi adj adk adl aes 5 b a d ad s
 

Sample Output
  
  
0 20 11 11 2
 

Source
 

Recommend
gaojie   |   We have carefully selected several similar problems for you:   2852  2847  2845  2850  2851 




#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;

#define all(x) (x).begin(), (x).end()
#define for0(a, n) for (int (a) = 0; (a) < (n); (a)++)
#define for1(a, n) for (int (a) = 1; (a) <= (n); (a)++)
#define mes(a,x,s)  memset(a,x,(s)*sizeof a[0])
#define mem(a,x)  memset(a,x,sizeof a)
#define ysk(x)  (1<<(x))
typedef long long ll;
//const int INF = 0x3f3f3f3f ;
const int maxn=  10000;
const int maxm=  100000;

int n,m;
char word[25];

struct Node
{
	int nex[26],cnt,last;
	void clear()
	{
		mes(nex,-1,26);
		cnt=0;
		last=-1;
	}


}nodes[3000000];
struct Tree
{
	int root,nnode;

	int newNode()
	{
		nodes[nnode++].clear();
		return nnode-1;
	}
	void init()
	{
		nnode=0;
		root=newNode();
	}

	void insert(char *s,int ind)
	{
		int now=root;
		for(int i=0;s[i];i++)
		{
			int y=s[i]-'a';
			if(nodes[now].nex[y]==-1)  nodes[now].nex[y]=newNode();
			now=nodes[now].nex[y];
			
			if(nodes[now].last!=ind ) nodes[now].cnt++;
			nodes[now].last=ind;
		}
		
	}
	int find(char *s)
	{
		// printf("find %s\n",s);
		int now=root;
		for(int i=0;s[i];i++)
		{
			int y=s[i]-'a';
			// printf("->%c\n",s[i]);
			if(nodes[now].nex[y]==-1)   return 0;
			now=nodes[now].nex[y];
		}
		return nodes[now].cnt;
	}

}dict;


int main()
{
	dict.init();
	scanf("%d",&n);
	for1(i,n)
	{
		scanf("%s",word);
		int len=strlen(word);
		for(int j=0;j<len;j++)
		{
			dict.insert(word+j,i);
		}
	}

	scanf("%d",&m);
	for1(i,m)
	{
		scanf("%s",word);
		printf("%d\n",dict.find(word));
	}



	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值