每日一题之 hiho1107 Shortest Proper Prefix(字典树变形)

These days Little Hi has been working on a way to improve the QAC performance. He collected N high-frequency queries. We say a string s is a proper prefix if there are no more than 5 collected queries have s as a prefix. A string s is a shortest proper prefix if s is a proper prefix and all the prefixes of s(except for s itself) are not proper prefixes. Little Hi wants to know the number of shortest proper prefixes given N collected queries.

Hint: the 4 shortest proper prefixes for Sample Input are “ab”, “bb”, “bc” and “be”. Empty string “” is not counted as a proper prefix even if N <= 5.

输入
The first line contains N(N <= 10000), the number of collected queries.

The following N lines each contain a query.

Each query contains only lowercase letters ‘a’-‘z’.

The total length of all queries are no more than 2000000.

Input may contain identical queries. Count them separately when you calculate the number of queries that have some string as a prefix.

输出
Output the number of shortest proper prefixes.

样例输入
12
a
ab
abc
abcde
abcde
abcba
bcd
bcde
bcbbd
bcac
bee
bbb
样例输出
4

题意:

在字典树中前缀出现次数不超过5的前缀的个数

思路:

用字典树模板,然后遍历这棵树,找到一个满足条件的前缀则++res

#include <iostream>
#include <cstring>
#include <string>
#include <vector>
#include <set>
#include <cstdlib>

using namespace std;

const int maxn = 30;

typedef struct Trie
{
    Trie *next[maxn];
    int v;
    Trie(){
        v = 0;
        for (int i = 0; i < maxn; ++i)
            next[i] = nullptr;
    }
};

Trie root;

int res = 0;

void creatTrie(string s)
{
    int len = s.length();
    Trie *p = &root, *q;
    for (int i = 0; i < len; ++i) {
        int idx = s[i]-'a';
        if (p->next[idx] == nullptr) {
            q = new Trie();
            q->v = 1;
            p->next[idx] = q;

        }
        else 
            p->next[idx]->v++;

        p = p->next[idx];
    }

}



void visit(Trie *root) 
{
    Trie *tmp = root;
    if (tmp->v <= 5 && tmp->v > 0) {
        ++res;
        return;
    }
    for (int i = 0; i < maxn; ++i) {
        if (tmp->next[i] != nullptr)
            visit(tmp->next[i]);
    }
}



int main()
{
    vector<string>str;
    set<string>prefix;
    int n;
    string s;
    cin >> n;
    for (int i = 0; i < n; ++i) {
        cin >> s;
        creatTrie(s);
    }

    visit(&root);

    cout << res << endl;

    return 0;
}

猜你喜欢

转载自blog.csdn.net/u014046022/article/details/81173150
今日推荐