字典树练习题HUD_1075

题目:https://acm.hdu.edu.cn/showproblem.php?pid=1075

写的时候那个cin和getline混合用会多输出一个空行。。。报了两次格式错误。。这里卡了10几分钟。。

AC代码:

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <string.h>
using namespace std;

const int SIZE = 26;

typedef struct Tree{
    string s;
    struct Tree *next[SIZE];
    bool exist;
    Tree(){
        s = "";
        memset(next,0,sizeof(next));
        exist = false;
    }
}Tire;
Tire *T = new Tire();

void insert(Tire *&T,string &e,string &s){

    Tire *t = T;
    int len = s.size();
    for(int i = 0;i<len;i++){
        if(t->next[s[i] - 'a'] == NULL)
            t->next[s[i]-'a'] = new Tire();
        t = t->next[s[i]-'a'];
    }
    t->s = e;
    t->exist = true;
}

string search(Tire *T,string &s){

    int len = s.size();
    Tire *t = T;
    for(int i = 0;i<len; i++){
        if(t->next[s[i]-'a'] == NULL)
            return "";
        t = t->next[s[i]-'a'];
    }
    return t->s;
}

int main(){

    ios_base::sync_with_stdio(false);
    cin.tie(0);
    string e,s;
    while(1){
        getline(cin,e);
        if(e == "START"){
            continue;
        }
        if(e == "END"){
            break;
        }
        for(int i = 0;i<e.size();i++){
            if(e[i] == ' '){
                s = e.substr(i+1,e.size()-i-1);
                e = e.substr(0,i);
                break;
            }
        }
        insert(T,e,s);
    }

    while(1){
        getline(cin,s);
        if(s == "START") continue;
        if(s == "END") break;

        string ans = "";
        int len = s.size();
        string t;
        for(int i = 0;i<len;i++){
            if(s[i] >= 'a' && s[i] <= 'z')
                t += s[i];
            else{
                string trans = search(T,t);
                if(trans.size() == 0) ans += t;
                else ans += trans;
                ans += s[i];
                t.clear();
            }
        }
        cout << ans << endl;
    }

    system("pause");
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值