HDU1075 What Are You Talking About(字典树+映射)

http://acm.hdu.edu.cn/showproblem.php?pid=1075

题意没什么好说的,就是一个字典树的查找。

这道题主要在于格式的输出上,反正坑了我好久的!~!在此吐槽吐槽,以此来平复心情。坑人啊!~!~!~

还有就是刚开始用的是字典树的v与数组id相对应来做的,结果一直RE,每次都把数组开效率,经过小小的测试5e5是能过的(应该可以在小点吧!没去测试)

还有一个坑点就是,如果你使用的是 字典树里面就已经包含了与之对应的单词,一定要注意在查询玩之后不能直接返回这个单词,还应该判断是否有这个单词,这就要我们再开一个变量来标记字典树中字符串的最后一位。(因为有可能这个单词不存在但是是另一个单词的前缀,在这里也是错了几次)具体的还是看代码吧!~·

 

#include<cstdio>
#include<string.h>
#include<algorithm>
#include<vector>
#include<iostream>
using namespace std;
const int MAX=26;
const int maxn=4000;
int N;
char ans[12];
struct Trie
{
    Trie *next[MAX];
    char v[12];
    int pos;
    Trie()
    {
        for(int i=0;i<MAX;i++)
            next[i]=NULL;
        pos=0;
    }
};
Trie root;
int Get_ID(char c){
    return c-'a';
}
void Insert(char s[])
{
    Trie *p=&root;
    int len_s=strlen(s);
    for(int i=0; i<len_s; i++)
    {
        int id=Get_ID(s[i]);
        if(p->next[id]==NULL)
        {
           p->next[id]=new Trie;
        }
        p=p->next[id];
    }
    p->pos=1;
    strcpy(p->v,ans);
}
char *Search(char s[])
{
    Trie *p=&root;
    int len_s=strlen(s);
    for(int i=0; i<len_s; i++)
    {
        int id=Get_ID(s[i]);
        if(p->next[id]==NULL)
            return NULL;
        p=p->next[id];
    }
    if(p->pos)
        return p->v;
    else
        return NULL;
}
int main()
{
    scanf("%s",ans);
    while(scanf("%s",ans)&&strcmp(ans,"END")!=0)
    {
        char s1[maxn];
        scanf("%s",s1);
        Insert(s1);
    }
    scanf("%s",ans);
    getchar();
    char s2[maxn];
    while(gets(s2)&&strcmp(s2,"END")!=0)
    {
        int len=strlen(s2);
        char s3[maxn];
        for(int i=0; i<len; i++)
        {
            if(s2[i]>='a'&&s2[i]<='z')
            {
                int j=0;
                while(s2[i]>='a'&&s2[i]<='z')
                {
                    s3[j]=s2[i];
                    i++;
                    j++;
                }
                i--;
                s3[j]='\0';
                char *t=Search(s3);
                if(t==NULL)
                {
                    printf("%s",s3);
                }
                else
                    printf("%s",t);
            }
            else
                printf("%c",s2[i]);
        }
        puts("");
    }
    return 0;
}

另外一种就是开着数组来记录的,这种情况反正RE了好多次,一直都以为自己的数组开的应该是足够大的,最后才发现数组还是小了点!~!

  1 #include<cstdio>
  2 #include<string.h>
  3 #include<algorithm>
  4 #include<vector>
  5 #include<iostream>
  6 using namespace std;
  7 const int MAX=26;
  8 const int maxn=500000;
  9 int N;
 10 int cnt;
 11 char ans[maxn][20];
 12 
 13 struct Trie
 14 {
 15     Trie *next[MAX];
 16     int v;
 17     Trie()
 18     {
 19         for(int i=0;i<MAX;i++)
 20             next[i]=NULL;
 21         v=0;
 22     }
 23 };
 24 Trie root;
 25 int Get_ID(char c){
 26     return c-'a';
 27 }
 28 
 29 void Insert(char s[])
 30 {
 31     Trie *p=&root;
 32     int len_s=strlen(s);
 33     for(int i=0; i<len_s; i++)
 34     {
 35         int id=Get_ID(s[i]);
 36         if(p->next[id]==NULL)
 37         {
 38            p->next[id]=new Trie;
 39         }
 40         p=p->next[id];
 41     }
 42     p->v=cnt;
 43 }
 44 int Search(char s[])
 45 {
 46     Trie *p=&root;
 47     int len_s=strlen(s);
 48     for(int i=0; i<len_s; i++)
 49     {
 50         int id=Get_ID(s[i]);
 51         if(p->next[id]==NULL)
 52             return 0;
 53         p=p->next[id];
 54     }
 55     return p->v;
 56 }
 57 int main()
 58 {
 59     cnt=1;
 60     while(scanf("%s",ans[++cnt]))
 61     {
 62         if(ans[cnt][0]=='S')
 63             continue;
 64         if(ans[cnt][0]=='E')
 65             break;
 66         char s1[maxn];
 67         scanf("%s",s1);
 68         Insert(s1);
 69     }
 70     char s2[maxn];
 71     getchar();
 72     while(gets(s2))
 73     {
 74         if(s2[0]=='S')
 75             continue;
 76         if(s2[0]=='E')
 77             break;
 78         int len=strlen(s2);
 79         char s3[maxn];
 80         int tem=0;
 81         for(int i=0; i<len; i++)
 82         {
 83             while(s2[i]>='a'&&s2[i]<='z')
 84             {
 85                 s3[tem]=s2[i];
 86                 i++;
 87                 tem++;
 88             }
 89             s3[tem]='\0';
 90             int t=Search(s3);
 91             if(t){
 92                 printf("%s",ans[t]);
 93             }
 94             else if(tem)
 95                 printf("%s",s3);
 96             if(s2[i]<'a'||s2[i]>'z')
 97                 printf("%c",s2[i]);
 98             tem=0;
 99         }
100         printf("\n");
101     }
102     return 0;
103 }

 这道题在做的时候要考虑太多的细节问题了,稍不注意就会WA,RE。。。。可能也是自己不太仔细的原因吧!~~在此记录一下,希望以后少犯这样低级错误!~!

posted @ 2018-03-18 14:44  孟加拉国  阅读(251)  评论(0编辑  收藏  举报