## 11221 - Magic square palindromes.

Moderator: Board moderators

New poster
Posts: 38
Joined: Tue Jul 17, 2007 3:21 pm
Location: East West University

### ....:(

jan vai at first i actually misunderstood the problem. now i edited my code and consider your input-output. it gives me right, bt i am still getting WA!!!!!where is the bug?
Eagle er moto daana meley urbo

Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Contact:
What if y is not a perfect square? Just generate a case with 26 characters where the first 25 form a magic square palindrome. Hope it helps.
Ami ekhono shopno dekhi...
HomePage

New poster
Posts: 38
Joined: Tue Jul 17, 2007 3:21 pm
Location: East West University

### ...<

hmm if y is not perfect square then the output will be the second sample output. thanks jan vai
Eagle er moto daana meley urbo

naffi
New poster
Posts: 23
Joined: Wed Mar 19, 2008 12:25 pm
Contact:

### 11221 Magic Square Pallindrome. Wrong Answer

got AC.
Last edited by naffi on Mon Sep 08, 2008 5:02 pm, edited 2 times in total.

amr saqr
New poster
Posts: 29
Joined: Tue Mar 11, 2008 6:35 pm

### Re: 11221 - Magic square palindromes

Could anyone tell me why am i getting TLE please ???!!
here is my code, and thanx in advance

Code: Select all

``Code Removed After AC``
Last edited by amr saqr on Wed May 07, 2008 10:53 pm, edited 1 time in total.
C++ Is The Best.

snail.123
New poster
Posts: 14
Joined: Wed Jun 13, 2007 3:29 am
Location: Taiwan

### Re: 11221 - Magic square palindromes

Amr Saqr,

I don't know why you got TLE, but your code got AC with 0.040s after the following block is deleted.

Code: Select all

``````             if (flag)
{
check="";
for (int s=input.size()-1;s>=input.size()-root;s--)
for (int t=s;t>=0;t-=root)
check+=input[t];
if (input!=check)
flag=false;
}
``````

amr saqr
New poster
Posts: 29
Joined: Tue Mar 11, 2008 6:35 pm

### Re: 11221 - Magic square palindromes

Thanx so much snail,
I've already found my mistake, it was in these lines of code

Code: Select all

``````for (int s=input.size()-1;s>=input.size()-root;s--)
for (int t=s;t>=0;t-=root)
``````
After a long time of debugging , i figured out that input.size() method returns unsigned integral type so when i assign this value to signed integer (s) it's converted to unsigned, the same mistake happens also when i assign the value of (s) to (t), so (t) never reaches a negative value which makes it break the loop,
it keeps going to its UINT_MAX and it falls in an infinite loop in some certain buggy test cases,
and thanx again.
C++ Is The Best.

snail.123
New poster
Posts: 14
Joined: Wed Jun 13, 2007 3:29 am
Location: Taiwan

### Re: 11221 - Magic square palindromes

I was suspecting the same thing, but the problem states that 0<length(L)<10000, which should not be big enough to make s into a negative value. I am still confused.

amr saqr
New poster
Posts: 29
Joined: Tue Mar 11, 2008 6:35 pm

### Re: 11221 - Magic square palindromes

Me too ,
anyway, as long as we're gettin Accepted, nothing else matters
C++ Is The Best.

empo
New poster
Posts: 19
Joined: Mon Jul 28, 2008 7:00 pm
Location: India

### Re: 11221 - Magic square palindromes

At last i got accepted..
Just putting all char in a squar dimension array..if possible then checking this 2d array ...should be mirror from both diagonals....
"Accepted" is my passion but RTE is my weakness.....

codemaster
New poster
Posts: 2
Joined: Fri Jun 12, 2009 4:14 am

### Re: 11221 - Magic square palindromes

