PKU 1580 String Matching
来源:互联网 发布:知乎 都井睦雄 编辑:程序博客网 时间:2024/06/05 15:30
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 1249 | Accepted: 710 |
Description
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:
-----------------------------
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.
Input
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.
Output
Sample Input
CAR CARTTURKEY CHICKENMONEY POVERTYROUGH PESKYA A-1
Sample Output
appx(CAR,CART) = 6/7appx(TURKEY,CHICKEN) = 4/13appx(MONEY,POVERTY) = 1/3appx(ROUGH,PESKY) = 0appx(A,A) = 1
Source
#include<iostream>
#include<string>
using namespace std;
int max(int a,int b)
{
return a>b?a:b;
}
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;
}
{
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)cout<<"appx("<<str1<<","<<str2<<") = "<<n/len<<endl;
else
{
c=gcd(len,n);
cout<<"appx("<<str1<<","<<str2<<") = "<<n/c<<"/"<<len/c<<endl;
}
}
return 0;
}
- PKU 1580 String Matching
- poj 1580 String Matching
- POJ 1580 String Matching G++
- KMP Matching: String Matching
- String Matching
- String Matching
- String Matching
- Naive String Matching: String Matching
- poj 1580 String Matching 【字符串处理】
- POJ 1580-String Matching(枚举)
- Finite Automaton Matching: String Matching
- Rabin Karp Matching: String Matching
- String matching with simmetrics
- Binary String Matching
- 字符串匹配(String Matching)
- Binary String Matching
- ZOJ 1170 String Matching
- Binary String Matching
- 读《ASP.NET 2.0 Website Programming—Problem - Design - Solution》有感(一)
- PHP生成静态页面详解
- 使用MFC开发ActiveX控件全过程- -
- Java程序文件路径中有空格的处理
- 站长必须了解的基础常识
- PKU 1580 String Matching
- Sohu面试经历
- ITK&VTK安装方法(综合几个网上的方法)
- SendMessage(HWND, WM_SETFONT,lParam,wParam) and SendMessage(HWND,EM_SETMARGINS,lParam,wParam)
- Words from the Heart
- 如何和坏老板相处
- 下周三出差,先去山西后去海南
- [英语阅读]日本教授教会鲸鱼“说话”
- 证券交易印花税明起调整为单边征收