poj 1580 String Matching

#include<iostream>
#include
<string>
using namespace std;
int gcd(int n,int m) //辗转相除法
{
int r;
if(n<m)swap(n,m);
while(m!=0)
{
r
=n%m;
n
=m;
m
=r; //永远符合n>=m
}
return n;
}
int main()
{
char str[10000],ctr[10000];
while(cin>>str&&strcmp(str,"-1")!=0)
{
cin
>>ctr;
int s,res=0;
for(int i=0;i<strlen(str);++i)
{
s
=0;
for(int j=0;i+j<strlen(str)&&j<strlen(ctr);++j)
if(str[i+j]==ctr[j])
s
++;
res
=max(res,s);
}
for(int i=0;i<strlen(ctr);++i)
{
s
=0;
for(int j=0;i+j<strlen(ctr)&&j<strlen(str);++j)
if(ctr[i+j]==str[j])
s
++;
res
=max(res,s);
}
printf(
"appx(%s,%s) = ",str,ctr);
int a=2*res,b=strlen(str)+strlen(ctr),c=gcd(a,b);
if(a==0)
printf(
"0\n");
else if(a==b)
printf(
"1\n");
else
printf(
"%d/%d\n",a/c,b/c);
}
return 0;
}

  

posted on 2011-07-22 22:40  sysu_mjc  阅读(173)  评论(0编辑  收藏  举报

导航