• C 找到两字串中最长的相同串


    // SS.cpp : Defines the entry point for the console application.
    //
    
    
    #include "stdio.h"
    #include "string.h"
    
    #define N 10
    
    
    int findStr(char str[],char pat[]);
    void allSubPat(char str[],char pat[]);
    void clrFind(char str[]);
    
    int main()
    
    {
        char s1[10]="12abc78";
        char s2[10]="7abc2";
        char find[10]={0};
        allSubPat(s1,s2);
    
        char ss[10]="bc";
        printf("    %d     ",findStr(s1,ss));
        return 0;
    }
    
    
    void allSubPat(char str[],char pat[]){
        int max;
        char subStr[N];
        char findMax[N];
    
        int start=0;
        for(start=0;pat[start]!='\0';start++){////枚举字符串  //以a开头 b开头 c开头...
            int len=0;//相对于本次字串开始位置的偏移量 
    
            for(;pat[start+len]!='\0';len++){////a开头的字串又有不同长度 ab  abc  abcd
                // 将start开始 到 偏移量这段字符装入subStr
                int k=0;
                clrFind(subStr);
                for(;k<=len;k++){
                    subStr[k]=pat[start+k];
                
                }
                //printf(find);//输出本次字串
                if(findStr(str,subStr)){//如果含有该子串
                    //检测本次子串的长度
                    if(len>max){
                        max=len;
                        strcpy(findMax,subStr);
                    }
                }
                printf("\n");
            
            }
            
        }
    
        printf(findMax);
    }
    
    void clrFind(char str[]){
        int i=0;
        for(;i<N;i++){
            str[i]=0;
        }
    }
    
    int findStr(char str[],char pat[]){
        int i=0;
        int j=0;
        int max=0;
        int flag=1;//默认假设匹配
        for(i=0;str[i]!='\0';i++){
            
            //找到了首字母相同的位置  //接下来看剩余字母是否相同 
            if(str[i]==pat[j]){
                while(str[i]!='\0' && pat[j]!='\0' &&str[i]==pat[j]){
                    ////只有总是相同才不断进入循环 //如果真的匹配 那么循环结束后j2应该来到pat末位
                    i++;
                    j++;
                }
                
                if(pat[j]=='\0'){//j指向末位
                    flag=1;
                    break;
                }else {
                    
                    flag=0;
                }
                
            }    
        }
        
        return flag;
        
    }
  • 相关阅读:
    cookie和session的区别和用法
    JavaScript深浅拷贝
    前端知识
    typescript -- ts
    vue笔记精华部分
    宜人贷项目里-----正则匹配input输入月份规则
    PHP-Socket-阻塞与非阻塞,同步与异步概念的理解
    PHP socket客户端长连接
    PHP exec/system启动windows应用程序,执行.bat批处理,执行cmd命令
    查看局域网内所有ip
  • 原文地址:https://www.cnblogs.com/cart55free99/p/2985685.html
Copyright © 2020-2023  润新知