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;
}