String Matching (poj1580), matchingpoj1580
/* String Matching
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:
Common letters * 2
-----------------------------
Length (word1) + length (word2)
Thus, for this example, appx (CAPILLARY, MARSUPIAL) = 6 // (9 + 9) = 1/3. obviusly, 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 compaction cing to zero or one shoshould have no denominator.
Sample Input
CAR CART
TURKEY CHICKEN
MONEY POVERTY
ROUGH PESKY
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
*/
# Include <stdio. h>
# Include <string. h>
Int gcd (int m, int n) // calculates the maximum approximate number;
{
If (n = 0)
Return m;
Else
Return gcd (n, m % n );
}
Int main ()
{
Char a [1, 100], B [2, 100];
While (scanf ("% s", )! = EOF)
{
If (strcmp (a, "-1") = 0)
Break;
Else
Scanf ("% s", B );
Int I, j, k, l, max = 0, t;
Int len1, len2, len;
Len1 = strlen ();
Len2 = strlen (B );
For (I = 0; I <len1; I ++)/* is equivalent to a [len1], starting from, I = j = 0, comparison between B [I ++] And a [j ++], t ++,
After B [0] compared with a [I], to a [len-1] and B [I] compared to t, and compared with the previous t, to get a larger t, continue the comparison from B [1] until it is better to end the comparison */
{
K = 0;
For (l = I, j = 0; l <len1; l ++, j ++)
{
If (a [l] = B [j])
K ++;
}
Max = max> k? Max: k;
}
For (I = 0; I <len2; I ++) // It is equivalent to B [len2]. Similar to the above, it is easier to give a different picture.
{
K = 0;
For (l = I, j = 0; l <len2; l ++, j ++)
{
If (B [l] = a [j])
K ++;
}
Max = max> k? Max: k;
}
Len = len1 + len2, max * = 2;
T = gcd (max, len );
If (len = max)
{
Printf ("appx (% s, % s) = 1 \ n", a, B );
Continue; // This continue cannot be omitted;
}
If (max = 0)
{
Printf ("appx (% s, % s) = 0 \ n", a, B );
Continue;
}
Else
Printf ("appx (% s, % s) = % d/% d \ n", a, B, max/t, len/t );
}
Return 0;
}
String str = new String ("abc"); how many String objects are created?
Of course there are two. equivalent to: String s = "abc ";
String str = new String ();
Str = s;
So str and s are two objects. But he hides it.
String str = new String ("1" + "2"); creates several objects.
There is only one object. there will be three Semantic Analyses: 1, 2, and 12, but in fact the C # compiler will be optimized during compilation, that is to say, constant operations like this will automatically calculate the results and compile them.
String str = new String ("12"); no difference.