ProgrammingVery hard
A naive strstr-style search looks for a pattern of length m = 50000 in a text of length n = 100000. The text is all 'a' and the pattern is 'a' repeated m-1 times followed by 'b', so there is never a match. Roughly how many character comparisons does the search make?
- A. About 10^5
- B. About 5 x 10^4
- C. About 2.5 x 10^9
- D. About 10^10
Correct answer: C. About 2.5 x 10^9
The naive method tries each of the n - m + 1 starting positions, and here it compares nearly all m characters before failing at each one. So the work is about (n - m) x m, roughly 2.5 x 10^9.
More C Programming questions
- What does strncmp("abc", "xyz", 0) return?
- char a[3] = {'x', 'y', 'z'}; Which call reads a correctly with no undefined behaviour?
- char a = "", b = "abc"; if (strlen(a) - strlen(b) > 0) puts("longer"); else puts("not longer"); What is printed?
- In C (not C++), what is sizeof('A')?
- char a[5] = "hi"; printf("%d", a[4]); What is printed?
- int n = -1; if (n < strlen("abc")) puts("yes"); else puts("no"); What is printed?