Letter Deletion
Time Limit:1s | Memory limit:32M |
Accepted Submit:159 | Total Submit:372 |
You are given two words (each word consists of upper-case English letters).
Try to delete some letters from each word so that the resulting words are equal.
What is the maximum possible length of the resulting word?
Input
There will be no more than 10 test cases.
Each test case consists of a single line, contaning the two words
separated by a single space. The length of each of these words is
between 1 and 200.
Output
For each test case output the maximum length of a
resulting word (the length of the longest word that can be created from
both words by removing some letters).
If the two words have no letters in common, output 0.
Sample input
AAABBB ABABAB
AXYAAZ CCCXCCCYCCCZCC
ABCDE EDCBA
Sample output
4
3
1
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#define MAX_VAL(x, y) ((x) > (y) ? (x) : (y))
#define MAX_VAL3(x, y, z) (MAX_VAL(MAX_VAL(x, y), z))
int main()
{
int i, j;
int len1, len2;
char str1[300], str2[300];
int result[300][300];
while (scanf("%s", &str1[1]) != EOF)
{
scanf("%s", &str2[1]);
len1 = strlen(&str1[1]);
len2 = strlen(&str2[1]);
memset(result, 0, sizeof(result));
for (i = 1; i <= len1; ++i)
{
for (j = 1; j <= len2; ++j)
{
if (str1[i] == str2[j])
result[i][j] = MAX_VAL3(result[i - 1][j - 1] + 1, result[i][j - 1], result[i - 1][j]);
else
result[i][j] = MAX_VAL(result[i - 1][j], result[i][j - 1]);
}
}
printf("%d\n", result[len1][len2]);
}
return 1;
}
|
阅读(1525) | 评论(0) | 转发(0) |