【题意简述】:题意很简单,就是将这两个字符串比较,移动着比较,求出最多的相同的元素个数,然后用题目中所给的公式,写出结果。
【分析】:本题要注意的就是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;
}