10405 - Longest Common Subsequence
Moderator: Board moderators
Thank you, I got AC
I will not paste functions I am not call next time.
Thank you for your hint
Thank you for your hint
-
- New poster
- Posts: 4
- Joined: Tue May 30, 2006 4:51 pm
10405
WHY??
#include<stdio.h>
int main()
{
int aa,bb,i,j,k,h;
char a[1001],b[1001];
while(gets(a)!=NULL&&gets(b)!=NULL)
{
aa=strlen(a);bb=strlen(b);
for(i=0,k=0,h=-1;i<aa;i++)
for(j=h+1;j<bb;j++)
if(a==b[j])
{k++;h=j;break;}
printf("%d\n",k);
}
return 0;
}
#include<stdio.h>
int main()
{
int aa,bb,i,j,k,h;
char a[1001],b[1001];
while(gets(a)!=NULL&&gets(b)!=NULL)
{
aa=strlen(a);bb=strlen(b);
for(i=0,k=0,h=-1;i<aa;i++)
for(j=h+1;j<bb;j++)
if(a==b[j])
{k++;h=j;break;}
printf("%d\n",k);
}
return 0;
}
-
- New poster
- Posts: 4
- Joined: Tue May 30, 2006 4:51 pm
thanks, but my code is still WR......Why?
Code: Select all
#include<stdio.h>
int main()
{
int aa,bb,i,j,k,h;
void reverse(char*a,char*b);
char a[1001],b[1001];
while(gets(a)!=NULL&&gets(b)!=NULL)
{
aa=strlen(a);bb=strlen(b);
if(aa>bb)
reverse(a,b);
aa=strlen(a);bb=strlen(b);
for(i=0,k=0,h=-1;i<aa;i++)
for(j=h+1;j<bb;j++)
if(a[i]==b[j])
{k++;h=j;break;}
printf("%d\n",k);
}
return 0;
}
void reverse(char*a,char*b)
{
int i,aa,bb;
char c[1001];
aa=strlen(a);bb=strlen(b);
for(i=0;i<aa;i++)
c[i]=a[i];
c[i]=0;
for(i=0;i<bb;i++)
a[i]=b[i];
a[i]=0;
for(i=0;i<aa;i++)
b[i]=c[i];
b[i]=0;
}
-
- New poster
- Posts: 4
- Joined: Tue May 30, 2006 4:51 pm
WR
Sorry, Daveon. I'm still WR.
Code: Select all
code is deleted
Last edited by linhomeyeu on Wed Jul 19, 2006 5:06 am, edited 1 time in total.
-
- New poster
- Posts: 4
- Joined: Tue May 30, 2006 4:51 pm
I think I'm the second one. However, I have to thank for your forbearing answer.(I'm not a English-mother-language person, so my English may be awful. please do not care it.)daveon wrote:Do you know of the DP solution to this Longest Common Subsequence problem?
If you just want to get AC, then you may look up this well known DP algorithm. However, if you are the one who likes to solve everything by yourself, then you'll have to prove that your algorithm is correct.
10405 WA
hi,i dont use dp for this problems,i try to make another code,but why i got WA ?
can anyone give me the testcase please ?
thanks.
Code: Select all
#include <stdio.h>
#include <string.h>
void main(){
freopen("longest.in","r",stdin);
freopen("longest.out","w",stdout);
char a[1001] = "";char b[1001]="";
while(gets(a)){
gets(b);
int ta = strlen(a);
int tb = strlen(b);
int max = 0;
int i = 0;int j = 0;
int tempi = ta;int tempj = tb;
int maks = 0;
while(i<ta){
j = 0;
while(j<tb){
if(a[i]==b[j]){
tempi = i+1;
int tempjj = j+1;
int byk = 1;
//printf("%d %d - ",i,j);
//printf("%c %c - ",a[i],b[j]);
while(tempi<ta){
tempj = tempjj;
while(tempj<tb){
if(a[tempi]==b[tempj]) {
//printf("%d %d - ",tempi,tempj);
//printf("%c %c - ",a[tempi],b[tempj]);
byk++;
tempjj=tempj+1;
tempj+=tb;
}
tempj++;
}
//printf("%d - ",tempjj);
tempi++;
}
//printf("\n");
if(byk>maks) maks = byk;
}
j++;
}
i++;
}
printf("%d\n",maks);
}
}
thanks.
-
- Experienced poster
- Posts: 109
- Joined: Sat Jun 23, 2007 9:53 pm
- Location: Brest, BELARUS
- Contact:
Hello World !
I have solved this problem (ok, AC) on C using usual LCS algo. My C prog ran during 1.807 sec and I am 2867 / 2899 in ranking. But I cannot understand how did some people solve this prob in 0.004 sec??! Even on Pascal, which is in most cases slower than C/C++! Any idea?
Thanx for attention
I have solved this problem (ok, AC) on C using usual LCS algo. My C prog ran during 1.807 sec and I am 2867 / 2899 in ranking. But I cannot understand how did some people solve this prob in 0.004 sec??! Even on Pascal, which is in most cases slower than C/C++! Any idea?
Thanx for attention
Now I lay me down to sleep...
my profile
my profile
-
- A great helper
- Posts: 383
- Joined: Mon Oct 18, 2004 8:25 am
- Location: Bangladesh
- Contact:
-
- Experienced poster
- Posts: 109
- Joined: Sat Jun 23, 2007 9:53 pm
- Location: Brest, BELARUS
- Contact:
Okay, I will try to implement bottom up as you have said.
0.027 is believable, but how did people get 0.004 seconds? Can it be cheating (knowing I/O for the prob) ?
0.027 is believable, but how did people get 0.004 seconds? Can it be cheating (knowing I/O for the prob) ?
Now I lay me down to sleep...
my profile
my profile
-
- A great helper
- Posts: 383
- Joined: Mon Oct 18, 2004 8:25 am
- Location: Bangladesh
- Contact:
-
- Experienced poster
- Posts: 109
- Joined: Sat Jun 23, 2007 9:53 pm
- Location: Brest, BELARUS
- Contact:
Can you please make a hint for me to feel the difference between bottom up and top down in this problem. o(n^2) solution of this kind:
is top-down? or bottom-up?
thanx in advance
Code: Select all
for i in sequence 1
for j in sequence 2
if s1[i]=s2[j] then
ans[i][j] = ans[i-1][j-1] + 1;
else if .... and so on ...
thanx in advance
Now I lay me down to sleep...
my profile
my profile