so true

          心懷未來,開創未來!
          隨筆 - 160, 文章 - 0, 評論 - 40, 引用 - 0
          數據加載中……

          LCS

          http://blog.csdn.net/v_JULY_v/article/details/6110269

          我的算法,本質上和上篇博客中提到的算法是一樣的:
          #include <iostream>
          #include <string>
          #include <fstream>
          #include <sstream>
          #include <stdint.h>
          #include <string.h>
          #include <pthread.h>
          #include <vector>
          #include <map>
          #include <set>

          using namespace std;

          int LCS(const char* X, const char* Y, char* R) {
              if (NULL == X || NULL == Y || NULL == R) {
                  return 0;
              }

              int xlen = strlen(X);
              int ylen = strlen(Y);

              map<int, map<int, int> > D;
              for (int i = 0; i < xlen; ++i) {
                  int max = 0;
                  for (int j = 0; j < ylen; ++j) {
                      max = std::max(X[i] == Y[j] ? 1 : 0, max);
                      if (i > 0) {
                          max = std::max(D[i - 1][j], max);
                          if (j > 0) {
                              max = std::max(D[i - 1][j - 1] + (X[i] == Y[j] ? 1 : 0), max);
                          }
                      }
                      D[i][j] = max;
                      printf("(%d,%d) = %d\n", i, j, max);
                  }
              }

              return D[xlen - 1][ylen - 1];
          }

          int main(int argc, char* argv[]) {
              const char* X = argc > 1 ? argv[1] : "abacbda";
              const char* Y = argc > 2 ? argv[2] : "cbada";
              char R[1024];
              printf("X:%s\n", X);
              printf("Y:%s\n", Y);
              int ret = LCS(X, Y, R);
              printf("ret:%d\n", ret);
              return 0;
          }

          posted on 2015-02-20 15:37 so true 閱讀(194) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 乐亭县| 鄂伦春自治旗| 尼木县| 浦城县| 泽普县| 礼泉县| 静海县| 德化县| 乐平市| 浏阳市| 双柏县| 佛冈县| 巴中市| 虞城县| 遂川县| 西青区| 新宾| 綦江县| 鲁山县| 萍乡市| 罗城| 定结县| 高邮市| 天气| 红河县| 柳林县| 六盘水市| 金坛市| 永清县| 保康县| 呼图壁县| 监利县| 泸定县| 德令哈市| 穆棱市| 鄄城县| 樟树市| 揭东县| 通海县| 甘孜县| 洞口县|