【C++】每周一题——2024.3.3

题目

Cpp
【问题描述】
字符环(来源:NOI题库)。有两个由字符构成的环,请写一个程序,计算这两个字符环上最长公共字符串的长度。例如,字符串“ABCEFAGADEGKABUVKLM”的首尾连在一起,构成一个环;字符串”MADJKLUVKL”的首尾连在一起,构成另一个环;“UVKLMA”是这两个环的一个公共字符串。
【输入格式】
有两行,每行一个不包含空格的字符串,每行的字符串首尾相连即为一个环。
【输出格式】
一行,输出一个整数,表示这两个字符环上最长公共字符串的长度。
【输入样例】
ABCEFAGADEGKABUVKLM MADJKLUVKL
【输出样例】
6
【数据范围】
字符串长度不超过255


分析

就是找两个字符串的最大的连续交集。只不过字符串首尾相连


思路

其实要考虑的只不过是最后一位的下一位是第一位而已。这也很简单,直接将该字符串复制一份接到它后面即可。然后就可以循环找子集了。


代码

  1. 框架

    int main(){
    	return 0;
    }
    

  2. 输入字符串

    #include<cstdio>	//scanf()
    char a[256], b[256]; 
    int main(){
    	scanf("%s %s", &a, &b);
    	return 0;
    }
    

  3. 拼接字符串
    注意,不能直接用strcat()函数拼接

    #include<cstdio>	//scanf()
    #include<cstring>	//strcpy(), strcat(), memset()
    char a[256], b[256], c[256];
    int main(){
    	scanf("%s %s", &a, &b);
    	strcpy(c, a);
    	strcat(a, c);
    	memset(c, 0, sizeof(c));
    	strcpy(c, b);
    	strcat(b, c);
    	return 0;
    }
    

  4. 遍历字符串a的子集(遍历头和尾,并同时求出子集)。详见该文张2.5版解题思路

    #include<cstdio>	//scanf()
    #include<cstring>	//strcpy(), strcat(), memset(), strlen()
    char a[256], b[256], c[256];
    int l;
    int main(){
    	scanf("%s %s", &a, &b);
    	strcpy(c, a);
    	strcat(a, c);
    	memset(c, 0, sizeof(c));
    	strcpy(c, b);
    	strcat(b, c);
    	l=strlen(a);
    	for(int i=0; i<l; i++){
    		memset(c, 0, sizeof(c));
    		for(int j=0; j<l-i; j++){
    			c[j]=a[i+j];
    		}
    	}
    	return 0;
    }
    

  5. 已经求出了一个字符串的子集,现在直接判断该子集是否同时存在于另一个字符串中。如果存在,就将该子集的长度比较存入变量中。

    #include<cstdio>	//scanf()
    #include<cstring>	//strcpy(), strcat(), memset(), strlen(), strstr()
    #include<cmath>		//fmax()
    char a[256], b[256], c[256];
    int l, ans;
    int main(){
    	scanf("%s %s", &a, &b);
    	strcpy(c, a);
    	strcat(a, c);
    	memset(c, 0, sizeof(c));
    	strcpy(c, b);
    	strcat(b, c);
    	l=strlen(a);
    	for(int i=0; i<l; i++){
    		memset(c, 0, sizeof(c));
    		for(int j=0; j<l-i; j++){
    			c[j]=a[i+j];
    			if(strstr(b, c)!=NULL){
    				ans=fmax(ans, j+1);
    			}
    		}
    	}
    	return 0;
    }
    

  6. 最后,输出变量即可。

    #include<cstdio>	//scanf(), printf()
    #include<cstring>	//strcpy(), strcat(), memset(), strlen(), strstr()
    #include<cmath>		//fmax()
    char a[256], b[256], c[256];
    int l, ans;
    int main(){
    	scanf("%s %s", &a, &b);
    	strcpy(c, a);
    	strcat(a, c);
    	memset(c, 0, sizeof(c));
    	strcpy(c, b);
    	strcat(b, c);
    	l=strlen(a);
    	for(int i=0; i<l; i++){
    		memset(c, 0, sizeof(c));
    		for(int j=0; j<l-i; j++){
    			c[j]=a[i+j];
    			if(strstr(b, c)!=NULL){
    				ans=fmax(ans, j+1);
    			}
    		}
    	}
    	printf("%d", ans);
    	return 0;
    }
    


答案

#include<cstdio>
#include<cstring>
#include<cmath>
char a[256], b[256], c[256];
int l, ans;
int main(){
	scanf("%s %s", &a, &b);
	strcpy(c, a);
	strcat(a, c);
	memset(c, 0, sizeof(c));
	strcpy(c, b);
	strcat(b, c);
	l=strlen(a);
	for(int i=0; i<l; i++){
		memset(c, 0, sizeof(c));
		for(int j=0; j<l-i; j++){
			c[j]=a[i+j];
			if(strstr(b, c)!=NULL){
				ans=fmax(ans, j+1);
			}
		}
	}
	printf("%d", ans);
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值