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萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章