WOJ1330 Common Permutation

Given two strings a and b, print the longest string x of letters such that there is a permutation of x that is a subsequence of a and there is a permutation of x that is a subsequence of b.

输入格式

The input file contains several cases, each case consisting of two consecutive lines. This means that lines 1 and 2 are a test case, lines 3 and 4 are another test case, and so on.
Each line contains one string of lowercase characters, with first line of a pair denoting a and the second denoting b. Each string consists of at most 1,000 characters.

输出格式

For each set of input, output a line containing x. If several x satisfy the criteria above, choose the first one in alphabetical order.

样例输入

pretty
women
walking
down
the
street

样例输出

e
nw
et

思路

对每个字符串,记录下每个字母出现的次数,然后进行比较,从a-z进行输出,输出较小的次数

#include <stdio.h>
#include <string.h>
#define MAX 1000

int main()
{
    char str_a[MAX], str_b[MAX];  
    int i, j;
    int len_a, len_b;
    int num_a[26]={0}, num_b[26]={0}; //用来统计每个字母出现的次数

    while(scanf("%s%s", str_a, str_b) != EOF)
    {
        memset(num_a, 0, sizeof(num_a));
        memset(num_b, 0, sizeof(num_b));

        len_a = strlen(str_a);
        len_b = strlen(str_b);

        for(i=0; i<len_a; i++)
            num_a[str_a[i]-'a']++;
        for(i=0; i<len_b; i++)
            num_b[str_b[i]-'a']++;

        for(i=0; i<26; i++)
        {
            for(j=0; j<(num_a[i]<num_b[i]?num_a[i]:num_b[i]); j++)
                printf("%c", 'a'+i);
        }
        printf("\n");

    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值