-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShiftOrP.java
More file actions
62 lines (59 loc) · 1.73 KB
/
Copy pathShiftOrP.java
File metadata and controls
62 lines (59 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
public class ShiftOrP {
public static void main(String[] args) {
String P = args[0].toLowerCase();
String T = args[1].toLowerCase();
int m = P.length();
int n = T.length();
int size = 26;// del alfabeto
int B[] = new int[size];// la tabla B representa las posiciones del patron y sus caracteres
int mask = 1;
// preprocessing
for (int i = 0; i < m; i++) {
int p = P.charAt(i) - 'a';
B[p] = (B[p] | mask);
mask = mask << 1;
}
for (int i = 0; i < size; i++) {
B[i] = ~B[i];
}
imprimeB(B);
// Searching
int D = ~0;
for (int i = 0; i < n; i++) {
System.out.println(i + 1 + ".- reading " + T.charAt(i));
System.out.println(format(D << 1));
System.out.println(format(B[T.charAt(i) - 'a']));
System.out.println("----------------");
D = (D << 1 | B[T.charAt(i) - 'a']);
System.out.println(format(D));
if ((D & 1 << (m - 1)) == 0)
System.out.println("ocurrance at " + (i - m + 1));
}
}
static void imprimeB(int[] A) {
for (int i = 0; i < A.length; i++) {
if (A[i] != 0)
System.out.println("B[" + (char) (i + 'a') + "] " + format(A[i]));
}
}
public static String format(int x) {
int y = 0;
if (x <= 255)
y = 1;
if (x <= 65535 && x > 255)
y = 2;
if (x > 65535)
y = 3;
String temp = Integer.toBinaryString(x);
String respuesta = "";
for (int i = 0; i < (y * 8); i++) {
if (i % 4 == 0 && i != 0)
respuesta = respuesta + " ";
if (i < (y * 8) - temp.length())
respuesta = respuesta + "0";
if (i >= (y * 8) - temp.length())
respuesta = respuesta + temp.charAt(i - ((y * 8) - temp.length()));
}
return respuesta;
}
}