353 - Pesky Palindromes
Moderator: Board moderators
-
- New poster
- Posts: 12
- Joined: Sat Aug 18, 2007 11:09 pm
- Location: CSE, University of Dhaka
-
- Experienced poster
- Posts: 103
- Joined: Tue Mar 25, 2008 11:00 pm
- Location: IUT-OIC, DHAKA, BANGLADESH
- Contact:
353 still getting wa....pliz anyone help
is anybody there for helping.......I am crying with this problem.
Code: Select all
#include<iostream.h>
#include<stdio.h>
void eachletter();
void palindroms();
int check();
int palincheck();
int i,j,p;
char s[81]="";
int main()
{
int i,j,k;
freopen("353.txt","r",stdin);
while(gets(s))
{
if(s[0]=='\0')printf("\n");
else
{
p=0;
eachletter();
palindroms();
printf("The string '%s' contains %d palindromes.\n",s,p);
}
}
fclose(stdin);
return 0;
}
int check()
{
for(j=i-1;j>=0;j--)
if(s[i]==s[j])return 0;
return 1;
}
void eachletter()
{
int m;
for(i=0;s[i]!='\0';i++)
{
m=check();
if(m)
p++;
}
}
int palincheck(int ii,int jj)
{
int v1,k;
for(v1=1;v1<=(jj-i)/2;v1++)
if(s[ii+v1]!=s[jj-v1])return 0;
for(k=ii-1;k>=ii-(jj-ii);k--)
if(s[k]==s[ii])
if(palincheck(k,ii))return 0;
return 1;
}
void palindroms()
{
for(i=0;s[i];i++)
for(j=i+1;s[j];j++)
if(s[j]==s[i])
if(palincheck(i,j))p++;
}
It is tough to become a good programmer.
It is more tough to become a good person.
I am trying both...............................
It is more tough to become a good person.
I am trying both...............................
Re: 353 WA
getting continuos WA.
anybody plzzzzzzzzzzzzzzzzz help me.
anybody plzzzzzzzzzzzzzzzzz help me.
my algorithom is
firs find palindrom of length 1,
then length 2 ,then 3,&
.......... n, where n is the length of the given string.
for examp for adam, i check a,d,a,m,ad,da,am,ada,dam,adam respectively .
is my approach is wrong?
Code: Select all
#include<iostream.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define mx 100
char p[1500][100];
int total;
int get(char s[])
{
int i;
for(i=1;i<=total;i++)
if(!strcmp(p[i],s))
return i;
return 0;
}
char*strrev(char ch[])
{
int l,i;
char t[mx];
l=strlen(ch);
l--;
for(i=0;i<=l;i++)
t[i]=ch[l-i];
t[i]='\0';
return t;
}
int main()
{
char temp[mx],substr[mx];
int i,j,l,c,n,f;
freopen("sujon.txt","r",stdin);
freopen("mine.txt","w",stdout);
int line=1;
while(gets(temp))
{
n=strlen(temp);
total=0;
for(l=1;l<=n;l++)///////////// length of substring
{
for(j=0;j<n-l+1;j++)
{
c=0;
for(i=j;i<=j+l-1;i++)
substr[c++]=temp[i];
substr[c]='\0';
if(!strcmp(substr,strrev(substr)))
{
f=get(substr);
if(!f)
strcpy(p[++total],substr);
}
}
}
//printf("The string '%s' contains %ld palindromes.\n",temp,total);
printf("The string '%s' contains %d palindromes.\n",temp,total);
//cout<<total<<"\n";
}
return 0;
}
-
- New poster
- Posts: 4
- Joined: Sat Apr 16, 2011 10:04 pm
- Location: IIT, DU
- Contact:
353 Pesky Palindromes
Getting a runtime error submitting the code below. I have no idea what is happening. Need help.
Code: Select all
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
int TRUE = 1;
int FALSE = 0;
char palindromeCollection[500][80];
int count = 0;
int prevoiuslyExists(char ch[])
{
for (int i = 0; i<=count; i++)
{
if (strcmp(ch,palindromeCollection[i]) == 0)
{
return TRUE;
}
}
strcpy(palindromeCollection[count], ch);
count++;
return FALSE;
}
int isPalind(char str[])
{
int len;
char str1[25];
len=strlen(str);
int i,j;
for (i=0,j=len-1; i<len; i++,j--)
str1[i]=str[j];
str1[i] = NULL;
if (strcmp(str, str1) == 0)
{
if (prevoiuslyExists(str) == TRUE)
{
return FALSE;
}
else
{
return TRUE;
}
}
else
{
return FALSE;
}
}
int main()
{
char input[80];
while (gets(input)!=NULL)
{
int palindCount = 0;
for (int i = 0; i<500; i++)
{
palindromeCollection[i][0] = NULL;
}
int length = strlen(input);
for (int i = 0; i<length; i++)
{
for (int j = i; j<length; j++)
{
char temp[80];
int a = 0;
for (int k = i; k<=j; k++, a++)
{
temp[a] = input[k];
}
temp[a] = NULL;
if (isPalind(temp) == TRUE)
{
palindCount++;
}
}
}
printf("The string '%s' contains %d palindromes.\n", input, palindCount);
count = 0;
}
return 0;
}
You are the one, who can make a difference...
Re: 353 Pesky Palindromes
I see several things that could led to an RE
Make the collection word size 81 as you need to give space for the NULl character as well.
In main function
Hope this helps
Make the collection word size 81 as you need to give space for the NULl character as well.
Code: Select all
char palindromeCollection[500][81]
Code: Select all
int isPalind(char str[])
{
int len;
char str1[81]; // Change this to 81 as well.
len=strlen(str);
int i,j;
for (i=0,j=len-1; i<j; i++,j--) // You don't need to loop through the whole length
str1[i]=str[j];
str1[len] = NULL; // Since you already know the length
if (strcmp(str, str1) == 0)
{
if (prevoiuslyExists(str) == TRUE)
{
return FALSE;
}
else
{
return TRUE;
}
}
else
{
return FALSE;
}
}
Code: Select all
char input[81]; // change this to 81 as well
..
..
char temp[81]; // change this to 81
hmm..
-
- Learning poster
- Posts: 96
- Joined: Tue Jul 19, 2011 12:19 pm
- Location: Dhaka, Bangladesh
- Contact:
353 --- Pesky Palindromes---WA
Please help me. I am getting W A......continuously
here is my code:
here is my code:
Code: Select all
#include <cstdio>
#include <cstring>
#include <cctype>
int palin(char s[100])
{
char *rn;
int ln=strlen(s),k=0,p=0,lm;
lm=ln/2;
for(int i=0 ; i<lm ; i++)
{
for(int j=0 ; j<lm-i ; j++)
{
if(s[j]==s[(lm-1-i)-j])
k=1;
else
{
k=0;
break;
}
}
if(k==1)
{
p++;
k=0;
}
}
p--;
for(int i=1 ; i<lm ; i++)
{
for(int j=i ; j<lm ; j++)
{
if(s[j]==s[(lm-1)-j])
k=1;
else
{
k=0;
break;
}
}
if(k==1)
{
p++;
k=0;
}
}
return p;
}
int main()
{
char in[100];
//while(scanf("%s",in)!=EOF)
while(gets(in))
{
char ch[100];
int pn=0, len=strlen(in), a;
if(len==1)
printf("The string '%s' contains 1 palindromes.\n",in);
else {
pn=palin(in);
for(int i=0 ; i<len ; i++)
{
ch[i]=in[i];
}
for(int i=0 ; i<len ; i++)
{
if(ch[i]!='\\' && isalpha(ch[i]))
{
char tmp=ch[i];
for(int j=0 ; j<len ; j++)
{
if(tmp==ch[j] && ch[j]!='\\')
{
pn++;
ch[j]='\\';
}
}
}
}
printf("The string '%s' contains %d palindromes.\n",in,pn);
}
}
return 0;
}
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 353 --- Nice Tests...
Try using scanf instead of gets.
Check input and AC output for thousands of problems on uDebug!
-
- Learning poster
- Posts: 96
- Joined: Tue Jul 19, 2011 12:19 pm
- Location: Dhaka, Bangladesh
- Contact:
Re: 353 --- Nice Tests...
Boss
Still W A............
Still W A............
Code: Select all
#include <cstdio>
#include <cstring>
#include <cctype>
int palin(char s[100])
{
char *rn;
int ln=strlen(s),k=0,p=0,lm;
lm=ln/2;
for(int i=0 ; i<lm ; i++)
{
for(int j=0 ; j<lm-i ; j++)
{
if(s[j]==s[(lm-1-i)-j])
k=1;
else
{
k=0;
break;
}
}
if(k==1)
{
p++;
k=0;
}
}
p--;
for(int i=1 ; i<lm ; i++)
{
for(int j=i ; j<lm ; j++)
{
if(s[j]==s[(lm-1)-j])
k=1;
else
{
k=0;
break;
}
}
if(k==1)
{
p++;
k=0;
}
}
return p;
}
int main()
{
char in[100];
while(scanf("%s",in)!=EOF)
{
char ch[100];
int pn=0, len=strlen(in), a;
if(len==1)
printf("The string '%s' contains 1 palindromes.\n",in);
else {
pn=palin(in);
for(int i=0 ; i<len ; i++)
{
ch[i]=in[i];
}
for(int i=0 ; i<len ; i++)
{
if(ch[i]!='\\' && isalpha(ch[i]))
{
char tmp=ch[i];
for(int j=0 ; j<len ; j++)
{
if(tmp==ch[j] && ch[j]!='\\')
{
pn++;
ch[j]='\\';
}
}
}
}
printf("The string '%s' contains %d palindromes.\n",in,pn);
}
}
return 0;
}
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 353 --- Nice Tests...
For input 1a1 my AC code returns 3 palindromes.
Check input and AC output for thousands of problems on uDebug!
-
- Learning poster
- Posts: 96
- Joined: Tue Jul 19, 2011 12:19 pm
- Location: Dhaka, Bangladesh
- Contact:
Re: 353 --- Nice Tests...
Boss,
Still W A............plz help
Still W A............plz help
Code: Select all
#include <cstdio>
#include <cstring>
#include <cctype>
int palin(char s[100])
{
char *rn;
int ln=strlen(s),k=0,p=0,lm;
lm=ln/2;
for(int i=0 ; i<lm ; i++)
{
for(int j=0 ; j<lm-i ; j++)
{
if(s[j]==s[(lm-1-i)-j])
k=1;
else
{
k=0;
break;
}
}
if(k==1)
{
p++;
k=0;
}
}
p--;
for(int i=1 ; i<lm ; i++)
{
for(int j=i ; j<lm ; j++)
{
if(s[j]==s[(lm-1)-j])
k=1;
else
{
k=0;
break;
}
}
if(k==1)
{
p++;
k=0;
}
}
return p;
}
int main()
{
char in[100];
while(scanf("%s",in)!=EOF)
{
char ch[100];
int pn=0, len=strlen(in), a;
if(len==1)
printf("The string '%s' contains 1 palindromes.\n",in);
else {
pn=palin(in);
for(int i=0 ; i<len ; i++)
{
ch[i]=in[i];
}
for(int i=0 ; i<len ; i++)
{
if(ch[i]!='\\' && ( isalpha(ch[i]) || isalnum(ch[i]) ) )
{
char tmp=ch[i];
for(int j=0 ; j<len ; j++)
{
if(tmp==ch[j] && ch[j]!='\\')
{
pn++;
ch[j]='\\';
}
}
}
}
printf("The string '%s' contains %d palindromes.\n",in,pn);
}
}
return 0;
}
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 353 --- Nice Tests...
For input [)[ my AC code returns 3 palindromes.
Check input and AC output for thousands of problems on uDebug!
-
- Learning poster
- Posts: 96
- Joined: Tue Jul 19, 2011 12:19 pm
- Location: Dhaka, Bangladesh
- Contact:
Re: 353 --- Nice Tests...
Guru
I can not understand why I am getting WA? Please, something.....................
I can not understand why I am getting WA? Please, something.....................
Code: Select all
#include <cstdio>
#include <iostream>
#include <cstring>
#include <cctype>
using namespace std;
int palin(char s[100])
{
char *rn;
int ln=strlen(s),k=0,p=0,lm;
lm=ln/2;
for(int i=0 ; i<lm ; i++)
{
for(int j=0 ; j<lm-i ; j++)
{
if(s[j]==s[(lm-1-i)-j])
k=1;
else
{
k=0;
break;
}
}
if(k==1)
{
p++;
k=0;
}
}
p--;
for(int i=1 ; i<lm ; i++)
{
for(int j=i ; j<lm ; j++)
{
if(s[j]==s[(lm-1)-j] )
k=1;
else
{
k=0;
break;
}
}
if(k==1)
{
p++;
k=0;
}
}
return p;
}
int main()
{
char in[100];
//freopen("in-353.txt","r",stdin);
//freopen("innn-353.txt","w",stdout);
while(cin>>in)
{
char ch[100];
int pn=0, len=strlen(in), a;
if(len==1){
printf ("The string '%s' contains 1 palindromes.\n", in);
}
else {
pn=palin(in);
for(int i=0 ; i<len ; i++)
{
ch[i]=in[i];
}
for(int i=0 ; i<len ; i++)
{
if(ch[i]!=' ' )
{
char tmp=ch[i];
for(int j=0 ; j<len ; j++)
{
if(tmp==ch[j] && ch[j]!=' ')
{
pn++;
ch[j]=' ';
}
}
}
}
printf ("The string '%s' contains %d palindromes.\n", in, pn);
}
}
return 0;
}
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 353 --- Nice Tests...
abcab 3 palindromes.
Check input and AC output for thousands of problems on uDebug!
Re: 353 --- Nice Tests...
AC ..
Last edited by Yousuf on Thu Aug 02, 2012 9:03 am, edited 1 time in total.