您的位置:首页 > 移动开发 > IOS开发

求两字符串的最长公有串 —— 我的“平移算法”

2005-11-21 15:29 260 查看
 
原创文章,转载请注明出处!
问题:
求A和B两个字符串的最长公共子串,并输出其长度。
分析:
我还没看过系统算法方面的书,不过我想以后要加强了,:-),目前我想到用“平移法”来计算最长公有串,还未分析过其它算法,不过我想,这个速度应该还不错。
原理:把两个字符串想像成两板木板,木板A短于木板B,让木板A在木板B上平移,从而找到垂直方向的公共字符串,进尔求得最长公有串

分解1 +-------------+
| A |
+-------------+
+---------------------+
| B |
+---------------------+
^
i
分解2 +-------------+
| A |
+-------------+
+---------------------+
| B |
+---------------------+
^
i
代码实现:
std get_substr strA strB strSub _tmain argc _TCHAR argv a b  substr

get_substra b substr

coutsubstrendl

system get_substr strA strB strSub sub_index sub_len s1 strA s2 strBstrlenstrA strlenstrB
swaps1 s2 len1 strlens1 len2 strlens2 x1x2 i len1 i len2ii
x1 i len1
x2
x1
x2 i lenx1 len1x2 len2 s1x1 s2x2lenlen sub_len
sub_index x2					sub_len len
lenx1x2sub_len
strncpystrSub s2 sub_indexsub_len sub_len
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  算法 iostream system
相关文章推荐