天下

记录修行的印记

动态规划算法(2),lcs算法,改进空间复杂度

const string LCS2(const string& strX,const string& strY)
{
    
int xlen=strX.size(); //横向长度
    int ylen=strY.size(); //纵向长度
    vector<int> arr(ylen); //当前行
    
    
int length=0;         //矩阵元素中的最大值
    int pos=0;             //矩阵元素最大值出现在第几列
    arr.assign(ylen,0);
    
for(int x=0;x<xlen;x++
    {
        
for(int y=0;y<ylen;y++)
        {
            
if (strX.at(x)==strY.at(y))
            {
                
if(y==0)
                    arr[y]
=1;
                
else
                    arr[y]
=arr[y-1]+1;
                
if(arr[y]>length)
                {
                    length
=arr[y];
                    pos
=y;
                }
            } 
        }
    }
    
string res=strY.substr(pos-length+1,length);
    
return res;
}

posted on 2013-03-16 18:17 天下 阅读(763) 评论(0)  编辑 收藏 引用 所属分类: 算法


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2015年7月>
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

导航

统计

常用链接

留言簿(4)

随笔分类(378)

随笔档案(329)

链接

最新随笔

搜索

最新评论