353 - Pesky Palindromes

All about problems in Volume 3. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 353 --- Nice Tests...

Post by brianfry713 » Wed Aug 01, 2012 3:25 am

My AC code reads a string at a time, not line by line. The input "boy ", with a trailing space (not including the quotes), would be converted to "boy" with no trailing space in my code.
Check input and AC output for thousands of problems on uDebug!

Yousuf
New poster
Posts: 13
Joined: Thu Jun 09, 2011 8:22 am

Re: 353 --- Nice Tests...

Post by Yousuf » Wed Aug 01, 2012 11:07 am

Code: Select all


AC.. :D 

Last edited by Yousuf on Thu Aug 02, 2012 9:05 am, edited 1 time in total.

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 353 --- Nice Tests...

Post by brianfry713 » Wed Aug 01, 2012 9:52 pm

The string 'gzdvljcpxpepn' contains 13 palindromes.
Check input and AC output for thousands of problems on uDebug!

Yousuf
New poster
Posts: 13
Joined: Thu Jun 09, 2011 8:22 am

Re: 353 --- Nice Tests...

Post by Yousuf » Thu Aug 02, 2012 9:07 am

Thanks " brianfry713" I get AC now. Thanks for help.

mirak
New poster
Posts: 1
Joined: Sun Feb 03, 2013 8:26 am

Re: 353 --- Nice Tests...

Post by mirak » Thu Feb 07, 2013 11:57 am

Code: Select all

import java.io.*;
import java.util.Formatter;
import java.util.Scanner;

public class Training {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		String output = "";
		while (input.hasNext("0")==false) {
			String s = input.next();
			int num = 0;
			char[] a = new char[s.length()];
			for (int i = 0; i < a.length; i++) {
				a[i] = s.charAt(i);
			}
			char[] b = new char[a.length];
			int k = 0;
			for (int i = 0; i < a.length; i++) {
				if (i == 0) {
					b[k] = a[i];
					k++;
				} else {
					boolean isTrue = numC(b, k, a[i]);
					if (isTrue == true) {
						b[k] = a[i];
						k++;
					}
				}
			}
			for (int i = 0; i <= s.length(); i++) {
				for (int j = i + 1; j <= s.length(); j++) {
					String u = s.substring(i, j);
					if (u.length() != 1) {
						if (isPalindrome(u, 0, u.length() - 1) == true) {
							num++;
						}
					}
				}
			}
			output+="The string "+"'"+s+"' contains "+(num+k)+" palindromes.\n";
		}
		output=output.trim();
		System.out.println(output);
	}

	public static boolean isPalindrome(String s, int i, int j) {
		if (i == j || i > j) {
			return true;
		} else {
			if (s.charAt(i) == s.charAt(j)) {
				return isPalindrome(s, i + 1, j - 1);
			} else {
				return false;
			}
		}
	}

	public static boolean numC(char[] ar, int j, char a) {
		int num = 0;
		for (int i = 0; i < j; i++) {
			if (ar[i] == a) {
				num++;
			}
		}
		if (num > 0) {
			return false;
		} else {
			return true;
		}
	}

}
i don't know why am gettin' WA here .. if someone can help ...

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 353 --- Nice Tests...

Post by brianfry713 » Thu Feb 07, 2013 11:05 pm

Check input and AC output for thousands of problems on uDebug!

alimbubt
New poster
Posts: 39
Joined: Tue Aug 07, 2012 10:40 pm
Location: BUBT,Dhaka, Bangladesh
Contact:

Re: 353 --- Nice Tests...

Post by alimbubt » Wed Mar 27, 2013 3:52 pm

Input:

Code: Select all

alim
bubt
illusion
acmicpc
worldfinal
mirpur
abdulalim
alimbubt
cseuuu
aaaaaaa
bbbbbbb
aaaaaaaaaammmmmmmmmmmmmmmaaaaaaaaaa
sssssssguggudfgighi
djsfdsgfjggjsdfjgdsjkg
iuwetrewuuuuuiweughsalfujgkf
iutwrigmdlbbckbj
newkeyboard
dffdg
Output:

Code: Select all

