HDU 1251 dictionary tree template question-number of times words appear in multiple searches

Source: Internet
Author: User

There is no data range in this question. I opened a 2005 crazy wa.

Then I opened 50000. I thought my template was wrong and I didn't have a meal in a day. I was so hungry that I still wanted to drop question a and eat it again. Who knows this is the problem ,,, haha ~~~

But record the methods learned for this question:

    for(rt = 0; *s; rt = nxt, ++s)    {        nxt=tree[rt][*s-tb];        if(!nxt)        {            nxt=tree[rt][*s-tb]=top;            memset(tree[top],0,sizeof(tree[top]));            top++;        }        tree[nxt][tk]++;//////////    }
////////////. If you change to tree [RT] [TK], it indicates that a node has several children, tree [NXT] [TK] ++ Number of words with the current letter


#include <cstdio>#include <cstring>#include <algorithm>#include <iostream>using namespace std;const int tk=26;const int tb='a';const int MAXN= 500000;int top,tree[MAXN][tk+2];void init(){    top=1;    memset(tree[0],0,sizeof(tree[0]));}void Insert(char *s, int Rank=1){    int rt,nxt;    for(rt = 0; *s; rt = nxt, ++s)    {        nxt=tree[rt][*s-tb];        if(!nxt)        {            nxt=tree[rt][*s-tb]=top;            memset(tree[top],0,sizeof(tree[top]));            top++;        }        tree[nxt][tk]++;    }}int sea(char *s){    int rt;    for(rt=0;rt=tree[rt][*s-tb];)    {        if(*(++s)==0)return tree[rt][tk];    }    return 0;}char str[150],pat[150];int main(){   //freopen("hdu1251.txt","r",stdin);    char cc;    int ans=0;    init();    while(gets(str)&&str[0])    {        Insert(str);    }    while(~scanf("%s",pat))    {        //cout << pat << endl;        printf("%d\n",sea(pat));    }    return 0;}


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.