Common Permutation
Input: standard input
Output: standard output
Time Limit: 4 seconds
Memory Limit: 32 MB
Giventwo strings of lowercase letters, aand b, print the longeststring x of lowercase letterssuch that there is a permutation of xthat is a subsequence of aand there is a permutation ofxthat is a subsequence ofb.
Input
Input file contains several lines of input.Consecutive two lines make a set of input. That means in the input file line1 and2 is a set of input, line3and4 is a set of input and so on.The first line of a pair containsaand the second containsb.Each string is on a separate line and consists of at most1000 lowercase letters.
Output
For each set of input, output a line containingx. If severalx satisfy the criteria above,choose the first one in alphabetical order.
Sample Input:
pretty
women
walking
down
the
street
Sample Output:
e
nw
et
这题开始WA了一次
后来对照了一份其他AC的代码
发现是没考虑到重复出现的情况
例如
abccccddd
bcccdd
输出不是bcd
而是bcccdd
AC代码如下:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main () {
char a[1005], b[1005];
while(gets(a) && gets(b)) {
int mark1[26] = {0};
int mark2[26] = {0};
int mark3[26] = {0};
char mark[27] = {"abcdefghijklmnopqrstuvwxyz"};
memset(mark2, 0, sizeof(mark2));
int i, j;
int len1 = strlen(a);
int len2 = strlen(b);
for(i = 0; i < len1; i++)
mark1[a[i] - 'a'] ++;
for(i = 0; i < len2; i++)
mark2[b[i] - 'a'] ++;
for(i = 0; i < 26; i++) {
if(mark1[i] == 0 || mark2[i] == 0)
mark3[i] = 0;
else {
if(mark1[i] > mark2[i])
mark3[i] = mark2[i];
else
mark3[i] = mark1[i];
}
}
for(i = 0; i < 26; i++) {
if(mark3[i] != 0) {
for(j = 0; j < mark3[i]; j++)
printf("%c", mark[i]);
}
}
printf("\n");
}
return 0;
}