hdu1075

来源:互联网 发布:apache 虚拟目录 编辑:程序博客网 时间:2024/05/16 02:37

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1075

基本上是个字典树的裸题了,不过数据处理的比较麻烦,纠结了好久。。。

#include<iostream>#include<cstdio>#include<cstring>#include<string>using namespace std;class Trie{public:    Trie(){}    void init()    {        for(int i = 0;i < 500000;++i)        {            tr[i].pos = 0; tr[i].str.clear();            memset(tr[i].next,NULL,sizeof(tr[i].next));        }        head = &tr[0]; pos = 0;    }    void insert(string st,string st1)    {        struct tree *h = head;        for(int i = 0;i < st1.size();++i)        {            if(h->next[st1[i] - 'a'] != NULL)                h = h->next[st1[i] - 'a'];            else             {                h->next[st1[i] - 'a'] = &tr[++pos];                h = h->next[st1[i] - 'a'];            }        }        h->str = st; h->pos = 1;    }    void search(string st)    {        struct tree *h = head;        bool flag = false;        for(int i = 0;i <st.size();++i)        {            if(h->next[st[i] - 'a'] !=NULL)                h = h->next[st[i] - 'a'];            else {h = NULL;break;}        }        if(h && h->pos)            cout<<h->str;        else             cout<<st;    }    ~Trie(){}private:    struct tree    {        int pos;        string str;        struct tree *next[30];    }tr[500000];    struct tree *head;    int pos;};class Trie t;int main(){    //freopen("1.txt","r",stdin);    char st1[3005],st2[3005],begin[10] = "START",end[10] = "END";    t.init();    scanf("%s",st1);    while(1)    {        scanf("%s",st1);        if(strcmp(st1,end) == 0) break;        scanf("%s",st2);        t.insert(st1,st2);    }    scanf("%s",st1);getchar();    while(1)    {        gets(st1);        if(strlen(st1) == 3 && strcmp(st1,end) == 0) break;        int j = 0;        memset(st2,'\0',sizeof(st2));        for(int i = 0;i < strlen(st1);++i)        {            if(st1[i] <= 'z' && st1[i] >= 'a')            {                st2[j++] = st1[i];            }            else             {                st2[j] = '\0';                t.search(st2);                printf("%c",st1[i]);                j = 0;                memset(st2,'\0',sizeof(st2));            }          }        if(strlen(st2) != 0)         {            t.search(st2);        }        printf("\n");    }    return 0;}


原创粉丝点击