Codeforces 858D Polycarp's phone book

题目:http://codeforces.com/problemset/problem/858/D

题意:给你一些9位的数字,问对于每串数字,最少输入几个数字才能使得输入的数字是它独有的子串

比赛的时候没什么思路,看了dalao们的代码才会做

我们可以用map来保存所有的子串是谁的,如果同时是2个或以上的,那这个子串就不能用

之后遍历map,对答案进行更新

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
using namespace std;
string ans[70005];
map<string,int> ma;
int main()
{
    ios::sync_with_stdio(0);
    int n;
    cin>>n;
    string s;
    for(int i=1;i<=n;i++)
    {
        cin>>s;
        ans[i]=s;
        for(int j=0;j<9;j++)
            for(int k=1;k+j<=9;k++)
        {
            int &t=ma[s.substr(j,k)];
            if (t==0||t==i) t=i;
            else t=-1;
        }
    }
    for(auto &t:ma)
        if (t.second>0)
        if (ans[t.second].size()>t.first.size())
            ans[t.second]=t.first;
    for(int i=1;i<=n;i++)
        cout<<ans[i]<<endl;
    return 0;
}

  

原文地址:https://www.cnblogs.com/bk-201/p/7542029.html