All about problems in Volume III. If there is a thread about your problem, please use it. If not, create one with its number in the subject.
Moderator: Board moderators
by uvasarker » Thu Jun 07, 2012 8:55 pm
Boss,
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;
}
-
uvasarker
- Learning poster
-
- Posts: 96
- Joined: Tue Jul 19, 2011 12:19 pm
- Location: Dhaka, Bangladesh
by brianfry713 » Fri Jun 08, 2012 12:56 am
For input [)[ my AC code returns 3 palindromes.
-
brianfry713
- Guru
-
- Posts: 1742
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
by uvasarker » Wed Jul 18, 2012 11:08 am
Guru
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;
}
-
uvasarker
- Learning poster
-
- Posts: 96
- Joined: Tue Jul 19, 2011 12:19 pm
- Location: Dhaka, Bangladesh
by brianfry713 » Thu Jul 19, 2012 2:40 am
abcab 3 palindromes.
-
brianfry713
- Guru
-
- Posts: 1742
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
by Yousuf » Tue Jul 31, 2012 1:43 pm
AC ..
Last edited by
Yousuf on Thu Aug 02, 2012 9:03 am, edited 1 time in total.
-
Yousuf
- New poster
-
- Posts: 8
- Joined: Thu Jun 09, 2011 8:22 am
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.
-
brianfry713
- Guru
-
- Posts: 1742
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
by Yousuf » Wed Aug 01, 2012 11:07 am
Last edited by
Yousuf on Thu Aug 02, 2012 9:05 am, edited 1 time in total.
-
Yousuf
- New poster
-
- Posts: 8
- Joined: Thu Jun 09, 2011 8:22 am
by brianfry713 » Wed Aug 01, 2012 9:52 pm
The string 'gzdvljcpxpepn' contains 13 palindromes.
-
brianfry713
- Guru
-
- Posts: 1742
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
by Yousuf » Thu Aug 02, 2012 9:07 am
Thanks " brianfry713" I get AC now. Thanks for help.
-
Yousuf
- New poster
-
- Posts: 8
- Joined: Thu Jun 09, 2011 8:22 am
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 ...
-
mirak
- New poster
-
- Posts: 1
- Joined: Sun Feb 03, 2013 8:26 am
by brianfry713 » Thu Feb 07, 2013 11:05 pm
-
brianfry713
- Guru
-
- Posts: 1742
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
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.
-
alimbubt
- New poster
-
- Posts: 39
- Joined: Tue Aug 07, 2012 10:40 pm
- Location: BUBT,Dhaka, Bangladesh
-
Return to Volume III
Who is online
Users browsing this forum: No registered users and 1 guest