1. 程式人生 > >Codeforces 861D - Polycarp's phone book

Codeforces 861D - Polycarp's phone book

problems com size soft logs 做的 define 出現 rst

861D - Polycarp‘s phone book

思路:用map做的話,只能出現一次循環,否則會超時。

代碼:

#include<bits/stdc++.h>
using namespace std;
#define f first
#define s second
#define pb push_back
#define mp make_pair
map<string,int> ma;
string ans[70005];
int main(){
    int n;
    string s,nu;
    cin>>n;
    for(int i=0
;i<n;i++){ cin>>s; ans[i+1] = ""; for(int j=0;j<9;j++){ nu = ""; for(int k=j;k<9;k++){ nu += s[k]; if(ma[nu] && ma[nu]!=i+1) ma[nu]=-1; else ma[nu] = i+1; } } }
for(auto it : ma){ if(it.s!=-1) if(ans[it.s]=="" || ans[it.s].size()>it.f.size() ) ans[it.s] = it.f; } for(int i=1;i<=n;i++) cout<<ans[i]<<endl; return 0; }

Codeforces 861D - Polycarp's phone book