• 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;
    }
    
  • 相关阅读:
    git
    Java命令行参数解析
    Java调用本地命令
    理解JavaScript继承
    python selenium自动化(三)Chrome Webdriver的兼容
    python selenium自动化(二)自动化注册流程
    python selenium自动化(一)点击页面链接测试
    使用python selenium进行自动化functional test
    JUnit中测试异常抛出的方法
    爬坑 http协议的options请求
  • 原文地址:https://www.cnblogs.com/HackHarry/p/8386262.html
Copyright © 2020-2023  润新知