The string 'alim' contains 4 palindromes.
The string 'bubt' contains 4 palindromes.
The string 'illusion' contains 7 palindromes.
The string 'acmicpc' contains 6 palindromes.
The string 'worldfinal' contains 9 palindromes.
The string 'mirpur' contains 5 palindromes.
The string 'abdulalim' contains 8 palindromes.
The string 'alimbubt' contains 8 palindromes.
The string 'cseuuu' contains 6 palindromes.
The string 'aaaaaaa' contains 7 palindromes.
The string 'bbbbbbb' contains 7 palindromes.
The string 'aaaaaaaaaammmmmmmmmmmmmmmaaaaaaaaaa' contains 35 palindromes.
The string 'sssssssguggudfgighi' contains 17 palindromes.
The string 'djsfdsgfjggjsdfjgdsjkg' contains 8 palindromes.
The string 'iuwetrewuuuuuiweughsalfujgkf' contains 18 palindromes.
The string 'iutwrigmdlbbckbj' contains 14 palindromes.
The string 'newkeyboard' contains 10 palindromes.
The string 'dffdg' contains 5 palindromes.
Give me six hours to chop down a tree and I will spend the first four sharpening the axe...(BUBT ILLUSION)
http://uhunt.felix-halim.net/id/155497
http://onlyprogramming.wordpress.com/

farhan36
New poster
Posts: 1
Joined: Wed Jun 12, 2013 3:54 pm

Re: 353 --- Nice Tests...

Post by farhan36 » Wed Jun 12, 2013 4:02 pm

i am getting WA for this problem. please help.

Code: Select all

#include<iostream>
#include<cstdio>
#include<new>
#include<cstring>
#include<string>
#include<cstdlib>
#include<iomanip>
#include<fstream>
#include<algorithm>
#include<functional>
#include<vector>
#include<list>
#include<deque>

using namespace std;

int main()
{
    string s;
    int count=0;
    bool flag = false,matched = false;
    while(getline(cin,s))
    {

        char ch[26]="\0";
        count =0;
        flag = false;
        matched = false;
        for(int i=0;i<s.length();i++)
        {
            if(count>0)
            {
                for(int j=0;j<count;j++)
                {
                    if(s[i]==ch[j])
                    {
                        flag = true;
                        break;
                    }
                }
                if(!flag) ch[count++] = s[i];
                flag = false;
            }

            else ch[count++] =s[i];
        }

        for(int i=0;i<s.length()-1;i++)
        {
            for(int j=s.length()-1;j>=i+1;j--)
            {
                int k=i;
                int l =j;
                int c=i;
                while(c<=(k+l)/2)
                {
                    if(s[k]!=s[l])
                    {
                        matched = true;
                        break;
                    }
                    k++;
                    l--;
                    c++;

                }
                if(!matched)
                {
                    count++;

                }
                matched = false;
            }
        }

        cout << "The string '" << s << "' contains " << count <<" palindromes.\n";
    }

}

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 353 --- Nice Tests...

Post by brianfry713 » Thu Jun 13, 2013 12:48 am

Try the I/O in the post before yours.
Check input and AC output for thousands of problems on uDebug!

User avatar
uDebug
A great helper
Posts: 475
Joined: Tue Jul 24, 2012 4:23 pm

Re: 353 --- Nice Tests...

Post by uDebug » Wed Dec 18, 2013 7:15 am

My code passed all the test cases mentioned in this thread but I was still get a WA. So, I thought of this some more and came up with the following input - on which my code didn't match the expected output. This way, I was able to troubleshoot my issue and get a AC. I'm sharing the test cases which helped me identify the bug in my code and the AC output.

Input:

Code: Select all

zzcatzztaczz
zzcatzzcatzzcat
AC Output:

Code: Select all

The string 'zzcatzztaczz' contains 10 palindromes.
The string 'zzcatzzcatzzcat' contains 5 palindromes.
Also note that since the problem clearly states "For each non-empty input line..." it's clear that there are no blank lines that need to be worried about as part of the input.
Check input and AC output for over 7,500 problems on uDebug!

Find us on Facebook. Follow us on Twitter.

solve14
New poster
Posts: 2
Joined: Sun Mar 09, 2014 6:41 pm

353 Runtime Error, what to do? Also need suggestion

Post by solve14 » Sun Mar 09, 2014 6:55 pm

Getting Runtime Error, don't have a clue, help is appreciated
Also suggestion on how to improve the all substring generator code part is appreciated.

