Code for ACODE on Spoj is not working - c

I have tried all possible test cases for this SPOJ question that I came across, but still my code is not getting accepted. Can't identify which test case it is failing.
I have considered the cases where zeroes can be inside the input. Also I have considered the cases of consecutive zeroes.
#include <stdio.h>
#include <string.h>
int main()
{
int n,i,ar[6010];
char str[6010];
unsigned long long int dp[6010];
while(1)
{
int flag=0;
scanf("%s",str);
if(str[0]=='0')
break;
for(i=0;str[i]!='\0';i++) //copy string to array
{
ar[i+1]=str[i]-'0';
}
n=i;
for(i=1;i<=n-1;i++) //checking for continous two zeroes
{
if(ar[i]==0&&ar[i+1]==0) flag=1;
if(ar[i]>2&&ar[i+1]==0)flag=1;
}
dp[1]=1;
if(ar[1]*10+ar[2]<=26&&ar[2]!=0)dp[2]=2;
else dp[2]=1;
if(ar[2]==0)dp[1]=0;
for(i=3;i<=n;i++)
{
if(ar[i]!=0)
{
dp[i]=dp[i-1];
if(ar[i-1]*10+ar[i]<=26)
{
dp[i]+=dp[i-2];
}
}
else
{
if(ar[i-2]*10+ar[i-1]<=26)
{
dp[i]=dp[i-2];
dp[i-1]=0;
}
else
{
dp[i]=dp[i-1];
dp[i-1]=0;
}
}
}
if(flag==0)
printf("%llu\n",dp[n]);
else
printf("0\n");
}
return 0;
}

Related

I'm trying to design a lexical Analyzer in C

My Code :
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int main()
{
int identifier_counter=0,constants_counter=0,operators_counter=0,delimeters_counter=0,i,j,x;
char expr[50],operators[50],identifiers[50],constants[50],delimeters[50];
printf("Enter the Expression: ");
scanf("%[^\n]s",expr);
for(i=0;i<strlen(expr);i++)
{
if(isspace(expr[i])){
continue;
}
else if(isalpha(expr[i]))
{
identifiers[identifier_counter]=expr[i];
identifier_counter++;
}
else if(isdigit(expr[i]))
{
x=(expr[i]-'0');
i=i+1;
while(isdigit(expr[i]))
{
x=x*10+(expr[i]-'0');
i++;
}
i=i-1;
constants[constants_counter]=x;
constants_counter++;
}
else if(expr[i]==','||expr[i]==';'||expr[i]=='{'||expr[i]=='}')
{
if(expr[i]==',')
{
delimeters[delimeters_counter]=',';
delimeters_counter++;
}
else if(expr[i]==';')
{
delimeters[delimeters_counter]=';';
delimeters_counter++;
}
else if(expr[i]=='{')
{
delimeters[delimeters_counter]='{';
delimeters_counter++;
}
else if(expr[i]=='}')
{
delimeters[delimeters_counter]='}';
delimeters_counter++;
}
}
else
{
if(expr[i]=='*')
{
operators[operators_counter]='*';
operators_counter++;
}
else if(expr[i]=='-')
{
operators[operators_counter]='-';
operators_counter++;
}
else if(expr[i]=='+')
{
operators[operators_counter]='+';
operators_counter++;
}
else if(expr[i]=='=')
{
operators[operators_counter]='=';
operators_counter++;
}
else if(expr[i]=='/')
{
operators[operators_counter]='/';
operators_counter++;
}
else if(expr[i]=='%')
{
operators[operators_counter]='%';
operators_counter++;
}
}
}
printf("\nIdentifiers are: ");
for(j=0;j<identifier_counter;j++)
{
printf("%c ",identifiers[j]);
}
printf("\nConstants are: ");
for(j=0;j<constants_counter;j++)
{
printf("%d ",constants[j]);
}
printf("\nDelimeters are: ");
for(j=0;j<delimeters_counter;j++)
{
printf("%c ",delimeters[j]);
}
printf("\nOperators are: ");
for(j=0;j<operators_counter;j++)
{
printf("%c ",operators[j]);
}
return 0;
}
My Queries:
I want to consider numbers or underscores after the variable's name as "identifiers".
for example: user input is "a4". I want the program to print "identifiers are: a4".
In my code 'a' is considered as an identifier and '4' as a constant.
If the user input is: "a 4" then it's ok if it prints 'a' as identifier and '4' as
constant, since there's a space between.
I want to generate a token for data types. If the user types 'int' it should return that
"datatypes are: int". I know one procedure but it's lengthy and time taking, I've tried to
search if there are any built-in functions so that they can return the data type, but I
couldn't find any.
Can you help me with these two queries and how I should update my code accordingly. Thank you.
You should split your problem into little pieces. First, how do you recognize a identifier? Write a function, that takes a string and returns the true, if it starts with an identifier. If you have done this, make the function return the length of the identifier. Now do the same thing with numbers and everything you want. Now you have build a tokenizer/lexer. I have an implementation (but a bigger one) here.

