HUD 1251 Puzzle Stats

Source: Internet
Author: User

/*It's not a difficult question, but it's a matter of the dictionary tree, but space is a problem. Start writing such a struct node{int next[27],sum[27]; bool over;} T[MAXN]; It's not going to work. The ER opened up the MLE asked Wmy very witty said with map works and then the card space to see them using pointers to dynamically allocate memory but I'm not very good at it .*/#include<iostream>#include<cstdio>#include<cstring>#include<map>#defineMAXN 480010using namespacestd;inttopt;Chars[ -];structnode{Map<int,int>next,sum; BOOLOver ;} T[MAXN];voidAdd_tree () {intnow=0, l=strlen (s);  for(intI=0; i<l;i++)      {          intx=s[i]-'a'+1; if(T[now].next[x]) {t[now].sum[x]++; now=T[now].next[x]; }        Else{topt++;t[now].next[x]=topt; T[NOW].SUM[X]++;now=topt; }      }}intFind_tree () {intans=0, p=0, now=0, l=strlen (s), X,pre;  while(p<=l-1) {x=s[p]-'a'+1; if(T[now].next[x]) pre=now,now=t[now].next[x],p++; Else return 0; }    returnt[pre].sum[x];}intMain () { while(Gets (s) &&strlen (s))    Add_tree ();  while(Gets (s)) Cout<<find_tree () <<Endl; return 0;}

HUD 1251 Puzzle Stats

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.