hihocoder 1107 Shortest Proper Prefix

原创 2015年11月20日 18:03:19

1107 : Shortest Proper Prefix

时间限制:10000ms
单点时限:1000ms
内存限制:512MB
描述

Query auto-completion(QAC) is widely used in many search applications. The basic idea is that when you type some string s in the search box several high-frequency queries which have s as a prefix are suggested. We say string s1 has string s2 as a prefix if and only if the first |s2| characters of s1 are the same as s2 where |s2| is the length of s2.

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

//trie树

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <algorithm>
#include <map>
#include <vector>

using namespace std;

struct trie{ //建立一颗字典树
    char key;
    int num;
    trie *next[26];
    trie(){
        num = 0;
        for(int i = 0; i < 26; i++) next[i] = NULL;
    }

};

trie root;//创建根节点

void ins(string s) { //插入新单词
    trie *head = &root;

    for(int i = 0; i < s.size(); i++) {
        int k = s[i] - 'a';

        if(head->next[k] == NULL) {
            trie *node = new trie;
            node->key = s[i];
            head->next[k] = node;

        }
        head = head->next[k];
        head->num++;
    }

}

int ans = 0; //题目所述proper prefix的个数
map<trie *,int>k; //建立一个映射,后面会用到

void bia(trie *head) {//遍历字典树

         for(int i = 0; i < 26; i++) {
            int flag = 0;

            if(head->num <= 5 &&head != &root) {//满足条件的节点
                    if(k[head] != 1)//若此节点之前未统计过
                    ans++;
                    flag = 1; //标记,此节点后面的分支都不用再遍历了
                    k[head] = 1;//已统计过的节点,标记一下
            }

            //不是最后一个节点且前面还没遇到满足条件的节点
             if(head->next[i] != NULL && !flag)        
               bia(head->next[i]);//递归

        }

}

int main(){

    string s;
    int n,m;
    cin >> n;
    while(n--) {
        cin >> s;
        ins(s);
    }

    trie *head = &root;

    bia(head);
    cout << ans;
    return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

hihoCoder 1107 Shortest Proper Prefix 微软苏州校招笔试 1月17日

字典树,需要用fre[u][v]记录出现频率。最后遍历字典树如果该节点出现频率no more than 5,ans++并返回。否则遍历其不为空的子节点。 代码中的两种dfs都可以,WA了好久最后是因为...
  • yixin94
  • yixin94
  • 2015年02月13日 23:30
  • 1428

hihocoder #1107 : Shortest Proper Prefix

时间限制:10000ms 单点时限:1000ms 内存限制:512MB 描述 Query auto-completion(QAC) is widely used in ...
  • youb11
  • youb11
  • 2015年05月20日 21:07
  • 639

#1107 : Shortest Proper Prefix

题目描述: #1107 : Shortest Proper Prefix 时间限制:10000ms 单点时限:1000ms 内存限制:512MB ...

Shortest Proper Prefix

Shortest Proper Prefix题目Query auto-completion(QAC) is widely used in many search applications. The b...

poj 2001 shortest prefix

前缀树的简单应用, 我的思路是每次加入单词是,为每个节点记录一个value,该value代表经过该节点的单词数目,每次取最短前缀时,只需要沿着单词查找第一个value值为1的节点,即表示只有该单词经...

Shortest unique prefix

Use the shortest unique prefix to represent each word in the array. For example: input: ["zeb...

[Encoding]How to do proper encoding output redirection on cmd.exe?

ProblemIf you are doing automation on windows and you are redirecting the output of different comman...

hihocoder week1 answer

  • 2014年11月17日 21:04
  • 1009B
  • 下载

serializer found for class org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer and no proper

欢迎使用Markdown编辑器写博客本Mark*强调内容*down编辑器使用StackEdit修改而来,用它写博客,将会带来全新的体验哦: Markdown和扩展Markdown简洁的语法 代码块高亮...

Apache Common - The Commons Proper(Apache Commons 正式项目)

组件 Attributes Runtime API to metadata attributes such as doclet tags.  元数据属性(如文件标签)的运行时AP...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:hihocoder 1107 Shortest Proper Prefix
举报原因:
原因补充:

(最多只允许输入30个字)