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

来源:互联网 发布:mac建立无线局域网 编辑:程序博客网 时间:2024/05/23 19:19

 Polycarp's phone book
time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard 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: 123456789100000000 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
3123456789100000000100123456
output
900001
input
4123456789193456789134567819934567891
output
21938191

直接用数组静态建树,把每一个字符串的每一个后缀加入到字典树中 这样就能遍历到每一个子串109ms

#include <iostream>#include <stdio.h>#include <string.h>#include <stack>#include <queue>#include <map>#include <set>#include <vector>#include <math.h>#include <bitset>#include <algorithm>#include <climits>using namespace std;const int N = 500000+10;typedef long long LL;char str[N][11], a[11];int ok[N*6], num[N*6], ch[N*6][11], sum[N*6];int main(){    int n, m=0;    scanf("%d", &n);    for(int i=0;i<n;i++)    {        scanf("%s",a);        int l=0;        for(int j=0;j<9;j++)        {            int k=0;            for(int p=j;p<9;p++)            {                int x=a[p]-'0';                if(!ch[k][x]) ch[k][x]=++m;                k=ch[k][x];                if(!ok[k]) num[l++]=k,ok[k]=1, sum[k]++;            }        }        for(int j=0;j<l;j++) ok[num[j]]=0;        for(int j=0;j<9;j++) str[i][j]=a[j];    }    for(int i=0;i<n;i++)    {        int mint=11, s=0, t=9;        for(int j=0;j<9;j++)        {            int k=0;            for(int p=j;p<9;p++)            {                k=ch[k][str[i][p]-'0'];                if(sum[k]==1&&p-j+1<mint)                {                    mint=p-j+1,s=j,t=p;                    break;                }            }        }        for(int j=s;j<=t;j++) putchar(str[i][j]);        puts("");    }    return 0;}



自己写的大暴力 时限4s 跑3.961s刚过去了。。。

#include <iostream>#include <bits/stdc++.h>using namespace std;const int N = 1000000+10;typedef long long LL;char str[70000+10][20];string ss, sx;unordered_map<string,int>q,qx;int main(){    int t;    scanf("%d", &t);    q.clear();    for(int i=0;i<t;i++)    {        scanf("%s",str[i]);        ss=str[i];        qx.clear();        for(int k=1;k<=9;k++)        {            for(int j=0;j+k-1<9;j++)            {                sx=ss.substr(j,k);                //cout<<sx<<endl;                if(!qx.count(sx)) qx[sx]=1,q[sx]+=1;            }        }    }    for(int i=0;i<t;i++)    {        ss=str[i];        int flag=0;        for(int k=1;k<=9;k++)        {            for(int j=0;j+k-1<9;j++)            {                sx=ss.substr(j,k);                //cout<<sx<<endl;                //cout<<q[sx]<<endl;                if(q[sx]==1)                {                    for(int h=j;h<=j+k-1;h++) printf("%c",str[i][h]);                    printf("\n");                    flag=1;                    break;                }            }            if(flag) break;        }    }    return 0;}

原创粉丝点击