【python】实例-python实现两个字符串中最大的公共子串

由于python中的for循环不像C++这么灵活,因此该用枚举法实现该算法:

C="abcdefhe"
D="cdefghe"

m=0
n=len(C)
E=[]
b=0
while(m<n):
    i=n-m
    while(i>=0):
        E.append(C[m:m+i])
        i-=1
    m+=1

for x in E:
    a=0
    if x in D:
        a=len(x)
        c=E.index(x)
    if a > b:#保存符合要求的最长字符串长度和地址
        b=a
        d=c

if b>0:
    print E[d]

  

参照C语言实现方法:

//3.定义一个求最大公共子字符串的函数
void maxChild(char str[],char str1[])
{
    //4.定义两个指针变量,用于记录相同的起始地址
    char *p,*q;
   
    //5.定义两个整型变量,用于保存最大公共长度
    int n=0,m=0;
   
    //6.通过for循环来遍历第一个字符串
    for(int i=0;i<strlen(str);i++)
    {
        //7.通过for循环遍历第二个字符串
        for(int j=0;j<strlen(str1);j++)
        {
            //8.每次比较完两个字符串的公共部分后,都设置m=0
            m = 0;
            //9.判断两个字符串起始相同,只要一有相同的,就同步进行判断
            if(str[i]==str1[j]&&str1[j]!=‘\0‘){
                //10.通过同步进行比较公共字符串
                for(int k=0;str[k+i]!=‘\0‘&&str1[k+j]!=‘\0‘&&str[k+i]==str1[k+j];k++)
                {
                    //11.记录公共字符个数和第一个匹配的地址
                    m++;
                    p = &str[i];
                }
                if(m>n)
                {
                    //12.保存大地址,和最大个数
                    q = p;
                    n = m;
                }
            }
        }
    }
   
    //13.判断是否有公共子字符串
    if(n>0){
        //14.进行输出最大公共子字符串
        for(int i=0;i<n;i++){
            printf("%c",*(q+i));
        }
    }else{
        printf("没有公共子字符串");
    }
}

  

时间: 2024-08-28 06:17:31

【python】实例-python实现两个字符串中最大的公共子串的相关文章

面试宝典_Python.常规算法.0002.输出任意两个字符串中最长公共子串?

面试题目: 1. 用PY实现求任意两个字符串最长的公共子串? 解题思路: 1. 先求出长度最小的字符串,然后遍历其索引,这样可以避免字符串索引溢出,然后判断对应索引的值是否相同,相同的话就加到目标字典,不同的话就更新目标字典索引,但不存储,最后再按照值长度逆向排序取出第一个元素即可. 具体实现: #!/usr/bin/env python # -*- coding: utf-8 -*- """ # # Authors: limanman # OsChina: http://x

两个字符串中最大相同的子串

/* * 两个字符串中最大相同的子串. *  *  * * 思路: * 1,既然取得是最大子串,先看短的那个字符串是否在长的那个字符串中. * 如果存在,短的那个字符串就是最大子串. * 2,如果不是呢,那么就将短的那个子串进行长度递减的方式去子串,去长串中判断是否存在. * 如果存在就已找到,就不用在找了. */ public class Test2 { public static void main(String[] args) { // TODO 自动生成的方法存根 String s1="

给定两个字符串,获取两个字符串中最大相同的子串

1 package weekpratisce; 2 3 ///给定两个字符串,获取两个字符串中最大相同的子串 4 public class Demo9 { 5 public static void main(String[] args) { 6 String xx = "aaaaaaaaaaddddddd", yy = "45ddddda"; 7 String str = getMaxsubstring(xx, yy); 8 System.out.println(s

取出两个字符串中最大相同的子串

//************************************************************************* //题目要求:4.取出两个字符串中最大相同的子串. //************************************************************************* public class SearchMaxSameString { public static void main(String[] args

获取两个字符串中最大的相同子串

public class 获取两个字符串中最大的相同子串 { public static void main(String[] args) { String a="abcwerthelloadcedf"; String b="cdhelloesadcedf"; String c=getSonString(a,b); System.out.println(c); } private static String getSonString(String a, String

java基础知识回顾之---java String final类普通方法的应用之“两个字符串中最大相同的子串”

/* * 3,两个字符串中最大相同的子串. * "qwerabcdtyuiop" * "xcabcdvbn" *  * 思路: * 1,既然取得是最大子串,先看短的那个字符串是否在长的那个字符串中. *   如果存在,短的那个字符串就是最大子串. * 2,如果不是呢,那么就将短的那个子串进行长度递减的方式取子串,去长串中判断是否存在. *   如果存在就已找到,就不用在找了. * 3.先找最大的子串,再递减子串找,找到,就停止 */ 原理图如图: 代码: publi

求两个字符串的最长公共子串——Java实现

要求:求两个字符串的最长公共子串,如"abcdefg"和"adefgwgeweg"的最长公共子串为"defg"(子串必须是连续的) public class Main03{ // 求解两个字符号的最长公共子串 public static String maxSubstring(String strOne, String strTwo){ // 参数检查 if(strOne==null || strTwo == null){ return null

两个字符串的最长公共子串

import java.util.Scanner; /* 求两个字符串的最长公共子串*/ public class stringDemo {     public static void main(String[] args){      Scanner scanner = new Scanner(System.in);      System.out.println("请输入第一个字符串:");      String str1 = scanner.nextLine();     

[URAL-1517][求两个字符串的最长公共子串]

Freedom of Choice URAL - 1517 Background Before Albanian people could bear with the freedom of speech (this story is fully described in the problem "Freedom of speech"), another freedom - the freedom of choice - came down on them. In the near fu