zoukankan      html  css  js  c++  java
  • Hdoj 2846

    原题链接

    描述

    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.

    输入

    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.

    输出

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

    样例输入

    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

    样例输出

    0
    20
    11
    11
    2

    思路

    字典树。
    刚开始我也不知道怎么做,后来听大佬的思路的。
    将每个单词的后缀都存入字典树,标记好源于同一个单词的后缀,然后判断。虽然时间复杂度很悬的样子,但还是AC了。

    代码

    #include <bits/stdc++.h>
    #define maxn 900000
    using namespace std;
    
    struct node
    {
    	int a[26];
    	int b;
    	int val;
    };
    
    vector<node> trie;
    int tot, sum;
    
    void create(char st[], int num)
    {
    	int len = strlen(st);
    	int u = 0;
    	for(int i = 0; i < len; i++)
    	{
    		int v = st[i] - 'a';
    		if(trie[u].a[v] == 0)
    		{
    			trie[u].a[v] = ++tot;
    			node pnew; 
    			for(int j = 0; j < 27; j++) pnew.a[j] = 0; 
    			pnew.b = -1; pnew.val = 0;
    			trie.push_back(pnew);
    		}
    		u = trie[u].a[v];
    		if(trie[u].b != num)
    		{
    			trie[u].b = num;
    			trie[u].val++;
    		}
    	}
    }
    
    void find(char st[])
    {
    	int len = strlen(st);
    	int u = 0, f = 1;
    	for(int i = 0; i < len; i++)
    		if(trie[u].a[st[i] - 'a'])
    			u = trie[u].a[st[i] - 'a'];
    		else {f = 0; break;}
    	if(f) sum += trie[u].val;
    	
    }
    
    int main()
    {
    	node pnew; 
    	for(int j = 0; j < 27; j++) pnew.a[j] = 0; 
    	pnew.b = -1; pnew.val = 0;
    	trie.push_back(pnew);
    	int n; scanf("%d", &n);
    	while(n--)
    	{
    		char st[21];
    		scanf("%s", st);
    		int len = strlen(st);
    		for(int i = 0; i < len; i++)
    			create(st + i, n);
    	}
    	scanf("%d", &n);
    	while(n--)
    	{
    		char st[21];
    		scanf("%s", st);
    		sum = 0;
    		find(st);
    		printf("%d
    ", sum);
    	}
    	return 0;
    }
    
  • 相关阅读:
    递归遍历多维数组(树数据结构)的超级简单方式,并且可以递归超过200层,摘自<<PHP精粹:编写高效PHP代码>>
    http协议传输二进制数据以及对输入流(php://input)和http请求的理解
    一个非常简单的RPC服务
    php://input 打开的数据流只能读取一次,即读取一次之后读取的值为空
    soap的简单实现(PHP)
    使用PHP的curl扩展实现跨域post请求,以及file_get_contents()百度短网址例子
    jquery选取iframe
    算法之棋盘覆盖
    词法分析之实验报告
    简单的词法分析小程序
  • 原文地址:https://www.cnblogs.com/HackHarry/p/8386262.html
Copyright © 2011-2022 走看看