I am trying to solve a problem of HackerRank. The problem wants me to find out if a string can be changed into a palindrome string or not with a certain number of changing characters of that string. That certain number will be given by input. I am giving the link of full description of the problem below.
In my code, I first checked if the string is already palindrome or not. Then I checked some cases under the first checking for the given number of changes to find out if that string can be changed into palindrome after changing the characters.
But I am getting different output for my code. Output that I found from code blocks is not the same as the output which I found in HackerRank.
Here is the link of the full problem statement
my code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void print(char str[],int n)
{
int i;
for(i=0;i<n;i++)
{
printf("%c",str[i]);
}
}
void cpy(char qtr[],char str[],int n)
{
int i;
for(i=0;i<n;i++)
{
qtr[i]=str[i];
}
}
void rev(char str[],int n)
{
int i;
char qtr[100000];
for(i=0;i<n;i++)
{
qtr[i]=str[n-1-i];
}
for(i=0;i<n;i++)
{
str[i]=qtr[i];
}
}
int main()
{
char str[100],str2[100000],qtr[100000],c;
int n,k,i;
scanf("%d",&n);
scanf("%d",&k);
fflush(stdin);
gets(str);
cpy(qtr,str,n);
rev(str,n);
cpy(str2,str,n);
int same=0;
if(strcmp(str2,qtr)==0)
{
same=1;
}
if(k==n)
{
c=qtr[i]+1;
for(i=0;i<k;i++)
{
if(qtr[i]==c)
{
c=c+1;
i=-1;
continue;
}
}
for(i=0;i<k;i++){qtr[i]=c;}
print(qtr,n);
return 0;
}
if(same==1)
{
if(n%2!=0)
{
if(qtr[n/2]!='9'){qtr[n/2]='9';}
else
{qtr[n/2]='1';}
k--;
for(i=0;i<n;i++)
{
if(k<0){printf("-1");break;}
if(k==0){printf(qtr,n);break;}
if(qtr[i]=='9')
{
qtr[i]='1';
qtr[n-1-i]='1';
k=k-2;
}
else
{
qtr[i]='9';
qtr[n-1-i]='9';
k=k-2;
}
}
}
else
{
for(i=0;i<n;i++)
{
if(k<0){printf("-1");break;}
if(k==0){print(qtr,n);break;}
if(qtr[i]=='9')
{
qtr[i]='1';
qtr[n-1-i]='1';
k=k-2;
}
else
{
qtr[i]='9';
qtr[n-1-i]='9';
k=k-2;
}
}
}
}
else
{
if(n%2==0)
{
int arr[n];
for(i=0;i<n;i++){arr[i]=0;}
while(k>=0)
{
int g=0;
for(i=0;i<n/2;i++)
{
if(qtr[i]!=qtr[n-i-1])
{
arr[i]=1;
arr[n-i-1]=1;
qtr[i]=qtr[n-i-1];
k--;
g=1;
}
}
if(k==0){print(qtr,n);return 0;}
if(k%2!=0)
{
for(i=0;i<n/2;i++)
{
if(arr[i]==1)
{
arr[i]=2;
qtr[i]=qtr[i]+1;
qtr[n-i-1]= qtr[i];
k--;g=1;
break;
}
}
}
if(k==0){print(qtr,n);return 0;}
if(k%2==0)
{
for(i=0;i<n/2;i++)
{
if(arr[i]==0)
{
arr[i]=3;
qtr[i]=qtr[i]+1;
qtr[n-i-1]= qtr[i];
k=k-2;g=1;
break;
}
}
}
if(k==0){print(qtr,n);return 0;}
if(g==1){printf("-1");return 0;}
}
printf("-1");return 0;
}
else
{
int arr[n];
for(i=0;i<n;i++){arr[i]=0;}
while(k>=0)
{
int g=0;
for(i=0;i<n/2;i++)
{
if(qtr[i]!=qtr[n-i-1])
{
arr[i]=1;
arr[n-i-1]=1;
qtr[i]=qtr[n-i-1];
k--;
g=1;
}
}
if(k==1){qtr[n/2]=qtr[n/2]+1;k--;print(qtr,n);return 0;}
if(k==0){print(qtr,n);return 0;}
if(k%2!=0)
{
for(i=0;i<n/2;i++)
{
if(arr[i]==1)
{
arr[i]=2;
qtr[i]=qtr[i]+1;
qtr[n-i-1]= qtr[i];
k--;g=1;
break;
}
}
}
if(k==1){qtr[n/2]=qtr[n/2]+1;k--;print(qtr,n);return 0;}
if(k==0){print(qtr,n);return 0;}
if(k%2==0)
{
for(i=0;i<n/2;i++)
{
if(arr[i]==0)
{
arr[i]=3;
qtr[i]=qtr[i]+1;
qtr[n-i-1]= qtr[i];
k=k-2;g=1;
break;
}
}
}
if(k==1){qtr[n/2]=qtr[n/2]+1;k--;print(qtr,n);return 0;}
if(k==0){print(qtr,n);return 0;}
if(g==1){printf("-1");return 0;}
}
printf("-1");return 0;
}
}
}
I was practising some data structures problems that I did previously but this time I don't know what is going wrong in my code. I looked over a long time but I did not found the mistake. When I'm printing I'm just getting the first character and it looks like e is not being updated. But I've written e++.
#include<stdio.h>
#include "ctype.h"
int stack[20];
int top = -1;
void push(int x)
{
stack[++top] = x;
}
int pop()
{
return stack[top--];
}
int priorityof(char x)
{
if(x=='(')
return 3;
else if(x=='+'|| x=='-')
return 1;
else if(x=='*'|| x=='/')
return 2;
}
int main()
{
char exp[20];
char *e;
e=exp;char x;
scanf("%c",exp);
while(*e!='\0')
{
if(isalnum(*e))
{
printf("%c", *e);
}
else if(*e=='(')
{
push(*e);
}
else if(*e==')')
{
while((x=pop())!='(')
printf("%c",x);
}
else {
while (priorityof(stack[top]) >= priorityof(*e)) {
printf("%c", pop());
push(*e);
}
}
e++;
}
while(top!=-1)
{
printf("%c",pop());
}
}
%c is for single character and reading your question it seems like you are giving more than one character so its a string, use %s.
#include<stdio.h>
#include "ctype.h"
int stack[20]; int top = -1;
void push(int x) {
stack[++top] = x;
}
int pop() { return stack[top--]; }
int priorityof(char x) {
if(x=='(') return 3;
else if(x=='+'|| x=='-') return 1;
else if(x=='*'|| x=='/') return 2;
}
int main() {
char exp[20];
char *e;
e=exp;char x;
scanf("%s",exp);
while(*e!='\0') { if(isalnum(*e)) { printf("%c", *e); } else if(*e=='(') { push(*e); } else if(*e==')') { while((x=pop())!='(') printf("%c",x); } else { while (priorityof(stack[top]) >= priorityof(*e)) { printf("%c", pop()); push(*e); } } e++; } while(top!=-1) { printf("%c",pop()); } }
While solving the NEXT PALINDROME question on SPOJ. I have considered for every possible case and wrote the code. But it is showing WA(Wrong Answer). I am not getting my mistake or any possible case where my code won't work.
Below is my code.
Please help me out.
Thank You.
#include<stdio.h>
#include<string.h>
char K[10000000];
int a[10000000];
int main()
{
int t,length,i,count,j,rem,carry;
scanf("%d",&t);
while(t--)
{
scanf("%s",K);
length=strlen(K);
count=0;
for(i=0;i<length;i++)
{
a[i]=K[i]-'0';
if(a[i]==9)
count++;
}
if(length==2 && count!=length)
{
if(a[0]<=a[1])
{
a[1]=++a[0];
}
else
{
a[1]=a[0];
}
for(i=0;i<length;i++)
{
printf("%d",a[i]);
}
printf("\n");
continue;
}
if(count==length)
{
if(length==1)
{
printf("11\n");
continue;
}
memset(a,0,sizeof(a));
a[0]=10;
a[length-1]=1;
for(i=0;i<length;i++)
{
printf("%d",a[i]);
}
printf("\n");
continue;
}
i=0;
j=length-1;
count=0;
while(j-i!=1 && j-i!=0)
{
if(a[i]>a[j])
{
a[j--]=a[i++];
count++;
}
else
{
if(a[i]<a[j])
count--;
a[j--]=a[i++];
}
}
if(count==0)
{
rem=a[i]+1;
a[i]=rem%10;
a[j++]=a[i--];
carry=rem/10;
while(carry!=0)
{
rem=a[i]+1;
a[i]=rem%10;
a[j++]=a[i--];
carry=rem/10;
if(i<0&&j>length-1)
break;
}
}
else
{
if(a[i]<a[j] || count<0)
{
rem=a[i]+1;
a[i]=rem%10;
a[j++]=a[i--];
carry=rem/10;
while(carry!=0)
{
rem=a[i]+1;
a[i]=rem%10;
a[j++]=a[i--];
carry=rem/10;
if(i<0&&j>length-1)
break;
}
}
else
a[j]=a[i];
}
for(i=0;i<length;i++)
{
printf("%d",a[i]);
}
printf("\n");
}
return 0;
}
# include <stdio.h>
int check(int a,int b);
int check1(int a,int b,int c,int d);
void recursive(int x,int pos[82]);
void scaledown(int pos[82]);
int pos[82];
int q=1;
long c=0;
int ch[10]={0,1,2,3,4,5,6,7,8,9};
int ar[10][10]= {{0,0,0,0,0,0,0,0,0,0},
{0,8,6,0,0,2,0,0,0,0},
{0,0,0,0,7,0,0,0,5,9},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,6,0,8,0,0},
{0,0,4,0,0,0,0,0,0,0},
{0,0,0,5,3,0,0,0,0,7},
{0,0,0,0,0,0,0,0,0,0},
{0,0,2,0,0,0,0,6,0,0},
{0,0,0,7,5,0,9,0,0,0}};
int size;
void main()
{
int i,j,k=1,a;
int pos[82];
printf("WELCOME TO THE ULTIMATE SUDOKU SOLVER");
printf("\n\n\n");
for(i=1;i<=9;i++)
{
for(j=1;j<=9;j++)
{
if(ar[i][j]==0)
{
pos[k]=(10*i)+j;
k+=1;
}
printf("%d",ar[i][j]);
printf(" ");
}
printf("\n");
}
size=k-1;
printf("\n");
scaledown(pos);
k=1;
for(i=1;i<=9;i++)
{
for(j=1;j<=9;j++)
{
if(ar[i][j]==0)
{
pos[k]=(10*i)+j;
k+=1;
}
}
}
size=k-1;
recursive(q,pos);
for(i=1;i<=9;i++)
{
for(j=1;j<=9;j++)
{
printf("%d",ar[i][j]);
printf(" ");
}
printf("\n");
}
printf("%d",c);
}
void recursive(int x,int p[82])
{
c++;
printf("%d",c);
printf("\n");
ar[p[x]/10][p[x]%10]+=1;
if(ar[p[x]/10][p[x]%10]>9&&q<=size)
{
ar[p[x]/10][p[x]%10]=0;
q--;
recursive(q,p);
}
if(check(p[x]/10,p[x]%10)==1&&q<size)
{
q++;
recursive(q,p);
}
if(check(p[x]/10,p[x]%10)==0&&ar[p[x]/10][p[x]%10]<9&&q<=size)
{
recursive(q,p);
}
if(ar[p[x]/10][p[x]%10]==9&&check(p[x]/10,p[x]%10)==0&&q<=size)
{
ar[p[x]/10][p[x]%10]=0;
q--;
recursive(q,p);
}
if(q==size&&check(p[x]/10,p[x]%10)==1){}
}
int check1(int a,int b,int c,int d)
{
int i,j;
for(i=c;i<=(c+2);i++)
{
for(j=d;j<=(d+2);j++)
{
if(i==a&&j==b){}
else
{
if(ar[i][j]==ar[a][b])
{
return 0;
}
}
}
}
return 1;
}
int check(int a,int b)
{
int i,j;
for(i=1;i<=9;i++)
{
if(i!=b)
{
if(ar[a][i]==ar[a][b])
{
return 0;
}
}
if(i!=a)
{
if(ar[i][b]==ar[a][b])
{
return 0;
}
}
}
if(a<4&&b<4)
{
if(check1(a,b,1,1)==0)
{
return 0;
}
}
if(a<4&&b>3&&b<7)
{
if(check1(a,b,1,4)==0)
{
return 0;
}
}
if(a<4&&b>6)
{
if(check1(a,b,1,7)==0)
{
return 0;
}
}
if(a>3&&a<7&&b<4)
{
if(check1(a,b,4,1)==0)
{
return 0;
}
}
if(a>3&&a<7&&b>3&&b<7)
{
if(check1(a,b,4,4)==0)
{
return 0;
}
}
if(a>3&&a<7&&b>6)
{
if(check1(a,b,4,7)==0)
{
return 0;
}
}
if(a>6&&b<4)
{
if(check1(a,b,7,1)==0)
{
return 0;
}
}
if(a>6&&b>3&&b<7)
{
if(check1(a,b,7,4)==0)
{
return 0;
}
}
if(a>6&&b>6)
{
if(check1(a,b,7,7)==0)
{
return 0;
}
}
return 1;
}
void scaledown(int p[82])
{
int i,j,w,count=0;
for(i=1;i<=size;i++)
{
for(j=1;j<=9;j++)
{
ar[p[i]/10][p[i]%10]=ch[j];
if(check(p[i]/10,p[i]%10)==0)
{
ch[j]=0;
count+=1;
}
}
if(count==8)
{
for(w=1;w<=9;w++)
{
if(ch[w]!=0)
{
ar[p[i]/10][p[i]%10]=ch[w];
}
}
}
else
{
ar[p[i]/10][p[i]%10]=0;
}
for(w=1;w<=9;w++)
{
ch[w]=w;
}
count=0;
}
}
probably, max recursively call.
You most likely busted the stack. Just a couple unrelated notes: You need to make your code simpler by separating the work into individual functions. You may have been able to keep track of everything in this small program, but if you continue to write code you will have to think about code layout/design/usability.
To solve sudoku puzzles without overflowing the stack, I suggest looking into the solution using constraint solvers. Here's something to get you started.