Page 4 of 5

Posted: Thu Sep 20, 2007 10:41 am
by rossi kamal
can anyone help i am getting TLE ...

Code: Select all

//11151.cpp

//largest palindrome 
#include<stdio.h>
#include<string.h>

int largest(int xx,int yy);
int max(int zz,int kk);


char a[1005];

int main()
{
	 
	
	 //freopen("inpalindrome.txt","r",stdin);     
     int n; 
	 
	 scanf("%d\n",&n);
	// getchar();
	 int ii;
     for(ii=1;ii<=n;ii++)
	 {	
		 
			
		 
			gets(a);
			printf("%d",largest(0,strlen(a)-1) );
			
	 

	}
	 




	return 0;
}

int largest(int i,int j)
{
	
	 if(i==j)
		 return 1;
	 
	// if(i>j)
	//	return 0;
	
	 
	 else if(i!=j && a[i]==a[j])
		 return largest(i+1,j-1)+2;



     else
		 return max(largest(i+1,j) ,largest(i,j-1) );
	



}


int max(int aaa,int bbb)
{
	if(aaa>=bbb)
		return aaa;
	else 
		return bbb;

}

Posted: Tue Sep 25, 2007 3:04 pm
by helloneo
You can try "memorization" or you can solve it with bottom-up DP..
Another thing.. taking input in your code is not ok.. see previous posts about that..

Posted: Sun Oct 07, 2007 7:05 pm
by sapnil
To rossi kamal

YES


Thanks
keep posting
Sapnil

Re: I still got WA

Posted: Sun Aug 17, 2008 4:40 pm
by bishop
FAQ wrote:Here is my trying input

Code: Select all

12
ADAM
MADAM

qweqweqwedadqweqweqwe

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
abcdefghijklmnopqrstuvwxyz
abcdefhh
abcabcabc
0101010101
a
abefgba
Line with nothing is empty string

Code: Select all

3
5
0
13
0
255
1
2
5
9
1
5
some more tricky tests please?
my code gives output same output
but result WA
why?? i cannot find ..

Code: Select all

AC

Re: 11151 - Longest Palindrome

Posted: Tue Aug 19, 2008 4:16 pm
by emotional blind
instead of this

Code: Select all

      if(n>0)
      {cout<<endl;}

you should simply print new line

Code: Select all

cout<<endl;

Re: 11151 - Longest Palindrome

Posted: Tue Aug 19, 2008 4:24 pm
by bishop
again WA
after editing

Re: 11151 - Longest Palindrome

Posted: Tue Aug 19, 2008 4:33 pm
by bishop
here my post again
about "WA"

Code: Select all

AC


Re: 11151 - Longest Palindrome

Posted: Tue Aug 19, 2008 4:44 pm
by emotional blind

Code: Select all

       for(i=0; i<=m; i++)
       for(j=0; j<=n; j++)   
       {
          if(X[i]==Y[j]){c[i][j]=c[i-1][j-1]+1;}

Here is the problem
when i = 0 and j = 0, you are trying to access c[-1][-1] by c[i-1][j-1], isn't it?

I think your loop should start from 1 for each i and j.

Re: 11151 - Longest Palindrome

Posted: Tue Aug 19, 2008 4:53 pm
by bishop
AC

Re: 11151 - Longest Palindrome

Posted: Tue Aug 19, 2008 4:56 pm
by emotional blind
So, What do you mean by posting your code again?
Did you get accepted?

Re: 11151 - Longest Palindrome

Posted: Tue Aug 19, 2008 5:03 pm
by bishop
sorry
that was for Wrong answer.......

Re: 11151 - Longest Palindrome

Posted: Tue Aug 19, 2008 5:49 pm
by bishop
arif bhai great helper............thank u :lol:

Re: 11151 - Longest Palindrome

Posted: Tue Aug 19, 2008 8:15 pm
by emotional blind
you mean, you got accepted?

Re: handling time limit on judge (I'm newbie)

Posted: Mon Oct 06, 2008 4:23 pm
by lnr
To Thanks 4 Alice
Your code:

while(true) {
type input - (cin.getline)
processing input - (algorithm for #11151)
print output - (cout)
}
May be you are not checking the end of file.
You are taking input on and on and on..................................................

Re: 11151 - Longest Palindrome

Posted: Tue Sep 22, 2009 9:06 pm
by islam_al_aarag
hi
i wrote a code to this problem in java
but i got WA
although i get all test cases in the topic right this is my code tell me if it is illegal to post coz i am new import java.util.*;
import java.io.*;
class Main
{
static String s;
static int[][] DP = new int[1001][1001];
public static int get(int i , int j)
{
if(i==s.length() || j==-1 )
return DP[s.length()-1][0];
else
{
if(s.charAt(i)==s.charAt(j)){
if(DP[j]==0) DP[j]=1+get(i+1,j-1);
return DP[j];
}
else
{
if(DP[j]==0) DP[j]=Math.max(get(i,j-1),get(i+1,j));
return DP[j];
}
}
}
public static void main(String[] args)throws IOException
{
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
int n=Integer.parseInt(stdin.readLine());
for(int i=0;i<n;i++)
{
s=stdin.readLine();
if(s.length()!=0) System.out.println(get(0,s.length()-1));
else System.out.println(0);
for(int j=0;j<s.length()+1;j++) Arrays.fill(DP[j] , 0);
}
}
}