## 10252 - Common Permutation

Moderator: Board moderators

### 10252

It is easy to solve it:

Is my solution correct?
input:
aab
baa

output:
aab

or

input:
aab
baa

output:
ab

Do you know in which may be error?
Thanks

Code: Select all
`/* @JUDGE_ID: 9072XC 10252 C++ */#include <string.h>#include <stdio.h>   int main() {char a[10000],b[10000];int prvkya[30];int prvkyb[30];int i=0;while (gets(a) != NULL) {   gets(b);   if (strcmp(a, "") == 0) break;   for (int k=0; k<26; k++) {      prvkya[k] = 0;      prvkyb[k] = 0;   }   for (i=0; i<strlen(a); i++) {      prvkya[a[i]-'a']++;   }   for (i=0; i<strlen(b); i++) {      prvkyb[b[i]-'a']++;   }   for (i=0; i<26; i++)    {      if ((prvkya[i]>0)&&(prvkyb[i]>0))     for (int l=(prvkyb[i]>prvkya[i]?prvkya[i]:prvkyb[i]); l>0; l--)   {        printf("%c",i+'a'); }   }   printf("n");}return 0;}`

MAY ANYBODY TEST MY SOURCE AND TELL ME WHAT'S WRONG. THANKS

<font size=-1>[ This Message was edited by: ahanys on 2002-03-19 12:17 ]</font>
ahanys
New poster

Posts: 24
Joined: Mon Nov 19, 2001 2:00 am
Location: N/A

I have the same question ...
I sent these two version described above to online judge, but got WA.

The following is my method:

1. a table storing each character with the number of appearing in the two string individual.
2. if the character appears one or more times, then I determine how many the same character to print.

As described above in the second step, I altered a little and made another version.
But both got wrong ... >"< ...

Couldn't I misunderstand the meaning of the problem??

DemonCris
New poster

Posts: 25
Joined: Sun Feb 24, 2002 2:00 am
Location: Taiwan

Print 'a' min('a' in s1, 'a' in s2) times...
But there can be blank lines too (n)
MegaS
New poster

Posts: 9
Joined: Tue Feb 05, 2002 2:00 am

I strongly recommend using "gets" to read all the input.

DemonCris
New poster

Posts: 25
Joined: Sun Feb 24, 2002 2:00 am
Location: Taiwan

for i=1 to length(str1)
for j=1 to length(str2)
begin
if str1[i]=str2[j] then
begin
delete char at j position in str2
add char at i position to temporary string
exit j loop
end
end
write alphabet. sorted temporary string

I got P.E. in Pascal, if anyone knows what's the catch ?

P.S: now I am trying to code it in C++, this is my sorting code and i don't know why I got SIGSEGV 11 error??? Please help !

unsigned int i,j;
char tmpStr[1000],ch[1];

for (i=0;i<strlen(tmpStr)-1;i++)
for(j=i+1;j<strlen(tmpStr);j++)
if (tmpStr[i]>tmpStr[j])
{
ch[0] = tmpStr[i];
tmpStr[i]=tmpStr[j];
tmpStr[j]=ch[0];
}
ante
New poster

Posts: 8
Joined: Wed Mar 20, 2002 2:00 am

Maybe you should make the array bigger by one element to make room for the zero-char endmarker.
Btw, why don't you just use "sort( tmpStr, tmpStr+strlen(tmpStr) )"? Or at least swap(...)? In C++, these things are available...
Stefan Pochmann
A great helper

Posts: 284
Joined: Thu Feb 28, 2002 2:00 am
Location: Germany

Hi!

I just made it in a simply way...
I have two tables from 1 to 255 each (they can be shorter)...
I read the letter and increse one call of the table -> inc(table[ord(char)])
then I do the same with the next line and put it into the second table...
Then
for x:=1 to 256 do
for y:=1 to min(tab[x],tab2[x]) do
Write(chr(x));

And that's all...

I hope it will help

Good Luck !
cyfra
Experienced poster

