posts - 403, comments - 310, trackbacks - 0, articles - 7
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          1. {a, b, c}上的串S中,任何兩個b都不相連,用正則表達式表示為
          (a|c|ba|bc)*(b|空)

           
          2. Pascal注釋的表示

          {(~})*}
          { } 中間為任意非}的符號,注意表達的嚴謹

           

          3. C注釋的表示就困難很多

          例如要表示ba ...(沒有ab)... ab這樣的字符串,不能簡單的寫成

          ba(~(ab))*ab

          因為~非運算符通常只適用于單字符,否則容易產生混淆。

          b*(a*~(a|b)b*)*a*

          像這樣的定義很難讀,而且難以證明其正確性,因此在真正的掃描程序中通常用特殊方法解決。

          posted @ 2007-07-17 20:46 ZelluX 閱讀(424) | 評論 (0)編輯 收藏

          今天收獲真不小啊
          看懂了第一個匯編程序,盡管簡單得不能再簡單了
          然后就是新聞組,盡管以前也去過微軟的中文新聞組,但沒有太多感興趣的內容。今天發現了個news.cn99.com,海量的信息(包括反動信息-,-),人氣也很高,剛問了個如何查看已經安裝的python-doc,10分鐘后就有人回復
          最后是一個關于bbs的彩色效果,其實qterm登錄bbs后本來就只是提供了一個shell,像
          <ESC><ESC>[1;31m
          之類的ANSI記號不是bbs提供的類似api的東西,而是會保存在bbs的原始數據里,qterm端接收這些符號,然后以指定的效果顯示。一個例子就是把這些符號("\033\033[1;31mhello world")顯示在gnome-terminal終端的時候就是用紅色字體顯示的。

          posted @ 2007-07-16 23:38 ZelluX 閱讀(309) | 評論 (0)編輯 收藏

          突然想通了一開始一直超時的原因。
          每次我都是把新的suspect并入第一個元素所在的集合中,但是由于使用了優化后的并集操作,有可能是第一個元素所在的集合并入了新的suspect所在的集合,也就是說此后last變量并沒有指向第一個元素所在集合的跟結點。于是在Union方法中,parent[root1]可能得到一個正整數,并導致Find方法死循環(根結點的parent為正)

          后來把Find方法放到Union方法中,問題解決了。

          #include <iostream>

          using namespace std;

          const int DEFAULT_SIZE = 100;

          class UFSets
          {
          private:
              
          int *parent;
              
          int size;
          public:
              UFSets(
          int s = DEFAULT_SIZE);
              
          ~UFSets() { delete [] parent; }
              
          void Union(int root1, int root2);
              
          int Find(int x);
              
          void Clear(int n);
          };

          UFSets::UFSets(
          int s)
          {
              size 
          = s;
              parent 
          = new int[size + 1];
              
          for (int i = 0; i <= size; i++)
                  parent[i] 
          = -1;
          }

          int UFSets::Find(int x)
          {
              
          int result = x;
              
          while (parent[result] >= 0)
                  result 
          = parent[result];
              
          return result;
          }

          void UFSets::Union(int root1, int root2)
          {
              
          // The old version didn't contain the following 3 setences.
              root1 = Find(root1);
              root2 
          = Find(root2);
              
          if (root1 == root2) return;

              
          int temp = parent[root1] + parent[root2];
              
          if (parent[root2] < parent[root1])
              {
                  parent[root1] 
          = root2;
                  parent[root2] 
          = temp;
              }
              
          else
              {
                  parent[root2] 
          = root1;
                  parent[root1] 
          = temp;
              }
          }

          void UFSets::Clear(int n)
          {
              
          for (int i = 0; i < n; i++)
                  parent[i] 
          = -1;
          }

          int main()
          {
              UFSets sets(
          30000);
              
          int n, m;
              
          while (true)
              {
                  cin 
          >> n >> m;
                  
          if (n == 0break;
                  sets.Clear(n);
                  
          for (int i = 0; i < m; i++)
                  {
                      
          int t;
                      cin 
          >> t;
                      
          int last, x;
                      cin 
          >> last;
                      last 
          = sets.Find(last);
                      
          for (int j = 1; j < t; j++)
                      {
                          cin 
          >> x;
                          sets.Union(last, x);
                          
          // int temp = sets.Find(x);
                          
          // if (temp != last)
                          
          //     sets.Union(last, temp);
                      }
                  }
                  
          int result = 1;
                  
          int target = sets.Find(0);
                  
          for (int i = 1; i < n; i++)
                      
          if (sets.Find(i) == target)
                          result 
          ++;
                  cout 
          << result << endl;
              }
              
          return 0;
          }


          posted @ 2007-07-15 11:12 ZelluX 閱讀(479) | 評論 (0)編輯 收藏

               摘要: 轉載自http://blog.csdn.net/fisher_jiang/archive/2006/08/25/1116918.aspx并查集 (Union-Find Sets) 并查集: (union-find sets)是一種簡單的用途廣泛的集合. 并查集是若干個不相交集合,能夠實現較快的合并和判斷元素所在集合的操作,應用很多。一般采取樹形結構來存儲并查集,并利用一個rank數組來存儲集合的...  閱讀全文

          posted @ 2007-07-14 14:13 ZelluX 閱讀(505) | 評論 (0)編輯 收藏

          PKU1330,普通樹的LCA問題。把結點A的所有父結點都保存在一個hash數組中,然后從結點B開始向上搜索,直到發現hash數組中存在的元素為止。
          #include <stdio.h>
          #include 
          <stdlib.h>

          typedef 
          struct TreeNode *pTree;

          struct TreeNode
          {
              
          int value;
              pTree father;
          };

          main()
          {
              
          int cases, n, i;
              scanf(
          "%d"&cases);
              pTree nodes[
          10000];
              
          for (i = 0; i < 10000; i++)
              {
                  nodes[i] 
          = malloc(sizeof(struct TreeNode));
                  nodes[i]
          ->value = i;
              }
              
          int flag[10000];
              
          while (cases > 0)
              {
                  cases 
          --;
                  scanf(
          "%d"&n);
                  
          for (i = 0; i < n; i++)
                  {
                      nodes[i]
          ->father = NULL;
                  }
                  
          int x, y;
                  
          for (i = 0; i < n - 1; i++)
                  {
                      scanf(
          "%d %d"&x, &y);
                      nodes[y 
          - 1]->father = nodes[x - 1];
                  }
                  scanf(
          "%d %d"&x, &y);
                  
          for (i = 0; i < n - 1; i++)
                      flag[i] 
          = 0;
                  pTree p 
          = nodes[x - 1];
                  
          while (p != NULL)
                  {
                      flag[p
          ->value] = 1;
                      p 
          = p->father;
                  }
                  p 
          = nodes[y - 1];
                  
          while (p != NULL)
                  {
                      
          if (flag[p->value])
                      {
                          printf(
          "%d\n", p->value + 1);
                          
          break;
                      }
                      p 
          = p->father;
                  }
              }
              
          return 0;
          }


          posted @ 2007-07-14 13:24 ZelluX 閱讀(479) | 評論 (0)編輯 收藏

          LCA (Lowest Common Ancestor)
          一篇轉載的文章,是對于二叉搜索樹的算法
          http://blog.csdn.net/AmiRural/archive/2006/06/07/777966.aspx

          [題目]:已知二元搜索樹(Binary Search Tree)上兩個結點的值,請找出它們的公共祖先。你可以假設這兩個值肯定存在。這個函數的調用接口如下所示:
                int FindLowestCommonAncestor(node *root, int value1, int value2);

              根據樹的存儲結構,我們可以立刻得到一個這樣的算法:從兩個給定的結點出發回溯,兩條回溯路線的交點就是我們要找的東西。這個算法的具體實現辦法是:從根 結點開始,先用這兩個結點的全體祖先分別生成兩個鏈表,再把這兩個鏈表第一次出現不同結點的位置找出來,則它們的前一個結點就是我們要找的東西。
              這個算法倒沒有什么不好的地方,但是它沒有利用二元搜索樹的任何特征,其他類型的樹也可以用這個算法來處理。二元搜索樹中左結點的值永遠小于或者等于當前 結點的值,而右結點的值永遠大于或者等于當前結點的值。仔細研究,4和14的最低公共祖先是8,它與4和14的其他公共祖先是有重要區別的:其他的公共祖 先或者同時大于4和4,或者同時小于4和14,只有8介于4和14之間。利用這一研究成果,我們就能得到一個更好的算法。
              從根結點出發,沿著兩個給定結點的公共祖先前進。當這兩個結點的值同時小于當前結點的值時,沿當前結點的左指針前進;當這兩個結點的值同時大于當前結點的 值時,沿當前結點的右指針前進;當第一次遇到當前結點的值介于兩個給定的結點值之間的情況時,這個當前結點就是我們要找的最的最低公共祖先了。
              這是一道與樹有關的試題,算法也有遞歸的味道,用遞歸來實現這一解決方案似乎是順理成章的事,可這里沒有這個必要。遞歸技術特別適合于對樹的多個層次進行 遍歷或者需要尋找某個特殊結點的場合。這道題只是沿著樹結點逐層向下前進,用循環語句來實現有關的過程將更簡單明了。

               int FindLowestCommonAncestor(node *root, int value1, int value2)
               {
                node 
          *curnode = root;
                
          while(1)
                   {
                     
          if (curnode->data>value1&&curnode->data>value2)
                       curnode 
          = curnode->left;
                     
          else if(curnode->data<value1&&curnode->data<value2)
                       curnode 
          = curnode->right;
                     
          else
                       
          return curnode->data;
                    }
                }

          C語言實現:

          /*二叉排序樹的生成及樹,任意兩結點的最低公共祖先        Amirural設計*/
          #include 
          <stdio.h>
          #define  null  0

          int counter=0;
          typedef 
          struct btreenode
          {
          int data;
           
          struct btreenode *lchild;
           
          struct btreenode *rchild;
          }bnode;

          bnode 
          *creat(int x,bnode *lbt,bnode *rbt)   //生成一棵以x為結點,以lbt和rbt為左右子樹的二叉樹
          {bnode *p;
           p
          =(bnode*)malloc(sizeof(bnode));
           p
          ->data=x;
           p
          ->lchild=lbt;
           p
          ->rchild=rbt;
           
          return(p);
          }

          bnode 
          *ins_lchild(bnode *p, int x)    //x作為左孩子插到二叉樹中
          {bnode *q;
          if(p==null)
            printf(
          "Illegal insert.");
          else
           {q
          =(bnode*)malloc(sizeof(bnode));
            q
          ->data=x;
            q
          ->lchild=null;
            q
          ->rchild=null;
            
          if(p->lchild!=null)                //若p有左孩子,則將原來的左孩子作為結點x的右孩子
               q->rchild=p->lchild;
            p
          ->lchild=q;
           }
           
          return(p);
          }

          bnode 
          *ins_rchild(bnode *p, int x)   //x作為右孩子插入到二叉樹
          {bnode *q;
           
          if(p==null)
              printf(
          "Illegal insert.");
           
          else
           {q
          =(bnode*)malloc(sizeof(bnode));
            q
          ->data=x;
            q
          ->lchild=null;
            q
          ->rchild=null;
            
          if(p->rchild!=null)                //若x有右孩子,則將原來的右孩子作為結點x的的左孩子
               q->lchild=p->rchild;
            p
          ->rchild=q;
           }
            
          return(p);
          }

          void prorder(bnode *p)
          {
          if(p==null)
             
          return;
           printf(
          "%d\t%u\t%d\t%u\t%u\n",++counter,p,p->data,p->lchild,p->rchild);
           
          if(p->lchild!=null)
             prorder(p
          ->lchild);
           
          if(p->rchild!=null)
             prorder(p
          ->rchild);
          }

          void print(bnode *p)                //嵌套括號表示二叉樹,輸出左子樹前打印左括號,
          {                                   //輸出右子樹后打印右括號。
            if(p!=null)
            {printf(
          "%d",p->data);
             
          if(p->lchild!=null||p->rchild!=null)
             {printf(
          "(");
            print(p
          ->lchild);
            
          if(p->rchild!=null)
             printf(
          ",");
            print(p
          ->rchild);
            printf(
          ")");
           }
          }
          }

          int FindLowestCommonAncestor(bnode *root, int value1, int value2)
          {
            bnode 
          *curnode = root;
            
          while(1)
            {
             
          if (curnode->data>value1&&curnode->data>value2)
                 curnode 
          = curnode->lchild;
             
          else if(curnode->data<value1&&curnode->data<value2)
                 curnode 
          = curnode->rchild;
             
          else
                 
          return curnode->data;
             }
          }

           
          main()
          {
           bnode 
          *bt,*p,*q;
           
          int x,y,v1,v2;
           printf(
          "輸入根結點:");
           scanf(
          "%d",&x);
           p
          =creat(x,null,null);
           bt
          =p;                                   //使bt p都指向根結點
           printf("輸入新的結點值:");
           scanf(
          "%d",&x);
           
          while(x!=-1)
             {p
          =bt;
              q
          =p;                    
           
          while(x!=p->data&&q!=null)              //q記錄當前根結點
             {p=q;
              
          if(x<p->data)
              q
          =p->lchild;
              
          else
              q
          =p->rchild;
             }
            
          if(x==p->data)
            {printf(
          "元素%d已經插入二叉樹中!\n",x);
            }
            
          else
             
          if(x<p->data)    ins_lchild(p,x);
                
          else             ins_rchild(p,x);
             scanf(
          "%d",&x);
             }
           p
          =bt;
           printf(
          "struct of the binary tree:\n");
           printf(
          "number\taddress\tdata\tlchild\trchild\n");
           prorder(p);
           printf(
          "\n");
           printf(
          "用括號形式輸出二叉樹:");
           print(p);

           printf(
          "\n請任意輸入樹中存在的兩個結點:");
           scanf(
          "%d,%d",&v1,&v2);
           y 
          = FindLowestCommonAncestor(p, v1, v2);
           printf(
          "輸出%d和%d的最低公共祖先:",v1,v2);
           printf(
          "%d\n",y);
          }

           

           運行結果:
          輸入根結點:20
          輸入新的結點值:8 22 4 12 10 14 -1       (以-1結束結點的輸入)
          struct of the binary tree:
          number   addresss      data      lchild         rchild
          1               4391168       20        4391104   4391040
          2               4391104       8          4390976   4399072
          3               4390976      4           0                 0
          4               4399072     12          4399008  4398944
          5               4399008     10          0                0
          6               4398644     14          0                0
          7               4391040     22          0                0
          用括號形式輸出:20(8(4,12(10,14)),22)   (輸出左子樹打印左括號,輸出右子樹后打印括號)
          請任意輸入樹中存在的兩個結點:4,14
          輸出4和14的最低祖先:8



          posted @ 2007-07-14 10:57 ZelluX 閱讀(2830) | 評論 (2)編輯 收藏

          1.一行代碼解決的輾轉相除法
          for(;;){if ((m %= n) == 0) return n;if ((n %= m) == 0) return m;}


          2.推進式的前綴表達式求值,沒見過這種遞歸@@
          char *a; int i;
          int eval()
          {
              
          int x = 0;
              
          while (a[i] == ' ') i++;
              
          if (a[i] == '+')
              {
                  i
          ++;
                  
          return eval() + eval();
              }
              
          if (a[i] == '*')
              {
                  i
          ++;
                  
          return eval() * eval();
              }
              
          while ((a[i] >= '0'&& (a[i] <= '9'))
                  x 
          = 10 * x + (a[i++- '0');
              
          return x;
          }

          posted @ 2007-07-13 11:43 ZelluX 閱讀(359) | 評論 (0)編輯 收藏

          1. 二維數組內存分配
          Algorithm in C上的方法,這種方法好處在于t[i]是指向各行首的指針,類似于Java的數組;而如果直接用malloc分配m*n*sizeof(int)大小的空間,則沒有這種效果,類似于C#中的[,]數組
          int **malloc2d(int r, int c)
          {
              int i;
              int **t = malloc(r * sizeof(int *));
              for (i = 0; i < r; i++)
                  t[i] = malloc(c * sizeof(int));
              return t;
          }

          posted @ 2007-07-12 19:33 ZelluX 閱讀(322) | 評論 (0)編輯 收藏

          1. 函數接收一維數組的形參聲明
          func1(str)
          char str[10];
          {
          }
          表示有界數組,數組的下標只能小于或等于傳遞數組的大小。10可省略,表示無界數組。

          但事實上兩者區別很小,編譯器只是讓函數接收一個指針。

          二維數組類似

          char str[][10];

          2. 宏

          #define MIN(a, b) (a<b) ? a : b

          使用該宏時表達式被直接替換,增加了代碼的速度,但因此增加了程序的長度。

          posted @ 2007-07-10 16:33 ZelluX 閱讀(288) | 評論 (0)編輯 收藏

          1.
          由于Python的判斷語句返回值的特殊性,and-or語句可以達到類似三元運算符的效果。
          bool ? a : b
          可以寫成
          bool and a or b

          2.
          print None 不會輸出任何信息
          需要顯示None,要使用print str(None)

          posted @ 2007-07-10 11:00 ZelluX 閱讀(463) | 評論 (0)編輯 收藏

          僅列出標題
          共39頁: First 上一頁 20 21 22 23 24 25 26 27 28 下一頁 Last 
          主站蜘蛛池模板: 岳阳市| 丘北县| 横峰县| 紫金县| 甘南县| 陆良县| 台州市| 岱山县| 石景山区| 泉州市| 大化| 砀山县| 焉耆| 达孜县| 金华市| 塘沽区| 恩平市| 玉龙| 溧阳市| 秭归县| 自治县| 辛集市| 都昌县| 西平县| 蓬溪县| 古浪县| 顺昌县| 鄱阳县| 都昌县| 砀山县| 呼玛县| 土默特右旗| 海晏县| 祁东县| 惠水县| 金门县| 玛多县| 巴东县| 栖霞市| 巴林右旗| 克拉玛依市|