Code: Select all

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
 class Palindrome {

   public int checkPalindrome(String str)
   {
       String newStr="";
       
       for(int i=str.length()-1;i>=0;i--)
       {
          newStr+=str.charAt(i);
       }
       
       if(str.equals(newStr))
       {
           return 1;
       }
       else
       {
           return 0;
       }
   }

    public static void main(String[] args)throws IOException {
        
      
       InputStreamReader isr = new InputStreamReader(System.in);
       BufferedReader br = new BufferedReader(isr);
       ArrayList<String>al=new ArrayList<String>(); 
       String sub,usrStr;
       int palindrome=0;
       Palindrome p=new Palindrome();
       
       while((usrStr=br.readLine())!=null)
       {
 //substring generator
           for(int i=0;i<usrStr.length();i++)
           {
               for(int j=1;j<=usrStr.length()-i;j++)
               {
                    sub = usrStr.substring(i, i+j);
                     al.add(sub);
                    
               }  
           }
           
           HashSet hs = new HashSet();
           hs.addAll(al);                            //will add the unique substrings
           al.clear();
           al.addAll(hs);
           
           for(int m=0;m<al.size();m++)
           {
               String checkStr=al.get(m);
                if(p.checkPalindrome(checkStr)==1)
                     {
                           palindrome++;
                     }
           }
           
           System.out.println("The string '"+usrStr+"' contains "+palindrome+" palindromes.");
           palindrome=0;
           al.clear();
       }

    }
}

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 353 Runtime Error, what to do? Also need suggestion

Post by brianfry713 » Mon Mar 10, 2014 8:06 pm

Use class Main
Check input and AC output for thousands of problems on uDebug!

sampad74
New poster
Posts: 29
Joined: Wed Jun 18, 2014 3:57 pm
Location: Bangladesh

Re: 353 - Pesky Palindromes

Post by sampad74 » Mon Jan 26, 2015 2:38 am

i got Wa.in my code i noticed that this if condition is not working properly.bt i don't know why??there may be some other mistakes.please help me..here is my code.

Code: Select all

#include<string.h>
#include<stdio.h>
char a[90];
int t;
int is(void)
{
    int i,g=0;
    for(i=0;i<(t/2);i++)
    {
        if(a[i]==a[t-i-1])
        {
            g++;
        }
        else break;
    }
    if(g==(t/2)) return 1;
    else return 0;
}
int main(void)
{
char s[180][90];
    while(gets(a))
    {

    t=strlen(a);
    //printf("t=%d\n",t);
    int i,j,k,c=0,l;
    if(is()==1)
    {
      //  printf("pelindrome\n");
        c=t/2;
    }
    else
    {
        int m1=0,n1;
    for(int i=0;i<t-1;i++)
    {
        for(j=1;j<t;j++)
        {
            int f=6;
                if(a[i]==a[j])
                {
                    n1=0;
                    s[m1][n1]=a[i];
                    f=0;
                    l=j-1;
                    for(k=i+1;k!=j;k++,l--)
                    {

                        if(a[k]!=a[l])
                        {
                            f=1;
                            //m1--;
                            break;
                        }
                        else
                        {
                            n1++;
                            s[m1][n1]=a[k];
                        }
                    }
                }
                if(f==0)
                {
                    int il;
                    for(il=0;il<m1;il++)
                    {
                        //printf("peli %s\n",s[m1]);
                        if(!strcmp(s[m1],s[il]))
                        {
                            //not working !!!!

                            //printf("m1 %d c %d il %d\n",m1,c,il);
                            f=1;
                        }
                    }
                    if(f==0)
                    {
                        m1++;
                        c++;
                    }
                }
        }
    }
    }
   //printf("uniq pelin %d\n",c);
    //if(t>0) c++;
    int v;
    for(i=0;i<t;i++)
    {
        v=1;
        for(j=i-1;j>=0;j--)
        {
            if(a[i]==a[j])
            {
                v=0;
                break;
            }
        }
        if(v==1) c++;
    }
    printf("The string '%s' contains %d palindromes.\n",a,c);

    }
    return 0;
}


brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 353 - Pesky Palindromes

Post by brianfry713 » Tue Jan 27, 2015 3:11 am

Try using scanf("%s") instead of gets.
The string 'aaa' contains 3 palindromes.
Check input and AC output for thousands of problems on uDebug!

Post Reply

Return to “Volume 3 (300-399)”