码迷,mamicode.com
首页 > 其他好文 > 详细

POJ1580 水题,积累!

时间:2014-08-26 21:28:56      阅读:243      评论:0      收藏:0      [点我收藏+]

标签:水题   积累   

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

【分析】:本题要注意的就是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;
}


POJ1580 水题,积累!

标签:水题   积累   

原文地址:http://blog.csdn.net/u013749862/article/details/38853657

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!