hdu1075

题目链接: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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值