Introduction to Algorithms
3rd Edition
ISBN: 9780262033848
Author: Thomas H. Cormen, Ronald L. Rivest, Charles E. Leiserson, Clifford Stein
Publisher: MIT Press
expand_more
expand_more
format_list_bulleted
Concept explainers
Question
Chapter 32.1, Problem 4E
Program Plan Intro
To analyze the execution time of a polynomial time
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
Let A be the set of all strings of decimal digits of length 5. For example 24157 and05189 are strings in A.(a) How many strings in A have exactly one 7?(b) How many strings in A have at least two 2’s?(c) How many strings in A have the digits in a strictly decreasing order? For example97643 and 54321 are such strings, but 14820 and 95421 are not.
Given a string S, find the longest palindromic substring in S. Substring of string S: S[ i . . . . j ] where 0 ≤ i ≤ j < len(S). Palindrome string: A string which reads the same backwards. More formally, S is palindrome if reverse(S) = S. Incase of conflict, return the substring which occurs first ( with the least starting index).
Example 1:
Input: S = "aaaabbaa" Output: aabbaa Explanation: The longest Palindromic substring is "aabbaa".
Please written by computer source
Given two strings x1…xn, y1…ym find the length of their longest common subsequence, that is, the largest k for which there exist indices i1<…<ik and j1<=<jk such that xi1…xik…=yj1=yjk. Show how to do this in time O(nm) and find its space complexity.
Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Similar questions
- A tandem repeat of a base string b in a string s is asubstring of s having at least two consecutive copies b (nonoverlapping). Develop andimplement a linear-time algorithm that, given two strings b and s, returns the index ofthe beginning of the longest tandem repeat of b in s. For example, your program shouldreturn 3 when b is abcab and s is abcabcababcababcababcab.arrow_forwardDescribe an algorithm that given two regular expressions R1 and R2 outputs YES if andonly if every string generated by R1 is also generated by R2. (I.e., output YES exactlywhen the set of strings generated by R1 is a subset of the set of strings generated by R2.)arrow_forwardConsider the problem of counting, in a given text, the number of substrings that start with an A and end with a B. For example, there are four such substrings in CABAAXBYA.a. Design a brute-force algorithm for this problem and determine its efficiency class.b. Design a more efficient algorithm for this problem with complexity O (n)arrow_forward
- Given a circular list of characters randomly ordered. For each character ‘’ in the list, it is requested to find the next greater character ‘’ in the list, and then to replace ‘’ by ‘’. If there is no greater character, replace ‘’ by ‘@’.Given that A<B<C<…<X<Y<ZExample 1:input: [E, D, B, A, K, W, G] output: [K, K, K, K, W, @, K]Explanation: Considering each character and how it is replaced:1) Consider E, it is requested to find the next greater character. We can easily notice that D, B, A are not greater than E, while K is the next greater character. This is why E is replaced by K. 2) The same scenario happens with D, B, and A, where they are all replaced by K.3) Consider K, the next greater character is W, and hence K is replaced by W. 4) Consider W, there is no character in such circular list greater than W, and hence it is replaced by @. 5) Consider G, as this is a circular list, the next character is E, which is not greater than G, while K is the next greater…arrow_forwardProve the correctness of the following pattern-matching algorithm. The input consists of a string S[1..n], and a pattern P[0..m − 1], where 1 ≤ m ≤ n. The algorithmlocates the first contiguous occurrence of the pattern P in the string S, that is, . = p if S[p..p + m − 1] = P, and . = n − m + 1 if the pattern P does not occur at all in the string S. function match(P, S, n, m) comment Find the pattern P[0..m − 1] in string S[1..n] 1. . := 0; matched := false 2. while (. ≤ n − m) ∧ ¬matched do 3. . := . +1; 4. r := 0; matched := true 5. while (r<m) ∧ matched do 6. matched := matched ∧ (P[r] = S[. + r]) 7. r := r + 1 8. return(.)arrow_forwardI actually forgot to type "xw" after "given by" in my question. Consider a text corpus consisting of N tokens of d distinct words and the number of times each distinct word w appears is given by xw.arrow_forward
- A palindrome is a nonempty string over some alphabet that reads the same forward and backward. Examples of palindromes are all strings of length 1, civic, racecar, and aibohphobia (fear of palindromes). Give an efficient algorithm to find the longest palindrome that is a subsequence of a given input string. For example, given the input character, your algorithm should return carac. What is the running time of your algorithm?arrow_forwardIf a is a symbol and n > 0, then an denotes a···a, or in words, the string of length n consisting of only a’s. Now consider the coding with two code words 0n and 1n, for some natural number n > 0. Show that this coding is perfect if and only if n is odd.arrow_forwardLet P2(x) be the least squares interpolating polynomial for f(x) := sin(πx) on the interval [0,1] (with weight function w(x) = 1). Determine nodes (x0,x1,x2) for the second-order Lagrange interpolating polynomial Pˆ2(x) so that P2 = Pˆ2. You are welcome to proceed theoretically or numerically using Python.arrow_forward
- Problem Alice was given a number line containing every positive integer, x, where 1<=x<=n. She was also given m line segments, each having a left endpoint, l (1<=l<=n), a right endpoint, r (1<=r<=n), and an integer weight, w. Her task is to cover all numbers on the given number line using a subset of the line segments, such that the maximum weight among all the line segments in the chosen subset is minimal. In other words, let S=(l1,r1,w1),(l2,r2,w2),....,(lk,rk,wk), represent a set of k line segments chosen by Alice, max(w1,w2,...,wk) should be minimized. All numbers 1,2,....n should be covered by at least one of k the chosen line segments. It is okay for the chosen line segments to overlap. You program should output the minimized maximum weight in the chosen subset that covers all the numbers on the number line, or -1 if it is not possible to cover the number line. Input format The first line of the input contains an integer, n - denoting the range of numbers…arrow_forwardGiven two strings A and B, find the minimum number of times A has to be repeated such that B is a substring of it. If no such solution, return -1.For example, with A = "abcd" and B = "cdabcdab".Return 3, because by repeating A three times (“abcdabcdabcd”), B is a substring of it; and B is not a substring of A repeated two times ("abcdabcd").Note:The length of A and B will be between 1 and 10000.Reference: https://leetcode.com/problems/repeated-string-match/description/"""def repeat_string(A, B): count = 1 tmp = A max_count = (len(B) / len(A)) + 1 while not(B in tmp): tmp = tmp + A if (count > max_count): count = -1 break.arrow_forwardLeetcide Given two strings A and B, find the minimum number of times A has to be repeated such that B is a substring of it. If no such solution, return -1.For example, with A = "abcd" and B = "cdabcdab".Return 3, because by repeating A three times (“abcdabcdabcd”), B is a substring of it; and B is not a substring of A repeated two times ("abcdabcd").Note:The length of A and B will be between 1 and 10000.Reference: https://leetcode.com/problems/repeated-string-match/description/"""def repeat_string(A, B): count = 1 tmp = A max_count = (len(B) / len(A)) + 1 while not(B in tmp): tmp = tmp + A if (count > max_count): count = -1 break.arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education