String Matching
Time Limit: 1000MS |
|
Memory Limit: 10000K |
Total Submissions: 2881 |
|
Accepted: 1500 |
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:
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.
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.
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
对于输入中给出的两个同一行的字符串进行比较,看最多能够有多少个共同的字符,比较方式是,将其中一个字符串和另一个字符串错位,比较错位之后,相同的位子上的相同字符数量。
解法,纯模拟。
注意点:
1)最后输出的数字,如果是整数的话,必须要以整数输出,否则以小数输出。
2)字符串错位的时候,可能会让移动的字符串超出另一个字符串,即是说,可能需要让整个字符串都错位出去(1WA)
3)字符串错位,需要正反两次计算,因为移动str1,和移动str2的结果是不同的,需要正反两次比较,取最大值(1WA)
测试数据:
1)AAAB BAAA 3/4
2)CAB ABC 2/3
代码(1AC 2WA):
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
char str[2][50];
int appx(char str1[], char str2[]){
int len1, len2;
int i, j;
int tmp, max;
len1 = strlen(str1);
len2 = strlen(str2);
for (i = max = 0; i < len2; i++){
for (tmp = j = 0; j < len1 && i + j < len2; j++){
if (str2[i + j] == str1[j]){
tmp++;
//printf("%c ", str1[j]);
}
}
if (max < tmp){
max = tmp;
}
}
return max;
}
int main(void){
int i, j;
int m, n, l;
int min;
while (scanf("%s", str[0]), strcmp(str[0], "-1") != 0){
scanf(" %s", str[1]);
m = strlen(str[0]);
n = strlen(str[1]);
m += n;
n = appx(str[1], str[0]);
l = appx(str[0], str[1]);
if (n < l){
n = l;
}
n *= 2;
//printf("")
for (i = 2; i <= (m < n ? m : n); i++){
while (n % i == 0 && m % i == 0){
n /= i;
m /= i;
}
}
if (n % m != 0){
printf("appx(%s,%s) = %d/%d\n", str[0], str[1], n , m);
}
else{
printf("appx(%s,%s) = %d\n", str[0], str[1], n / m);
}
}
return 0;
}