• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            bon

              C++博客 :: 首頁 :: 聯系 :: 聚合  :: 管理
              46 Posts :: 0 Stories :: 12 Comments :: 0 Trackbacks

            常用鏈接

            留言簿(2)

            我參與的團隊

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            題目很簡單,給出一個字典,再給一些串,查詢這些串是否在字典中。
            如果對字典排序后用二分查找,時間復雜度為O(N*logN),由于題目沒有給出字典大小,這個界也不好估計。不過我用排序+二分TLE了。
            下面是用Hash Table實現的算法,空間復雜度會高些(因為Hash表要很大的空間,來使得key分散開),但每次插入跟查詢的時間幾乎是O(1),所以93ms就能AC。
             1 #include <iostream>
             2 #include <list>
             3 #include <string>
             4 #include <algorithm>
             5 
             6 using namespace std;
             7 const int maxn=50000;
             8 struct node{
             9     int idx;
            10     node *next;
            11 }htable[maxn];
            12 
            13 //node *htable[maxn];
            14 char dic[maxn][200];
            15 
            16 unsigned long hash(char *key)
            17 {
            18     unsigned long h = 0;
            19     while(*key)
            20     {
            21         h = (h << 4+ *key++;
            22         unsigned long g = h & 0xf0000000L;
            23         if(g)
            24             h ^= g >> 24;
            25         h &= ~g;
            26     }
            27     return h % maxn;
            28 }
            29 
            30 void insert(char *key,int idx)
            31 {
            32     unsigned long i=hash(key);
            33     node *cur=htable[i].next;
            34     node *pre=&htable[i];
            35     while(cur!=NULL && strcmp(dic[cur->idx],key)!=0) {pre=cur;cur=cur->next;}
            36     // 將新的key加入到hash table中
            37     if(cur==NULL){
            38         node *a=new node();
            39         a->idx=idx;
            40         a->next=NULL;
            41         pre->next=a;
            42     }
            43 }
            44 
            45 bool search(char *key)
            46 {
            47     unsigned long i=hash(key);
            48     node *cur=htable[i].next;//,*pre=NULL;
            49     while(cur!=NULL && strcmp(dic[cur->idx],key)!=0) {cur=cur->next;}
            50     if(cur==NULL) return false;
            51     return true;
            52 }
            53 
            54 int main()
            55 {
            56     //freopen("in.txt","r",stdin);
            57     char s[200];
            58     int i,j,k,n;
            59     scanf("%d",&n);
            60 
            61     // initialization
            62     for(i=0;i<maxn;i++) htable[i].next=NULL;
            63     // 插入數據
            64     for(i=0;i<n;i++){
            65         getchar();
            66         scanf("%s",s);
            67         strcpy(dic[i],s);
            68         insert(s,i);
            69     }
            70 
            71     scanf("%d",&n);
            72     for(i=0;i<n;i++){
            73         int flag=0;
            74         while(true){
            75             scanf("%s",s);
            76             if(strcmp(s,"-1")==0break;
            77             if(!search(s)){
            78                 if(flag==0){
            79                     printf("Email %d is not spelled correctly.\n",i+1);
            80                     flag=1;
            81                 }
            82                 printf("%s\n",s);
            83             }
            84         }
            85         if(flag==0) printf("Email %d is spelled correctly.\n",i+1);
            86     }
            87     printf("End of Output\n");
            88     return 1;
            89 }

            這是超時的版本,用了list的STL跟binary_search函數來查找。但超時可能是因為用C++的STL和輸入輸出,因為Discuss中有人用快排+二分過的。
             1 list<string> dic;//=new list<string>(51000);
             2 char tmp[200];
             3 int main()
             4 {
             5     int i,j,k,n;
             6     string s;
             7     scanf("%d",&n);
             8     for(i=0;i<n;i++) {getchar();scanf("%s",tmp);dic.push_back(new string(tmp));}
             9     //dic.insert("anc");
            10     //sort(dic.begin(),dic.end());
            11     dic.sort();
            12     for(list<string>::iterator it=dic.begin();it!=dic.end();it++) cout<<(*it)<<endl;
            13     scanf("%d",&n);
            14     for(i=0;i<n;i++){
            15         int flag=1;
            16         while(cin>>&& s!="-1"){
            17             if(!binary_search(dic.begin(),dic.end(),s)){
            18                 if(flag==1){
            19                     printf("Email %d is not spelled correctly.\n",i+1);
            20                     flag=0;
            21                 }
            22                 cout<<s<<endl;
            23             }
            24         }
            25         if(flag==1) printf("Email %d is spelled correctly.\n",i+1);
            26     }
            27     printf("End of Output\n");
            28     return 1;
            29 }
            posted on 2008-03-16 13:55 bon 閱讀(208) 評論(0)  編輯 收藏 引用 所屬分類: Programming Contest
            Google PageRank 
Checker - Page Rank Calculator
            久久久久无码专区亚洲av| 麻豆一区二区99久久久久| 伊人久久综在合线亚洲2019| 久久99国内精品自在现线| 久久久久久青草大香综合精品| 四虎国产精品成人免费久久| 久久亚洲sm情趣捆绑调教| 嫩草影院久久国产精品| 热久久视久久精品18| 久久精品www| 久久人与动人物a级毛片| 伊人久久大香线焦综合四虎| 亚洲人AV永久一区二区三区久久| 99久久精品午夜一区二区 | 99久久婷婷国产综合亚洲| 热RE99久久精品国产66热| 国产精品一久久香蕉国产线看观看 | 久久影视国产亚洲| 青青青国产成人久久111网站| 亚洲性久久久影院| 久久精品国产欧美日韩| 久久综合久久久| 久久久久久国产精品无码超碰| 亚洲欧美日韩精品久久亚洲区| 日本久久久久久中文字幕| 久久亚洲精品国产精品| 亚洲国产精品高清久久久| 99久久这里只精品国产免费| 久久久精品人妻无码专区不卡| 色综合久久最新中文字幕| 国产精品免费看久久久| 久久ww精品w免费人成| 亚洲va久久久噜噜噜久久狠狠 | 久久只这里是精品66| 久久人人爽人人精品视频| 狠狠精品久久久无码中文字幕 | 99久久国产热无码精品免费久久久久 | 久久毛片一区二区| 久久国产欧美日韩精品| 怡红院日本一道日本久久| 中文精品久久久久国产网址|