1、区别
1.1对于一个字符串而言,比如:pikachu
1.2字串是在字符串中,取出一块(连续的),如:pik, ach, kac等
1.3子序列指的是从字符串中,顺序取出字符,但是可以不连续:如:pau, kch, icu等
2、代码
#include<iostream>
#include<vector>
#include<string>
using namespace std;
string a, b, c, d;
// 最长公共子串
void con_substr(string a, string b){
vector<vector<int> > dp(a.size(),vector<int>(b.size(), 0));//谁为行谁为列可根据个人喜好而定
int Max = 0;
for(int i = 1; i < a.size(); i++){
for(int j = 1; j < b.size() ; j++){
if(a[i] == b[j]){
dp[i][j] = dp[i-1][j-1] + 1;
Max = max(dp[i][j], Max); //这一步其实也能算上一个简化过程写在这里面为了降低
//时间复杂度,另写两个for或者一个for遍历多不好
}
}
}
//---------------------------------------------
for(int i = 0; i < a.size(); i++){
for(int j = 0; j < b.size(); j++){
cout << dp[i][j]; //用来显示运算过程
} //用来显示运算过程
cout << " " << endl; //用来显示运算过程
}
//--------------------------------------------------
cout << Max;
}
int main(){
a = " "; //有特别用意
b = " ";
cin >> c;
getchar();
cin >> d;
a += c;
b += d;
con_substr(a, b);
return 0;
}
最长公共子串输入与输出: