最长公共子串java_最长公共子串

4

import java.util.Scanner;

/**

* Dynamic Programming

*

* State:

*   dp[i][j]: 表示以s1[i]为结尾的子串和以s2[j]为结尾的子串的最长公共连续子串的长度

*

* Initial State:

*   dp[i][0] = (s1[i] == s2[0]) ? 1 : 0;  when j == 0

*   dp[0][j] = (s1[0] == s2[j]) ? 1 : 0;  when i == 0

*

* State Transition:

*   dp[i][j] = (s1[i] == s2[j]) ? dp[i - 1][j - 1] + 1 : 0;  (i > 0 && j > 0)

*

* @author wylu

*/

public class Main {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

while (scanner.hasNext()) {

char[] s1 = scanner.nextLine().toCharArray();

char[] s2 = scanner.nextLine().toCharArray();

int res = 0;

int[][] dp = new int[s1.length][s2.length];

for (int i = 0; i < s1.length; i++) {

for (int j = 0; j < s2.length; j++) {

if (s1[i] == s2[j]) {

dp[i][j] = (i == 0 || j == 0) ? 1: dp[i - 1][j - 1] + 1;

res = Math.max(res, dp[i][j]);

}

}

}

System.out.println(res);

}

}

}

发表于 2019-01-10 16:23:21

回复(0)

5

Python解法

LCS问题就是求两个字符串最长公共子串的问题。

解法: 用一个矩阵来记录两个字符串中所有位置的两个字符之间的匹配情况,若是匹配则为1,否则为0。

求出对角线最长的1的序列,其对应的位置就是最长匹配子串的位置。 def find_lcsubstr(s1, s2):

m = [[0 for i in range(len(s2) + 1)] for j in range(len(s1) + 1)] # 生成0矩阵,为方便后续计算,比字符串长度多了一列

mmax = 0 # 最长匹配的长度

for i in range(len(s1)):

for j in range(len(s2)):

if s1[i] == s2[j]:

m[i + 1][j + 1] = m[i][j] + 1

mmax = max(mmax, m[i + 1][j + 1])

return mmax # 返回最长长度

print(find_lcsubstr(input(), input()))

编辑于 2019-02-24 11:54:12

回复(1)

2

动态规划,思路和LCS差不多。dp原为二维数组,优化为一维。dp[i][j]表示以s1[i] 与 s2[j]为结尾的最大连续公共子串长度。 import java.util.*;

public class Main {

private static final int MAX = 1005;

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

char[] s1 = sc.next().toCharArray();

char[] s2 = sc.next().toCharArray();

int[] dp = new int[MAX];

int ans = 0;

for (int i=0; i!=s1.length; i++) {

for (int j=s2.length-1; j>=0; j--) {

if (s1[i] == s2[j]) {

dp[j+1] = dp[j] + 1;

ans = Math.max(dp[j+1], ans);

}

else { dp[j+1] = 0; }

}

}

System.out.println(ans);

}

}

编辑于 2019-02-05 23:04:33

回复(0)

1

//动态规划,记录字符串以i,j结尾成功匹配时的最大长度,取所有值中最大

#include

#include

using namespace std;

int main() {

string s1,s2;

cin>>s1>>s2;

int dp[s1.length()][s2.length()];

int maxnum = 0;

for(int i = 0; i 

for(int j = 0; j 

if(i == 0 || j == 0) {

dp[i][j] = (s1[i] == s2[j]) ? 1:0;

}

else {

dp[i][j] = (s1[i] == s2[j]) ? dp[i-1][j-1]+1 : 0;

}

maxnum = max(maxnum, dp[i][j]);

}

}

cout<

return 0;

}

发表于 2020-07-04 11:05:58

回复(0)

1

//动态规划

import java.util.*;

public class Main{

public static void main(String[] args){

String a,b;

int result=0;

int n,m;

Scanner sc=new Scanner(System.in);

a=sc.nextLine();

b=sc.nextLine();

m=a.length();

n=b.length();

int dp[][]=new int[n][m];

for(int i=0;i

for(int j=0;j

if(a.charAt(j)==b.charAt(i)){

dp[i][j]=((i==0)||(j==0))?1:dp[i-1][j-1]+1;

result=Math.max(result,dp[i][j]);

}

}

}

System.out.println(result);

}

}

编辑于 2020-06-17 17:17:00

回复(0)

1

主要使用str1.indexOf(str2.substring(j,j+i))方法,

如果str1含有str2的子串的话会返回索引

如果不包含咋返回-1 import java.util.*;

public class Main {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

String s1 = sc.next();

String s2 = sc.next();

if (s1.isEmpty() || s2.isEmpty()){

System.out.println(0);

}

int res = s1.length()> s2.length() ? getMaxComm(s1,s2) : getMaxComm(s2,s1);

System.out.println(res);

}

public static int getMaxComm(String s1, String s2){

int maxLen = 0;

int currLen = 0;

for(int i = 1; i <= s2.length(); i++){

for(int j = 0; j 

int res = s1.indexOf(s2.substring(j,j+i));

if(res != -1){

currLen = i;

maxLen = Math.max(currLen, maxLen);

}

}

}

return maxLen;

}

}

发表于 2020-04-22 21:17:02

回复(0)

1

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

String str1 = sc.nextLine().trim();

String str2 = sc.nextLine().trim();

System.out.println(maxCommonStr(str1, str2));

sc.close();

}

private static int maxCommonStr(String str1, String str2) {

if (str1 == null || str2 == null) {

return -1;

}

int len1 = str1.length();

int len2 = str2.length();

int[][] dp = new int[len1 + 1][len2 + 1];

int lmax = 0;

for (int i = 1; i <= len1; i++) {

for (int j = 1; j <= len2; j++) {

if (str1.charAt(i - 1) == str2.charAt(j - 1)) {

dp[i][j] = dp[i - 1][j - 1] + 1;

if(dp[i][j] > lmax)

{

lmax = dp[i][j];

}

}

}

}

return lmax;

}

}

发表于 2020-03-26 21:38:41

回复(0)

0

#include

#include

using namespace std;

int main()

{

string a, b;

while (cin >> a >> b)

{

if (a.size() > b.size())

swap(a, b);

string str_m;//存储最长公共子串

for (int i = 0; i < a.size(); i++)

{

for (int j = i; j < a.size(); j++)

{

string temp = a.substr(i, j - i + 1);

if(b.find(temp)==b.npos)

break;

else if (str_m.size() < temp.size())

str_m = temp;

}

}

cout << str_m.size() << endl;

}

return 0;

}

发表于 2020-09-17 21:27:31

回复(0)

0

#include

using namespace std;

class Solution {

public:

int findLength(vector& A, vector& B) {

int i_max=A.size();

int j_max=B.size();

int len=0;

vector > dp(i_max, vector (j_max, 0));

//step1:初始化

for(int i=0;i

if(A[i]==B[0]){

dp[i][0]=1;

}

}

for(int j=0;j

if(B[j]==A[0]){

dp[0][j]=1;

}

}

//step2:状态转移

for(int i=1;i

for(int j=1;j

if(A[i]==B[j]){

dp[i][j]=dp[i-1][j-1]+1;

}

len=max(len, dp[i][j]);

}

}

return len;

}

};

int main(){

string str1,str2;

cin>>str1>>str2;

vector A,B;

for(int i=0;i

A.push_back(str1[i]);

}

for(int i=0;i

B.push_back(str2[i]);

}

Solution s;

int res=s.findLength(A,B);

cout<

return 0;

}

发表于 2020-09-06 12:59:33

回复(0)

0

动态规划解法

#include

#include

#include

#include

using namespace std;

int maxSame(string s1, string s2)

{

int len1 = s1.length();

int len2 = s2.length();

vector> dp(len1+1, vector(len2+1, 0));

int res = 0, pos = 0;

for(int i = 1; i <= len1; i++)

{

for(int j = 1; j 

{

if(s1[i-1] == s2[j-1])

dp[i][j] = dp[i-1][j-1] + 1;

res = max(res, dp[i][j]);

}

}

return res;

}

int main()

{

string str1, str2;

while(getline(cin, str1)  && getline(cin, str2))

cout <

return 0;

}

发表于 2020-07-23 00:43:20

回复(0)

0

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

public class Main {

public static void main(String[] args) throws IOException {

BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));

char [] c1 = bf.readLine().toCharArray();

char [] c2 = bf.readLine().toCharArray();

soultion(c1,c2);

}

