Blog Style Conversion program Experiment Chapter 2
Test Instructions:
Given n (n<=30000) strings, all string lengths are added up to 300000 (the string contains only lowercase letters).
Solve which strings can be referred to as the first string.
The condition that a string can be called the first string is that there is a dictionary order in which it is ranked first.
Method:
Wzq brain hole hash tree .....
However, there is no superior trie tree.
And my brain hole of this tree benefits have what: for the moment do not know.
What's the downside: it's easy to get stuck with a constant.
So why do you write this? Just because I told WJC this question, I must use a hash of things to get past!
* Target achieved √
parsing:
For the following, please do not spit groove this tree how spicy chicken, because I also know that this tree is very spicy chicken, but the flag of his own shed tears to be finished.
Talk about normal practice first.
We first get out of the trie tree, and then for each string, we traverse it, and if there are other strings at the end of the path, then the string must not be selected, otherwise, for each layer of the string, we have a forward edge to the other points that exist on the layer.
After all the edges are connected, we run a topology for the ones that were previously connected, find out if there is a ring, and if there is a ring, you will not pick the string, otherwise the string is optional.
Then we'll talk about the wzq Brain cave tree.
How does this tree come true? Suppose that there is a string abbc.
What is the insertion process for this string?
The initial hash value is 0, the first letter is a.
So insert (0,a), the hash value becomes X
Then insert (x,b), the hash value becomes Y
Then insert (y,b) and the hash value becomes Z
Continue inserting (z,c), the hash value becomes the hash value of the whole string
Last inserted (hash,-1).
Then the practice is similar to the trie tree spicy.
Stand for a flag
Maybe one day I'll find out the merits of this thing?
Code:
Didn't write Trie tree, look at the brain hole tree
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#define N 300100#define M 3000100#Define base 131#define mod 10000007usingnamespace Std;typedef unsignedLong LongUllintNCharAll[m+n];intFir[n];inthash[ -];intHEAD[MOD];inthead2[ -];intCnt2;intBoom[n];intmap[ -][ -];structnode{int from, To,next,lenth; Ull Val;} Edge[n],edge2[n];intCntvoidInit () {memset (head,-1,sizeof(head)); Cnt=1;}voidInit2 () {memset (map,0,sizeof(map)); memset (head2,-1,sizeof(head2)); Cnt2=1; }voidEDGEADD2 (int from,intTo) {Edge2[cnt2]. from= from, Edge2[cnt2].to=to; edge2[cnt2].next=head2[ from]; head2[ from]=cnt2++;}voidEdgeadd (int from,intTo,ull val) {edge[cnt]. from= from, Edge[cnt].to=to,edge[cnt].val=val; edge[cnt].next=head[ from]; head[ from]=cnt++;}intFind (Ull x,intNO) {intSzy=x%mod; for(inti=head[szy];i!=-1; i=edge[i].next) {if(Edge[i].val==x&&edge[i].to==no) {return 1;} }return 0;}voidINS (ull x,intNO) {intSzy=x%mod; Edgeadd (szy,no,x);}int inch[ -];intFind2 (intALPHA1,intALPHA2) { for(inti=head2[alpha1];i!=-1; i=edge2[i].next) {if(EDGE2[I].TO==ALPHA2)return 1; }return 0;}voidWork (Ull x,intAlpha) {intSzy=x%mod; for(inti=head[szy];i!=-1; i=edge[i].next) {if(Edge[i].val==x&&edge[i].to!=alpha&&!map[alpha][edge[i].to]) edgeadd2 (alpha,edge[i].to),inch[edge[i].to]++; }}intvvvv[ -];intsta[ -];BOOLCheck () {intL=1, r=0; for(intI=1; i<= -; i++)if(!inch[i]) Sta[++r]=i; while(L<=R) {intU=sta[l++]; for(inti=head2[u];i!=-1; i=edge2[i].next) {intto=edge2[i].to;inch[to]--;if(!inch[to]) Sta[++r]=to; } }returnr== -;}intTail[n];intMain () {init (); scanf"%d", &n);intLa=0; for(intI=1; i<=n;i++) {fir[i]=la+1; scanf'%s ', all+la+1);intLen=strlen (all+la+1); Tail[i]=la+len; la+=len+1; } for(intI=1; i<=n;i++) {ull hasha=0; for(intj=fir[i];j<=tail[i];j++) {if(!find (hasha,all[j]-' A '+1)) Ins (hasha,all[j]-' A '+1); hasha=hasha*Base+ALL[J]; } ins (hasha,-1); }intans=0; for(intI=1; i<=n;i++) {init2 (); Memsetinch,0,sizeof(inch)); Ull hasha=0; for(intj=fir[i];j<=tail[i];j++) {if(Find (hasha,-1)) {boom[i]=1; Break;} Work (hasha,all[j]-' A '+1); hasha=hasha*Base+ALL[J]; }if(!check ()) boom[i]=1;if(!boom[i]) ans++; } printf ("%d\n", ans); for(intI=1; i<=n;i++) {if(!boom[i]) printf ("%s\n", All+fir[i]); }}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Bzoj 3012 [Usaco2012 dec]first! Wzq Brain hole hash Tree (positive solution trie tree)