Very Basic Encryption

#include <stdio.h>
int limit;
char alp[26]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','w','x','y','z'};
void encode(char message[21],char enc_message[21],int key);
void decode(char enc_message[21],char dec_message[21],int key);
main()
{
int key,i=0,j=0;
char message[21];
char enc_message[21];
char dec_message[21];
char encrypted[21];
char a='\0';
printf("Input the characters to encrypt\n");
while(i<21 && a!='\n')
{
scanf("%c",&a);
message[i]=a;
i=i+1;
}
for(i=0;;i++) /*custom strlen*/
{
if( message[i]= '\0')
{
limit=i;
break;
}
}
printf("Input the key");
scanf("%d",key);
for(i=0;i<21;i++)
{
enc_message[i]=message[i];
}
encode(message[21],enc_message[21],key);
for(i=0;i<21;i++)
{
dec_message[i]=enc_message[i];
}
for(i=0;i<limit;i++)
{
printf("%c",enc_message[i]);
}
printf("\n\n");
decode(enc_message[21],dec_message[21],key);
for(i=0;i<limit;i++)
{
printf("%c",dec_message[i]);
}
}
void encode(char message[21],char enc_message[21],int key)
{
/*char temp[21];*/
int x,y;
for(x=0;x<limit;x++) /* message check */
{
for(y=0;y<26;y++) /* <----- alphabet check */
{
if (enc_message[x]==alp[y]) enc_message[x]=alp[y+key];
}
}
}
/*------------------------------------------------------------------------*/
void decode(char enc_message[21],char dec_message[21],int key)
{
int x,y;
for (x=0;x<limit;x++)
{
for(y=0;y<26;y++)
{
if (dec_message[x]==alp[y+key]) dec_message[x]=alp[y];
}
}
}
The compiler says,the mistake has to do with the way I call functions(and write them)and says: passing argument1 of 'encode' makes pointer from integer without a cast ,and that is for argument 2 of 'encode' and the exact same for 'decode'
Thanks in advance!
You are passing a single element and it's not even a valid element, try
decode(enc_message, dec_message, key);
Also, format your code so it's readable that is really important, and looping to compute the length of the string to use it in another loop is not a very smart thing, print it in a loop like
for (int i = 0 ; enc_message[i] != '\0' ; ++i) ...
also, don't over use break, just think about the logical condition for the loop, it's the same one where you break. Code is much more readable if the condition appears in the right place.

Why do i get this runtime error in this C programme? Please show me what's wrong

The task can be found here: http://www.talentbuddy.co/challenge/51846c184af0110af3822c32
And my programme regarding this task is the following:
#include <stdio.h>
#include<string.h>
void tokenize_query(char *query, char *punctuation) {
int i,j,ok=1,k,t;
char x[1000];
for(i=0;i<strlen(query);i++)
{
ok=1;
for(j=0;j<strlen(punctuation);j++)
{
if(query[i]==punctuation[j] || query[i]==' ')
ok=0;
}
if(ok!=0)
{
x[k]=query[i];
k++;
}
else {
for(t=0;t<k;t++)
{
printf("%c",x[t]);
}
k=0;
printf("\n");
}
}
}
k is uninitialised in the line
x[k]=query[i];
so you'll probably try to write beyond the end of the memory allocated for x.
The easiest fix is to initialise k when you declare it
int i,j,ok=1,k=0,t;
// ^^

