http://hihocoder.com/problemset/problem/1289
This is the second question of the Microsoft written test, the person is less than the third question, this question is a string matching problem, you should be able to use the dictionary tree to solve. But then there was an idea to do it offline, sort all the queries, then use rule to match queries, dye them, and dye each query only once. It turns out that if the second method of the game should be able to take full points, but I used the first method, resulting in only 10 points ... Because I did not consider the same rule occurred two times, but the dictionary tree will be directly covered by the following rule, it is necessary to determine the minimum update index, but the case of offline dyeing only once, so the second will skip this pit point directly.
The dictionary tree is better considered, that is, directly into the first mask portion of rule, and then add an index and isOK tag is the number of rule and rule type, and then the subsequent IP matches, only the index is the smallest. And then some boundaries and non-existent conditions for a special sentence.
At that time finished writing the dictionary tree WA only took 10 points, and then the dictionary tree did not find where to write the problem, has been tangled with is not data overflow and so on. Alas, can only show that the level has not yet, and do not know whether to enter the interview.
Code:
#include <iostream>#include<cstdio>#include<cstdlib>#include<cmath>#include<cstring>#include<algorithm>#include<Set>#include<map>#include<queue>#include<vector>#include<string>#defineLL Long Longusing namespacestd;Const intMAXN =100005;Const intMaxLen = +;//Len indicates the binary maximum length of the numberstructtrie{intindex; intnext[2];} TREE[MAXN*MaxLen];BOOLisok[maxn*MaxLen];intCNT;voidInittree () {CNT=0; memset (Tree,-1,sizeof(tree));voidAddintXBOOLFlagintIndexintLen) { intnow =0; BOOLK; for(inti = len-1; I >=0; i--) {k= x& (1<<i); if(Tree[now].next[k] = =-1) Tree[now].next[k]= ++CNT; now=Tree[now].next[k]; } if(Tree[now].index = =-1|| Tree[now].index > Index)//Only because of this situation, only 10 points were taken. {Tree[now].index=index; Isok[now]=Flag; }}BOOLQueryintx) { intv =-1, now =0; BOOLk, Flag; for(inti = maxlen-1; I >=0; i--) { if(Tree[now].index! =-1) { if(v = =-1|| V >Tree[now].index) {v=Tree[now].index; Flag=Isok[now]; }} k= x& (1<<i); if(Tree[now].next[k] = =-1) Break; now=Tree[now].next[k]; } if(Tree[now].index! =-1) { if(v = =-1|| V >Tree[now].index) {v=Tree[now].index; Flag=Isok[now]; } } if(v = =-1)return true; Else returnFlag;}intN, M;Charop[ the];Charstr[ the];voidinput () {BOOLFlag; intK, A, B, C, D, R, Len; for(inti =0; I < n; ++i) {scanf ("%s%s", OP, str); if(op[0] =='a') flag =true; ElseFlag =false; R= -1; Len=strlen (str); for(intj =0; J < Len; ++j) {if(Str[j] = ='/') {sscanf (str+j+1,"%d", &R); STR[J]=' /'; }} sscanf (str,"%d.%d.%d.%d", &a, &b, &c, &d); K= (a<< -) + (b<< -) + (c<<8)+D; if(r! =-1) K >>= ( +-R); ElseR =MaxLen; Add (k, flag, I, r); }}voidWork () {BOOLFlag; intK, A, B, C, D; for(inti =0; I < m; ++i) {scanf ("%d.%d.%d.%d", &a, &b, &c, &d); K= (a<< -) + (b<< -) + (c<<8)+D; Flag=query (k); if(flag) printf ("yes\n"); Elseprintf"no\n"); }}intMain () {//freopen ("test.in", "R", stdin); while(SCANF ("%d%d", &n, &m)! =EOF) {Inittree (); Input (); Work (); } return 0;}
View Code
ACM Learning process-hihocoder 1289 403 Forbidden (Dictionary tree | | (Offline && sort && dye))