LCS算法示例的主函数调用

作者finallyly 出处 博客园(转载请注明作者和出处)

LCS算法示例的主函数调用 main.cpp
#include  " stdafx.h "
#include 
" stringprocess.h "
#include 
< iostream >
#include 
" windows.h "
using   namespace  std;
wstring String2Wstring(
string  sResult)
{
    
int  iWLen = MultiByteToWideChar( CP_ACP,  0 , sResult.c_str(), sResult.size(),  0 0  ); //  计算转换后宽字符串的长度。(不包含字符串结束符)  

    wchar_t 
* lpwsz =   new  wchar_t [iWLen + 1 ];  


    MultiByteToWideChar( CP_ACP, 
0 , sResult.c_str(), sResult.size(), lpwsz, iWLen );  //  正式转换。  

    lpwsz[iWLen] 
=  L ' \0 ' ;   

    wstring wsResult(lpwsz);  

    delete []lpwsz;  

    
return  wsResult;  
    
}

string  Wstring2String(wstring wsResult)
{
    
string  sResult;  
    
int  iLen =  WideCharToMultiByte( CP_ACP, NULL, wsResult.c_str(),  - 1 , NULL,  0 , NULL, FALSE );  //  计算转换后字符串的长度。(包含字符串结束符)  
     char   * lpsz =   new   char [iLen];  
    WideCharToMultiByte( CP_OEMCP, NULL, wsResult.c_str(), 
- 1 , lpsz, iLen, NULL, FALSE);  //  正式转换。  
    sResult.assign( lpsz, iLen - 1  );  //  对string对象进行赋值。  
    delete []lpsz;  
    
return  sResult;

}

int  _tmain( int  argc, _TCHAR *  argv[])
{
    

    

    
    wstring wstr1
= L " 生命无国界,我们不应该庆祝日本地震 " ;
    wstring wstr2
= L " 我们的生命无国界,因此不应该庆祝日本地震 " ;
    
string  str1 = Wstring2String(wstr1);
    
string  str2 = Wstring2String(wstr2);
    cout
<< " 待对比的字符串为: " << endl;
    cout
<< " x串:  " << str1.c_str() << endl;
    cout
<< " y串:  " << str2.c_str() << endl;
    
const  wchar_t  * xpart = wstr1.c_str();
    
const  wchar_t  * ypart = wstr2.c_str();
    stringprocess p;
    
int  len = p.CalculateLongestCommonSequenceLen(xpart,ypart);
    
if  (len > 0 )
    {
        wstring wlcsequence
= p.GetLongestCommonSequence();
        
string  lcsequence = Wstring2String(wlcsequence);
        cout
<< " 最大公共字串为: " << lcsequence.c_str() << endl;
        
int * xpartinfo = new   int [wlcsequence.size()];
        
int   * ypartinfo = new   int [wlcsequence.size()];
        p.GetIndexesInfo(xpartinfo,ypartinfo);
        cout
<< " 最大公共字串在x字符串中的位置: " << endl;
        
for  ( int  i = 0 ;i < wlcsequence.size();i ++ )
        {
            cout
<< xpartinfo[i] << " ; " ;
        }
        cout
<< endl;
        cout
<< " 最大公共字串在y字符串中的位置: " << endl;
        
for  ( int  i = 0 ;i < wlcsequence.size();i ++ )
        {
            cout
<< ypartinfo[i] << " ; " ;
        }cout
<< endl;

        delete xpartinfo;
        delete ypartinfo;

    }
    cout
<< len << endl;

    
    cout
<< " finish " << endl;
    
int  f;
    cin
>> f;
    
}

 

 

你可能感兴趣的:(算法)