861D(字典树)

D. 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
3
123456789
100000000
100123456
output
9
000
01
input
4
123456789
193456789
134567819
934567891
output
2
193
81
91
题意:给你n个长度为10的字符串,每次让你找出一个字符串的最小子串,使得其余n - 1个字符串都不包含这个子串。题目保证这n个字符串都不相同
也就是一定有解
解题思路:我们把这n个字符串的每一个字符串的后缀(每个字符串共10个后缀)都取出来,插入到一颗trie树上,这个这n个字符串一共10 * n个后缀都在这棵
trie树上。然后我们对树上的每个节点维护一个sum,sum表示到当前节点也就是一个字符串的某个后缀包含从根节点到该节点表示的字符串的字符串的个数。所以
我们对于一个字符串,我们只需要把这个字符串的所有后缀取出来,在这棵树上跑一边就行,跑的时候遇到sum = 1表示只有本字符串含有这个子串(从根节点到
当前节点),那么我们就找到了一个最短的,然后在所有后缀中取最优值就行。

#include <bits/stdc++.h>
using namespace std;
const int maxn = 700000;
int n;
int tot;
int len;
char st[70000 + 10][12];
struct node{
    int sum;
    node *Next[11];
}Node[2 * maxn];
node *root;
char ch[100];
char S[100];
map<string, int> mp;
int Min;
string ans;
void init()
{
    tot = 0;
    for(int i = 0; i < maxn; i++)
    {
        Node[i].sum = 0;
        memset(Node[i].Next, 0, sizeof(Node[i].Next));
    }
    root = &Node[tot++];
}
void Insert(char *s, int depth, node* p)
{
    s[depth] = ch[depth];
    s[depth + 1] = '\0';
    string test = string(s);
    if(!mp[test])
    {
        mp[test] = 1;
        p->sum++;
    }
    if(depth == len - 1) return;
    int num = ch[depth + 1] - '0';
    if(p->Next[num]) Insert(s, depth + 1, p->Next[num]);
    else
    {
        node *newnode = &Node[tot++];
        p->Next[num] = newnode;
        Insert(s, depth + 1, p->Next[num]);
    }
}
void solve(char *s, int depth, node* p)
{
    s[depth] = ch[depth];
    s[depth + 1] = '\0';
    string test = string(s);
    if(p->sum == 1)
    {
        if(depth < Min)
        {
            Min = depth;
            ans = test;
        }
        return;
    }
    if(depth == len - 1) return;
    int num = ch[depth + 1] - '0';
    solve(s, depth + 1, p->Next[num]);
}

int main()
{
    while(~scanf("%d", &n))
    {
        init();
        char term[100];
        for(int i = 1; i <= n; i++)
        {
            mp.clear();
            scanf("%s", st[i]);
            int l = strlen(st[i]);
            for(int j = 0; j < l; j++)
            {
               int res = 0;
               for(int k = j; k < l; k++)
               {
                   ch[res++] = st[i][k];
               }
               ch[res] = '\0';
               len = strlen(ch);
               char ss = ch[0];
               if(root->Next[ss - '0']) Insert(S, 0, root->Next[ss - '0']);
               else
               {
                  node *newnode = &Node[tot++];
                  root->Next[ss - '0'] = newnode;
                  Insert(S, 0, root->Next[ss - '0']);
               }
            }
        }
        for(int i = 1; i <= n; i++)
        {
            Min = 11;
            int l = strlen(st[i]);
            for(int j = 0; j < l; j++)
            {
                int res = 0;
                for(int k = j; k < l; k++)
                {
                    ch[res++] = st[i][k];
                }
                ch[res] = '\0';
                len = strlen(ch);
                solve(S, 0, root->Next[ch[0] - '0']);
            }
            cout<<ans<<endl;
        }
    }
    return 0;
}


发布了150 篇原创文章 · 获赞 208 · 访问量 15万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章