Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1292109
  • 博文数量: 196
  • 博客积分: 4141
  • 博客等级: 中将
  • 技术积分: 2253
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-21 20:04
文章存档

2019年(31)

2016年(1)

2014年(16)

2011年(8)

2010年(25)

2009年(115)

分类:

2009-06-16 17:30:51

Letter Deletion

Time Limit:1sMemory limit:32M
Accepted Submit:159Total 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;
}

阅读(1482) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~