zoukankan      html  css  js  c++  java
  • LCS算法示例的主函数调用

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

    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(), 00 );// 计算转换后宽字符串的长度。(不包含字符串结束符)  

        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;
        
    }
  • 相关阅读:
    HDU4628+状态压缩DP
    Javascript 去掉字符串前后空格的五种方法
    Javascript 数组之判断取值和数组取值
    ASP.NET MVC 出现错误 “The view 'XXX' or its master was not found or no view engine support”
    ASP.NET MVC 页面调整并传递参数
    ASP.NET MV3 部署网站 报"Could not load file or assembly ' System.Web.Helpers “ 错的解决方法
    ASP.NET MVC 控制器向View传值的三种方法
    CSharp 如何通过拼接XML调用存储过程来查询数据
    SQLServer : EXEC和sp_executesql的区别
    关于SQLServer2005的学习笔记—异常捕获及处理
  • 原文地址:https://www.cnblogs.com/finallyliuyu/p/1985685.html
Copyright © 2011-2022 走看看