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 command enables you to initialize quotas on a file system?
jeka57 [31]
 Quotas can be enabled by mounting a partition with the usrquota (and optionally the grpquota) mount option(s).Normally this is done by editing /etc/fstab and adding usrquota to the mount options part of the entry for some filesystem.Then that <span>filesystem must be remounted ("mount -o remount filesystem").</span>
4 0
3 years ago
As a photographer, what will be the driving force behind everything that you produce?
GREYUIT [131]

Answer:

You have to like it to be successful in photography.

Explanation:

8 0
3 years ago
Benching system are prohibited in
enyata [817]
Providence yep ur welcome
3 0
3 years ago
What types of storage can be used to access your data on another computer?
allochka39001 [22]
A USB drive is portable and can be used.
4 0
4 years ago
Read 2 more answers
Kyle owns a growing delivery business. He would like to buy two more vans but first he needs to figure out related costs and his
guajiro [1.7K]

He will use a spreadsheet. hope this helped


4 0
4 years ago
Read 2 more answers
Other questions:
  • While the term ________ refers to an elaborate solo song frequently used in opera, the term ________ refers to sung dialogue fre
    12·1 answer
  • The small diagonal arrow in some command groups lower-right corner is a __________.
    10·1 answer
  • What the benefit is of folders when working with files
    13·2 answers
  • Which statement accurately describes the process for storing AutoRecovered files?
    7·2 answers
  • According to the video, what are some hazards Stationary Engineers may face? Check all that apply. noise, hazardous materials, d
    7·2 answers
  • Write a compound inequality that is represented by the graph.
    14·1 answer
  • Which of the following is false? Group of answer choices A) A string may include letters, digits, and various special characters
    7·1 answer
  • The word “Intrapersonal”means----------------------.Within the personSelf talkExtrovertOutgoing person
    10·2 answers
  • Discuss the core technologies and provide examples of where they exist in society. Discuss how the core technologies are part of
    14·1 answer
  • The USGS and National Weather Service have many sensors collecting data about natural events.
    8·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!