如图,这就是一棵字典树,通过这样一棵字典树,我们可以进行一些字符串以及数字的匹配,统计。
Given a list of phone numbers, determine if it is consistent in the sense that no number is the prefix of another. Let's say the phone catalogue listed these numbers:
Emergency 911Alice 97 625 999Bob 91 12 54 26In this case, it's not possible to call Bob, because the central would direct your call to the emergency line as soon as you had dialled the first three digits of Bob's phone number. So this list would not be consistent.
Input
The first line of input gives a single integer, 1 ≤ t ≤ 40, the number of test cases. Each test case starts with n, the number of phone numbers, on a separate line, 1 ≤ n ≤ 10000. Then follows n lines with one unique phone number on each line. A phone number is a sequence of at most ten digits.
Output
For each test case, output "YES" if the list is consistent, or "NO" otherwise.
Sample Input
2 3 911 97625999 91125426 5 113 12340 123440 12345 98346Sample Output
NO YES 这道题目就是典型的字典树,直接套模板即可。(不过要注意树的存储方式) #include<cstdio> #include<cstring> using namespace std; int Next[100005][10],n,cnt,num[100005]; char s[10005][15]; int add(char *s) { int len=strlen(s),p=0; for(int i=0;i<len;i++) { int id=s[i]-'0';//(添加一个字母) if(!Next[p][id])//(如果还未添加) { Next[p][id]=++cnt;//(给字典树节点标号) num[cnt]=0; } p=Next[p][id]; if(num[p]==0x7fffffff) return 1;//(有没有单词在此结束) num[p]++;//(统计重复次数) } if(num[p]>1) { return 1;//(这个单词结束后,还有没有同它一样的) } else { num[p]=0x7fffffff; return 0; } } void solve() { scanf("%d",&n); memset(Next,0,sizeof(Next)); for(int i=1;i<=n;i++) { scanf("%s",s[i]); } cnt=0; for(int i=1;i<=n;i++) { if(add(s[i])) { printf("NO\n"); return; } } printf("YES\n"); } int main() { int T; scanf("%d",&T); while(T--) { solve(); } return 0; }In an edge-weighted tree, the xor-length of a path p is defined as the xor sum of the weights of edges on p:
⊕ is the xor operator.
We say a path the xor-longest path if it has the largest xor-length. Given an edge-weighted tree with n nodes, can you find the xor-longest path?
Input
The input contains several test cases. The first line of each test case contains an integer n(1<=n<=100000), The following n-1 lines each contains three integers u(0 <= u < n),v(0 <= v < n),w(0 <= w < 2^31), which means there is an edge between node u and v of length w.
Output
For each test case output the xor-length of the xor-longest path.Sample Input
4 0 1 3 1 2 4 1 3 6Sample Output
7Hint
The xor-longest path is 0->1->2, which has length 7 (=3 ⊕ 4)
也是字典树,但是要注意,push_back()用多了会超时。所以超时了#include<iostream> #include<cstdio> #include<vector> #include<cstring> using namespace std; bool pd[100001]; vector<int> lj[100005][2]; struct queue { int p; int n; }; queue qu[110000]; int num[100005]; long long cf[33]; int ans=0,cnt=0,dx=0; int next[2000005][2]; void add(int num1) { int num=num1; int p=0; for(int i=30;i>=0;i--) { int id; if(num>=cf[i]) { id=1; num-=cf[i]; } else id=0; if(!next[p][id]) { next[p][id]=++cnt; } p=next[p][id]; } } int check(int num1) { int num=num1; int temp=0; int p=0; for(int i=30;i>=0;i--) { int id; if(num>=cf[i]) { id=0; num-=cf[i]; } else id=1; if(next[p][id]) { temp+=cf[i]; p=next[p][id]; } else { if(id==0) p=next[p][1]; else p=next[p][0]; } } return temp; } int main() { freopen("t2.in","r",stdin); freopen("t2.out","w",stdout); cf[0]=1; for(int i=1;i<32;i++) { cf[i]=(cf[i-1]<<1); } int n,u,v,w; while(scanf("%d",&n)!=EOF) { ans=0; cnt=0; memset(pd,false,sizeof(pd)); memset(next,0,sizeof(next)); memset(num,0,sizeof(num)); memset(lj,0,sizeof(lj)); for(int i=0;i<n-1;i++) { scanf("%d%d%d",&u,&v,&w); lj[u][0].push_back(v); lj[u][1].push_back(w); lj[v][0].push_back(u); lj[v][1].push_back(w); } int head=0,tail=0; qu[0].p=0; qu[0].n=0; pd[0]=true; while(head<=tail) { int op=qu[head].p; int on=qu[head].n; head++; int len=lj[op][0].size(); for(int i=0;i<len;i++) { if(!pd[lj[op][0][i]]) { tail++; pd[lj[op][0][i]]=true; qu[tail].p=lj[op][0][i]; qu[tail].n=on^lj[op][1][i]; num[qu[tail].p]=qu[tail].n; } } } for(int i=0;i<n;i++) { ans=max(ans,num[i]); add(num[i]); } for(int i=0;i<n;i++) { ans=max(ans,check(num[i])); } printf("%d\n",ans); } return 0; }