|
Try to use the variable name i in both methods....
mkdir ~/cs170/hw7 cp ~cs170002/share/hw7/*.java ~/cs170/hw7 cd ~/cs170/hw7 |
|
The string s1 overlaps the string s2 in the following manners:
1. Frodo odometer - 0 overlapping character ("trivial" overlap) 2. Frodo odometer - 1 overlapping character 3. Frodo odometer - 3 overlapping characters |
|
The maximum overlap of s1 = "Frodo" on s2 = "odometer" is equal to 3
s1 can overlap s2 in the following manners:
1. Frodo od - 0 overlapping character 2. Frodo od - 1 overlapping character |
Therefore, the maximum overlap of "Frodo" and "od" is 1 (character)
They can overlap in the following manners:
1. Frodo do - 0 overlapping character 2. Frodo do - 2 overlapping character |
The maximum overlap of "Frodo" and "do" is 2 (characters)
|
Example:
maxOverlap("Frodo", "odometer") returns 3 maxOverlap("Frodo", "od") returns 1 maxOverlap("Frodo", "do") returns 2 |
How to find maximum overlap of s1 = "abcabcab" and s2 = "abcabc" 1) Try every possible position to align a suffic of s1 to a prefix of s2: 01234567 abcabcab abcabc 01234567 abcabcab abcabc 01234567 abcabcab abcabc 01234567 abcabcab abcabc 01234567 abcabcab abcabc 01234567 abcabcab abcabc 01234567 abcabcab abcabc (You don't really need to do this case, If you don't find any match so far, you know the maximum overlap is 0) |
|
s1.charAt(s1Start) | v s1 = "..................." ".............." ^ | s2.charAt(0) |
we can use this overlap(s1, s1Start, s2) method to find the maximum overlap easier
s1Start | V s1 = "................." s2 = "..............." |
The method overlap returns true if there is an overlap; otherwise, it returns false.
Examples:
s1 = "abcabcab"; s2 = "abcabc"; |
|
The TestOverlap.java contains a main() method that let you test the overlap(s1, s1Start, s2) before using it to solve the "find maximum overlap" problem.
public class TestOverlap { // Write the overlap() method here public static void main(String[] args) { String x1, x2; int i, j; x1 = "abcabcab"; x2 = "abcabc"; for ( i = 2; i < 8; i++ ) { System.out.println( "\n" + x1 ); for ( j = 0; j < i; j++ ) System.out.print( " " ); System.out.println( x2 ); System.out.print( "Overlap: " ); if ( overlap( x1, i, x2 ) ) System.out.println( " **** YES ! " ); else System.out.println( " no " ); } } } |
Note:
|
Compile and run the test.
abcabcab abcabc Overlap: no abcabcab abcabc Overlap: **** YES ! abcabcab abcabc Overlap: no abcabcab abcabc Overlap: no abcabcab abcabc Overlap: **** YES ! abcabcab abcabc Overlap: no |
1. Start matching s2 against s1 from the left and run to the end: First position | v s1 = "............" "........" <----- this is s2 Second position | v s1 = "............" "........" <----- this is s2 ... Last position | v s1 = "............" "........" <----- this is s2 What's the first position ? What's the last position ? (What kind of statement would you use to instruct the computer to perform this ???) Warning: The first position is computed differently when s1 is longer than s2 and s1 is shorter than s2 You must handle both cases (because my test program uses s1 that is longer and shorter than s2). |
|
public class Overlap { // Cut and paste your overlap() method definition here // Write the maxOverlap() method here public static void main(String[] args) { String x1, x2; x1 = "abcabcabc"; x2 = "bcabcxx"; System.out.println( x1 + " " + x2 + " = " + maxOverlap(x1, x2) ); x1 = "Frodo"; x2 = "odometer"; System.out.println( x1 + " " + x2 + " = " + maxOverlap(x1, x2) ); x1 = "Frodo"; x2 = "Odometer"; System.out.println( x1 + " " + x2 + " = " + maxOverlap(x1, x2) ); x1 = "Frodo"; x2 = "od"; System.out.println( x1 + " " + x2 + " = " + maxOverlap(x1, x2) ); x1 = "Frodo"; x2 = "do"; System.out.println( x1 + " " + x2 + " = " + maxOverlap(x1, x2) ); } } |
Note:
|
abcabcabc bcabcxx = 5 Frodo odometer = 3 Frodo Odometer = 0 Frodo od = 1 Frodo do = 2 |
/home/cs170002/turnin-hw Overlap.java hw7 /home/cs170002/turnin-hw TestOverlap.java hw7a |