Question

In a text of length 1000 with the following pattern 1234123412341234... How many successful comparisons and...

In a text of length 1000 with the following pattern 1234123412341234... How many successful comparisons and how many unsuccessful comparisons are made by the brute-force string-matching algorithm to search for all occurrences of 341? We are looking for two answers here. (A successful comparison is when one character from the pattern matches one character from the text, otherwise it is unsuccessful.)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here pattern is:1234

Text lenght is:1000

Therefore,in the text 1234 repeated 250 times(i.e total text length/pattern length=1000/4)

searching string is:341

successful comparisons=249(i.e the string 341 present 249 times in in the given pattern of 1234.....)

unsuccessful comparisons=3(i.e

1.1 comapre with 3 false

2.2 compare with 3 flase

3.finally at end 3 and 4 characters compares successfully but 1 is not present)

#include <stdio.h>
#include <string.h>
char str[100], sub[100]="341";
int count = 0, count1 = 0;
void main()
{
    int i, j, l, l1, l2,k,m,n;
    for(k=0;k<250;k++)
    {
        strcat(str,"1234");
    }  
    printf("%s\n",str);
    l1 = strlen(str);
    l2 = strlen(sub);
    for (i = 0; i < l1;)
    {
        j = 0;
        count = 0;
        while ((str[i] == sub[j]))
        {
            count++;
            i++;
            j++;
        }
        if (count == l2)
        {
            count1++;                                 
            count = 0;
        }
        else            i++;
    }  
    printf("successful comparisons are:%d\n",count1);
}

Add a comment
Know the answer?
Add Answer to:
In a text of length 1000 with the following pattern 1234123412341234... How many successful comparisons and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Advanced Data Structures Give 3 Pattern/String Matching algorithms. Given a Text and a Pattern, apply the...

    Advanced Data Structures Give 3 Pattern/String Matching algorithms. Given a Text and a Pattern, apply the Boyer-Moore algorithm, The KMP algorithm(this is the one i need help on the most), The Brute Force algorithm and match the pattern in the below string. Also, write the algorithm. Text: CBADBCACBADCBBACACBCAABCA Pattern: ACBCAABC

  • Task Algorithms: Pattern Matching (in java) Write a program that gets two strings from user, size...

    Task Algorithms: Pattern Matching (in java) Write a program that gets two strings from user, size and pattern, and checks if pattern exists inside size, if it exists then program returns index of first character of pattern inside size, otherwise it returns -1. The method should not use built-in methods such as indexOf , find, etc. Only charAt and length are allowed to use. Analyze the time complexity of your algorithm. Your solution is not allowed to be> = O...

  • If the length of the array P is 4 and the length of the array T...

    If the length of the array P is 4 and the length of the array T is 14, how many shifts of P need to be performed in the string searching algorithm? a. 9 b. 10 c. 11 d. 12 What is the best case for the naive string search algorithm? a. The first character of the pattern P isn't present in text T b. All characters in pattern P are different c. All characters in text T are different...

  • Overview: Pattern-Matching (aka String Search) is the process of algorithmically finding copies of a pattern P...

    Overview: Pattern-Matching (aka String Search) is the process of algorithmically finding copies of a pattern P inside a (generally much larger) text T. The goal is to implement and compare four classical string-matching algorithms. Input: Your code should work for any text either inputted directly or read in from a file. However, for testing - input file has been provided: The Gettysburg Address (by President Abraham Lincoln, 1863) You should minimally search for these three patterns in each text: FREE,...

  • Overview: Pattern-Matching (aka String Search) is the process of algorithmically finding copies of a pattern P...

    Overview: Pattern-Matching (aka String Search) is the process of algorithmically finding copies of a pattern P inside a (generally much larger) text T. The goal is to implement and compare four classical string-matching algorithms. Input: Your code should work for any text either inputted directly or read in from a file. However, for testing - input file has been provided: The Gettysburg Address (by President Abraham Lincoln, 1863) You should minimally search for these three patterns in each text: FREE,...

  • !!!!!!!Java!!!!! When you are confident that your methods work properly and that you can generate random...

    !!!!!!!Java!!!!! When you are confident that your methods work properly and that you can generate random text with my generateText method, you can move on to the second step. Create a third class called Generator within the cs1410 package. Make class. This class should have a main method that provides a user interface for random text generation. Your interface should work as follows: Main should bring up an input dialog with which the user can enter the desired analysis level...

  • Download the file patternMatch.c from the course website. This file contains part of a program to...

    Download the file patternMatch.c from the course website. This file contains part of a program to do pattern matching. The function main looks like this: int main() { char text (40), pattern (40), *position; int textlength, patternlength; printf ("Enter text: "); textlength = readline (text, 40); printf ("Enter pattern: "); patternlength = readline (pattern, 40); position = findmatch (pattern, text, patternlength, textlength); printmessage (position, text, patternlength, textlength); The function readline should read a line of characters from the keyboard. The...

  • 6.15 Program 6: Using Arrays to Count Letters in Text 1. Introduction In this program, you...

    6.15 Program 6: Using Arrays to Count Letters in Text 1. Introduction In this program, you will practice working with arrays. Your program will read lines of text from the keyboard and use an array to track the number of times each letter occurs in the input text. You will use the contents of this array to generate a histogram (bar graph) indicating the relative frequencies of each letter entered. Test cases are available in this document. Remember, in addition...

  • Write a c++ program in that file to perform a “Search and Replace All” operation. This...

    Write a c++ program in that file to perform a “Search and Replace All” operation. This operation consists of performing a case-sensitive search for all occurrences of an arbitrary sequence of characters within a file and substituting another arbitrary sequence in place of them. Please note: One of the biggest problems some students have with this exercise occurs simply because they don’t read the command line information in the course document titled “Using the Compiler’s IDE”. Your program: 1. must...

  • From the Tony Gaddis text, the chapter on C-String and Class String: String Length: Write a...

    From the Tony Gaddis text, the chapter on C-String and Class String: String Length: Write a Function that passes in a C-String and using a pointer determine the number of chars in the string.                                           Data:   “This is a test string for string length” Prt String Backwards: Write a Function that passes in a C-String and prints the string backwards.      Data: “This is a test string for string backwards” replaceSubstring: Write a Function that accepts three C-Strings – str1,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT