What Are You Talking About HDU1075

一开始我也想用map  但是处理不好其他字符。。

看了题解   多多学习! 很巧妙  就是粗暴的一个字符一个字符的来 分为小写字母和非小写字母两个部分  一但单词结束的时候就开始判断。

#include<bits/stdc++.h>
using namespace std;

int main()
{
    string a,b;
    map<string ,string >ma;
    cin>>a;
    while(cin>>a&&a!="END")
    {
        cin>>b;ma[b]=a;

    }
    cin>>a;
    char s[3500];getchar();
    while(gets(s))
    {
        if(!strcmp(s,"END"))break;

        int n=strlen(s);
        a="";
        for(int i=0;i<n;i++)
        {
         if(islower(s[i]))a+=s[i];
         else
         {
             if(ma.find(a)!=ma.end())
                cout<<ma[a];

             else
                 cout<<a;

             cout<<s[i];
             a="";


         }


        }
        cout<<endl;


    }




}
View Code

 

字典树写法

注意malloc   和初始化  字符串赋值用strcpy  不申请内存根本无法使用

因为 gets 和getchar 的问题检查了半小时  注意!!!

gets会吸收\n给忘记了。。。。

有关字典树的指针写法规范一下

#include <iostream>
#include <cstdio>
#include <cstring>
#include<malloc.h>
using namespace std;

struct node
{
    char *val;
    node *next[26];
    int flag;
    node()
    {
        for(int i=0;i<26;i++)
        {
            next[i]=NULL;

        }
        flag=0;
    }
};
node *p,*root=new node();

void change(char *s,char *v)
{
    p=root;
    for(int i=0;s[i]!='\0';i++)
    {
        int ch=s[i]-'a';
        if(p->next[ch]==NULL)
            p->next[ch]=new node();
        p=p->next[ch];

    }
    p->flag=1;
    p->val=(char*)malloc((strlen(v)+1)*sizeof(char));
    strcpy(p->val,v);

}

void find1(char *s)
{
    p=root;
    for(int i=0;s[i]!='\0';i++)
    {
        int ch=s[i]-'a';
        if(p->next[ch]==NULL)
        {
            printf("%s",s);return;
        }
        p=p->next[ch];
    }
   if(p->flag)printf("%s",p->val);
   else   printf("%s",s);

}

int main()
{
     char a[3010],b[3010];
     gets(a);
     while(scanf("%s",a)==1)
     {
         if(!strcmp(a,"END"))break;
         scanf("%s",b);
         change(b,a);

     }
    
     getchar();
     gets(a);
     char s[3010];
     while(gets(a))
     {
         if(!strcmp(a,"END"))break;
         int k=0;
         for(int i=0;i<strlen(a);i++)
         {
             if(islower(a[i]))s[k++]=a[i];
             else
             {
                 s[k]='\0';
                 find1(s);
                 printf("%c",a[i]);
                 k=0;
             }
         }

         printf("\n");
     }

return 0;
}
View Code

 

转载于:https://www.cnblogs.com/bxd123/p/10344411.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值