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)  編輯  收藏


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


          網站導航:
           
          主站蜘蛛池模板: 永川市| 桐庐县| 扬州市| 湛江市| 新乡县| 凉山| 平远县| 清苑县| 清丰县| 灵山县| 思南县| 宜宾市| 射洪县| 大厂| 泸西县| 昭平县| 普宁市| 随州市| 郯城县| 孝义市| 宜君县| 长宁县| 卓资县| 尖扎县| 桃江县| 扶余县| 昌黎县| 清水县| 桐梓县| 兴和县| 全州县| 泽州县| 晋江市| 洛隆县| 涞源县| 喀喇沁旗| 拉孜县| 历史| 克东县| 孝昌县| 浦北县|