Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS1020E Sitin 1 Discussion -- Counting Palindromes.

Similar presentations


Presentation on theme: "CS1020E Sitin 1 Discussion -- Counting Palindromes."— Presentation transcript:

1 CS1020E Sitin 1 Discussion -- Counting Palindromes

2 2  given a R*C matrix with each cell consist of a single lowercase alphabet.  to count how many palindromes in the matrix, in horizontal, vertical and diagonal direction.  palindrome: a string which reads the same backward or forward  input: The first line will consist of two integers separated by single space representing R and C respectively. The next R lines consist of C characters representing the matrix  output: the number of palindromes in the matrix. Problem Description

3 3 assemble every substring in every row, column and diagonal, and send it to a common function to check whether it is a palindrome. If yes, add one to the total number of palindromes How to solve this problem

4 4 int main() { read input; solve(R,C,matrix); output; } int solve(int R, int C, string matrix[100]) { assemble substring in each row; for each substring, check palindrome; assemble substring in each column; for each substring, check palindrome; assemble substring in each diagonal; for each substring, check palindrome; } bool checkPalindrome(string str) { string reverse_str=str; std::reverse(reverse_str.begin(), reverse_str.end()); if(reverse_str==str) return true; return false; } Framework

5 5 for ( int i=0; i<R; i++ ) { //search-by-row iterator for ( int j=0; j<C; j++ ) { //starting column of search iterator for ( int k=1; k<=C-j; k++ ) { //length of search iterator test=matrix[i].substr( j, k ); if(checkPalindrome(test)) pal_num++;// if palindrome, pal_num++} } Details assemble substring in each row; for each substring, check palindrome: assemble substring in each column; for each substring, check palindrome: for ( int i=0; i<C; i++ ) { //search-by-column iterator for ( int j=0; j<R; j++ ) { //starting row of search iterator test=matrix[j][i];//load first character of column; check only begins with second character as single character are counted as horizontal for ( int k=j+1; k<R; k++ ) { //position of search test.push_back( matrix[k][i] ); if(checkPalindrome(test)) pal_num++;// if palindrome, pal_num++ }

6 6 for ( int i=0; i<R; i++ ) { //search-by-row iterator for ( int j=0; j<C; j++ ) { //starting column of search test=matrix[i][j]; for ( int k=1; k<R-i && k<C-j; k++ ) { //position of search test.push_back( matrix[i+k][j+k] ); if(checkPalindrome(test)) pal_num++;// if palindrome, pal_num++ } Details assemble substring in each diagonal; for each substring, check palindrome:

7 7 about algorithm: 1. cannot correctly loop for the starting/ending/center point of the text that they want to check 2. double counting. 3. cannot correctly construct the word 4. cannot correctly check whether the word is palindrome or not. 5. used low-level functions in C to implement rather than string class Common problems/errors:

8 8 about programming style: 1. did not write pre and post conditions 2. few comments for the tricky code snippets Common problems/errors:

9 9 1. checking on each row, column and diagonal instead of assembling a string and pass it to a common function to do the checking. -- can be correct, but not a good programming habit. Remarks:

10 END OF FILE


Download ppt "CS1020E Sitin 1 Discussion -- Counting Palindromes."

Similar presentations


Ads by Google