11151 - Longest Palindrome
Moderator: Board moderators
-
- Experienced poster
- Posts: 106
- Joined: Sun Feb 17, 2002 2:00 am
- Location: Seoul, South Korea
- Contact:
11151 - Longest Palindrome
hmm.. It's so tricky. It just looks like an easy problem, but I received WA repeatedly... Is there any critical input?
I love Problem Solving!!!




-
- A great helper
- Posts: 383
- Joined: Mon Oct 18, 2004 8:25 am
- Location: Bangladesh
- Contact:
Re: 11151 Longest Palindrome
My code takes emtpy string as input.soyoja wrote:Is there any critical input?
What about yours?
11151 WA
I am getting wrong answer but i dont know why
can anyone tell me
Here is the code
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
string s,t1,t2;
int main()
{
//freopen("d.in","rt",stdin);
int t;
int i,j;
cin>>t;
getline(cin,s);
for(i=0;i<t;i++)
{
getline(cin,s);
if(s=="")
{
cout<<"0"<<endl;
continue;
}
for(j=0;j<s.length();j++)
{
t1 = s.substr(0,s.length()-j);
t2 = s.substr(0,s.length()-j);
reverse( t1.begin() , t1.end() );
if(t1 == t2)
{
cout<<t1.length()<<endl;
break;
}
t1 = s.substr(j);
t2 = s.substr(j);
reverse( t1.begin() , t1.end() );
if(t1 == t2 )
{
cout<<t1.length()<<endl;
break;
}
}
}
return 0;
}
can anyone tell me
Here is the code
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
string s,t1,t2;
int main()
{
//freopen("d.in","rt",stdin);
int t;
int i,j;
cin>>t;
getline(cin,s);
for(i=0;i<t;i++)
{
getline(cin,s);
if(s=="")
{
cout<<"0"<<endl;
continue;
}
for(j=0;j<s.length();j++)
{
t1 = s.substr(0,s.length()-j);
t2 = s.substr(0,s.length()-j);
reverse( t1.begin() , t1.end() );
if(t1 == t2)
{
cout<<t1.length()<<endl;
break;
}
t1 = s.substr(j);
t2 = s.substr(j);
reverse( t1.begin() , t1.end() );
if(t1 == t2 )
{
cout<<t1.length()<<endl;
break;
}
}
}
return 0;
}
-
- Experienced poster
- Posts: 136
- Joined: Fri Apr 15, 2005 3:47 pm
- Location: Singapore
- Contact:
Try this input :
I think the output should be :
CMIW...
Code: Select all
1
qweqweqwedadqweqweqwe
Code: Select all
3
-
- A great helper
- Posts: 383
- Joined: Mon Oct 18, 2004 8:25 am
- Location: Bangladesh
- Contact:
Oput should bejan_holmes wrote:Try this input :
I think the output should be :Code: Select all
1 qweqweqwedadqweqweqwe
CMIW...Code: Select all
3
Code: Select all
13
I still got WA
Here is my trying input
Line with nothing is empty string
some more tricky tests please?
Code: Select all
12
ADAM
MADAM
qweqweqwedadqweqweqwe
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
abcdefghijklmnopqrstuvwxyz
abcdefhh
abcabcabc
0101010101
a
abefgba
Code: Select all
3
5
0
13
0
255
1
2
5
9
1
5
Take the entire line as input
Yes, instead of taking a string (cin>>line in C++), take the entire line as input (use cin.getline). That way, even if the line is empty, it is still considered as valid input.
-
- A great helper
- Posts: 383
- Joined: Mon Oct 18, 2004 8:25 am
- Location: Bangladesh
- Contact:
there is no critical input. what algorithm did you use?
Giorgi Saghinadze
http://acm.uva.es/problemset/usersjudge.php?user=32393
http://acm.uva.es/problemset/usersjudge.php?user=32393
Hi. I am newcomer in c++ language. I got ac in the contest with pascal, but same algorithm in c++ got wa. I wish somebody explain me, what is wrong in c++ code:Giorgi wrote:there is no critical input. what algorithm did you use?
Code: Select all
got AC
Last edited by artie on Mon Jan 01, 2007 5:23 am, edited 1 time in total.
Taking input is faulty..artie wrote:Hi. I am newcomer in c++ language. I got ac in the contest with pascal, but same algorithm in c++ got wa. I wish somebody explain me, what is wrong in c++ code:
I don't know exactly what it is.. but try this..
Code: Select all
scanf("%d", &t);
gets(s);
while (t--) {
gets(s);
...
...
}
Thanks a lot!helloneo wrote:Taking input is faulty..artie wrote:Hi. I am newcomer in c++ language. I got ac in the contest with pascal, but same algorithm in c++ got wa. I wish somebody explain me, what is wrong in c++ code:
I don't know exactly what it is.. but try this..
Code: Select all
scanf("%d", &t); gets(s); while (t--) { gets(s); ... ... }
Could anyone explain me why my input was faulty?
-
- Experienced poster
- Posts: 105
- Joined: Wed May 25, 2005 7:23 am
It is a known problem. You cannot mix scanf and gets . Use scanf or gets but not both in the same program.
What happens is scanf("%d",&t) reads an integer and then stops. So the buffer has \n . now when gets reads it gets an empty line. Also don't use gets use fgets . Don't mix fgets and scanf . Use fgets and then use strtoul for integer conversion
http://c-faq.com/stdio/scanfinterlace.html
What happens is scanf("%d",&t) reads an integer and then stops. So the buffer has \n . now when gets reads it gets an empty line. Also don't use gets use fgets . Don't mix fgets and scanf . Use fgets and then use strtoul for integer conversion
http://c-faq.com/stdio/scanfinterlace.html
-
- A great helper
- Posts: 383
- Joined: Mon Oct 18, 2004 8:25 am
- Location: Bangladesh
- Contact: