• <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 閱讀(201) 評論(0)  編輯 收藏 引用 所屬分類: Programming Contest
            Google PageRank 
Checker - Page Rank Calculator
            国产福利电影一区二区三区久久久久成人精品综合 | 亚洲欧美日韩久久精品第一区| 久久99国内精品自在现线| 免费观看成人久久网免费观看| 日本一区精品久久久久影院| 久久精品亚洲男人的天堂| 久久这里只有精品18| 国产成人AV综合久久| 影音先锋女人AV鲁色资源网久久| 久久91精品国产91久久小草| 久久综合视频网| 久久美女人爽女人爽| 一本色道久久综合| 久久99精品免费一区二区| 亚洲AV无码久久精品蜜桃| 亚洲伊人久久综合影院| 99久久精品久久久久久清纯| 亚洲第一极品精品无码久久| 久久久黄片| 66精品综合久久久久久久| 日韩久久久久久中文人妻| 亚洲欧美另类日本久久国产真实乱对白| 日韩精品无码久久久久久| 蜜桃麻豆WWW久久囤产精品| 久久www免费人成看国产片| 韩国无遮挡三级久久| 久久亚洲精品成人AV| 久久久久久精品免费看SSS| 久久亚洲欧洲国产综合| 久久久WWW免费人成精品| 一本大道加勒比久久综合| 久久精品国产精品亚洲精品| 久久精品日日躁夜夜躁欧美| 尹人香蕉久久99天天拍| 少妇久久久久久被弄到高潮| 青青草国产精品久久久久| 国产亚洲色婷婷久久99精品| 久久亚洲中文字幕精品一区| 久久精品国产亚洲7777| 午夜精品久久久久久影视777| 色婷婷久久久SWAG精品|