近期,我在网上看了一些动态规划求字符串最长公共子序列的代码。可是无一例外都是处理英文字符串,当处理汉字字符串时。常常会出现乱码或者不对的情况。

我对代码进行了改动。使用wchar_t类型存储字符串,可以正确的处理英文字符串和汉字字符串的最长公共子序列。

代码例如以下:

#include "stdafx.h"
#include <iostream>
#define N 1000
using namespace std;
//str1存储字符串1,str2存储字符串2
wchar_t str1[N],str2[N];
//lcs存储最长公共子序列
wchar_t lcs[N];
//c[i][j]存储str1[1...i]与str2[1...j]的最长公共子序列的长度
int c[N][N];
//flag[i][j]标记是那种子问题
//flag[i][j]==0为str1[i]==str2[j]
//flag[i][j]==1为c[i-1][j]>=s[i][j-1]
//flag[i][j]==-1为c[i-1][j]<s[i][j-1]
int flag[N][N];
//
int getLCSlength(const wchar_t *s1, const wchar_t *s2)
{
int i;
int len1 = wcslen(s1);
int len2 = wcslen(s2);
for(i=1;i<=len1;i++)
c[i][0] = 0;
for(i=0;i<=len2;i++)
c[0][i] = 0;
int j;
for(i=1;i<=len1;i++)
for(j=1;j<=len2;j++)
{
if(s1[i-1]==s2[j-1])
{
c[i][j] = c[i-1][j-1] +1;
flag[i][j] = 0;
}
else if(c[i-1][j]>=c[i][j-1])
{
c[i][j] = c[i-1][j];
flag[i][j] = 1;
}
else
{
c[i][j] = c[i][j-1];
flag[i][j] = -1;
}
}
return c[len1][len2];
}
wchar_t* getLCS(const wchar_t *s1, const wchar_t *s2,int len,wchar_t *lcs)
{
int i = wcslen(s1);
int j = wcslen(s2);
while(i&&j)
{
if(flag[i][j]==0)
{
lcs[--len] = s1[i-1];
i--;
j--;
}
else if(flag[i][j]==1)
i--;
else
j--;
}
return lcs;
}
int main(int argc, wchar_t* argv[])
{
//本函数用来配置地域的信息,设置当前程序使用的本地化信息
setlocale(LC_ALL,"chs");
int cases;
cout<<"请输入案例的个数:"<<endl;
cin>>cases;
while(cases--)
{
int i;
cout<<"请输入字符串1:"<<endl;
wcin>>str1;
cout<<"请输入字符串2:"<<endl;
wcin>>str2;
int lcsLen = getLCSlength(str1,str2);
cout<<"最长公共子序列长度:"<<lcsLen<<endl;
wchar_t *p = getLCS(str1,str2,lcsLen,lcs);
cout<<"最长公共子序列为:"<<endl;
for(i=0;i<lcsLen;i++)
wcout<<lcs[i];
wcout<<endl;
} return 0;
}

C++求解汉字字符串的最长公共子序列 动态规划的更多相关文章

  1. Python-求解两个字符串的最长公共子序列

    一.问题描述 给定两个字符串,求解这两个字符串的最长公共子序列(Longest Common Sequence).比如字符串1:BDCABA:字符串2:ABCBDAB.则这两个字符串的最长公共子序列长 ...

  2. (字符串)最长公共子序列(Longest-Common-Subsequence,LCS)

    问题: 最长公共子序列就是寻找两个给定序列的子序列,该子序列在两个序列中以相同的顺序出现,但是不必要是连续的. 例如序列X=ABCBDAB,Y=BDCABA.序列BCA是X和Y的一个公共子序列,但是不 ...

  3. 【ACM】最长公共子序列 - 动态规划

    最长公共子序列 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 咱们就不拐弯抹角了,如题,需要你做的就是写一个程序,得出最长公共子序列.tip:最长公共子序列也称作最 ...

  4. nyoj 36-最长公共子序列 (动态规划,DP, LCS)

    36-最长公共子序列 内存限制:64MB 时间限制:3000ms Special Judge: No accepted:18 submit:38 题目描述: 咱们就不拐弯抹角了,如题,需要你做的就是写 ...

  5. UVA 11107 Life Forms——(多字符串的最长公共子序列,后缀数组+LCP)

    题意: 输入n个序列,求出一个最大长度的字符串,使得它在超过一半的DNA序列中连续出现.如果有多解,按照字典序从小到大输出所有解. 分析:这道题的关键是将多个字符串连接成一个串,方法是用不同的分隔符把 ...

  6. Atcoder F - LCS (DP-最长公共子序列,输出字符串)

    F - LCS Time Limit: 2 sec / Memory Limit: 1024 MB Score : 100100 points Problem Statement You are gi ...

  7. lintcode:最长公共子序列

    题目 最长公共子序列 给出两个字符串,找到最长公共子序列(LCS),返回LCS的长度. 样例 给出"ABCD" 和 "EDCA",这个LCS是 "A& ...

  8. Lintcode--005(最长公共子序列)

    Given two strings, find the longest common subsequence (LCS).     最长公共子序列 Your code should return th ...

  9. 华为OJ之最长公共子序列

    题目描述: 对于两个给定的字符串,给出他们的最长公共子序列. 题目分析: 1,在之前的博文(http://www.cnblogs.com/yonguo123/p/6711360.html)中我们讨论了 ...

随机推荐

  1. C - Lucky Numbers (easy)

    Problem description Petya loves lucky numbers. Everybody knows that positive integers are lucky if t ...

  2. C#之纯数字判断

    public bool isNaN(string temp) { ; i <temp.Length; i++) { byte tempByte = Convert.ToByte(temp[i]) ...

  3. Android引导页

    源码地址:https://github.com/myloften/IntroSliderSample 博客地址:http://blog.csdn.net/loften_93663469/article ...

  4. Leetcode0019--Remove Nth Node From End of List 移除链表第N个结点

    [转载请注明]http://www.cnblogs.com/igoslly/p/8672656.html 看一下题目: Given a linked list, remove the nth node ...

  5. SAP computer之architecture

    Simple-As-Possible computer introduces all the cruicial ideas behind computer operation without bury ...

  6. TimerTask定时任务

    web.xml <listener> <listener-class>com.sign.listener.NFDFlightDataTaskListener</liste ...

  7. 【转】VMWare vCenter 6.0安装配置

    版权声明: 专注于"GIS+"前沿技术的研究与交流,将云计算技术.大数据技术.容器技术.物联网与GIS进行深度融合,探讨"GIS+"技术和行业解决方案:文章允许 ...

  8. BZOJ 4712: 洪水 挖坑待补

    Code: #include<bits/stdc++.h> #define setIO(s) freopen(s".in","r",stdin) # ...

  9. ffmpeg中关于EAGAIN的理解及非阻塞IO

    ffmpeg为在linux下开发的开源音视频框架,所以经常会碰到很多错误(设置errno),其中EAGAIN是其中比较常见的一个错误(比如用在非阻塞操作中).  try again,从字面上来看,是提 ...

  10. 1.2、使用pip安装Python包

    大多数 Python 包都使用 pip 实用工具安装,使用 virtualenv 创建虚拟环境时会自动安装 pip.激活虚拟环境后,pip 所在的路径会被添加进 PATH. 注:如果你在 Python ...