题意:给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 (" ") #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; }