최 장 LCS
 #include <iostream>
#include <iterator>
#include <vector>
#include <initializer_list>  template<typename T=char, typename U=int>
class CountLCS{
 private:
  std::vector<T> X;//   DNA  
  std::vector<T> Y;//   DNA  
  std::vector<std::vector<U>> B; 
  std::vector<std::vector<U>> C;
  
  template<typename u=int>
  void printLCS(const u& I, const u& J);
  public:
   template<typename Type>//               Type=T         . 
   CountLCS(const std::initializer_list<Type>& il_one, const std::initializer_list<Type>& il_two);
   
   ~CountLCS();
   void LCSLength();
   void print();
};  template<typename T, typename U>
template<typename Type>
CountLCS<T, U>::CountLCS(const std::initializer_list<Type>& il_one, const std::initializer_list<Type>& il_two)
               try:X(il_one),
                   Y(il_two)
{
 //std::cout<<"xxxx"<<std::endl;
  
  if(X.empty() || Y.empty()){
   throw std::string("they must be not empty
");
   }
   
   int x=X.size();
   int y=Y.size();
   std::vector<U> container_B(x);
   std::vector<U> container_C(y+1);
   
   for(int i=0; i<x+1; ++i){
    C.push_back(container_C);
    if( i>=1 ){
     B.push_back(container_B);
    }
   }
   
  std::cout<<"success to create"<<std::endl;
  
}catch(const std:: string errorMessage){
 std::cout<<errorMessage<<std::endl;
}  template<typename T, typename U>
CountLCS<T, U>::~CountLCS()
{
 X.clear();
 Y.clear();
 int temp=B.size();
 for(int i=0; i<temp; ++i){
  B[i].clear();
  C[i].clear();
 }
 C[temp].clear();
 std::cout<<"destroy it"<<std::endl;
}  template<typename T, typename U>
void CountLCS<T, U>::LCSLength()
{
 int m=this->X.size();
 int n=this->Y.size();
 
 std::cout<<"success to enter"<<std::endl;
 for(int i=1; i<m+1; ++i){
  for(int j=1; j<n+1; ++j){
   
   if(X[i-1] == Y[j-1]){ //                 . 
    C[i][j] = C[i-1][j-1]+1; //                      +1; 
    B[i-1][j-1] = 1; 
    
   }else{
   
    if(C[i-1][j] >= C[i][j-1]){ //                            , 
    C[i][j] = C[i-1][j];
    B[i-1][j-1] = 2; 
    
   }else{
    C[i][j] = C[i][j-1];
    B[i-1][j-1] = 3;
   }
  }
 }
}
}  template<typename T, typename U>
void CountLCS<T, U>::print()
{
 int m=this->X.size();
 int n=this->Y.size();
 std::ostream_iterator<U> out(std::cout, "  ");
 
 std::cout<<"print C:"<<std::endl;
 //std::cout<<B[0][0]<<std::endl;
 //for(int i=0; i<n; ++i){
  //copy(B[i].begin(), B[i].begin(), out);
  //std::cout<<std::endl;
 //}
 
 for(int i=0; i<m+1; ++i){
  for(int j=0; j<n+1; ++j){
   *(out++)=C[i][j];
  }
  std::cout<<std::endl;
 }
 
 std::cout<<"print LCS:"<<std::endl;
 //this->printLCS(m-1, n-1);
}  template<typename T, typename U>
template<typename u>
void CountLCS<T, U>::printLCS(const u& I, const u& J)
{
 
 //std::cout<<"test"<<std::endl;
 if(I==0 || J==0){//       0           . 
  return;
 }
 //std::cout<<"test"<<std::endl;
 if(B[I][J] == 1){//  B       1     X Y         。
 std::cout<<"test"<<std::endl;
     printLCS(I-1, J-1); 
  std::cout<<X[I]<<"  ";
  
 }else if(B[I][J] == 2){ 
  printLCS(I-1, J);
  
 }else{
  printLCS(I, J-1);
  
 }
}  int main()
{
 //std::cout<<"123"<<std::endl;
 CountLCS<> myLCS({'A', 'B', 'C', 'B', 'D', 'A', 'B'}, {'B', 'D', 'C', 'A', 'B', 'A'});
 myLCS.LCSLength();
 myLCS.print();
 return 0;
}
                이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.