• 【C++】每周一题——2024.3.3(手滑再再写一篇)


    题目

    Cpp
    【问题描述】
    求N个字符串的最长公共子串,2 < N<=20,字符串长度不超过255。
    例如:N=3,由键盘依次输入三个字符串为
    What is local bus?
    Name some local buses.
    local bus is a high speed I/O bus close to the processer.
    则最长公共子串为"local bus"。


    分析

    找n个字符串中的最大公共子串。


    思路

    先遍历出其中两个字符串的所有公共子集,然后后面每输入一个字符串就排除掉几个不存在当中的,最后找出最长的输出。


    代码

    1. 框架

      int main(){
      	return 0;
      }
      
      • 1
      • 2
      • 3

    2. 先输入前两个字符串。

      #include	//scanf()
      char a[256], b[256];
      int main(){
      	scanf("%[^\n]\n%[^\n]", &a, &b);
      	return 0;
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6

    3. 找出这两个字符串的公共子串。详情可见这篇

      #include	//scanf()
      #include	//strlen(), memset(), strstr(), strcpy()
      char a[256], b[256], t[256], c[256*256][256];
      int x;
      int main(){
      	scanf("%[^\n]\n%[^\n]", &a, &b);
      	for(int i=0; i<strlen(a); i++){
      		memset(t, 0, sizeof(t));
      		for(int j=0; j<strlen(a)-i; j++){
      			t[j]=a[i+j];
      			if(strstr(b, t)!=NULL){
      				strcpy(c[x], t);
      				x++;
      			}
      		}
      	}
      	return 0;
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18

      数组t:临时用,存放当前遍历到的子串。
      二维数组c:存放遍历到的所有公共子串。
      整形变量x:代表所有公共子串的数量。


    4. 输入剩下的字符串,边输入一边删除不存在的子串。记得修改变量x。

      #include	//scanf()
      #include	//strlen(), memset(), strstr(), strcpy()
      char a[256], b[256], t[256], c[256*256][256];
      int x;
      int main(){
      	scanf("%[^\n]\n%[^\n]", &a, &b);
      	for(int i=0; i<strlen(a); i++){
      		memset(t, 0, sizeof(t));
      		for(int j=0; j<strlen(a)-i; j++){
      			t[j]=a[i+j];
      			if(strstr(b, t)!=NULL){
      				strcpy(c[x], t);
      				x++;
      			}
      		}
      	}
      	while(scanf("\n%[^\n]", &b)!=EOF){
      		for(int i=x-1; i>=0; i--){
      			if(strstr(b, c[i])==NULL){
      				for(int j=i; j<x; j++){
      					strcpy(c[j], c[j+1]);
      				}
      				x--;
      			}
      		}
      	}
      	return 0;
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24
      • 25
      • 26
      • 27
      • 28

    5. 找出公共子串中,最长的子串,并输出。

      #include	//scanf(), printf()
      #include	//strlen(), memset(), strstr(), strcpy()
      char a[256], b[256], t[256], c[256*256][256];
      int x;
      int main(){
      	scanf("%[^\n]\n%[^\n]", &a, &b);
      	for(int i=0; i<strlen(a); i++){
      		memset(t, 0, sizeof(t));
      		for(int j=0; j<strlen(a)-i; j++){
      			t[j]=a[i+j];
      			if(strstr(b, t)!=NULL){
      				strcpy(c[x], t);
      				x++;
      			}
      		}
      	}
      	while(scanf("\n%[^\n]", &b)!=EOF){
      		for(int i=x-1; i>=0; i--){
      			if(strstr(b, c[i])==NULL){
      				for(int j=i; j<x; j++){
      					strcpy(c[j], c[j+1]);
      				}
      				x--;
      			}
      		}
      	}
      	memset(a, 0, sizeof(a));
      	for(int i=0; i<x-1; i++){
      		if(strlen(c[i])>strlen(a)){
      			strcpy(a, c[i]);
      		}
      	}
      	printf("%s", a);
      	return 0;
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24
      • 25
      • 26
      • 27
      • 28
      • 29
      • 30
      • 31
      • 32
      • 33
      • 34
      • 35


    答案

    #include
    #include
    char a[256], b[256], t[256], c[256*256][256];
    int x;
    int main(){
    	scanf("%[^\n]\n%[^\n]", &a, &b);
    	for(int i=0; i<strlen(a); i++){
    		memset(t, 0, sizeof(t));
    		for(int j=0; j<strlen(a)-i; j++){
    			t[j]=a[i+j];
    			if(strstr(b, t)!=NULL){
    				strcpy(c[x], t);
    				x++;
    			}
    		}
    	}
    	while(scanf("\n%[^\n]", &b)!=EOF){
    		for(int i=x-1; i>=0; i--){
    			if(strstr(b, c[i])==NULL){
    				for(int j=i; j<x; j++){
    					strcpy(c[j], c[j+1]);
    				}
    				x--;
    			}
    		}
    	}
    	memset(a, 0, sizeof(a));
    	for(int i=0; i<x-1; i++){
    		if(strlen(c[i])>strlen(a)){
    			strcpy(a, c[i]);
    		}
    	}
    	printf("%s", a);
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35

  • 相关阅读:
    鉴源论坛 · 观模丨基于搜索的测试生成
    Python3-pdf文件的相关操作,分割和合并page,PyPDF2的使用
    【云原生 | Kubernetes 系列】----亲和与反亲和
    使用Python PyQt5完成残缺棋盘覆盖仿真作业
    【网络数据采集】python爬取豆瓣top250电影目录
    J9数字论:一文看懂私有链与联盟链
    Complete Probability Spaces
    异步机制的简单实现
    Opencv(C++)笔记--打开摄像头、保存摄像头视频
    AI&Cloud 分论坛 07-AI原生数据库与RAG【文档管理】
  • 原文地址:https://blog.csdn.net/Happynetizens/article/details/136437185