HDU2846:Repository

Repository
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 7787 Accepted Submission(s): 2450

Problem Description
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.

Input
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.

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

Sample Input
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

Sample Output
0
20
11
11
2

题意大概是给你一大堆数, 再给你一大堆查询, 要输出每个查询在那堆数里出现了多少次数。

可以想到用字典树来做,要注意一下两点:

  1. 比如说输入了 “abcde” 吧, 那怎么利用字典树来插入字符串,使后面的每个查询都能准确反映个数呢。
    可以这样做 :先插入abcde 再插入bcde 再插入cde 再插入de 再插入e。这样就能使每个查询都能得到准确的个数。
  2. 还有非常重要的一点!比如说输入 “acc” 首先插入acc 再插入cc 再插入c, 这样如果查询 “c” 的个数的话,正确的应该是1个,
    因为只有一个字符串,但结果输出是两个(如果不加筛选的话),这就需要去除掉重复的子串。

下面附上ac代码:

//AC PLZ!!!!!  :)
#include <bits/stdc++.h>
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm>
#include <ctype.h>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <time.h>
#include <stdlib.h>
#include <map>
//#include <sys/time.h>
//#include <unistd.h>
using namespace std;
#define INF 0x3f3f3f3f
#define MOD 1
#define mod(x) ((x) % MOD)
#define maxn 2000010
typedef long long int ll;
int p, q, mp[500000][26], sum[500000], check[500000], tot;
char s[30], temps[30];
void ins(int nowi) {
    int len = strlen(temps);
    int root = 0;
    for(int i = 0; i < len; i++) {
        int id = temps[i] - 'a';
        if(!mp[root][id]) {
            mp[root][id] = ++tot;
        }
        root = mp[root][id];
        if(check[root] != nowi) {//避免重复
            sum[root]++;
            check[root] = nowi;
        }
    }
    return;
}
int sear() {
    int len = strlen(s);
    int root = 0;
    for(int i = 0; i < len; i++) {
        int id = s[i] - 'a';
        if(!mp[root][id]) {
            return 0;
        }
        root = mp[root][id];
    }
    return sum[root];
}
int main() {
    tot = 0;
    memset(mp, 0, sizeof(mp));
    memset(sum, 0, sizeof(sum));
    memset(check, 0, sizeof(check));
    scanf("%d", &p);
    for(int i = 1; i <= p; i++) {
        scanf("%s", s);
        int slen = strlen(s);
        for(int j = 0; j < slen; j++) {
            strncpy(temps, s + j, strlen(s));
            ins(i);
        }
    }
    scanf("%d", &q);
    for(int i = 0; i < q; i++) {
        scanf("%s", s);
        printf("%d\n", sear());
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_43555854/article/details/88808099
今日推荐