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


本数据集来源于 2024 年 7 月在江西省中东部余干县、贵溪市、金溪县丘陵林地采集的千枚岩、红砂岩、花岗岩母质发育红壤关键带剖面土壤实测数据,空间覆盖 3 个县域不同岩性风化壳林地,采样点位经纬度分别为千枚岩剖面 P10(116.8316°E,28.5269°N)、红砂岩剖面 P08(117.1048°E,28.3492°N)、花岗岩剖面 P04(116.6883°E,27.9963°N);垂直空间采样深度存在差异,千枚岩与花岗岩剖面采样深度 0~600 cm,红砂岩剖面采样深度 0~450 cm,垂直分层采样分辨率为 0~50 cm 区间分 0~20 cm、20~50 cm 两层,50 cm 以下土层以 50 cm 为固定间隔分层,整套数据集共包含 36 条土壤剖面分层记录,其中 P10 千枚岩剖面 13 条、P08 红砂岩剖面 11 条、P04 花岗岩剖面 13 条。数据采集时间为 2024 年 7 月,实验室理化指标、矿物测试、酸碱滴定及统计建模工作于 2024 年 7 月 —2026 年 5 月完成,无时间序列连续监测数据,仅为单次野外剖面采样静态数据集。 数据集包含野外剖面基础信息、土壤酸碱滴定原始数据、土壤酸度指标、交换性盐基与交换性酸、土壤机械组成、有机质、黏土与原生矿物半定量 XRD 数据、无定形 / 晶形铁铝氧化物含量。全量理化指标计量单位统一规范:酸缓冲容量 pHBC 单位为 cmol・kg⁻¹・pH⁻¹,交换性酸、交换性盐基离子单位为 cmol・kg⁻¹,矿物以质量百分比(%)表示,、黏粒 / 粉粒 / 砂粒、有机质、铁铝氧化物单位均为g/kg,pH 为无量纲数值。 覆盖范围: 中位纬度: 28.2616 中位经度: 116.89654999999999 南界纬度: 27.9963 西界经度: 116.6883 北界纬度: 28.5269 东界经
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值