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;
    }
    
  • 相关阅读:
    Vue 全局过滤器和局部过滤器使用方式
    Vue 标签显示/隐藏的方式对比demo
    OpenSSL SSL_read: Connection was reset, errno 10054
    Vue 自定义全局指令和局部指令demo
    Vue 列表渲染demo
    Vue Class样式和style样式编写
    vue中的函数(methods),计算属性(computed),监听器(watch)的区别demo
    Vue 常用指令
    针对 WebSocket 协议的 Locust 压测脚本实现(基于 Locust 1.0 以上版本)
    Locust 脚本开发入门(4)
  • 原文地址:https://www.cnblogs.com/HackHarry/p/8386262.html
Copyright © 2011-2022 走看看