1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Serga [27]
3 years ago
13

A palindrome is a string of characters that is the same when reversed (e.g., ‘affa’). Single characters are palindromes. Suppose

you are given a string S of N characters and wish to produce an N-by-N matrix P, where P = 1 if i ≤ j and S[i,...,j] is a palindrome, and Pij = 0 otherwise.
(A) Matrix P can be computed using brute force by separately examining each substring of S and determining whether it is a palindrome. Describe this algorithm. What is the algorithm’s running time?
(B) It is possible to compute matrix P more efficiently using dynamic programming. Give a dynamic programming algorithm to compute P, and analyze its running time. (Hint: Begin by filling in the values along the diagonal of the matrix.)
Computers and Technology
1 answer:
Nina [5.8K]3 years ago
5 0

Answer:

Check the explanation

Explanation:

a) This done can be executed through the use of brute force by the given algorithm: Consider any two indices i and j then we can check if string from s[i....j] is palindrome or not in O(L) where L is length of string.

Algorithm to check if string is palindrome :

Takes String s as argument and the two indices i,j between which the string is to be considered

bool palindromeCheck(s,i,j) :

        y = j

        for x in range(i,j) :

             if(s[x]==s[y]) :

                      y--

                      continue

            else :

                      return false

       return true

Now taking a look at the  part a) we will be calling the function to confirm and execute palindrome for all i<=j that is a total of points of the order of O(n^2) and the string checking will take O(n) in the worst case making it O(n^3).

b) To advance over we became aware of the fact that repeated calculation is being done i.e when we confirmed the string s[i...j] we also tested in between strings like s[i+1,j-1] etc in the same time. Consequently we used memorization to store the outcome that we calculate and reuse them we need them later.

We know that P[i][i] will always be one since a single character string will always be a palindrome.

Using this as our base case to check for P[i][j] we need to check if the ith and the jth character are equal and also if P[i+1][j-1] is true i.e is a palindrome.

We come up with the following recurrence

P[i][j] = 1 if i=j

P[i][j] = P[i+1][j-1] ; if i>=j and s[i]==s[j]

            0; otherwise

There are order of O(n^2) states and each one of the state necessitates only constant and regular time character checking for its evaluation and then assigning the value of 1, P[i+1][j-1],0 as a result of that which is again a constant time operation. Therefore all the P[][] matrix values can be found in O(n^2) time.

Pseudo code :

Let n be the length of the string

for x in range(1,n) :

        P[x][x]=1

for sz in range(2,n) : # Since we have already computed answer for string of size 1

        for x in range (1,n-sz+1):

                   y=x+sz - 1

                   if x == y :

                            if sz==2 :

                                      P[x][y] = 1

                            else :

                                      P[x][y] = P[x+1][y-1]

                  else :

                            P[x][y] = 0

From the implementation also it is clear that the running time of the algorithm is O(n^2).

You might be interested in
What is A cell that can hold or display data​
g100num [7]

Answer:

excel

Explanation:

5 0
3 years ago
What is folded card publishing?​
Alexeev081 [22]

Answer:

Folded card This layout type is for creating greeting cards by printing pages on a sheet and then folding the sheet to make the card. If you choose the Folded card layout, then sheet fold options are displayed. Select an option in the list to specify how you will fold your publication

Explanation:

3 0
2 years ago
Read 2 more answers
A circuit has a resistance of 300 ohm and a current of 0.024 A. What is the voltage in the circuit?
fomenos
**Formula

If u want to calculate voltage:
Current x Resistance

so the answer is D

Current:
Voltage ÷ Resistance

Resistance:
Voltage ÷ Current
7 0
3 years ago
1.an electronic index of books A. web page
zimovet [89]
It was quite difficult to understand what you need. Anyway, I've got it. I guess you need to much all the terms to each sentence. So I think I've done it right. Check it out:

1.an electronic index of books - <span>B. computer catalog
</span><span>
2.a device which categorizes and locates web sites - </span><span>H. search engine
</span><span>
3.to draw a conclusion - </span>D. infer<span> 

4.a block of information stored in an HTML file on a server - </span><span>A. web page
</span><span>
5.the table of contents of a web site - </span><span>G. home page
</span><span>
6.a software package which retrieves information from any or all available Internet servers - </span><span>I. browser
</span><span>
7.a highlighted word or phrase within a web page which acts as a "bridge" to another web page or site - </span><span>F. hyperlink
</span><span>
8.a topic sentence - </span><span>C. key sentence
</span><span>
9.a term which aids in narrowing a web search - </span>E. keyword
5 0
3 years ago
If powerpoint is allowed to dominate a speech, it can divert attention from the speaker's message.
aev [14]
<span>The statement that if powerpoint is allowed to dominate a speech, it can divert attention from the speaker's message is true.
</span>
PowerPoint provides a common infrastructure, a template for the organization of speech, and for the logic of argumentation<span> and presentations should use both visual and verbal forms of presentation in order to dominate.</span>
3 0
3 years ago
Other questions:
  • Create a script that asks for the visitor's weight in pounds and his/her height in inches. The program should then calculate the
    13·1 answer
  • A delimiter is used to do which of these? A. Separate two lines of data from each other. B. Separate two fields within a line of
    9·2 answers
  • Assign courseStudent's name with Smith, age with 20, and ID with 9999. Use the PrintAll member function and a separate cout stat
    14·2 answers
  • As a culture chnages so does its language<br><br> A.true <br> B.false
    5·1 answer
  • Arrange the following units of storage in descending<br> order. B, TB,KB, GB,MB
    5·1 answer
  • When reading words with a scanner object, a word is defined as ____?
    15·1 answer
  • Write a public static method named evens that takes in 1 argument int a, and returns a String containing all positive even numbe
    8·1 answer
  • What is the total running time of counting from 1 to n in binary if the time needed to add 1 to the current number i is proporti
    5·1 answer
  • MSWord is a popular___________ program.​
    7·2 answers
  • The set Canvas.PaintColor to block sets the paint color for the _____ .
    5·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!