Download code

Jump to: navigation, search

Back to Matching_with_allowed_mismatch_(Java)

Download for Windows: zip

Download for UNIX: zip, tar.gz, tar.bz2

MatchingWithAllowedMismatch.java

 1 /* The authors of this work have released all rights to it and placed it
 2 in the public domain under the Creative Commons CC0 1.0 waiver
 3 (http://creativecommons.org/publicdomain/zero/1.0/).
 4 
 5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 
13 Retrieved from: http://en.literateprograms.org/Matching_with_allowed_mismatch_(Java)?oldid=12042
14 */
15 
16 
17 import static org.junit.Assert.assertEquals;
18 import org.junit.Test;
19 
20 import java.util.ArrayList;
21 import java.util.Arrays;
22 import java.util.Collection;
23 
24 public class MatchingWithAllowedMismatch {
25 	public static Collection<String> getMatches(String t, String p, int k) {
26 		Collection<String> result = new ArrayList<String>();
27 		for (int i = 0; i < t.length(); i++) {
28 			
29 			int j = 0;
30 			int h = i;
31 			int count = 0;
32 			int n = p.length();
33 
34 			while (true) {
35 				
36 				int L = SimpleLongestCommonExtension.longestCommonExtension(p, j, t, h);
37 				
38 				if (j + 1 + L == n + 1) {
39 				    result.add(t.substring(i, i + n));
40 				    break;
41 				}
42 				
43 				else if (count < k) {
44 				    count++;
45 				    j = j + L + 1;
46 				    h = h + L + 1;
47 				} else if (count == k) {
48 				    break;
49 				}
50 			}
51 		}
52 		return result;
53 	}
54 	@Test
55 	public void testGetMismatches() {
56 		
57 		Collection<String> results = getMatches("abentbananaend", "bend", 2);
58 		assertEquals(Arrays.asList("bent", "bana", "aend"), results);
59 	}
60 }


hijacker
hijacker
hijacker
hijacker

SimpleLongestCommonExtension.java

 1 /* The authors of this work have released all rights to it and placed it
 2 in the public domain under the Creative Commons CC0 1.0 waiver
 3 (http://creativecommons.org/publicdomain/zero/1.0/).
 4 
 5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 
13 Retrieved from: http://en.literateprograms.org/Matching_with_allowed_mismatch_(Java)?oldid=12042
14 */
15 
16 
17 import static org.junit.Assert.assertEquals;
18 import org.junit.Test;
19 
20 public class SimpleLongestCommonExtension {
21 	
22 	public static int longestCommonExtension(String t1, int i1, String t2, int i2) {
23 	    int res = 0;
24 	    for (int i = i1; i < t1.length() && i2 < t2.length(); i++, i2++) {
25 	        if (t1.charAt(i) == t2.charAt(i2))
26 	            res++;
27 	        else
28 	            return res;
29 	    }
30 	    return res;
31 	}
32 
33 	
34 	@Test
35 	public void testGetLongestCommonExtension() {
36 	    int res = longestCommonExtension("zsdabcdefghj", 3, "abcdezas", 0);
37 	    assertEquals(5, res);
38 	}
39 }


hijacker
hijacker
hijacker
hijacker