您的位置:首页 > 其它

String Matching

2012-05-12 17:45 162 查看

题目描述:

It's easy to tell if two words are identical - just check the letters. But how do you tell if two words are almost identical? And how close is "almost"?

There are lots of techniques for approximate word matching. One is to determine the best substring match, which is the number of common letters when the words are compared letter-byletter.

The key to this approach is that the words can overlap in any way. For example, consider the words CAPILLARY and MARSUPIAL. One way to compare them is to overlay them:

CAPILLARY

MARSUPIAL

There is only one common letter (A). Better is the following overlay:

CAPILLARY

MARSUPIAL

with two common letters (A and R), but the best is:

CAPILLARY

MARSUPIAL

Which has three common letters (P, I and L).

The approximation measure appx(word1, word2) for two words is given by:

common letters * 2

-----------------------------

length(word1) + length(word2)

Thus, for this example, appx(CAPILLARY, MARSUPIAL) = 6 / (9 + 9) = 1/3. Obviously, for any word W appx(W, W) = 1, which is a nice property, while words with no common letters have an appx value of 0.

输入样例:

CAR CART
TURKEY CHICKEN
MONEY POVERTY
ROUGH PESKY
A A
-1


输出样例:

appx(CAR,CART) = 6/7
appx(TURKEY,CHICKEN) = 4/13
appx(MONEY,POVERTY) = 1/3
appx(ROUGH,PESKY) = 0
appx(A,A) = 1


输入描述:

The input for your program will be a series of words, two per line, until the end-of-file flag of -1.

Using the above technique, you are to calculate appx() for the pair of words on the line and print the result.

The words will all be uppercase.

输出描述:

Print the value for appx() for each pair as a reduced fraction,Fractions reducing to zero or one should have no denominator.

#include<iostream>

#include<string>

using namespace std;

int f(char *str1,char *str2)

{

int i,j,len1,len2,max=0,n;

len1=strlen(str1);

len2=strlen(str2);

for(i=0;i<len2;i++)

{

n=0;

for(j=0;j<len1;j++)

{

if(i+j>=len2)

break;

if(str1[j]==str2[i+j])

n++;

}

if(n>max)

max=n;

}

for(i=0;i<len1;i++)

{

n=0;

for(j=0;j<len2;j++)

{

if(i+j>=len1)

break;

if(str2[j]==str1[i+j])

n++;

}

if(n>max)

max=n;

}

return max;

}

int gcd(int n,int m)//求最大公约数

{

while(m)

{

int temp;

temp=m;

m=n%m;

n=temp;

}

return n;

}

int main()

{

char str1[200],str2[200];

while(cin>>str1,strcmp(str1,"-1"))

{

cin>>str2;

int c,n,len;

len=strlen(str1)+strlen(str2);

n=2*f(str1,str2);

if(n%len==0)//输出则应该是1

cout<<"appx("<<str1<<","<<str2<<") = "<<n/len<<endl;

else

{

c=gcd(len,n);

cout<<"appx("<<str1<<","<<str2<<") = "<<n/c<<"/"<<len/c<<endl;

}

}

return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: