首页 > 代码库 > poj 1580 String Matching(比较字符串的相似程度,四个for循环即可)
poj 1580 String Matching(比较字符串的相似程度,四个for循环即可)
String Matching
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 3717 | Accepted: 1913 |
Description
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:
with two common letters (A and R), but the best is:
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.
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
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.
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
Print the value for appx() for each pair as a reduced fraction,Fractions reducing to zero or one should have no denominator.
Sample Input
CAR CART TURKEY CHICKEN MONEY POVERTY ROUGH PESKY A A -1
Sample Output
appx(CAR,CART) = 6/7 appx(TURKEY,CHICKEN) = 4/13 appx(MONEY,POVERTY) = 1/3 appx(ROUGH,PESKY) = 0 appx(A,A) = 1
Source
Pacific Northwest 1999
思路:
就是简单地 一个暴力遍历。
体会:
细节很重要,团队默契很重要。因为与队友的对题意理解的分歧,导致这道水题到最后愣是没有AC出来。虽说最后是我的理解正确,但是因为这个分歧,直接影响了团队配合的默契,还是要提高自身能力,提高团队配合力度。心向往之,行而不止。加油!
代码如下:
#include<stdio.h> #include<string.h> int gcd(int a,int b) { return !b?a:gcd(b,a%b); } char a[1010],b[1010],temp[2022]; int main() { int i,j,i1,j1; while(~scanf("%s",a),strcmp(a,"-1")) { scanf("%s",b); int len1=strlen(a); int len2=strlen(b); int max=0; for(i=0;i<len1;i++) { for(j=0;j<len2;j++) { int m=0; for(i1=i,j1=j;i1<len1&&j1<len2;i1++,j1++) { if(a[i1]==b[j1]) ++m; } if(max<m) max=m; } } if(max==0) printf("appx(%s,%s) = 0\n",a,b); else { int s; s=len1+len2; max*=2; if(s==(max)) printf("appx(%s,%s) = 1\n",a,b); else { int f; f=gcd(s,max); printf("appx(%s,%s) = %d/%d\n",a,b,max/f,s/f); } } } return 0; }
poj 1580 String Matching(比较字符串的相似程度,四个for循环即可)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。