POJ1580 水题,积累!

本文介绍了如何通过移动比较两个字符串来找到最多相同的元素,并使用给定的公式计算结果。通过实例代码展示了for循环的运用,重点在于理解并积累不同的循环形式。

【题意简述】:题意非常easy。就是将这两个字符串比較,移动着比較。求出最多的同样的元素个数。然后用题目中所给的公式,写出结果。

【分析】:本题要注意的就是for循环的形式。注意积累就可以。

详见代码:

//196K  0Ms
#include<iostream>
#include<cstring>
using namespace std;
#define M 25
char a[M],b[M];
int len1,len2;

int gcd(int a,int b)
{
	if(b == 0)
		return a;
	else return gcd(b,a%b);
}
int main()
{
	int tmp;
	while(cin>>a)
	{
		if(strcmp(a,"-1") == 0)
			break;
		cin>>b;
		len1 = strlen(a);
		len2 = strlen(b);
		int max = 0;
		
		for(int i = 0;i<len1;i++)
		{
			tmp = 0;
			for(int j = 0, k = i;j<len2,k<len1;j++,k++)  // 这里的形式注意积累
			{
				if(a[k] == b[j])
					tmp++;
			}
			if(max<tmp)
				max = tmp;
		}
		
		for(int i = 0;i<len2;i++)
		{
			tmp = 0;
			for(int j = 0, k = i;j<len1,k<len2;j++,k++)
			{
				if(b[k] == a[j])
					tmp++;
			}
			if(max<tmp)
				max = tmp;
		}
		
		max *= 2;
		int tmp1 = gcd(max,len1+len2);
		if(max == 0)
			cout<<"appx("<<a<<","<<b<<") = "<<0<<endl;
		else if(max == len1+len2)
			cout<<"appx("<<a<<","<<b<<") = "<<1<<endl;
		else
			cout<<"appx("<<a<<","<<b<<") = "<<max/tmp1<<"/"<<(len1+len2)/tmp1<<endl;
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值