HDU 2846 Repository(字典樹,標記)

來源:互聯網
上載者:User

標籤:style   blog   http   color   os   io   for   ar   

題目

 

 

字典樹,注意初始化的位置~!!位置放錯,永遠也到不了終點了org。。。。

我是用數組類比的字典樹,這就要注意記憶體開多少了,,要開的不大不小剛剛好真的不容易啊。。。。

我用了val來標記是否是同一個串分解而來的,儲存的是串的編號

num記錄數目。

 

 

//string &replace(iterator first0, iterator last0,const_iterator first, const_iterator last);//把[first0,last0)之間的部分替換成[first,last)之間的字串#include<iostream>#include<cstdio>#include<list>#include<algorithm>#include<cstring>#include<string>#include<queue>#include<stack>#include<map>#include<vector>#include<cmath>#include<memory.h>#include<set>using namespace std;#define ll __int64int pos;struct tt{    int val,arr[27],num;    tt(){        val=-1,num=0;        memset(arr,-1,sizeof(arr));    }}a[500010]; //用數組類比的字典樹開的記憶體大小也是一個技術活,太大超記憶體,太小runtime errer。。void insert(char *s,int val,int id,int d){    if(s[d]==‘\0‘)return;    int t=s[d]-‘a‘;    if(a[id].arr[t]==-1)        a[id].arr[t]=++pos;    id=a[id].arr[t];    if(a[id].val!=val)    {        a[id].val=val;        a[id].num++;    }    insert(s,val,id,d+1);}int solve(char *s,int id){    int ans=0;    int len=strlen(s);    for(int i=0;i<len;i++)    {        int t=s[i]-‘a‘;        if(a[id].arr[t]==-1)            return 0;        else             id=a[id].arr[t];    }    return a[id].num;}int main(){    int t;    pos=0;//注意初始化的位置    scanf("%d",&t);    while(t--)    {        char s[30];        scanf("%s",s);        int len=strlen(s);        for(int i=0;i<len;i++)        {            insert(s+i,t,0,0);        }    }    int n;    scanf("%d",&n);    while(n--)    {        char s[30];        scanf("%s",s);        int ans=solve(s,0);        printf("%d\n",ans);    }    return 0;}
View Code

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.