

i think the chance of getting WA is very high when you use lcs algo to solve this prob.
Moderator: Board moderators
Hope it helps youGiven two strings of lowercase letters ...
Code: Select all
#include <stdio.h> /* printf */
#include <string.h> /* strlen */
/*******************************
* ACM Contest - Problem 10252 *
*******************************/
int min(int a, int b)
{
if (a < b)
return a;
else
return b;
}
int main(void)
{
char sta[2001], stb[2001];
int frqa[26], frqb[26];
unsigned int i, l1, l2;
int j, c;
while (1){
if (gets(sta) == NULL) goto fim;
gets(stb);
l1 = strlen(sta);
l2 = strlen(stb);
if ((l1 < 1) || (l2 < 1)){
printf("\n");
continue;
}
for (i=0;i<26;i++){frqa[i] = frqb[i] = 0;}
for (i=0;i<l1;i++){
c = sta[i] - 'a' + 1;
if ((c >= 0) && (c < 26))
frqa[c]++;
}
for (i=0;i<l2;i++){
c = stb[i] - 'a' + 1;
if ((c >= 0) && (c < 26))
frqb[c]++;
}
for (i=0;i<26;i++)
if ((frqa[i] > 0) && (frqb[i] > 0))
for (j=0;j<min(frqa[i],frqb[i]);j++)
printf("%c",i+96);
printf("\n");
}
fim:
return 0;
}
Code: Select all
pretty
women
walking
down
the
street
ab
ba
abc
acd
down
won
prettywoman
walkingdown
inging
singing
abc
efg
ghi
a
a
Code: Select all
e
nw
et
ab
ac
now
anow
ggiinn
a
Code: Select all
z
z
Code: Select all
ab
ba
Code: Select all
ab
Code: Select all
Ca
Ca
Code: Select all
ac