网易互娱笔试第4题,给定一些手机号以及对应的归属地,查询手机号的归属地。
2
3
1357xxxxxxx GuangZhou01
13680xxxxxx GuangZhou01
13799xxxxxx HangZhou02
3
13579246810
13680246810
13799999999
5
3333xxxxxxx nowhere
3334xxxxxxx somewhere
33355555xxx nobody
33355566666 somebody
33366666xxx NULL
10
33333333333
33344444444
33444444444
44444443334
33355566666
33355533355
33366666633
66663336633
33355533341
33338888888
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
const int Inf=1e9;
#define N 20010
#define M 10 //child :0-9
struct Trie{
string s;
int next; //to next node index,or =-1
}rt[N][M];
int cnt; //point to index which just used
void Trie_clear(int k){
for(int i=0;i<M;++i){
rt[k][i].next=-1;
rt[k][i].s.clear();
}
}
void Insert(string &s1,string& s2){
int k=0; //point to root node
for(int i=0;i<(int)s1.length();++i){
int id=s1[i]-'0';
if(rt[k][id].next==-1){
rt[k][id].next=++cnt;
Trie_clear(cnt);
}
if(s1[i+1]=='x'){
rt[k][id].s=s2;
break;
}
k=rt[k][id].next;
}
}
string find_val(string &s1){
int k=0; //point to root node
for(int i=0;i<(int)s1.length();++i){
int id=s1[i]-'0';
if(rt[k][id].next==-1){
return "";
}
if(rt[k][id].s!=""){
return rt[k][id].s;
}
k=rt[k][id].next;
}
return "";
}
int main()
{
ios::sync_with_stdio(0);
// freopen("in.txt","r",stdin);
int T,n,m;
cin>>T;
while(T--){
cin>>n;
string s1,s2;
cnt=0;
Trie_clear(cnt);
for(int i=0;i<n;++i){
cin>>s1>>s2;
s1+='x';
Insert(s1,s2);
}
cin>>m;
while(m--){
cin>>s1;
s1+='x';
string ans=find_val(s1);
if(ans=="") cout<<"unknown"<<endl;
else cout<<ans<<endl;
}
}
return 0;
}