-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGroverIrusta1991.java
82 lines (68 loc) · 1.61 KB
/
GroverIrusta1991.java
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import java.util.Scanner;
//Author: Grover Irusta
//University: Universidad Privada Domingo Savio - Tarija
//Date: 24-07-16
public class PenneyGame1991 {
static char[] T, P;
static int n, m;
static int [] b;
//algormitmo KMP
static void kmpPreprocess()
{
int i = 0, j = -1; b[0] = -1;
while (i < m)
{
while (j >= 0 && P[i] != P[j]) j = b[j];
i++; j++;
b[i] = j;
}
}
static int kmpSearch()
{
int i = 0, j = 0,cont=0;
while (i < n) {
while (j >= 0 && T[i] != P[j]) j = b[j];
i++; j++;
if (j == m) {
cont++;
j = b[j];
}
}
return cont;
}
// FIN
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String[]secuencias ={"TTT", "TTH", "THT", "THH", "HTT", "HTH", "HHT" , "HHH"};
int [] vector;
int casos = in.nextInt();
int contador=0;
while (casos-->0)
{
vector= new int[8];
int num = in.nextInt();
String cad1 = in.next();
for (int i = 0; i < vector.length; i++)
{
String cad2 = secuencias[i];
T = cad1.toCharArray();
P = cad2.toCharArray();
n = T.length;
m = P.length;
b = new int[400001];
kmpPreprocess();
contador = kmpSearch();
vector[i]= contador;
}
System.out.print(num+" ");
for (int i = 0; i < vector.length; i++)
{
if (i+1==vector.length)
System.out.print(vector[i]);
else
System.out.print(vector[i]+" ");
}
System.out.println();
}
}
}