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;
    }
    
  • 相关阅读:
    俩人搞对象,山上骑马
    历史不会偏袒任何一个缺乏正义、良知的人。
    力量和对力量的控制
    超级管理员
    电信F412
    prim算法
    Maven pom.xml配置详解
    PorterDuffXfermode的用法
    使用MaskFilter
    Android drawText获取text宽度的三种方式
  • 原文地址:https://www.cnblogs.com/HackHarry/p/8386262.html
Copyright © 2011-2022 走看看