Posts: 144
Joined: Thu Nov 22, 2001 2:00 am
Location: Gdynia, Poland

I solved it in C++ and still P.E.
Does anyone knows how to solve it without P.E

P.S:
Thanx Stefan for C++ tips.
ante
New poster

Posts: 8
Joined: Wed Mar 20, 2002 2:00 am

In the contest, everybody got a P.E. because they had no newline char at the end of the file. So try not putting a newline char after your last line.

During the contest, this was extremely annoying, since P.E.'s weren't accepted. They rejudged it afterwards, accepting P.E.'s, but of course lots of people had already wasted their time on it. Somebody had 35 submissions...
Stefan Pochmann
A great helper

Posts: 284
Joined: Thu Feb 28, 2002 2:00 am
Location: Germany

I got "WA" when I used "string". But got acc when i used char array. does any body know the reason??????

<font size=-1>[ This Message was edited by: ram on 2002-04-02 22:17 ]</font>
ram
New poster

Posts: 30
Joined: Wed Mar 06, 2002 2:00 am

Highly depends on how you use them. I guess you used "cin >> s" and gets(...), repsectively?
Stefan Pochmann
A great helper

Posts: 284
Joined: Thu Feb 28, 2002 2:00 am
Location: Germany

I used cin>>"string variable" and cin.getline("array variable",1003,'n')

But why should it make a difference??? Any idea???

<font size=-1>[ This Message was edited by: ram on 2002-04-03 08:54 ]</font>
ram
New poster

Posts: 30
Joined: Wed Mar 06, 2002 2:00 am

It makes a difference, if there is a blank line. With cin.getline you read only the 'n', with cin>>string you get the next line.
Guru

Posts: 724
Joined: Wed Dec 19, 2001 2:00 am
Location: Germany

I give up from this problem and P.E. response, i tried many methods for output but none of them works,
if anyone has AC whthout P.E would you please send it to me at mailto: anblasko@globalnet.hr
ante
New poster

Posts: 8
Joined: Wed Mar 20, 2002 2:00 am

### 10252 - Common Permutation

i don't know what the wrong case is.
i use LCS.
thanks.

#include<stdio.h>
#include<string.h>
#define N 1500

int p=0,c[N][N],f=0;
char b[N][N],x[N],y[N],a[N];

void print(int i,int j)
{
if(i==0 || j==0) return;
if(b[i][j]=='\\')
{
print(i-1,j-1);
/* printf("%c",x[i]);*/
a[f++]=x[i];
p++;
}
else if(b[i][j]=='|')
print(i-1,j);
else print(i,j-1);
}

main()
{
int m,n,i,j,l;
int coun=0,q[27];
char temp;
while(1)
{
if(gets(x)==NULL) break;
gets(y);
m=strlen(x);
n=strlen(y);
for(i=m-1;i>=0;i--) x[i+1]=x[i];
for(i=n-1;i>=0;i--) y[i+1]=y[i];
x[m+1]=y[n+1]=0;
l=p=0;
for(i=1;i<=m;i++) c[i][0]=0,b[i][0]=' ';
for(i=0;i<=n;i++) c[0][i]=0;
b[0][0]=' ';
for(i=1;i<=m;i++)
for(j=1;j<=n;j++)
if(x[i]==y[j])
{
c[i][j]=c[i-1][j-1]+1;
l++;
b[i][j]='\\';
}
else if(c[i-1][j]>=c[i][j-1])
{
c[i][j]=c[i-1][j];
b[i][j]='|';
}
else
{
c[i][j]=c[i][j-1];
b[i][j]='-';
}
f=0;
print(m,n);
a[f]=0;
for(i=0;i<26;i++) q[i]=0;
for(i=0;i<f;i++) q[a[i]-'a']++;
for(i=0;i<26;i++)
if(q[i])
printf("%c",i+'a');
printf("\n");
}
return 0;
}
[/b]
"Everything should be made simple, but not always simpler"
anupam
A great helper

Posts: 405
Joined: Wed Aug 28, 2002 6:45 pm

Next