1. 程式人生 > >Codeforces Round #434 D

Codeforces Round #434 D

cos name mes stream algorithm codeforce 1.0 ace string

Polycarp‘s phone book

題意:給n個號碼,求出每個號碼最短的能唯一定位該號碼的字符串

思路:暴力 把每個號碼的子串放進map裏,然後從長度短的開始暴力每一個子串出現的次數,出現一次的就是能定位的,同一個號碼裏的相同子串只記錄一次,如0000 ,子串000出現2次,但是只記錄一次(當然也可以字典樹或者後綴數組寫拉,但是暴力是墜吼滴

AC代碼:

#include "iostream"
#include "iomanip"
#include "string.h"
#include "stack"
#include "queue"
#include "string"
#include "vector
" #include "set" #include "map" #include "algorithm" #include "stdio.h" #include "math.h" #pragma comment(linker, "/STACK:102400000,102400000") #define bug(x) cout<<x<<" "<<"UUUUU"<<endl; #define mem(a,x) memset(a,x,sizeof(a)) #define step(x) fixed<< setprecision(x)<< #define mp(x,y) make_pair(x,y) #define
pb(x) push_back(x) #define ll long long #define endl ("\n") #define ft first #define sd second #define lrt (rt<<1) #define rrt (rt<<1|1) using namespace std; const ll mod=1e9+7; const ll INF = 1e18+1LL; const int inf = 1e9+1e8; const double PI=acos(-1.0); const int N=1e5+100; string s[70002]; map
<string,int> M,m[70002]; int main(){ ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); int n; cin>>n; for(int i=1; i<=n; ++i){ cin>>s[i]; } for(int i=1; i<=n; ++i){ for(int j=0; j<9; ++j){ for(int k=1; k+j<=9; ++k){ string ss=s[i].substr(j, k); if(!m[i][ss]){ m[i][ss]++; M[ss]++; } } } } for(int i=1; i<=n; ++i){ int flag=0; for(int k=1; k<=9; ++k){ for(int j=0; j+k<=9; ++j){ string ss=s[i].substr(j, k); if(M[ss]==1){ cout<<ss<<endl; flag=1; break; } } if(flag) break; } } return 0; }

Codeforces Round #434 D