mshubham's blog

By mshubham, 13 years ago, In English

I am getting wrong answer with space optimization technique

Here is my code

int main(){
	int test;
	for(scanf("%d",&test);test>0;test--){
		char str1[6110];
		char str2[6110];
		scanf("%s",str1);
		int l=strlen(str1);
		for(int i=0;i<l;i++) str2[i]=str1[l-i-1];
		int L[3][6110];
		int tmp=1;
		memset(L,0,sizeof L);
		for(int i=1;i<=l;++i){
			for(int j=1;j<=l;++j){
				if(str1[i-1]==str2[j-1]) L[tmp][j]=1+L[!tmp][j];
				else L[tmp][j]=max(L[tmp][j-1],L[!tmp][j]);
			}
			tmp=!tmp;
		}
		int ans=l-L[!tmp][l];
		printf("%d\n",ans);
	}
	return 0;
}
  • Vote: I like it
  • -1
  • Vote: I do not like it

»
13 years ago, hide # |
← Rev. 2  
Vote: I like it +3 Vote: I do not like it
if(str1[i-1]==str2[j-1]) L[tmp][j]=1+L[!tmp][j-1];