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;
}
相关文章推荐
- POJ 1580 String Matching(我的水题之路——字符串公共子串的比较雏形)
- poj 1580 String Matching【gcd辗转相除法】
- KMP Matching: String Matching
- String Matching: The Simple way to implement String Matching
- String Matching(poj1580)
- poj1580 String Matching (枚举,gcd)
- String Matching: The Simple way to implement String Matching
- HDU1306 String Matching 【暴力】
- POJ 1580 && HDU 1306 String Matching(水~)
- POJ 1580 String Matching 很纠结,代码写了很多,还是过了,再对比一下人家的代码,惭愧
- hdoj 1306 String Matching
- soj 2307 String Matching(KMP)
- 字符串匹配(String Matching)
- String matching整理
- 九度oj-1094-String Matching
- 字符串匹配(string matching)算法之一 (Naive and Rabin_Karp)
- 【字符串】poj1582 String Matching
- String Matching(poj1580)
- 字符串匹配(string matching)算法之二:利用有限自动机
- hdu 1036 String Matching