import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
public class haz {
private static class IntegerList extends LinkedList<Long> {
}
public static void main(String[] args) throws IOException {
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
Integer n = Integer.parseInt(input.readLine());
Integer[] tab = new Integer[n];
String[] values = input.readLine().split(" ");
int i = 0;
for(String s : values) {
tab[i] = Integer.parseInt(s);
++i;
}
Integer m = Integer.parseInt(input.readLine());
String automat = input.readLine();
run(tab, n, automat, m);
}
private static void run(Integer[] tab, Integer n, String automat, Integer m) {
long nww;
if (n < m) {
nww = nww(n, m);
}
else {
nww = nww(m, n);
}
int p = 0;
int a = 0;
long x;
long minx = Long.MAX_VALUE;
long minr = Integer.MAX_VALUE;
int mini = 0;
boolean t = true;
long r;
IntegerList[] lost = new IntegerList[n];
long[] delta = new long[n];
for(int i = 0; i < nww; ++i) {
if(p >= n) {
p -= n;
}
if(a >= m) {
a -= m;
}
if(automat.charAt(a) == 'P') {
--tab[p];
if (tab[p] == 0) {
System.out.println(i + 1);
return;
}
if (lost[p] == null) {
lost[p] = new IntegerList();
}
lost[p].add((long) (i+1));
--delta[p];
}
else {
++tab[p];
++delta[p];
}
++a;
++p;
}
for(int i = 0; i < n; ++i) {
if (delta[i] < 0) {
t = false;
x = -tab[i]/delta[i];
r = tab[i] + x * delta[i];
if (x < minx) {
minx = x;
mini = i;
minr = r;
}
else if (x == minx) {
if (r < minr) {
minr = r;
mini = i;
}
}
}
}
if (t) {
System.out.println(-1);
}
else {
long result = minx * nww + lost[mini].get((int) minr);
System.out.println(result);
}
}
private static long nww(Integer a, Integer b) {
long res = a * b;
int c;
while (b != 0) {
c = a % b;
a = b;
b = c;
}
return res/a;
}
}
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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 | import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.LinkedList; public class haz { private static class IntegerList extends LinkedList<Long> { } public static void main(String[] args) throws IOException { BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); Integer n = Integer.parseInt(input.readLine()); Integer[] tab = new Integer[n]; String[] values = input.readLine().split(" "); int i = 0; for(String s : values) { tab[i] = Integer.parseInt(s); ++i; } Integer m = Integer.parseInt(input.readLine()); String automat = input.readLine(); run(tab, n, automat, m); } private static void run(Integer[] tab, Integer n, String automat, Integer m) { long nww; if (n < m) { nww = nww(n, m); } else { nww = nww(m, n); } int p = 0; int a = 0; long x; long minx = Long.MAX_VALUE; long minr = Integer.MAX_VALUE; int mini = 0; boolean t = true; long r; IntegerList[] lost = new IntegerList[n]; long[] delta = new long[n]; for(int i = 0; i < nww; ++i) { if(p >= n) { p -= n; } if(a >= m) { a -= m; } if(automat.charAt(a) == 'P') { --tab[p]; if (tab[p] == 0) { System.out.println(i + 1); return; } if (lost[p] == null) { lost[p] = new IntegerList(); } lost[p].add((long) (i+1)); --delta[p]; } else { ++tab[p]; ++delta[p]; } ++a; ++p; } for(int i = 0; i < n; ++i) { if (delta[i] < 0) { t = false; x = -tab[i]/delta[i]; r = tab[i] + x * delta[i]; if (x < minx) { minx = x; mini = i; minr = r; } else if (x == minx) { if (r < minr) { minr = r; mini = i; } } } } if (t) { System.out.println(-1); } else { long result = minx * nww + lost[mini].get((int) minr); System.out.println(result); } } private static long nww(Integer a, Integer b) { long res = a * b; int c; while (b != 0) { c = a % b; a = b; b = c; } return res/a; } } |
English