codeforces 858D Polycarp's phone book(字典树)

题目链接

D. Polycarp’s phone book
time limit per test4 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
There are n phone numbers in Polycarp’s contacts on his phone. Each number is a 9-digit integer, starting with a digit different from 0. All the numbers are distinct.

There is the latest version of Berdroid OS installed on Polycarp’s phone. If some number is entered, is shows up all the numbers in the contacts for which there is a substring equal to the entered sequence of digits. For example, is there are three phone numbers in Polycarp’s contacts: 123456789, 100000000 and 100123456, then:

if he enters 00 two numbers will show up: 100000000 and 100123456,
if he enters 123 two numbers will show up 123456789 and 100123456,
if he enters 01 there will be only one number 100123456.
For each of the phone numbers in Polycarp’s contacts, find the minimum in length sequence of digits such that if Polycarp enters this sequence, Berdroid shows this only phone number.

Input
The first line contains single integer n (1 ≤ n ≤ 70000) — the total number of phone contacts in Polycarp’s contacts.

The phone numbers follow, one in each line. Each number is a positive 9-digit integer starting with a digit from 1 to 9. All the numbers are distinct.

Output
Print exactly n lines: the i-th of them should contain the shortest non-empty sequence of digits, such that if Polycarp enters it, the Berdroid OS shows up only the i-th number from the contacts. If there are several such sequences, print any of them.

Examples
input
3
123456789
100000000
100123456
output
9
000
01
input
4
123456789
193456789
134567819
934567891
output
2
193
81
91

题意:给你n个电话号码,问你未出现的最小长度的子串是什么。

思路:题意很简单,最开始我的想法是利用哈希,对每个子串都哈希成一个数字,然后放到multiset里面,对每个串再进行扫描,先把子串给删去,然后如果再查询的时候没有这个子串就直接输出它,结果时间复杂度太高T了,放了有几天,发现同样的思路字典树的写法就直接A了,220ms

代码如下:

#include<iostream>
#include<cstring>
#include<string>
#include<set>
#include<vector>
#include<cmath>
#include<map>
#include<algorithm>
#include<queue>

using namespace std;
const int MAX = 10;
struct Trie{
    struct Trie *next[MAX];
    int v;
};
Trie *root;
void Creat(char *str,int s,int t){
    Trie *p = root,*q;
    for(int i=s;i<t;i++){
        int id = str[i] - '0';
        if(p->next[id] == NULL){
            q = (Trie*)malloc(sizeof(Trie));
            for(int i=0;i<MAX;++i)
                q->next[i] = NULL;
            q->v = 0;
            p->next[id] = q;
            p = p->next[id];
        }
        else{
            p = p->next[id];
        }
    }
    p->v++;
}

int Find(char *str,int s,int t){
    Trie *p = root;
    for(int i=s;i<t;i++){
        int id = str[i] - '0';
        if(p->next[id] == NULL)
            return 0;
        else
            p = p->next[id];
    }
    return p->v;
}
void Dle(char *str,int s,int t){
    Trie *p = root;
    for(int i=s;i<t;++i){
        int id = str[i] - '0';
        p = p->next[id];
    }
    p->v--;
}
void init(){
    root = (Trie*)malloc(sizeof(Trie));
    root->v = 0;
    for(int i=0;i<MAX;++i)
        root->next[i] = NULL;
}
char str[70010][11];
int main(void){
    int n;

    init();
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%s",str[i]);
        for(int len=1;len<=9;++len){
            for(int s=0;s+len<=9;++s){
                Creat(str[i],s,s+len);
            }
        }
    }
    //先在字典树中删去这个子串
    for(int i=1;i<=n;i++){
        for(int len=1;len<=9;++len){
            for(int s=0;s+len<=9;++s){
                Dle(str[i],s,s+len);
            }
        }
        //查询其他字符串不具有的子串
        for(int len=1;len<=9;++len){
            bool isfind = false;
            for(int s=0;s+len<=9;++s){
                if(!Find(str[i],s,s+len)){
                    for(int v=s;v<s+len;++v)
                        putchar(str[i][v]);
                    printf("\n");
                    isfind = true;
                    break;
                }
            }
            if(isfind)  break;
        }
        //再把它的子串重新放入
        for(int len=1;len<=9;++len){
            for(int s=0;s+len<=9;++s){
                Creat(str[i],s,s+len);
            }
        }
    }

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值