zoukankan      html  css  js  c++  java
  • 最长公共子串

    #include <stdlib.h>
    #include <stdio.h>
    
    #define MAX 10
    
    int comMatrix[MAX][MAX];
    char comstr[MAX];
    
    char *commonString(char string1[], char string2[])
    {
        int len1=0,len2=0;
        int flag=0;
        int max=0;
        int i, j;
    
        while(string1[len1]!='') len1++;
        while(string2[len2]!='') len2++;
    
        for(i=0;i<len1;i++)
        {
            for(j=0;j<len2;j++)
            {
                if(string1[i]==string2[j]) comMatrix[i][j]=1;
                else comMatrix[i][j]=0;
    
                if(i>0&&j>0&&comMatrix[i][j]==1) comMatrix[i][j]+=comMatrix[i-1][j-1];
    
                if(comMatrix[i][j]>=max)
                {
                    max=comMatrix[i][j];
                    flag=j;
                }
            }
    
        }
    
        for(i=0,j=flag-max+1;j<=flag;i++,j++)
        {
            comstr[i]=string2[j];
        }
        comstr[max]='';
    
        return comstr;
    }
    
    
    int main()
    {
        char *A="aomcdf", *B="pmcdfa";
    
        printf("%s
    ", commonString(A, B));
    }

     将字符串问题转化为矩阵问题,并且在矩阵构造过程中求解

  • 相关阅读:
    hdoj1587
    欧拉定理及其应用
    hdoj1571
    hdoj1050
    POJ推荐50题
    poj2593
    hdoj1286
    hdoj1215七夕节
    我的Linux软件
    ACM题目推荐--《算法艺术与信息学竞赛》(转)
  • 原文地址:https://www.cnblogs.com/mr-redrum/p/3509150.html
Copyright © 2011-2022 走看看