lintcode:Longest Common Substring
来源:互联网 发布:天心软件怎么用 编辑:程序博客网 时间:2024/06/07 16:27
Given two strings, find the longest common substring.
Return the length of it.
Example
Given A="ABCD"
, B="CBCE"
, return 2
.
Note
The characters in substring should occur continuously in original string. This is different with subsequence.
class Solution {public: /** * @param A, B: Two string. * @return: the length of the longest common substring. */ int longestCommonSubstring(string &A, string &B) { // write your code here int sizeA = A.size(); int sizeB = B.size(); int max_len = 0; for (int i=0; i<sizeA; i++) { int cur_i = i; int cur_len = 0; bool match = false; for (int j=0; j<sizeB && cur_i<sizeA; j++) { if (A[cur_i] == B[j]) { if (!match) { match = true; cur_i = i+1; cur_len = 1; } else { cur_i++; cur_len++; } max_len = max(max_len, cur_len); } else if (A[cur_i] != B[j]) { match = false; cur_i = i; cur_len = 0; } } } return max_len; }};
0 0
- Lintcode - Longest common substring
- lintcode:Longest Common Substring
- [LintCode]Longest Common Substring(Python)
- [Lintcode]Longest Common Substring最长公共子串
- LintCode之最长公共子串(Longest Common Substring)
- Lintcode - Longest common subsequence
- lintcode: Longest Common Prefix
- Longest common substring
- spoj1811 Longest Common Substring
- Longest Common Substring
- Longest common subsequence / substring
- HDU1403 Longest Common Substring
- longest common substring
- Longest Common Substring
- [刷题]Longest Common Substring
- longest-common-substring
- Longest Common Substring
- PIQ19: Longest Common Substring
- Java多线程系列--“JUC集合”01之 框架
- Serializable序列化
- 使用PHP连接SAE中的MySql数据库
- jquery实现的网页计数器
- MFC用Picture Control显示图像
- lintcode:Longest Common Substring
- FZU Problem 2168 防守阵地 I
- EMACS-Indentation(缩进)2
- Python学习笔记(三)
- 第三章第45题
- iOS开发 纯代码适配技巧 baseViewController
- 用django在SAE搭建个人博客(一)
- PHP实现冒泡循环
- zhuan