Moderator: Board moderators
a b c
| | |
a b c
If match(above) then go if not check the second string just the same way
aa aa aa
a a aaa
ab ac abc
10
0
2
aa aa aa
1)
s1[0]->s3[0]
s1[1]->s3[1] so at the end of s3 return 1;
s2[0]->s3[1] so at the end of s3 return 1;
s2[1]->s3[1] so at the end of s3 return 1;
s1[1]->s3[0]
s2[0]->s3[1] so at the end of s3 return 1;
s2[1]->s3[1] so at the end of s3 return 1;
Riyad wrote:would u care to tell us your DP solution which runs in O(n^3)iLL be really glad to know one
if u dont want to give a spoiler u can PM me ur idea , anywayz thanx for ur reply ...


vinay wrote:ohh..
I realise that my function is same as
Marko's
then why is it TLE???
Marko can I send u my code

#include<iostream>
#include<cstdio>
#include<string>
using namespace std;
char s1[61],s2[61],s3[61];
int sz1,sz2,sz3,dp[61][61][61];
int fun(int i,int j,int k){
if(sz1-i+sz2-j<sz3-k) return 0;
if(dp[i][j][k]!=-1) return dp[i][j][k];
dp[i][j][k]=0;
for(int index=i;index<sz1;index++){
if(s1[index]==s3[k]){
if(k==sz3-1) dp[i][j][k]=(dp[i][j][k]+1)%10007;
else
dp[i][j][k]=(dp[i][j][k]+fun(index+1,j,k+1))%10007;
}
}
for(int index=j;index<sz2;index++){
if(s2[index]==s3[k]){
if(k==sz3-1) dp[i][j][k]=(dp[i][j][k]+1)%10007;
else
dp[i][j][k]=(dp[i][j][k]+fun(i,index+1,k+1))%10007;
}
}
return dp[i][j][k]%10007;
}
int main(){
int t;
scanf("%d",&t);
while(t--){
scanf("%s%s%s",s1,s2,s3);
sz1=strlen(s1);
sz2=strlen(s2);
sz3=strlen(s3);
for(int i=0;i<=sz1;i++){
for(int j=0;j<=sz2;j++){
for(int k=0;k<=sz3;k++){
dp[i][j][k]=-1;
}
}
}
printf("%d\n",fun(0,0,0)%10007);
}
return 0;
}

vinay wrote:here it goes...
1
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

Users browsing this forum: No registered users and 0 guests