1. 程式人生 > >HDU1075 What Are You Talking About(字典樹+映射)

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

void scan return 字符串 strlen ons 直接 esp strcmp

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。。。。可能也是自己不太仔細的原因吧!~~在此記錄一下,希望以後少犯這樣低級錯誤!~!

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