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;
    }
    
  • 相关阅读:
    win10系统下office 2019激活
    如何根据【抖音分享链接】去掉抖音水印
    Java多线程学习之ThreadLocal源码分析
    Java多线程学习之synchronized总结
    Java多线程学习之线程的取消与中断机制
    Java多线程学习之Lock与ReentranLock详解
    Java多线程学习之线程池源码详解
    MyBatis 一、二级缓存和自定义缓存
    Spring 高级依赖注入方式
    Spring 依赖注入的方式
  • 原文地址:https://www.cnblogs.com/HackHarry/p/8386262.html
Copyright © 2011-2022 走看看