• <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>

            This blog has been shut down permanently.

              C++博客 :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
              13 隨筆 :: 0 文章 :: 25 評論 :: 0 Trackbacks

            最開始用了很原始的數(shù)據(jù)初始化方法,多謝dskit指點(diǎn),現(xiàn)在已經(jīng)改正為哈希字典了。因為在創(chuàng)建新節(jié)點(diǎn)的時候浪費(fèi)時間比較嚴(yán)重,所以如果改成用向量會更好一些。

            #include<stdio.h>
            #include
            <stdlib.h>
            int map[25]= {2223334445556667077888999};
            typedef 
            struct LNode{
                
            int data[8];
                
            struct LNode *next;
            }
            ;
            int main()
            {
                
            int row,c,tag,i,j,n,temp[7];//輸入次數(shù)
                n=0;//電話號碼個數(shù)
                LNode *head=0//指向鏈表頭節(jié)點(diǎn)
                LNode *front= 0//指向當(dāng)前節(jié)點(diǎn)的前一位置
                LNode *tail= 0//指向當(dāng)前遍歷到的節(jié)點(diǎn)
                scanf("%d\n",&row);
                
            if(row<=0&&row>100000return 0;
                
            for(i=0; i<row; i++)//將有效輸入存入temp數(shù)組
                {
                   
            for(j=0; (c= getchar())&&(c!='\n')&&(j<7); )
                   
            {
                       
            if(c!='-')
                       
            {
                           
            if(c>='A'&&c<='Y')temp[j++]= map[c-'A'];
                           
            else if(c>='0'&&c<='9')temp[j++]= c-'0';
                       }

                   }

                   
            if(n==0//新建第一節(jié)點(diǎn)
                   {
                       LNode 
            *p= (LNode *)malloc(sizeof(LNode));
                       
            for(int j=0; j<7; j++)
                           p
            ->data[j]= temp[j];
                       p
            ->data[7]= 1;
                       p
            ->next= 0;
                       head
            = p;
                       n
            ++;
                       
            continue;
                   }

                   
            else //新建非第一節(jié)點(diǎn)
                   {
                       tag
            = -1;
                       
            for(front= head,tail= head; tail!= 0; front= tail,tail= tail->next)
                       
            {
                            
            for(j=0; j<7; j++)
                            
            {
                                
            if(tail->data[j]>temp[j])   //找到的目標(biāo)節(jié)點(diǎn)值大于temp
                                {
                                    
            if(front==tail){tag=2;break;}
                                    
            else {tag=3;break;}
                                }

                                
            else if(tail->data[j]<temp[j])  //目標(biāo)節(jié)點(diǎn)值小于temp
                                {
                                    
            if(tail->next==0){tag= 0;break;}
                                    
            else break;
                                }

                                
            else if(j==6&&tail->data[j]==temp[j]){tag=1;tail->data[7]+=1;break;} //目標(biāo)節(jié)點(diǎn)值等于temp
                            }

                            
            if(tag==-1)continue;      //未找到插入點(diǎn)
                            else if(tag==1)break;     //目標(biāo)節(jié)點(diǎn)值等于temp
                            else if(tag==0)           //目標(biāo)節(jié)點(diǎn)值小于temp
                            {
                                LNode 
            *p= (LNode *)malloc(sizeof(LNode));
                                
            for(j=0; j<7; j++)
                                    p
            ->data[j]= temp[j];
                                p
            ->data[7]= 1;
                                p
            ->next= 0;
                                tail
            ->next= p;
                                n
            ++;
                                
            break;
                            }

                            
            else if(tag==2)   //找到的目標(biāo)節(jié)點(diǎn)值大于temp,目標(biāo)節(jié)點(diǎn)為頭結(jié)點(diǎn)
                            {
                                LNode 
            *p= (LNode *)malloc(sizeof(LNode));
                                
            for(j=0; j<7; j++)
                                    p
            ->data[j]= temp[j];
                                p
            ->data[7]= 1;
                                p
            ->next= head;
                                head
            = p;
                                n
            ++;
                                
            break;
                            }

                            
            else if(tag==3)   //找到的目標(biāo)節(jié)點(diǎn)值大于temp,目標(biāo)節(jié)點(diǎn)非頭結(jié)點(diǎn)
                            {
                                LNode 
            *p= (LNode *)malloc(sizeof(LNode));
                                
            for(j=0; j<7; j++)
                                   p
            ->data[j]= temp[j];
                                p
            ->data[7]= 1;
                                p
            ->next= tail;
                                front
            ->next= p;
                                n
            ++;
                                
            break;
                            }

                        }

                    }

                }

                tag
            = 0;
                
            for(tail= head; tail!=0; tail= tail->next)    //遍歷鏈表輸出
                {
                    
            if(tail->data[7]>1)
                    
            {
                        tag
            = 1;
                        
            for(i=0; i<8; i++)
                        
            {
                            
            if(i==3)printf("-");
                            
            if(i==7)printf(" ");
                            printf(
            "%d",tail->data[i]);
                        }

                        printf(
            "\n");
                    }

                }

                
            if(tag==0)printf("No duplicates.");
            }
            posted on 2009-11-11 00:44 iZ 閱讀(1868) 評論(4)  編輯 收藏 引用

            評論

            # re: POJ1002鏈表算法 2009-11-11 21:54 夢芭莎
            不錯,收藏一下算法,省得自己寫  回復(fù)  更多評論
              

            # re: POJ1002鏈表算法 2009-11-12 15:18 凡客誠品官方網(wǎng)
            收藏了!!  回復(fù)  更多評論
              

            # re: POJ1002鏈表算法 2009-11-15 21:51 iSsay
            其實可以優(yōu)化的地方很多,就是不知道該怎么改了  回復(fù)  更多評論
              

            # re: POJ1002鏈表算法[未登錄] 2009-11-16 22:44 dskit
            #include<iostream>
            using std::cin;
            using std::cout;
            using std::endl;

            #ifdef DEBUG
            #include<string>
            using std::string;
            using std::freopen;
            string input_file_name = __FILE__;
            #endif

            //here add other file need to included, and declare namespace need to use.
            #include<stdlib.h>
            #include<string.h>
            #include<stdio.h>
            using namespace std;
            //here declare global variables;
            char dictionary[26] = {2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 0, 7, 7, 8, 8, 8, 9, 9, 9, 0};
            char no[50];
            char oarray[10];
            bool flag;

            struct Trie_node
            {
            int count;
            Trie_node* next[10];
            };

            Trie_node root;

            Trie_node node_pool[200002];
            int index;
            Trie_node* p;

            void insert(char* s)
            {
            p = &root;
            while(*s != '\0')
            {
            if(p->next[*s - '0'] == NULL)
            p->next[*s - '0'] = &node_pool[index++];
            p = p->next[*s - '0'];
            ++s;
            }

            ++p->count;
            }

            void inline travel(Trie_node* node, int c)
            {
            for(int i = 0; i < 10; ++i)
            {

            if(node->next[i] == NULL)
            continue;

            oarray[c] = (char)(i + '0');

            if(c >= 6)
            {
            if(node->next[i]->count > 1)
            {
            flag = true;
            printf("%c%c%c-%c%c%c%c %d\n", oarray[0], oarray[1],
            oarray[2], oarray[3],
            oarray[4], oarray[5],
            oarray[6], node->next[i]->count);
            }
            }
            else
            {
            travel(node->next[i], c + 1);
            }
            }

            }


            //here add funtions.
            void inline format_number(char* no)
            {
            //int len = strlen(no);
            //printf("%s\n", no);
            int count = 0;
            int sum = 0;
            for(int i = 0; no[i] != '\0'; ++i)
            {
            ++sum;
            if(no[i] >= 'A' && no[i] <= 'Z')
            {
            no[i] = '0' + dictionary[no[i] - 'A'];
            }
            else if(no[i] == '-')
            {
            ++count;
            }

            if(no[i] != '-')
            no[i - count] = no[i];

            }
            no[sum - count] = '\0';
            //printf("%s\n", no);
            }

            int main(int argc, char* argv[])
            {
            #ifdef DEBUG
            if(!freopen((input_file_name.substr(0, input_file_name.size() - 4) + ".in").c_str(), "r", stdin))
            { cout << "input data error, not found input file." << endl; return -1; }
            #endif

            //here add code for solve problem.
            int n = 0;
            //input
            scanf("%d", &n);
            flag = false;
            index = 0;
            getchar();
            for(int i = 0; i < n; ++i)
            {
            gets(no);
            //puts(no);
            format_number(no);
            insert(no);
            }

            travel(&root, 0);

            if(!flag)
            {
            printf("No duplicates.\n");
            }

            return 0;
            }
              回復(fù)  更多評論
              


            只有注冊用戶登錄后才能發(fā)表評論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            日本精品一区二区久久久| 麻豆亚洲AV永久无码精品久久| 久久综合久久综合久久| 久久毛片免费看一区二区三区| 少妇无套内谢久久久久| 亚洲午夜久久久久久噜噜噜| AV色综合久久天堂AV色综合在| 精品国产热久久久福利| 伊人久久大香线蕉综合影院首页| 国产精品久久久久天天影视| 伊人精品久久久久7777| 四虎国产精品免费久久久| 久久综合狠狠综合久久| 久久久久亚洲国产| 久久se精品一区精品二区国产| 久久久久久九九99精品| 国产精品久久久久久久久久影院| 久久精品免费观看| 久久男人Av资源网站无码软件| 日本加勒比久久精品| 国产精品美女久久久久网| 亚洲国产综合久久天堂 | 久久亚洲精品无码播放| 国产精品毛片久久久久久久| 亚洲国产精品久久久天堂| 国产香蕉久久精品综合网| 久久高潮一级毛片免费| 国产高清国内精品福利99久久| 精品乱码久久久久久久| 成人妇女免费播放久久久| 色欲久久久天天天综合网精品| 伊人色综合久久天天人守人婷| 精品国产一区二区三区久久蜜臀| 91久久精品国产成人久久| 四虎国产永久免费久久| 久久青青草原综合伊人| 91精品无码久久久久久五月天| 国产精品欧美久久久久无广告| 久久99精品久久久久久齐齐| 久久免费香蕉视频| 婷婷国产天堂久久综合五月|