public static void soultion(char [] c1,char [] c2){

int max = 0;

int [][] dep = new int[c1.length][c2.length];

for (int i = 0; i 

for (int j = 0; j 

if(c1[i]==c2[j]){

if(i==0||j==0){

dep[i][j] = 1;

}else{

dep[i][j] = dep[i-1][j-1] + 1;

}

}

max = Math.max(dep[i][j],max);

}

}

System.out.println(max);

}

}

发表于 2019-10-22 14:25:59

回复(0)

0

Java 代码通过 import java.util.Scanner;

public class Main {

public static void main(String[] args) {

/* String str1 = "helloword";

String str2 = "loop";*/

Scanner s = new Scanner(System.in);

String str1 = s.nextLine();

String str2 = s.nextLine();

//System.out.println(str1.substring(1,2));

System.out.println(lcsIdnex(str1,str2));

}

private static String lcsString(String a,String b){

char[] c1 = a.toCharArray();

char[] c2 = b.toCharArray();

int[][] d = new int[c1.length][c2.length];

for (int i = 0; i < c1.length; i++) {

if(c1[i]==c2[0]){

d[i][0] = 1;

}

}

for (int i = 0; i < c2.length; i++) {

if(c2[i]==c1[0]){

d[0][i] = 1;

}

}

int max=-1,index=0;

for (int i = 1; i < c1.length; i++) {

for (int j = 1; j < c2.length; j++) {

if(c1[i]==c2[j]){

d[i][j]=d[i-1][j-1]+1;

if(max<=d[i][j]){

max = d[i][j];

index =i;

}

}

}

}

return a.substring(index-max+1, index+1);

}

private static int lcsIdnex(String a,String b){

char[] c1 = a.toCharArray();

char[] c2 = b.toCharArray();

int[][] d = new int[c1.length][c2.length];

for (int i = 0; i < c1.length; i++) {

if(c1[i]==c2[0]){

d[i][0] = 1;

}

}

for (int i = 0; i < c2.length; i++) {

if(c2[i]==c1[0]){

d[0][i] = 1;

}

}

int max=-1,index=0;

for (int i = 1; i < c1.length; i++) {

for (int j = 1; j < c2.length; j++) {

if(c1[i]==c2[j]){

d[i][j]=d[i-1][j-1]+1;

if(max<=d[i][j]){

max = d[i][j];

index =i;

}

}

}

}

return max;

}

}

发表于 2019-08-02 11:53:57

回复(0)

0

//暴力解法:

int longcommonSubstring(string s1, string s2)

{

if (s1.empty() || s2.empty())

return 0;

int l1 = s1.size(), l2 = s2.size();

int res = 0;

for (int i = 0; i < l1; i++)

{

for (int j = 0; j < l2; j++)

{

int m = i;

int k = j;

int len = 0;

while (s1[m] == s2[k] && m < l1&&k < l2)

{

len++;

m++;

k++;

}

res = max(res, len);

}

}

return res;

}

//DP

int longcommonSubstring(string s1, string s2)

{

if (s1.empty() || s2.empty())

return 0;

int l1 = s1.size(), l2 = s2.size();

int res = 0;

vector> DP(l1, vector(l2));

for (int i = 0; i < l1; i++)

{

for (int j = 0; j < l2; j++)

{

if (s1[i] == s2[j])

{

if (i == 0 || j == 0)

DP[i][j] = 1;

else

DP[i][j] = 1 + DP[i - 1][j - 1];

}

else

DP[i][j] = 0;

res = max(DP[i][j], res);

}

}

return res;

}

发表于 2019-07-18 09:43:25

回复(0)

0

//LCS 最长公共子串的问题

#include

#include

#include

using namespace std;

