Solutions to Assessment 1. In the bad character rule, what are the values of R(P) for the given pattern. CLICKINLINK (Please refer the lecture). Ans: a) C-1,K-10,N-9,I-8,L-2 b) C-1,K-10,N-9,I-8,L-7 c) C-4,K-10,N-9,I-8,L-7 d) C-1,K-5,N-9,I-8,L-7 Ans: c 2. What is the preprocessing time and space complexity using bad character rule in Boyer Moore algorithm. a) O(n) time and O(K) space, where n is length of pattern and K is size of alphabet set. b) O(log n) time and O(K) space, where n is length of pattern and K is size of alphabet set. c) O(n+K) time and O(n+K) space, where n is length of pattern and K is size of alphabet set. d) O(K) time and O(n) space, where n is length of pattern and K is size of alphabet set. Ans: a 3. What is the worst case time complexity of Boyer Moore string matching algorithm when the length of pattern is n and length of the text is m. a) O(n) b) O(m) c) O(n*m) d) (On+m) Ans: c 4. What is the time complexity of Boyer Moore algorithm when all the characters in the text and pattern are same. For e.g. T = AAAAAAAAAAAAAAAAAA, P= AAAAA. Length of T is m and length of P is n. a) O(m) b) O(n) c) O(n*m) d) O(n+m) Ans: c 5. Boyer moore is a left to right comparison algorithm. a) True
b) False 6. Which of the following is true about the following declaration? FILE *fp; a) FILE is a keyword in C for representing files and fp is a variable of FILE type b) FILE is a structure and fp is a pointer to the structure of FILE type c) FILE is a stream d) FILE is a buffered stream 7. When fopen() is not able to open a file, it returns a) EOF b) NULL c) Runtime Error d) Some compiler dependent value 8. If you create a file with the same name as an existing file, you will be prompted to rename the file. TRUE or FALSE? a) True b) False 9. What is the output of this C code? #include <stdio.h> int main() char buf[12]; stderr = stdin; fscanf(stderr, "%s", buf); printf("%s\n", buf); a) Compilation Error b) Undefined behavior c) Whatever the user types as input to the program
d) None of the above Ans: c 10. Files of which of the following types can be created? a) Text b) Binary oth 11. Which one of the following is valid for opening a read-only ASCII file? a) fileopen (filename, "r"); b) fileopen (filename, "read"); c) fopen (filename, "readonly"); d) fopen (filename, "r"); Ans: d 12. Which of the following function declarations indicate a bad design? a) int add(int,int); b) int add(float, int); c) float add(int,int); d) float add(float,int); and c 13. void funct(void) The above function declaration indicates a) it returns nothing and takes no arguments b) it returns nothing and takes arguments c) it returns a value and takes arguments d) it returns a value and takes no arguments Ans: a 14. If the number of actual arguments to a function call does not match with formal arguments, it will result in a) no error b) compilation error c) logical error d) syntax error
15. Any function can be called from any other function. TRUE or FALSE? a) True b) False Ans: a 16. strcmp() is a function used for: a) Copying one string to another b) Comparing two strings c) Concatenating two strings d) None of these 17.Input/output function prototypes and macros are defined in which header file? a) conio.h b) stdlib.h c) stdio.h d) math.h Ans: C 18. Which of the following library functions can be used to convert a string to a integer? a) itoa b) atoi c) ltoa d) ultoa
Solutions to Programming Assignments Question 1: Given a text T[0..n-1] and a pattern P[0..m-1]. Print the starting index of all the occurrences of pattern P in text T. If pattern P doesn t occur in T, then print '-1'. You are supposed to implement Boyer Moore algorithm using bad character rule as explained in the lecture. Input: First line contains the text T. Second line contains the pattern P Output: Index of each occurrence of pattern P in small brackets as shown in the examples below. Constraints: 1 <= length(p) <= length(t) < 100 Examples: Input 1: ABABABBAC ABA Ouput 1: (0)(2) Input 2: CCDCCDC DC Output 2: (2)(5) Input 3: XYZYZ ZX Output 3:
-1 Test Cases: Public test cases: Input ABABABBBBABDBA BAB XYXYYXYYXYYY XYYX AAAAAAAAAAA AAAAAAAAAA XYAZZJJA NNB ABABABABAB AB Output (1)(3)(8) (2)(5) (0)(1) -1 (0)(2)(4)(6)(8) Private test cases: Input XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX X ABCDABCDAABBAABBACDACDYAAB Y ABCDEFGHIJKLMNOPQRST JKL Output (0)(1)(2)(3)(4)(5)(6)(7)(8) (9)(10)(11)(12)(13)(14)(15) (16)(17)(18)(19)(20)(21)(22 )(23)(24)(25)(26)(27)(28)(2 9)(30)(31)(32)(33)(34)(35) (22) (9) THISISFOO THISISFOO XXYYXYXYZZYZZAAB CCC (0) -1 Solution:
#include<limits.h> #include<string.h> #include<stdio.h> #define CHAR_SET_SIZE 256 /* A function to get maximum of two integers */ int maximum (int a, int b) return (a > b)? a: b; /* Preprocessing function for Boyer Moore's bad character rule */ /* R[i] is the last occurrence of the character 'str[i]' in the pattern 'str'.(refer lecture) */ void preprocess(char *str, int size, int R[CHAR_SET_SIZE]) int i; for(i = 0; i < CHAR_SET_SIZE; i++) R[i] = -1; for(i = 0; i < size; i++) R[(int) str[i]] = i; /* Complete this function using Boyer Moore's bad character rule */ /* For pre processing use the function preprocess() defined above */ /* You also have to print the output in this function */ void patternmatcher( char *T, char *P) int m = strlen(p); int n = strlen(t); int countnum=0; int R[CHAR_SET_SIZE]; //Bad Character Array preprocess(p, m, R); // Fill R by calling preprocess() for the given Pattern P int s = 0; // s is shift of the Pattern P with respect to text T while(s <= (n - m))
int j = m-1; /* Decrease index j of P while characters of P and T match at this shift s */ while(j >= 0 && P[j] == T[s+j]) j--; /* If P matches at current shift, then j becomes -1 after the above loop */ /* Shift P so that the next character of T aligns with the last occurrence of it in P */ if (j < 0) printf("(%d)", s); countnum++; s += (s+m < n)? m-r[(int)t[s+m]] : 1; //tests if P occurs at the end of T else /* Shift P so that the bad character in T aligns with the last occurrence of it in P.*/ s += maximum(1, j - R[(int)T[s+j]]); if(countnum == 0) printf("-1"); //end of patternmatcher() /** Main function */ int main() char T[100]; char P[100]; scanf("%s%s",t,p); patternmatcher(t, P); return 0;