Longest Common Subsequence-Segmentation fault

I have to write a program to determine the longest common sub sequence.
Input:
The first argument will be a file that contains two strings per line, semicolon delimited. You can assume that there is only one unique subsequence per test case. e.g.
XMJYAUZ;MZJAWXU
Output:
The longest common subsequence. Ensure that there are no trailing empty spaces on each line you print. e.g.
MJAU
I am using Dev C++ .. And it is compiling Fine!...But this question is a programming challenge and when i submit my answer it's showing me a segmentation fault!
I have written the following code and i am getting a Segmentation Fault where am i wrong?
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char str1[100],str2[100];
int len1;
int len2;
void printLCS(char b[len1][len2],char str1[],int i,int j)
{
if(i==0 || j==0)
return;
if(b[i][j]=='c')
{
printLCS(b,str1,i-1,j-1);
printf("%c",str1[i-1]);
}
else if(b[i][j]=='l')
printLCS(b,str1,i,j-1);
else
printLCS(b,str1,i-1,j);
}
void Seq(char str1[],char str2[])
{
int i,j;
len1=strlen(str1);
len2=strlen(str2);
int LCS[len1+1][len2+1];
char b[len1][len2];
for(i=0;i<=len1;i++)
{
LCS[i][0]=0;
}
for(j=0;j<=len2;j++)
{
LCS[0][j]=0;
}
for(i=1;i<=len1;i++)
{
for(j=1;j<=len2;j++)
{
if(str1[i-1]==str2[j-1])
{
LCS[i][j]=1+LCS[i-1][j-1];
b[i][j]='c';
}
else if(LCS[i-1][j]>=LCS[i][j-1])
{
LCS[i][j]=LCS[i-1][j];
b[i][j]='u';
}
else
{
LCS[i][j]=LCS[i][j-1];
b[i][j]='l';
}
}
}
printLCS(b,str1,len1,len2);
}
int main(int argc,char *argv[])
{
if(argc!=2)
{
printf("Invalid Number of Arguments:\n");
exit(0);
}
FILE *fp;
fp=fopen(argv[1],"r");
if(fp==NULL)
{
printf("File can't be opened:\n");
exit(0);
}
char c;
c=fgetc(fp);
while(c!=EOF)
{
int k=0;
if(c=='\n')
c=fgetc(fp);
while(c!=';')
{
str1[k]=c;
k++;
c=fgetc(fp);
}
str1[k]='\0';
c=fgetc(fp);
k=0;
while(c!=EOF && c!='\n')
{
str2[k]=c;
k++;
c=fgetc(fp);
}
str2[k]='\0';
Seq(str1,str2);
printf("\n");
if(c==EOF)
{
break;
}
else
c=fgetc(fp);
}
return 0;
}
I dont know system of this site but;
i compiled with no error,
and result was true.
You didnt close file. Maybe memory leak etc. didnt allowed by site.
And, dont use global variables, unless you dont know another solution
this usage is very very bad! ISO C90 forbids this, anyway
int len1;
int len2;
void printLCS(char b[len1][len2]...
good luck.
If you've got access to a Mac or Linux system, there's a fantastic tool called valgrind which can help you track down these kinds of errors: basically it runs your program in a virtual machine and monitors what it reads and writes to memory.
Whilst I can't compile your code, I'm pretty suspicious about this for loop:
for(i=1;i<=len1;i++)
{
for(j=1;j<=len2;j++)
{
if(str1[i-1]==str2[j-1])
{
LCS[i][j]=1+LCS[i-1][j-1];
b[i][j]='c';
}
else if(LCS[i-1][j]>=LCS[i][j-1])
{
LCS[i][j]=LCS[i-1][j];
b[i][j]='u';
}
else
{
LCS[i][j]=LCS[i][j-1];
b[i][j]='l';
}
}
}
Arrays in C and C++ start at 0, so the maximum offset you're interested in is probably strlen - 1. Try changing your for loops to
for(i=1;i<len1;i++)
{
for(j=1;j<len2;j++)
{
...
}
}

Missing prototype in my practice arithmetic quiz program, and almost every other program I make

Yesterday I was able to make a program (ASCII converter etc etc) that had the same problem [ Every function had a missing prototype error when I build the program ] I was able to fix it through random trial and error having no idea how I did it. Here's my arithmetic quiz practice program. I also tried putting int initialize(),clear(),exit(),additionquiz(),subtractionquiz(),divisionquiz(),multiplicationquiz(); and it still gave me a missing prototype.
#include <stdio.h>
/* Main Menu */
int numbers[10];
int main()
{
while(1==1)
{
int choice;
initialize();
printf("Arithmetic Quiz Program\n");
printf("1 - Addition\n2 - Subtraction\n3 - Multiplication\n4 - Division\n5 - Exit\n");
scanf("%d",&choice);
if(choice==1)
{
clear();
additionquiz();
}
else if(choice==2)
{
clear();
subtractionquiz();
}
else if(choice==3)
{
clear();
multiplicationquiz();
}
else if(choice==4)
{
clear();
divisionquiz();
}
else if(choice==5)
{
exit();
}
else
{
printf("%cPlease choose a number from 1 - 5",7);
clear();
continue;
}
}
return 0;
}
/* For clearing the page */
int clear()
{
int i;
for(i=0;i<25;i++)
{
printf("\n");
}
}
/* Assigns the array */
int initialize()
{
numbers[0]=6;
numbers[1]=0;
numbers[2]=2;
numbers[3]=5;
numbers[4]=3;
numbers[5]=1;
numbers[6]=9;
numbers[7]=4;
numbers[8]=7;
numbers[9]=8;
return 0;
}
/* addition quiz */
int addition()
{
int a,diff,b,answer,choice;
a=0;
diff=1;
b=a+diff;
while(1==1)
{
if(a>9)
{
a=0;
diff++;
}
if(b>9)
{
b=0;
}
if(diff>9)
{
diff=0;
}
printf("%d + %d = ",number[a],number[b]);
scanf("%d",&answer);
if(answer==number[a]+number[b])
{
printf("\nCORRECT!!!\n");
a++;
}
else
{
printf("\nWRONG!!!\n");
clear();
additionquiz();
}
printf("\nWhat do you want to do next?\n1 - Answer another addition Question\n2 - Go back to main menu\n3 - Exit program\n");
scanf("%d",&choice);
if(choice==1)
{
clear();
additionquiz();
}
else if(choice==2)
{
clear();
main();
}
else if(choice==3)
{
exit();
}
else
{
printf("%cPlease choose a number from 1 to 3",7);
}
}
return 0;
}
/* The subtraction quiz */
int subtraction()
{
int a,diff,b,answer,choice;
a=0;
diff=1;
b=a+diff;
while(1==1)
{
if(a>9)
{
a=0;
diff++;
}
if(b>9)
{
b=0;
}
if(diff>9)
{
diff=0;
}
if(numbers[a]-numbers[b]<0)
{
a++;
subtraction();
}
printf("%d - %d = ",numbers[a],numbers[b]);
scanf("%d",&answer);
if(answer==numbers[a]-numbers[b])
{
printf("CORRECT!!!\n\n");
}
else
{
printf("WRONG!!!\n\n");
clear();
subtractionquiz();
}
printf("\nWhat do you want to do next?\n1 - Answer another subtraction Question\n2 - Go back to main menu\n3 - Exit program\n");
scanf("%d",&choice);
if(choice==1)
{
clear();
subtractionquiz();
}
else if(choice==2)
{
clear();
main();
}
else if(choice==3)
{
exit();
}
else
{
printf("%cPlease choose a number from 1 to 3",7);
}
}
return 0;
}
/* multiplication quiz */
int multiplicationquiz()
{
int a,diff,b,answer,choice;
a=0;
diff=1;
b=a+diff;
while(1==1)
{
if(a>9)
{
a=0;
diff++;
}
if(b>9)
{
b=0;
}
if(diff>9)
{
diff=0;
}
printf("%d * %d = ",number[a],number[b]);
scanf("%d",&answer);
if(answer==number[a]*number[b])
{
printf("\nCORRECT!!!\n");
a++;
}
else
{
printf("\nWRONG!!!\n");
clear();
multiplicationquiz();
}
printf("\nWhat do you want to do next?\n1 - Answer another multiplication Question\n2 - Go back to main menu\n3 - Exit program\n");
scanf("%d",&choice);
if(choice==1)
{
clear();
multiplicationquiz();
}
else if(choice==2)
{
clear();
main();
}
else if(choice==3)
{
exit();
}
else
{
printf("%cPlease choose a number from 1 to 3",7);
}
}
return 0;
}
/* Division quiz */
int divisionquiz()
{
int a,diff,b,answer,choice,remain;
a=0;
diff=1;
b=a+diff;
while(1==1)
{
if((numbers[a]<numbers[b])||numbers[b]==0)
{
a++;
clear();
divisionquiz();
}
if(a>9)
{
a=0;
diff++;
}
if(b>9)
{
b=0;
}
if(diff>9)
{
diff=0;
}
printf("%d % %d = \n",numbers[a],numbers[b]);
printf("What is the whole number?\n");
scanf("%d",&answer);
printf("What is the remainder? (0 if none\n)");
scanf("%d",&remain);
if(answer==numbers[a]/numbers[b] && remain==numbers[a]%numbers[b])
{
printf("\nCORRECT!!!");
a++;
}
else
{
printf("\nWRONG!!!");
clear();
divisionquiz();
}
printf("\nWhat do you want to do next?\n1 - Answer another division Question\n2 - Go back to main menu\n3 - Exit program\n");
scanf("%d",&choice);
if(choice==1)
{
clear();
divisionquiz();
}
else if(choice==2)
{
clear();
main();
}
else if(choice==3)
{
exit();
}
else
{
printf("%cPlease choose a number from 1 to 3",7);
}
}
return 0;
}
exit is an external function and you need to include its header at the top of your source code:
#include <stdlib.h> // exit
Please notice that in the function call the addition function is called addition and in the function definition additionquiz. Same for the substraction.
For the other functions, you should declare them before you call them: that is before the main function definition.
int initialize(void);
int clear(void);
int additionquiz(void);
int subtractionquiz(void);
int divisionquiz(void);
int multiplicationquiz(void);
int main(void)
{
/* ... */
Note that declaring all the functions in one go like this:
int initialize(void), clear(void), additionquiz(void),
subtractionquiz(void), divisionquiz(void), multiplicationquiz(void);
is permitted but it is not very readable and may surprise the reader.
Finally, if these functions are not called from another source code, you should tell the reader (and the compiler) by adding a static specifier at the beginning of the declaration like this:
static int clear(void); // the function is only called in this source code
The C compiler works from top-to-bottom. It must know that your functions exist before you attempt to call them. So you have two choices:
Define your functions above main (i.e. move the entire function bodies).
Declare your functions above main. i.e. put int initialize();, etc. above main.
Note also that in C, int initialize() is different to int initialize(void). You should be using the second version.
More information on what the guy above me just said can be found here. This page gives you an overview of how to suppress or enable different kinds of warnings in your code:
http://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html
Also another tip. You wrote a couple while loops in your code with this syntax:
while (1==1)
{
...
}
this will do the same thing
while(1)
{
}
Here's why: A while loop, an if statement, else statement, and an else if statement will
all perform the code beneath them if the code inside the parenthesis is true. Since 1 in C is true and 0 is false, my while loop above works the same as yours.
First, learn to compile with all warnings enabled and with debugging information (e.g. with gcc -Wall -g on Linux). Then improve your program till no warnings are given by the compiler (trust the compiler).
Then, learn to have a declaration for each of your function. Start your sole source file with them, or, if you have several source files, make a header file with them.
So you could add just after your #include lines:
// clear the screen
void clear(void);
// initialize the numbers
int initilize(void);
// addition quiz
int addition(void);
// subtraction quiz
int subtraction(void);
// multiplication quiz
int multiplicationquiz(void);
// division quiz
int divisionquiz(void);
By the way, your functions might be better named, and you could have formal arguments in them (else use void as the argument list). And I don't understand why they all return an int which you don't use.

Resources