void findLength(string& str1,string& str2) {

int size1 = str1.size();

int size2 = str2.size();

int res = 0;

vector > lcs(size1,vector(size2,0));

for(int i=0;i

{

for(int j=0;j

{

if(str1[i]==str2[j])

{

if(i==0||j==0)

{

lcs[i][j]=1;

}

else{

lcs[i][j]=lcs[i-1][j-1]+1;

}

}

res=max(res,lcs[i][j]);

}

}

cout<

}

int main() {

string str1,str2;

cin>>str1;

cin>>str2;

fun(str1,str2);

return 0;

}

发表于 2019-06-24 22:37:35

回复(0)

0

哪位大佬可以帮忙看下,动态规划方法使用递归的时候哪里有问题。自己测得例子都能过,是哪里考虑不周么,感谢。 import java.util.Scanner;

public class Main{

int maxLen(String stra, String strb){

if(stra.length() == 0 || strb.length() == 0) return 0;

if(stra.charAt(0) != strb.charAt(0)){

int a = maxLen(stra.substring(1),strb);

int b = maxLen(stra,strb.substring(1));

return a>b?a:b;

}else{

int i = 1;

int j = 1;

int count = 1;

while(i

if(stra.charAt(i) == strb.charAt(j)){

count++;

i++;

j++;

}else{

break;

}

}

int a = maxLen(stra.substring(i),strb);

int b = maxLen(stra,strb.substring(j));

int c = a>b?a:b;

return c>count?c:count;

}

}

public static void main(String[] args){

Scanner sc = new Scanner(System.in);

Main obj = new Main();

String stra = sc.nextLine();

String strb = sc.nextLine();

System.out.println(obj.maxLen(stra, strb));

return;

}

}

编辑于 2019-06-11 10:44:17

回复(0)

0

果然笨方法要考虑很多很多临界条件 #include

#include

using namespace std;

int main()

{

string aa,bb,a,b;

cin>>aa>>bb;

if(aa.length()>bb.length())

{

a=aa;b=bb;

}

else

{

a=bb;b=aa;

}

int ma=0,la=a.length(),lb=b.length();

//  cout<

for(int i=0;i

{

int temp=0,m;

if(i>la-lb)

{

m=la-i;

}

else

{

m=lb;

}

for(int j=0;j

{

if(j==0&&a[i]==b[0])

{

temp=1;

continue;

}

if(a[i+j]==b[j])

{

++temp;

ma=max(ma,temp);

}

else

{

temp=0;

}

}

}

cout<

}

发表于 2019-06-03 16:53:02

回复(0)

0

#include

using namespace std;

int main()

{

string str1,str2;

getline(cin,str1);

getline(cin,str2);

int ans = 0, cnt = 0;    //ans为最大公共连续子串的长度

for(int i = 0; i < str1.length(); i++)

{

for(int j = 0; j < str2.length(); j++)

{

cnt = 0;

int k = i;

while(str1[k] == str2[j])  //相等就循环累加

{

cnt++;

k++;

j++;

}

ans = max(ans,cnt);   //ans取最大值

}

}

cout << ans << endl;

return 0;

}

发表于 2019-05-26 13:15:30

回复(0)

0

#include

int main()

{

char str1[1000], str2[1000];

fgets(str1, 1000, stdin);

fgets(str2, 1000, stdin);

int num = 0;

for(int i = 0; str1[i + num] != '\0'; ++i) {

for(int j = 0; str2[j + num] != '\0'; ++j) {

int m = i, n = j, lnum = 0;

while(str1[m] != '\0' && str2[n] != '\0' && str1[m++] == str2[n++])

++lnum;

if(lnum > num)

num = lnum;

}

}

printf("%d\n", num);

return 0;

}

发表于 2019-05-04 13:34:14

回复(0)

0

#include

#include

int main()

{

char str1[1000]={'\0'},str2[1000]={'\0'};

int sum=0,count=0;

scanf("%s%s",str1,str2);

for(int i=0;i

{

for(int j=0;j

{

count=0;

int k=i;

while(str1[k]==str2[j])//如果相等就循环累加

{

count++;

k++;

j++;

}

if(count>sum)

{

sum=count;//最大值赋值给sum

}

}

}

printf("%d\n",sum);

return 0;

}

发表于 2019-04-24 21:47:59

回复(1)

0

import sys

string1=sys.stdin.readline().strip()

string2=list(sys.stdin.readline().strip())

# 遍历所有可能子集存入son列表中

son=[]

for i in range(len(string2)):

for j in range(len(string2)+1):

if i

son.append(string2[i:j])

# 查找公共连续子集的长度集合

length=[]

for i in son:

i=''.join(i)

if i in string1:

length.append(len(i))

print(max(length))

发表于 2019-03-31 19:55:21

回复(0)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值