HDU-1705 What Are You Talking About(字典树、STL)

这道题有两种做法,第一种是字典树,相当于一个字典树的模板题了
用string来存对应的单词,然后构建字典树,在每个单词的最后要标记结束,然后把相应的单词存进string 也就是我的程序里面的dic里
注意:字典树的指针p一定要从1开始

#include <iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<climits>
#include<vector>
#include<queue>
#include<string>
#include <cmath>
#include<set>
#include<map>
#define mod 999997
#define lson l, mid, root << 1
#define rson mid + 1, r, root << 1 | 1
#define father  l , r , root
#define lowbit(x) ( x & ( - x ) )
using namespace std;
typedef long long ll;
const int maxn = 500010;
const int inf = 0x3f3f3f3f;
int mp[maxn][26];
int vis[maxn];
string a,b,dic[maxn];
int k=1;//一定要从1开始
void Insert(){
    int p=0;
    int len=b.size();
    for(int i=0;i<len;i++){
        int n=b[i]-'a';
        if(mp[p][n]==0){
            mp[p][n]=k++;
        }
        p=mp[p][n];
    }
    vis[p]=1;//每一个火星文单词结束标记
    dic[p]=a;//对应的英文
}
bool Find(char *str){
    int p=0;
    int len=strlen(str);
    for(int i=0;i<len;i++){
        int n=str[i]-'a';
        if(mp[p][n]==0)
            return 1;
        p=mp[p][n];
    }
    if(vis[p]==1){
        cout<<dic[p];
        return 0;
    }
    return 1;
}

int main( ){
    char s[3300],c,str[3300];
    memset(vis,0,sizeof(vis));
    memset(mp,0,sizeof(mp));
    cin>>a;
    while(cin>>a){
        if(a=="END")break;
        cin>>b;
        Insert();//插入火星文
    }
    cin>>a;
    c=getchar();//读回车
    while(1){
        int cnt=0;
        while(c=getchar()){
            if(c=='\n')break;
            s[cnt++]=c;
        }
        s[cnt]=0;
        if(strcmp(s,"END")==0)break;
        int num=0;
        for(int i=0;i<cnt;i++){
            if(s[i]<='z'&&s[i]>='a'){
                str[num++]=s[i];
            }else{
                str[num]=0;
                bool flag=Find(str);
                if(flag)cout<<str;
                cout<<s[i];
                num=0;
            }
        }
        cout<<endl;
    }
}

第二种做法就很简单了,直接用stl mp存入字典就ok,然后直接查找

#include <iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<climits>
#include<vector>
#include<queue>
#include<string>
#include <cmath>
#include<set>
#include<map>
#define mod 999997
#define lson l, mid, root << 1
#define rson mid + 1, r, root << 1 | 1
#define father  l , r , root
#define lowbit(x) ( x & ( - x ) )
using namespace std;
typedef long long ll;
const int maxn = 20010;
const int inf = 0x3f3f3f3f;
int main( ){
   string a,b;
   map<string,string>mp;
   cin>>a;
    while(cin>>a){
    	if(a=="END") break;
        cin>>b;
        mp[b]=a;
    }
    cin>>a;
    char s[3010],c;
    c=getchar();
    while(1){
        int cnt=0;
        while(c=getchar()){
            if(c=='\n')
            break;
            s[cnt++]=c;
        }
        s[cnt]=0;
        char ss[3100];
        if(strcmp(s,"END")==0)break;
        int num=0;
        for(int i=0;i<cnt;i++){
            if(s[i]<='z'&&s[i]>='a'){
                ss[num++]=s[i];//这里我用string失败了,然后直接用的char,我也不知道原因,所以我以后还是直接用char字符串吧
            }
            else{
                ss[num]=0;
                if(mp.find(ss)!=mp.end()){
                    cout<<mp[ss];
                }else{
                    cout<<ss;
                }
                num=0;
                cout<<s[i];
            }
        }
        cout<<endl;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值