cse220sol2

We denote t1 as top for the rst stack and t2 as top

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: We denote T1 as top for the rst stack and T2 as top for the second stack. The algorithm is like this: Init() { T1=-1; T2=n; } The following procedure checks for stack overflow: int isFull() { return (T2-T1==1); } Problem 2: Grade 5 pts You have a character string as input to your program. You need to nd out whether the string is of the form a C b. Here C is the letter C, a and b are sequences of As and Bs, and a and b must be mirror images of each other, e.g., a = ABAAB, b = BAABA. For full grade you must give an O(N) algorithm where N is the number of letters in the input string. Keep two variables T1 and T2. T1 is an index to the rst character of the string and T2 is an index to the last character of the string. Compare the characters that correspond to positions T1 and T2. If they do not match, then the input is not of the desired form. If they match, then you increment T1 and decrement T2 and follow the same rule. The algorithm terminates when both T1 and T2 nd character C. Since this scheme traverses the whole input string, is O ( n ). Problem 3: Grade 10 pts You have two sorted lists, L 1 ; L 2 : You know the lengths of each list, L 1 has length N 1 and L 2 has length N 2 : Design algorithms to output a sorted list L 1 \ L 2 : You need to give two algorithms. One should have complexity O ( N 1 + N 2...
View Full Document

{[ snackBarMessage ]}

Page1 / 3

We denote T1 as top for the rst stack and T2 as top for the...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online