• <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++博客 :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
              13 隨筆 :: 0 文章 :: 25 評論 :: 0 Trackbacks

            最開始用了很原始的數據初始化方法,多謝dskit指點,現在已經改正為哈希字典了。因為在創建新節點的時候浪費時間比較嚴重,所以如果改成用向量會更好一些。

            #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];//輸入次數
                n=0;//電話號碼個數
                LNode *head=0//指向鏈表頭節點
                LNode *front= 0//指向當前節點的前一位置
                LNode *tail= 0//指向當前遍歷到的節點
                scanf("%d\n",&row);
                
            if(row<=0&&row>100000return 0;
                
            for(i=0; i<row; i++)//將有效輸入存入temp數組
                {
                   
            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//新建第一節點
                   {
                       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 //新建非第一節點
                   {
                       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])   //找到的目標節點值大于temp
                                {
                                    
            if(front==tail){tag=2;break;}
                                    
            else {tag=3;break;}
                                }

                                
            else if(tail->data[j]<temp[j])  //目標節點值小于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;} //目標節點值等于temp
                            }

                            
            if(tag==-1)continue;      //未找到插入點
                            else if(tag==1)break;     //目標節點值等于temp
                            else if(tag==0)           //目標節點值小于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)   //找到的目標節點值大于temp,目標節點為頭結點
                            {
                                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)   //找到的目標節點值大于temp,目標節點非頭結點
                            {
                                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 閱讀(1864) 評論(4)  編輯 收藏 引用

            評論

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

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

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

            # 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;
            }
              回復  更多評論
              

            香蕉久久久久久狠狠色| 国内精品久久久久久99| 青青草原综合久久大伊人| 国内精品伊人久久久久妇| 无码超乳爆乳中文字幕久久| 7777久久亚洲中文字幕| 久久精品国产WWW456C0M| 99久久综合国产精品免费| 99久久人妻无码精品系列| 久久se这里只有精品| 久久超乳爆乳中文字幕| 久久人妻少妇嫩草AV蜜桃| 精品国产乱码久久久久久郑州公司 | 精品综合久久久久久98| 99久久精品影院老鸭窝| 四虎国产精品免费久久| 久久国产精品久久| 性欧美丰满熟妇XXXX性久久久 | 免费观看成人久久网免费观看| 久久久久亚洲AV成人网人人软件| 久久久久九九精品影院| 婷婷久久久亚洲欧洲日产国码AV| 国产一区二区精品久久岳| 久久久久久国产精品免费无码 | 99久久精品国产高清一区二区 | 精品久久久久久国产| 丁香五月网久久综合| 国产亚州精品女人久久久久久 | 国产精品成人99久久久久| 久久久免费观成人影院| 久久久久亚洲精品日久生情| 久久天天躁狠狠躁夜夜96流白浆| 国产精品久久永久免费| 欧洲性大片xxxxx久久久| 午夜人妻久久久久久久久| 久久国产高清字幕中文| 区亚洲欧美一级久久精品亚洲精品成人网久久久久 | 国产成人无码精品久久久性色| 99国产精品久久| 久久亚洲国产成人影院| 久久天堂电影网|