CodeForces - 861D D. Polycarp's phone book STL map套set

problem

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: 123456789, 100000000 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.

outputPrint 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
3
123456789
100000000
100123456
9
000
01

 

题意

给一大堆电话号码,问每个电话号码与其他串不重复的最短子串

题解:STL鬼题map<st, set<>> 表示能以st为标识符的字符串

#include <bits/stdc++.h>

using namespace std;

map <string , set<int> > cnt;
int n;
const int maxn = 7e4+2;
string st;
string Ans[maxn];
int vis[maxn];
int main()
{
    scanf("%d",&n);
    for (int i = 1; i <= n;i++){

        cin >> st;
        for (int l = 1; l <= 9;l++){
            for (int j = 0; j <= 9 - l; j++){
                string st2(st, j, l);
                cnt[st2].insert(i);
            }
        }
    }
    for (map <string, set<int> >::iterator ite = cnt.begin(); ite != cnt.end();ite++){
        pair<string, set<int> > x = *ite;
        string st3 = x.first;
        set <int> num = x.second;
        if (num.size() == 1){
            int x = *num.begin();
            if (Ans[x] == "" || st3.length() < Ans[x].length()) Ans[x] = st3;
        }
    }
    for (int i = 1; i <= n; i++){
        cout << Ans[i] << endl;
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值