Download presentation
Presentation is loading. Please wait.
Published byBryce Newman Modified over 8 years ago
1
CS1020 SIT IN LAB 1 Pattern Matching
2
Problem Given a RxC letters matrix and a RpxCp pattern matrix,find how many ways to match the patterns and letters. Rp <= R <= 50 && Cp <= C <= 50 An asterisk “*” is used (in the pattern matrix) if any letter can be used for that cell.
3
Example Sample Input: 6 2 AAAXAA CCDXCX 2 2 AA C* Sample Output: 3
4
Algorithm Brute Force: Match the letters 1 character by 1 character. Letters: A A A X A A C C D X C X Patterns: A A C *
5
Algorithm Brute Force: Match the letters 1 character by 1 character. Letters: A A A X A A C C D X C X Patterns: A A C *
6
Algorithm Brute Force: Match the letters 1 character by 1 character. Letters: A A A X A A C C D X C X Patterns: A A C *
7
Algorithm Brute Force: Match the letters 1 character by 1 character. Letters: A A A X A A C C D X C X Patterns: A A C *
8
Algorithm Brute Force: Match the letters 1 character by 1 character. Letters: A A A X A A C C D X C X Patterns: A A C *
9
Sample Code (Pseudocode) While ( Checking each cell in letters matrix) { While (Matching each cell in patterns matrix) { if (Not all match) { break; } else { add one way; } } }
10
Java code (ANY_CHARACTER = ‘*’) int calculateNumberOfWays(int letterRow, int letterColumn, char[][] letterMatrix, int patternRow, int patternColumn, char[][] patternMatrix) { int ways = 0; for (int i = 0; i <= letterRow - patternRow; i++) { for (int j = 0; j <= letterColumn - patternColumn; j++) { boolean isMatched = true; for (int k = 0; k < patternRow; k++) { for (int l = 0; l < patternColumn; l++) { if (patternMatrix[k][l] == '*' || patternMatrix[k][l] == letterMatrix[i+k][j+l]) { continue; } else { isMatched = false; break; } } } if (isMatched) { ways++; } } } return ways; }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.