最长公共连续子串
来源:互联网 发布:日本军国主义知乎 编辑:程序博客网 时间:2024/05/17 23:56
给出两个字符串(可能包含空格),找出其中最长的公共连续子串,输出其长度。
输入描述:
输入为两行字符串(可能包含空格),长度均小于等于50.
输出描述:
输出为一个整数,表示最长公共连续子串的长度。#include <iostream>
#include <string.h>
#include <vector>
using namespace std;
int main()
{
string str1;
string str2;
getline(cin,str1);
getline(cin,str2);
int len1 = str1.size();
int len2 = str2.size();
if(len1<1 || len2<1)
{
cout<<0<<endl;
return 0;
}
vector<vector<int>> dp(len1,vector<int>(len2,0));
for(int ii = 0;ii<len2;++ii)
{
if(str1[0] ==str2[ii])
dp[0][ii] = 1;
}
for(int jj = 0;jj<len1;++jj)
{
if(str1[jj] ==str2[0])
dp[jj][0] = 1;
}
for(int ii = 1;ii<len1;++ii)
{
for(int jj = 1;jj<len2;++jj)
{
if(str1[ii] ==str2[jj])
dp[ii][jj] = dp[ii-1][jj-1]+1;
}
}
int longest = 0;
for(int ii = 0;ii<len1;++ii)
{
for(int jj = 0;jj<len2;++jj)
{
if(dp[ii][jj] >longest)
longest = dp[ii][jj];
}
}
cout<<longest<<endl;
}
阅读全文
0 0
- 最长公共连续子串
- 最长公共连续子串
- 最长连续公共子串
- 最长连续公共子串算法
- 最长连续公共子串算法
- 最长公共子串(连续)问题
- 最长公共子串(连续)问题
- 最长公共子串(连续)算法
- 最长公共子串要求连续
- 最长公共子串(连续)问题
- 最长公共子串(连续)问题
- 最长公共子串(连续)问题
- 最长公共子串(连续)问题
- 求最长连续公共子串
- 最长公共子串(连续)
- 动态规划-----最长公共连续子串
- [编程题] 最长公共连续子串
- 【编程题】最长公共连续子串
- Socket PRGM: chat_1Vn
- Python--多线程网络编程
- [堆] hihoCoder Challenge 29 D. 不上升序列
- [leetcode] Add to List 169. Majority Element
- 震撼发布2017年Android百大框架排行榜
- 最长公共连续子串
- shell中关于数字变量比较报错的问题
- C++中string erase函数的用法
- React Native Navigator 引入的那些坑
- 九宫格抽奖
- Q1:控制台输出helloworld
- Day 23 内置、匿名函数及递归
- sort函数用重载运算符出错
- 使用Jvisualvm监控线程死锁案例分析