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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是用C语言实现字典树的示例代码: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_CHILDREN 26 struct TrieNode { int count; // 记录单词出现次数 struct TrieNode* children[MAX_CHILDREN]; }; struct Trie { struct TrieNode* root; }; struct TrieNode* create_node() { struct TrieNode* node = (struct TrieNode*)malloc(sizeof(struct TrieNode)); node->count = 0; for (int i = 0; i < MAX_CHILDREN; i++) { node->children[i] = NULL; } return node; } void insert(struct Trie* trie, char* word) { struct TrieNode* p = trie->root; for (int i = 0; i < strlen(word); i++) { int index = word[i] - 'a'; if (p->children[index] == NULL) { p->children[index] = create_node(); } p = p->children[index]; } p->count++; } int search(struct Trie* trie, char* word) { struct TrieNode* p = trie->root; for (int i = 0; i < strlen(word); i++) { int index = word[i] - 'a'; if (p->children[index] == NULL) { return 0; } p = p->children[index]; } return p->count; } int main() { struct Trie trie; trie.root = create_node(); insert(&trie, "apple"); insert(&trie, "app"); insert(&trie, "application"); int count = search(&trie, "app"); printf("单词 app 出现的次数为:%d\n", count); return 0; } ``` 以上代码中,`create_node` 函数用于创建字典树节点,`insert` 函数用于向字典树中插入单词,`search` 函数用于在字典树中查找单词出现的次数。`main` 函数则测试了该字典树在插入单词 `"apple"`、`"app"`、`"application"` 后查找单词 `"app"` 出现的次数的结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值