getting wa...........
#include <stdio.h>
#include <string.h>
#include <math.h>
char in[10000];
char help[10000];
char check[10000][10000];
int main()
{
int ts;
scanf("%d",&ts);
int cs=0;
while(ts)
{
cs++;
int flag=1;
ts--;
fflush(stdin);
gets(in);

int lenin=strlen(in);
int lenhelp=0;
for(int i=0;i<lenin;i++)
{
if(in>='a' && in<='z')
help[lenhelp++]=in;

}
// printf("%d\n",lenhelp);
double ck=sqrt(lenhelp);
int chk=ck;
if(ck-chk>0)
flag=0;
if(flag)
for(int i=0,k=0;i<chk;i++)
{
for(int j=0;j<chk;j++)
{
check[j]=help[k++];
}
}

if(flag)
for(int i=0,k=0;i<chk;i++)
for(int j=0;j<chk;j++)
if(check[j]!=help[k++])
{
flag=0;
break;
}
if(flag)
for(int i=chk-1,k=0;i>=0;i--)
for(int j=chk-1;j>=0;j--)
if(check[j]!=help[k++])
{
flag=0;
break;
}
if(flag)
for(int i=chk-1,k=0;i>=0;i--)
for(int j=chk-1;j>=0;j--)
if(check[j]!=help[k++])
{
flag=0;
break;
}

if(flag)
printf("Case #%d:\n%d\n",cs,chk);
else
printf("Case #%d:\nNo magic \n",cs);

}
return 0;

}

codemaster
New poster
Posts: 2
Joined: Fri Jun 12, 2009 4:14 am

### Re: 11221 - Magic square palindromes

someone pls help!!!

rahian
New poster
Posts: 5
Joined: Wed Jul 30, 2008 3:27 am
Location: CUET
Contact:

### Re: 11221 - Magic square palindromes

U have to check whether it is palindromes or not...

kashius
New poster
Posts: 1
Joined: Tue Jan 03, 2012 1:03 am

### Re: 11221 - Magic square palindromes

help why WA ?

Code: Select all

``````#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#define TAM 10002

char word[TAM], str[TAM], A[TAM], B[TAM], C[TAM], D[TAM];
char square[TAM][TAM];

bool palindromo(int len){
for(int i = 0, j = len-1 ; i < len && j>=0 ; ++i, j--){
if( A[i]!=A[j] || B[i]!=B[j] || C[i]!=C[j] || D[i]!=D[j]
|| A[i]!=B[i] || A[i]!=C[i] || A[i]!=D[i])
return false;
}
return true;
}

int main(){
int i, ii, t, k, len_str;
bool flag ;
scanf("%d", &t);
for(int tt = 1; tt <= t; tt++){
fflush(stdin);
gets(word);
i = 0, len_str = 0;
for(; i < strlen(word); ++i){
if(word[i] >= 'a' && word[i] <= 'z'){
str[len_str] = word[i];
len_str++;
}
}
printf("Case #%d:\n", tt);
flag = true;
k = (int)sqrt((double)len_str);
if(  k*k  != len_str  ){
printf("No magic :(\n");
}
else{
for(int i = 0 ; i < len_str; ++i){
square[i/k][i%k] = str[i];
}

int pos = 0;
for(int i = 0; i < k; ++i){
for(int j = 0; j < k ; ++j){
A[pos] = square[i][j];
pos++;
}
}

pos = 0;
for(int i = 0; i < k; ++i){
for(int j = 0; j < k ; ++j){
B[pos] = square[j][i];
pos++;
}
}

pos = 0;
for(int i = k-1; i >= 0; --i){
for(int j = k-1; j >= 0 ; --j){
C[pos] = square[i][j];
pos++;
}
}

pos = 0;
for(int i = k-1; i >= 0; --i){
for(int j = k-1; j >= 0 ; --j){
D[pos] = square[j][i];
pos++;
}
}
palindromo(len_str) == true ?  printf("%d\n", k) : printf("No magic :(\n");

}
}
return 0;
}
``````

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

### Re: 11221 - Magic square palindromes

The way you're reading in the input won't work. After the scanf of T, the first call to gets() is going to return an empty string. Try reading or redirecting your input from a file.
Check input and AC output for thousands of problems on uDebug!