exec_outcome
stringclasses 1
value | code_uid
stringlengths 32
32
| file_name
stringclasses 111
values | prob_desc_created_at
stringlengths 10
10
| prob_desc_description
stringlengths 63
3.8k
| prob_desc_memory_limit
stringclasses 18
values | source_code
stringlengths 117
65.5k
| lang_cluster
stringclasses 1
value | prob_desc_sample_inputs
stringlengths 2
802
| prob_desc_time_limit
stringclasses 27
values | prob_desc_sample_outputs
stringlengths 2
796
| prob_desc_notes
stringlengths 4
3k
⌀ | lang
stringclasses 5
values | prob_desc_input_from
stringclasses 3
values | tags
sequencelengths 0
11
| src_uid
stringlengths 32
32
| prob_desc_input_spec
stringlengths 28
2.37k
⌀ | difficulty
int64 -1
3.5k
⌀ | prob_desc_output_spec
stringlengths 17
1.47k
⌀ | prob_desc_output_to
stringclasses 3
values | hidden_unit_tests
stringclasses 1
value |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
PASSED | c88cf5a50187156c6dd0e87645ff6ba3 | train_110.jsonl | 1638110100 | William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above. | 256 megabytes | import java.util.Scanner;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.util.Arrays;
public class Cv {
//==========================Solution============================//
public static void main(String[] args) {
FastScanner in = new FastScanner();
Scanner input = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
int t = input.nextInt();
while (t-- > 0) {
int n = input.nextInt();
int[] arr = new int[n];
int u = 0;
int max = 0;
int sum = 0;
for (int i = 0; i < n; i++) {
arr[i] = input.nextInt();
while (arr[i] % 2 == 0) {
arr[i]/=2;
u++;
}
}
for (int i = 0; i < n; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
for (int i = 0; i < n; i++) {
sum+=arr[i];
}
out.println((sum - max) + (max * (long)(Math.pow(2, u))));
}
out.close();
}
//==============================================================//
static class FastScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
public String next() {
while (!st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
public String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
byte nextByte() {
return Byte.parseByte(next());
}
short nextShort() {
return Short.parseShort(next());
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return java.lang.Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
}
} | Java | ["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"] | 1 second | ["50\n46\n10\n26\n35184372088846"] | NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$. | Java 8 | standard input | [
"greedy",
"implementation",
"math",
"number theory"
] | f5de1e9b059bddf8f8dd46c18ce12683 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i < 16)$$$, the contents of William's array. | 900 | For each test case output the maximal sum of array elements after performing an optimal sequence of operations. | standard output | |
PASSED | 200ad313ee337c4eaa5d1f13f2a35324 | train_110.jsonl | 1638110100 | William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above. | 256 megabytes |
import java.util.*;
import java.lang.*;
import java.io.*;
import static java.lang.Math.*;
import static java.lang.System.out;
public class Main {
static class Reader{
BufferedReader br;
StringTokenizer st;
public Reader(boolean f) throws IOException{
if(f) {
br = new BufferedReader(new FileReader("input.txt"));
}else{
br=new BufferedReader(new InputStreamReader(System.in));
}
}
String next(){
while(st==null || !st.hasMoreTokens()){
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt(){
return Integer.parseInt(next());
}
long nextLong(){
return Long.parseLong(next());
}
double nextDouble(){
return Double.parseDouble(next());
}
String nextLine(){
String str="";
try {
str=br.readLine().trim();
} catch (Exception e) {
e.printStackTrace();
}
return str;
}
}
static class Writer {
private final BufferedWriter bw;
public Writer(boolean f) throws IOException {
if(f) {
this.bw = new BufferedWriter(new FileWriter("output.txt"));
}else{
this.bw = new BufferedWriter(new OutputStreamWriter(out));
}
}
public void print(Object object) throws IOException {
bw.append("" + object);
}
public void println(Object object) throws IOException {
print(object);
bw.append("\n");
}
public void close() throws IOException {
bw.close();
}
}
public static void main(String[] args){
try {
Reader in=new Reader(false);
Writer out =new Writer(false);
int t = 1;
t=in.nextInt();
while(t-- > 0){
// write code here
int n = in.nextInt();
Long[] arr = new Long[n];
long count = 0;
for (int i = 0; i < n; i++) {
long a = in.nextLong();
arr[i]=a;
while(arr[i]%2==0){
arr[i]/=2;
count++;
}
}
Arrays.sort(arr);
long max = arr[arr.length-1];
long sum = (long) (max*pow(2,count));
for (int i = 0; i < n - 1; i++) {
sum +=arr[i];
}
out.println(sum);
}
out.close();
} catch (Exception e) {
return;
}
}
} | Java | ["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"] | 1 second | ["50\n46\n10\n26\n35184372088846"] | NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$. | Java 8 | standard input | [
"greedy",
"implementation",
"math",
"number theory"
] | f5de1e9b059bddf8f8dd46c18ce12683 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i < 16)$$$, the contents of William's array. | 900 | For each test case output the maximal sum of array elements after performing an optimal sequence of operations. | standard output | |
PASSED | 893db2e02c9f041cb89bc50fb2960e5e | train_110.jsonl | 1638110100 | William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above. | 256 megabytes | import java.util.Scanner;
public class A1609 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int T = in.nextInt();
for (int t=0; t<T; t++) {
int N = in.nextInt();
int[] A = new int[N];
long power = 1;
int maxIdx = -1;
int max = 0;
for (int n=0; n<N; n++) {
int a = in.nextInt();
while (a%2 == 0) {
a /= 2;
power *= 2;
}
A[n] = a;
if (a > max) {
max = a;
maxIdx = n;
}
}
long answer = 0;
for (int n=0; n<N; n++) {
if (n == maxIdx) {
answer += A[n]*power;
} else {
answer += A[n];
}
}
System.out.println(answer);
}
}
}
| Java | ["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"] | 1 second | ["50\n46\n10\n26\n35184372088846"] | NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$. | Java 8 | standard input | [
"greedy",
"implementation",
"math",
"number theory"
] | f5de1e9b059bddf8f8dd46c18ce12683 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i < 16)$$$, the contents of William's array. | 900 | For each test case output the maximal sum of array elements after performing an optimal sequence of operations. | standard output | |
PASSED | fa271af5d739ce42fa6487a64d49624a | train_110.jsonl | 1638110100 | William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above. | 256 megabytes |
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Scanner;
public class Main {
public static void main(String[] args){ new Main().run(); }
int N = 20;
int a[] =new int[N];
void run()
{
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t>0)
{
int n =sc.nextInt();
int num = 0;
for(int i=0;i<n;i++)
{
int tmp =sc.nextInt();
while(tmp%2==0)
{
num++;
tmp/=2;
}
a[i] = tmp;
}
Arrays.sort(a,0,n);
long ans = 0;
for(int i=0;i<n-1;i++)
ans += a[i];
ans += (long)Math.pow(2,num)*a[n-1];
System.out.println(ans);
t--;
}
}
} | Java | ["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"] | 1 second | ["50\n46\n10\n26\n35184372088846"] | NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$. | Java 8 | standard input | [
"greedy",
"implementation",
"math",
"number theory"
] | f5de1e9b059bddf8f8dd46c18ce12683 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i < 16)$$$, the contents of William's array. | 900 | For each test case output the maximal sum of array elements after performing an optimal sequence of operations. | standard output | |
PASSED | aa7aeff2da262e93c54d00bfb417597e | train_110.jsonl | 1638110100 | William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above. | 256 megabytes | import java.io.*;
import java.util.*;
public class A_Divide_and_Multiply {
public static void main(String[] args) {
MyScanner sc = new MyScanner();
out = new PrintWriter(new BufferedOutputStream(System.out));
int t = sc.nextInt();
while (t-- > 0) {
long n = sc.nextLong(), arr[] = sc.readLongArray((int) n);
int count = 0;
for (int i = 0; i < arr.length; i++) {
while (arr[i] % 2 == 0) {
count++;
arr[i] /= 2;
}
}
sort(arr);
while (count-- > 0)
arr[arr.length - 1] *= 2;
long sum = 0;
for (long i : arr)
sum += i;
out.println(sum);
}
out.close();
}
public static PrintWriter out;
public static long mod = (long) 1e9 + 7;
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
int[] readArray(int n) {
int a[] = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
long[] readLongArray(int n) {
long a[] = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
static void SieveOfEratosthenes(int n, boolean prime[]) {
prime[0] = false;
prime[1] = false;
for (int p = 2; p * p <= n; p++) {
if (prime[p] == true)
for (int i = p * p; i <= n; i += p)
prime[i] = false;
}
}
static void dfs(int root, boolean[] vis, int[] value, ArrayList[] gr, int prev) {
vis[root] = true;
value[root] = 3 - prev;
prev = 3 - prev;
for (int i = 0; i < gr[root].size(); i++) {
int next = (int) gr[root].get(i);
if (!vis[next])
dfs(next, vis, value, gr, prev);
}
}
static boolean isPrime(int n) {
for (int i = 2; i * i <= n; i++)
if (n % i == 0)
return false;
return true;
}
static int abs(int a) {
return a > 0 ? a : -a;
}
static int max(int a, int b) {
return a > b ? a : b;
}
static int min(int a, int b) {
return a < b ? a : b;
}
static long pow(long n, long m) {
if (m == 0)
return 1;
long temp = pow(n, m / 2);
long res = ((temp * temp) % mod);
if (m % 2 == 0)
return res;
return (res * n) % mod;
}
static long modular_add(long a, long b) {
return ((a % mod) + (b % mod)) % mod;
}
static long modular_sub(long a, long b) {
return ((a % mod) - (b % mod) + mod) % mod;
}
static long modular_mult(long a, long b) {
return ((a % mod) * (b % mod)) % mod;
}
static long lcm(int a, int b) {
return (a / gcd(a, b)) * b;
}
static long gcd(long a, long b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
static class Pair {
int u, v;
Pair(int u, int v) {
this.u = u;
this.v = v;
}
static void sort(Pair[] coll) {
List<Pair> al = new ArrayList<>(Arrays.asList(coll));
Collections.sort(al, new Comparator<Pair>() {
public int compare(Pair p1, Pair p2) {
return p1.u - p2.u;
}
});
for (int i = 0; i < al.size(); i++) {
coll[i] = al.get(i);
}
}
}
static void sort(int[] a) {
ArrayList<Integer> list = new ArrayList<>();
for (int i : a)
list.add(i);
Collections.sort(list);
for (int i = 0; i < a.length; i++)
a[i] = list.get(i);
}
static void sort(long a[]) {
ArrayList<Long> list = new ArrayList<>();
for (long i : a)
list.add(i);
Collections.sort(list);
for (int i = 0; i < a.length; i++)
a[i] = list.get(i);
}
static int[] array(int n, int value) {
int a[] = new int[n];
for (int i = 0; i < n; i++)
a[i] = value;
return a;
}
} | Java | ["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"] | 1 second | ["50\n46\n10\n26\n35184372088846"] | NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$. | Java 8 | standard input | [
"greedy",
"implementation",
"math",
"number theory"
] | f5de1e9b059bddf8f8dd46c18ce12683 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i < 16)$$$, the contents of William's array. | 900 | For each test case output the maximal sum of array elements after performing an optimal sequence of operations. | standard output | |
PASSED | f1d255efa508d434035f2dfca447825b | train_110.jsonl | 1638110100 | William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above. | 256 megabytes |
import java.util.Arrays;
import java.util.Scanner;
public class DivideAndMultiply {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int test = sc.nextInt();
while (test-- > 0) {
int n = sc.nextInt();
int[] nums = new int[n];
for (int i = 0; i < n; i++) {
nums[i] = sc.nextInt();
}
System.out.println(findMaxSum(nums, n));
}
}
private static long findMaxSum(int[] nums, int n) {
long sum = 0;
if (n == 1) {
return nums[0];
}
long[] copy = new long[n];
int countOfDivision = 0;
for (int i = 0; i < n; i++) {
copy[i] = nums[i];
while (copy[i] % 2 == 0) {
copy[i] /= 2;
countOfDivision++;
}
}
Arrays.sort(copy);
long pow = (long) Math.pow(2, countOfDivision) * copy[n - 1];
copy[n - 1] = pow;
for (long value : copy) {
sum += value;
}
return sum;
}
}
| Java | ["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"] | 1 second | ["50\n46\n10\n26\n35184372088846"] | NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$. | Java 8 | standard input | [
"greedy",
"implementation",
"math",
"number theory"
] | f5de1e9b059bddf8f8dd46c18ce12683 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i < 16)$$$, the contents of William's array. | 900 | For each test case output the maximal sum of array elements after performing an optimal sequence of operations. | standard output | |
PASSED | 782172c08617c754e65216bed17e7c7a | train_110.jsonl | 1638110100 | William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above. | 256 megabytes | import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int t = scn.nextInt();
for (int k = 0; k < t; k++) {
int n = scn.nextInt();
long[] arr = new long[n];
for (int i = 0; i < n; i++) {
arr[i] = scn.nextLong();
}
long mult = 1;
for (int i = 0; i < n; i++) {
while(arr[i] %2 == 0){
mult *= 2;
arr[i] /= 2;
}
}
Arrays.sort(arr);
arr[n - 1] *= mult;
long ans = 0;
for (long ele : arr) {
ans = ans + ele;
}
System.out.println(ans);
}
}
} | Java | ["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"] | 1 second | ["50\n46\n10\n26\n35184372088846"] | NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$. | Java 8 | standard input | [
"greedy",
"implementation",
"math",
"number theory"
] | f5de1e9b059bddf8f8dd46c18ce12683 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i < 16)$$$, the contents of William's array. | 900 | For each test case output the maximal sum of array elements after performing an optimal sequence of operations. | standard output | |
PASSED | b026e4ed51c12e2f0d1d6c44c437854a | train_110.jsonl | 1638110100 | William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;
public class test {
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(System.out);
int t = Integer.parseInt(br.readLine());
while (t-- > 0) {
int n = Integer.parseInt(br.readLine());
StringTokenizer tokenizer = new StringTokenizer(br.readLine());
int[] a = new int[n];
int k = 0;
int max = Integer.MIN_VALUE;
for (int i = 0; i < n; i++) {
a[i] = Integer.parseInt(tokenizer.nextToken());
while (a[i] % 2 == 0) {
k++;
a[i] = a[i] / 2;
}
if (max < a[i])
max = a[i];
}
long sum = 0l;
for (int i = 0; i < n; i++) {
sum += a[i];
}
sum -= max;
sum += Math.pow(2, k) * max;
pw.println(sum);
}
pw.flush();
pw.close();
br.close();
}
} | Java | ["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"] | 1 second | ["50\n46\n10\n26\n35184372088846"] | NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$. | Java 8 | standard input | [
"greedy",
"implementation",
"math",
"number theory"
] | f5de1e9b059bddf8f8dd46c18ce12683 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i < 16)$$$, the contents of William's array. | 900 | For each test case output the maximal sum of array elements after performing an optimal sequence of operations. | standard output | |
PASSED | b8a345dd9d106a0981d01f03f9af1578 | train_110.jsonl | 1638110100 | William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above. | 256 megabytes | import java.io.*;
import java.util.*;
//import javafx.util.*;
public class Main
{
static FastReader in = new FastReader();
public static void main(String args[])throws IOException
{
/*
* star,rope,TPST
* BS,LST,MS,MQ
*/
int t = i();
while(t-- > 0){
int n = i();
long[] arr = inputLong(n);
if(n == 1){
System.out.println(arr[0]);
continue;
}
Arrays.sort(arr);
for(int i = 0;i < n - 1;i++){
if(arr[i]%2 == 0){
while(arr[i]%2 == 0){
arr[n-1] *= 2;
arr[i] /= 2;
}
}
}
long sum1 = 0;
for(int i = 0;i < n;i++){
sum1 += arr[i];
}
long sum2 = 0;
Arrays.sort(arr);
while(arr[n-1]%2 == 0){
arr[n-2] *= 2;
arr[n-1] /= 2;
}
for(int i = 0;i < n;i++){
sum2 += arr[i];
}
long ans = Math.max(sum1,sum2);
System.out.println(ans);
}
}
static int i()
{
return in.nextInt();
}
static long l()
{
return in.nextLong();
}
static int[] input(int N){
int A[]=new int[N];
for(int i=0; i<N; i++)
{
A[i]=in.nextInt();
}
return A;
}
static long[] inputLong(int N) {
long A[]=new long[N];
for(int i=0; i<A.length; i++)A[i]=in.nextLong();
return A;
}
}
class Pair{
int x;
int y;
Pair(int x, int y){
this.x = x;
this.y = y;
}
}
class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br=new BufferedReader(new InputStreamReader(System.in));
}
String next()
{
while(st==null || !st.hasMoreElements())
{
try
{
st=new StringTokenizer(br.readLine());
}
catch(IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str="";
try
{
str=br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
} | Java | ["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"] | 1 second | ["50\n46\n10\n26\n35184372088846"] | NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$. | Java 8 | standard input | [
"greedy",
"implementation",
"math",
"number theory"
] | f5de1e9b059bddf8f8dd46c18ce12683 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i < 16)$$$, the contents of William's array. | 900 | For each test case output the maximal sum of array elements after performing an optimal sequence of operations. | standard output | |
PASSED | 412078c6244f5a69939ce6e20793a207 | train_110.jsonl | 1638110100 | William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above. | 256 megabytes | import java.util.*;
public class Stc {
static Scanner scan = new Scanner(System.in);
public static void main(String[] args) {
int n = scan.nextInt();
while (n-- > 0)
A();
}
public static void A(){
int n = scan.nextInt();
List<Integer> list = new ArrayList<>();
for (int i = 1; i < 5; i++) {
list.add((int) Math.pow(2,5));
}
int arr[] = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = scan.nextInt();
}
int times = 1;
for (int i = 0; i < n; i++) {
if(list.contains(arr[i]))continue;
while (arr[i]%2==0){
arr[i]/=2;
times++;
}
}
long max = 0;
Arrays.sort(arr);
long res = arr[n-1] * ((long)Math.pow(2,times-1));
for(int l : arr)if(l!=max)res+=l;
System.out.println(res-arr[n-1]);
}
} | Java | ["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"] | 1 second | ["50\n46\n10\n26\n35184372088846"] | NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$. | Java 8 | standard input | [
"greedy",
"implementation",
"math",
"number theory"
] | f5de1e9b059bddf8f8dd46c18ce12683 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i < 16)$$$, the contents of William's array. | 900 | For each test case output the maximal sum of array elements after performing an optimal sequence of operations. | standard output | |
PASSED | 4cbfa1ef9f70cd6f91bebf67e9e3c900 | train_110.jsonl | 1638110100 | William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above. | 256 megabytes | import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
Scanner in = new Scanner(System.in);
int test = in.nextInt();
for(int ii = 1;ii<=test;ii++) {
long n = in.nextLong(),c = 0;
Long a[] = new Long[(int) n];
Arrays.setAll(a, i->in.nextLong());
for(long i = 0;i<n;i++) {
while(a[(int) i]%2==0) {
c++;
a[(int) i]/=2;
}
}
Arrays.sort(a);
long sum =(long) (a[(int) (n-1)]*(Math.pow(2, c)));
for(int i = 0;i<n-1;i++) {
sum+=a[i];
}
System.out.println(sum);
}
}
} | Java | ["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"] | 1 second | ["50\n46\n10\n26\n35184372088846"] | NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$. | Java 8 | standard input | [
"greedy",
"implementation",
"math",
"number theory"
] | f5de1e9b059bddf8f8dd46c18ce12683 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i < 16)$$$, the contents of William's array. | 900 | For each test case output the maximal sum of array elements after performing an optimal sequence of operations. | standard output | |
PASSED | c335b4c94d04a6bd5be471f42aab9e3b | train_110.jsonl | 1638110100 | William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above. | 256 megabytes | import java.util.*;
import java.io.*;
import static java.lang.System.*;
import static java.lang.Math.max;
import static java.lang.Math.min;
import static java.lang.Math.abs;
public class pre1{
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static void main(String args[]){
FastReader obj = new FastReader();
int tc = obj.nextInt();
while(tc--!=0){
int n = obj.nextInt();
long arr[] = new long[n];
for(int i=0;i<n;i++) arr[i] = obj.nextLong();
int count = 0;
for(int i=0;i<n;i++){
while(arr[i]%2==0){
count++;
arr[i] /= 2;
}
}
Arrays.sort(arr);
long sum = arr[n-1] * 1l<<count;
for(int i=n-2;i>=0;i--) sum+=arr[i];
out.println(sum);
}
}
}
| Java | ["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"] | 1 second | ["50\n46\n10\n26\n35184372088846"] | NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$. | Java 8 | standard input | [
"greedy",
"implementation",
"math",
"number theory"
] | f5de1e9b059bddf8f8dd46c18ce12683 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i < 16)$$$, the contents of William's array. | 900 | For each test case output the maximal sum of array elements after performing an optimal sequence of operations. | standard output | |
PASSED | 172f18af3cb926ddcc92fa1940ab422f | train_110.jsonl | 1638110100 | William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above. | 256 megabytes | import java.util.Scanner;
public class Prueba {
public static void main(String[] args) {
int casos, n;
Scanner sc = new Scanner(System.in);
casos = sc.nextInt();
for(int i=0; i<casos; i++) {
n = sc.nextInt();
long[] numeros = new long[n];
for(int j = 0; j<n; j++) {
numeros[j] = sc.nextInt();
}
int exponente = 0;
while(tienePar(numeros)) {
for(int j = 0; j<n; j++) {
if(numeros[j]%2==0) {
numeros[j] = numeros[j]/2;
exponente++;
}
}
}
long mayor = 0;
int posicion = 0;
for(int j=0; j<n; j++) {
if(numeros[j]>mayor) {
mayor = numeros[j];
posicion = j;
}
}
double numeroFinal = mayor*(Math.pow(2, exponente));
numeros[posicion] = (long) numeroFinal;
long respuesta = 0;
for(int j=0; j<n; j++) {
respuesta+= numeros[j];
}
System.out.println(respuesta);
}
}
public static boolean tienePar(long[] numeros) {
boolean par = false;
for(int i=0; i<numeros.length; i++) {
if(numeros[i]%2==0) {
par = true;
}
}
return par;
}
}
| Java | ["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"] | 1 second | ["50\n46\n10\n26\n35184372088846"] | NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$. | Java 8 | standard input | [
"greedy",
"implementation",
"math",
"number theory"
] | f5de1e9b059bddf8f8dd46c18ce12683 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i < 16)$$$, the contents of William's array. | 900 | For each test case output the maximal sum of array elements after performing an optimal sequence of operations. | standard output | |
PASSED | 16fe8b6839adcfe304d218c1128727a2 | train_110.jsonl | 1638110100 | William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above. | 256 megabytes | import java.util.Scanner;
import java.util.ArrayList;
public class Main {
public static Scanner reader=new Scanner(System.in);
public static void main (String[]args) {
ArrayList<Long> numbers=new ArrayList<Long>();
int counter=0;
int t=0;
int casesNum=0;
casesNum=reader.nextInt();
reader.nextLine();
long max=0;
int maxPos=-1;
long sum=0;
for (int y =0; y<casesNum ;y++) {
numbers.clear();
t=reader.nextInt();
counter=0;
maxPos=0;
max=0;
for (int i=0; i<t ;i++) {
numbers.add(reader.nextLong());
}
for (int i=0;i<numbers.size();i++) {
if (numbers.get(i)%2==0) {
while (numbers.get(i)%2==0){
numbers.set(i,numbers.get(i)/2);
counter++;
}
}
}
for (int i=0;i<numbers.size();i++) {
if(numbers.get(i)%2!=0) {
if (numbers.get(i)>max) {
max=numbers.get(i);
maxPos=i;
}
}
}
for (int i=0; i<counter;i++) {
numbers.set(maxPos, numbers.get(maxPos)*2);
}
sum=0;
for (int i=0; i<numbers.size();i++) {
sum+=numbers.get(i);
}
System.out.println(sum);
}
}
}
| Java | ["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"] | 1 second | ["50\n46\n10\n26\n35184372088846"] | NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$. | Java 8 | standard input | [
"greedy",
"implementation",
"math",
"number theory"
] | f5de1e9b059bddf8f8dd46c18ce12683 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i < 16)$$$, the contents of William's array. | 900 | For each test case output the maximal sum of array elements after performing an optimal sequence of operations. | standard output | |
PASSED | 25ab08a6cb3b646e94946a245e19e6c6 | train_110.jsonl | 1638110100 | William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above. | 256 megabytes |
import java.util.ArrayList;
import java.util.Scanner;
import java.lang.Math;
public class MyClass{
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int casos = sc.nextInt();
ArrayList<Long> input = new ArrayList<>();
int t, c, o;
long sum , k;
for(int h= casos; h>0; h--) {
t = sc.nextInt();
for(int i = t; i> 0; i--){
input.add(sc.nextLong());
}
c= 0;
for (int i = 0; i< input.size();i++) {
while(input.get(i)%2 == 0) {
input.set(i,input.get(i)/2);
c++;
}
}
k = input.get(0);
o = 0;
for(int i = 1; i<input.size(); i++) {
if (input.get(i)>k) {
k = input.get(i);
o = i;
}
}
input.set(o, k * (long) (Math.pow(2, c)));
sum = 0;
for(int i = 0; i < input.size(); i++) {
sum += input.get(i);
}
System.out.println(sum);
t = 0;
c = 0;
o = 0;
k =0;
sum = 0;
input.clear();
}
}
} | Java | ["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"] | 1 second | ["50\n46\n10\n26\n35184372088846"] | NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$. | Java 8 | standard input | [
"greedy",
"implementation",
"math",
"number theory"
] | f5de1e9b059bddf8f8dd46c18ce12683 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i < 16)$$$, the contents of William's array. | 900 | For each test case output the maximal sum of array elements after performing an optimal sequence of operations. | standard output | |
PASSED | 98a331dba90e60eb56a827a3b579e097 | train_110.jsonl | 1638110100 | William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above. | 256 megabytes | import java.util.Scanner;
public class Maximizar_suma{
public static void main(String args[]) {
Scanner reader=new Scanner(System.in);
int nTimes=0;
nTimes=reader.nextInt();
for(int h=0;h<nTimes;h++){
int t=reader.nextInt();
long[] numArray=new long[t];
int carryCounter=0;
int maxOddPosition=0;
for(int i=0;i<t;i++)
{
numArray[i]=reader.nextInt();
while(numArray[i] % 2==0)
{
numArray[i]/=2;
carryCounter++;
}
if(numArray[maxOddPosition]<numArray[i])
{
maxOddPosition=i;
}
}
numArray[maxOddPosition]*=Math.pow(2,carryCounter);
long total=0;
for(int i=0;i<t;i++)
{
total+=numArray[i];
}
System.out.println(total);
}
}
} | Java | ["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"] | 1 second | ["50\n46\n10\n26\n35184372088846"] | NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$. | Java 8 | standard input | [
"greedy",
"implementation",
"math",
"number theory"
] | f5de1e9b059bddf8f8dd46c18ce12683 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i < 16)$$$, the contents of William's array. | 900 | For each test case output the maximal sum of array elements after performing an optimal sequence of operations. | standard output | |
PASSED | 2eec0a8064f842c83030d467edfde89b | train_110.jsonl | 1638110100 | William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above. | 256 megabytes | import java.util.Scanner;
public class Main {
private static Scanner in = new Scanner(System.in);
public static void main(String[] args) {
int t = Integer.parseInt(in.nextLine());
for (int i = 0; i < t; i++) {
int n = Integer.parseInt(in.nextLine());
String array = in.nextLine();
String[] parts = array.split(" ");
int[] arrayInt = new int[parts.length];
int pairsCounter = 0;
int notPair = 0;
int indexMaxOdd = 0;
for (int j = 0; j < parts.length; j++) {
arrayInt[j] = Integer.parseInt(parts[j]);
while(arrayInt[j] %2 == 0) {
pairsCounter++;
arrayInt[j] /= 2;
}
}
notPair = arrayInt[0];
for (int j = 0; j < arrayInt.length; j++) {
if(notPair < arrayInt[j]) {
notPair = arrayInt[j];
indexMaxOdd = j;
}
}
long maximumSum = (long)(notPair * Math.pow(2, pairsCounter));
for (int j = 0; j < arrayInt.length; j++) {
if(j != indexMaxOdd) {
maximumSum += arrayInt[j];
}
}
System.out.println(maximumSum);
}
}
}
| Java | ["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"] | 1 second | ["50\n46\n10\n26\n35184372088846"] | NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$. | Java 8 | standard input | [
"greedy",
"implementation",
"math",
"number theory"
] | f5de1e9b059bddf8f8dd46c18ce12683 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i < 16)$$$, the contents of William's array. | 900 | For each test case output the maximal sum of array elements after performing an optimal sequence of operations. | standard output | |
PASSED | 97de7519b83f896e5a5048be577d7d44 | train_110.jsonl | 1638110100 | William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above. | 256 megabytes | //import com.sun.security.jgss.GSSUtil;
import java.util.Arrays;
import java.util.Scanner;
public class forces {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int t = scanner.nextInt();
while (t-- > 0) {
int n = scanner.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = scanner.nextInt();
Arrays.sort(a);
int mul = 0;
for (int i=0;i<n;i++){
while (a[i]%2==0){
a[i]/=2;
mul++;
}
}
Arrays.sort(a);
long sum = 0;
for (int i=0;i<n-1;i++)
sum+=a[i];
sum+= (long) Math.pow(2,mul)*a[n-1];
System.out.println(sum);
}
}
}
| Java | ["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"] | 1 second | ["50\n46\n10\n26\n35184372088846"] | NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$. | Java 8 | standard input | [
"greedy",
"implementation",
"math",
"number theory"
] | f5de1e9b059bddf8f8dd46c18ce12683 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i < 16)$$$, the contents of William's array. | 900 | For each test case output the maximal sum of array elements after performing an optimal sequence of operations. | standard output | |
PASSED | a8b907fd7223690478c9115b49f1b1aa | train_110.jsonl | 1638110100 | William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above. | 256 megabytes | import java.util.*;
import java.io.*;
import java.math.*;
public class Codeforces {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter writer = new PrintWriter(System.out);
int T = Integer.parseInt(br.readLine());
while(T-- > 0) {
int n = Integer.parseInt(br.readLine());
int k = 0;
StringTokenizer st = new StringTokenizer(br.readLine());
int[] a = new int[n];
for(int i = 0; i < n; i++) a[i] = Integer.parseInt(st.nextToken());
for(int i = 0; i < n; i++) {
while(a[i]%2 == 0) {
k++;
a[i] /= 2;
}
}
Arrays.sort(a);
BigInteger sum = new BigInteger("0");
for(int i = 0; i < n-1; i++) sum = sum.add(BigInteger.valueOf(a[i]));
BigInteger last = BigInteger.valueOf(a[n-1]);
for(int i = 0; i < k; i++) last = last.multiply(BigInteger.valueOf(2));
sum = sum.add(last);
writer.println(sum);
}
writer.close();
br.close();
}
}
| Java | ["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"] | 1 second | ["50\n46\n10\n26\n35184372088846"] | NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$. | Java 8 | standard input | [
"greedy",
"implementation",
"math",
"number theory"
] | f5de1e9b059bddf8f8dd46c18ce12683 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i < 16)$$$, the contents of William's array. | 900 | For each test case output the maximal sum of array elements after performing an optimal sequence of operations. | standard output | |
PASSED | 946a4b721f0e1c3eb1b41059a554f848 | train_110.jsonl | 1638110100 | William has array of $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. He can perform the following sequence of operations any number of times: Pick any two items from array $$$a_i$$$ and $$$a_j$$$, where $$$a_i$$$ must be a multiple of $$$2$$$ $$$a_i = \frac{a_i}{2}$$$ $$$a_j = a_j \cdot 2$$$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class fastTemp {
public static void main(String[] args) throws IOException, NumberFormatException {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while (t-- > 0) {
int n = sc.nextInt();
int count = 0;
int arr[] = new int[n];
for(int i=0;i<n;i++){
arr[i] = sc.nextInt();
if(arr[i]%2==0){
while(arr[i]%2==0){
arr[i] = arr[i]/2;
count++;
}
}
}
Arrays.sort(arr);
long sum = 0l;
for(int i=0;i<n-1;i++){
sum += arr[i];
}
sum += arr[n-1]*Math.pow(2,count);
System.out.println(sum);
}
}
} | Java | ["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"] | 1 second | ["50\n46\n10\n26\n35184372088846"] | NoteIn the first example test case the optimal sequence would be: Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{4}{2} = 2$$$ and $$$a_1 = 6 \cdot 2 = 12$$$, making the array look as: [12, 2, 2]. Pick $$$i = 2$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_2 = \frac{2}{2} = 1$$$ and $$$a_1 = 12 \cdot 2 = 24$$$, making the array look as: [24, 1, 2]. Pick $$$i = 3$$$ and $$$j = 1$$$. After performing a sequence of operations $$$a_3 = \frac{2}{2} = 1$$$ and $$$a_1 = 24 \cdot 2 = 48$$$, making the array look as: [48, 1, 1]. The final answer $$$48 + 1 + 1 = 50$$$.In the third example test case there is no way to change the sum of elements, so the answer is $$$10$$$. | Java 8 | standard input | [
"greedy",
"implementation",
"math",
"number theory"
] | f5de1e9b059bddf8f8dd46c18ce12683 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 15)$$$, the number of elements in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i < 16)$$$, the contents of William's array. | 900 | For each test case output the maximal sum of array elements after performing an optimal sequence of operations. | standard output | |
PASSED | 936ba0f6ccff6a3310d4019905ee13e9 | train_110.jsonl | 1638110100 | Before becoming a successful trader William got a university degree. During his education an interesting situation happened, after which William started to listen to homework assignments much more attentively. What follows is the correct formal description of the homework assignment:You are given a string $$$s$$$ of length $$$n$$$ only consisting of characters "a", "b" and "c". There are $$$q$$$ queries of format ($$$pos, c$$$), meaning replacing the element of string $$$s$$$ at position $$$pos$$$ with character $$$c$$$. After each query you must output the minimal number of characters in the string, which have to be replaced, so that the string doesn't contain string "abc" as a substring. A valid replacement of a character is replacing it with "a", "b" or "c".A string $$$x$$$ is a substring of a string $$$y$$$ if $$$x$$$ can be obtained from $$$y$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. | 256 megabytes |
import java.util.Scanner;
public class prob2 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int num = input.nextInt();
int numOfQ = input.nextInt();
input.nextLine();
String s = input.nextLine();
char[] c = s.toCharArray();
int count =0;
for (int i = 0; i < c.length-2; i++) {
String v = s.substring(i,i+3);
if(v.equalsIgnoreCase("abc"))
count++;
}
do {
int pos = input.nextInt();
char newC = input.next().charAt(0);
boolean is1 = isCon(pos,c);
c[pos-1] = newC;
boolean is2 = isCon(pos,c);
if(is1&&!is2)
count--;
else if(!is1&&is2)
count++;
System.out.println(count);
numOfQ--;
}while (numOfQ!=0);
}
public static boolean isCon(int pos,char[] ar){
boolean flag = false;
String f;
if (pos+2 <= ar.length){
f = ar[pos-1]+""+ar[pos]+""+ar[pos+1];
if (f.equalsIgnoreCase("abc") ) {
flag =true;
}
}
if (pos-2 >= 1){
f = ar[pos-3]+""+ar[pos-2]+""+ar[pos-1];
if (f.equalsIgnoreCase("abc") ) {
flag =true;
}
}
if (pos-1>=1 && pos+1<=ar.length ){
f = ar[pos-2]+""+ar[pos-1]+""+ar[pos];
if (f.equalsIgnoreCase("abc") ) {
flag =true;
}
}
return flag;
}
}
| Java | ["9 10\nabcabcabc\n1 a\n1 b\n2 c\n3 a\n4 b\n5 c\n8 a\n9 b\n1 c\n4 a"] | 2 seconds | ["3\n2\n2\n2\n1\n2\n1\n1\n1\n0"] | NoteLet's consider the state of the string after each query: $$$s =$$$ "abcabcabc". In this case $$$3$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcaccabb". This string does not contain "abc" as a substring. $$$s =$$$ "bbcabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bccabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bccbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcaabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcabbcabc". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaac". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaac". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "ccabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccaaccaab". In this case the string does not contain "abc" as a substring and no replacements are needed. | Java 11 | standard input | [
"implementation",
"strings"
] | db473ad780a93983667d12b1357c6e2f | The first line contains two integers $$$n$$$ and $$$q$$$ $$$(1 \le n, q \le 10^5)$$$, the length of the string and the number of queries, respectively. The second line contains the string $$$s$$$, consisting of characters "a", "b" and "c". Each of the next $$$q$$$ lines contains an integer $$$i$$$ and character $$$c$$$ $$$(1 \le i \le n)$$$, index and the value of the new item in the string, respectively. It is guaranteed that character's $$$c$$$ value is "a", "b" or "c". | 1,100 | For each query output the minimal number of characters that would have to be replaced so that the string doesn't contain "abc" as a substring. | standard output | |
PASSED | 0b06d9b5614c1c2f44aa102e646cd221 | train_110.jsonl | 1638110100 | Before becoming a successful trader William got a university degree. During his education an interesting situation happened, after which William started to listen to homework assignments much more attentively. What follows is the correct formal description of the homework assignment:You are given a string $$$s$$$ of length $$$n$$$ only consisting of characters "a", "b" and "c". There are $$$q$$$ queries of format ($$$pos, c$$$), meaning replacing the element of string $$$s$$$ at position $$$pos$$$ with character $$$c$$$. After each query you must output the minimal number of characters in the string, which have to be replaced, so that the string doesn't contain string "abc" as a substring. A valid replacement of a character is replacing it with "a", "b" or "c".A string $$$x$$$ is a substring of a string $$$y$$$ if $$$x$$$ can be obtained from $$$y$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. | 256 megabytes | import java.io.*;
import java.util.*;
public class vigilant2 {
static BufferedReader sc = new BufferedReader(new InputStreamReader(System.in));
static PrintWriter out = new PrintWriter(System.out);
public static void main(String[] args) throws IOException {
String line1 = sc.readLine();
int space1 = line1.indexOf(" ");
int n = Integer.parseInt(line1.substring(0, space1));
int q = Integer.parseInt(line1.substring(space1+1));
String se = sc.readLine();
char[] s = se.toCharArray();
int abcct = 0;
for(int i = 0; i<n-2; i++){
if(se.substring(i, i+3).equals("abc")) abcct++;
}
String oldsub;
String newssub;
for(int i = 0; i<q; i++){
String temp = sc.readLine();
int space = temp.indexOf(" ");
int index = Integer.parseInt(temp.substring(0, space))-1;
char replacement = temp.charAt(temp.length()-1);
oldsub = String.valueOf(Arrays.copyOfRange(s, Math.max(index-2, 0), Math.min(index+3, s.length)));
s[index] = replacement;
newssub = String.valueOf(Arrays.copyOfRange(s, Math.max(index-2, 0), Math.min(index+3, s.length)));
if(oldsub.contains("abc") && !newssub.contains("abc")) abcct--;
else if(!oldsub.contains("abc") && newssub.contains("abc")) abcct++;
out.println(abcct);
}
sc.close();
out.close();
}
}
| Java | ["9 10\nabcabcabc\n1 a\n1 b\n2 c\n3 a\n4 b\n5 c\n8 a\n9 b\n1 c\n4 a"] | 2 seconds | ["3\n2\n2\n2\n1\n2\n1\n1\n1\n0"] | NoteLet's consider the state of the string after each query: $$$s =$$$ "abcabcabc". In this case $$$3$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcaccabb". This string does not contain "abc" as a substring. $$$s =$$$ "bbcabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bccabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bccbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcaabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcabbcabc". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaac". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaac". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "ccabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccaaccaab". In this case the string does not contain "abc" as a substring and no replacements are needed. | Java 11 | standard input | [
"implementation",
"strings"
] | db473ad780a93983667d12b1357c6e2f | The first line contains two integers $$$n$$$ and $$$q$$$ $$$(1 \le n, q \le 10^5)$$$, the length of the string and the number of queries, respectively. The second line contains the string $$$s$$$, consisting of characters "a", "b" and "c". Each of the next $$$q$$$ lines contains an integer $$$i$$$ and character $$$c$$$ $$$(1 \le i \le n)$$$, index and the value of the new item in the string, respectively. It is guaranteed that character's $$$c$$$ value is "a", "b" or "c". | 1,100 | For each query output the minimal number of characters that would have to be replaced so that the string doesn't contain "abc" as a substring. | standard output | |
PASSED | 0baf4f76b8d79f950ac0008f6a859c13 | train_110.jsonl | 1638110100 | Before becoming a successful trader William got a university degree. During his education an interesting situation happened, after which William started to listen to homework assignments much more attentively. What follows is the correct formal description of the homework assignment:You are given a string $$$s$$$ of length $$$n$$$ only consisting of characters "a", "b" and "c". There are $$$q$$$ queries of format ($$$pos, c$$$), meaning replacing the element of string $$$s$$$ at position $$$pos$$$ with character $$$c$$$. After each query you must output the minimal number of characters in the string, which have to be replaced, so that the string doesn't contain string "abc" as a substring. A valid replacement of a character is replacing it with "a", "b" or "c".A string $$$x$$$ is a substring of a string $$$y$$$ if $$$x$$$ can be obtained from $$$y$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. | 256 megabytes | import java.io.*;
import java.util.*;
import java.math.BigInteger;
public class Main {
private boolean oj = System.getProperty("ONLINE_JUDGE") != null;
private FastWriter wr;
private Reader rd;
public final int MOD = 1000000007;
/************************************************** FAST INPUT IMPLEMENTATION *********************************************/
class Reader {
BufferedReader br;
StringTokenizer st;
public Reader() {
br = new BufferedReader(
new InputStreamReader(System.in));
}
public Reader(String path) throws FileNotFoundException {
br = new BufferedReader(
new InputStreamReader(new FileInputStream(path)));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public int ni() throws IOException {
return rd.nextInt();
}
public long nl() throws IOException {
return rd.nextLong();
}
char nc() throws IOException {
return rd.next().charAt(0);
}
public String ns() throws IOException {
return rd.nextLine();
}
public Double nd() throws IOException {
return rd.nextDouble();
}
public int[] nai(int n) throws IOException {
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = ni();
}
return arr;
}
public long[] nal(int n) throws IOException {
long[] arr = new long[n];
for (int i = 0; i < n; i++) {
arr[i] = nl();
}
return arr;
}
/************************************************** FAST OUTPUT IMPLEMENTATION *********************************************/
public static class FastWriter {
private static final int BUF_SIZE = 1 << 13;
private final byte[] buf = new byte[BUF_SIZE];
private final OutputStream out;
private int ptr = 0;
private FastWriter() {
out = null;
}
public FastWriter(OutputStream os) {
this.out = os;
}
public FastWriter(String path) {
try {
this.out = new FileOutputStream(path);
} catch (FileNotFoundException e) {
throw new RuntimeException("FastWriter");
}
}
public FastWriter write(byte b) {
buf[ptr++] = b;
if (ptr == BUF_SIZE) innerflush();
return this;
}
public FastWriter write(char c) {
return write((byte) c);
}
public FastWriter write(char[] s) {
for (char c : s) {
buf[ptr++] = (byte) c;
if (ptr == BUF_SIZE) innerflush();
}
return this;
}
public FastWriter write(String s) {
s.chars().forEach(c -> {
buf[ptr++] = (byte) c;
if (ptr == BUF_SIZE) innerflush();
});
return this;
}
private static int countDigits(int l) {
if (l >= 1000000000) return 10;
if (l >= 100000000) return 9;
if (l >= 10000000) return 8;
if (l >= 1000000) return 7;
if (l >= 100000) return 6;
if (l >= 10000) return 5;
if (l >= 1000) return 4;
if (l >= 100) return 3;
if (l >= 10) return 2;
return 1;
}
public FastWriter write(int x) {
if (x == Integer.MIN_VALUE) {
return write((long) x);
}
if (ptr + 12 >= BUF_SIZE) innerflush();
if (x < 0) {
write((byte) '-');
x = -x;
}
int d = countDigits(x);
for (int i = ptr + d - 1; i >= ptr; i--) {
buf[i] = (byte) ('0' + x % 10);
x /= 10;
}
ptr += d;
return this;
}
private static int countDigits(long l) {
if (l >= 1000000000000000000L) return 19;
if (l >= 100000000000000000L) return 18;
if (l >= 10000000000000000L) return 17;
if (l >= 1000000000000000L) return 16;
if (l >= 100000000000000L) return 15;
if (l >= 10000000000000L) return 14;
if (l >= 1000000000000L) return 13;
if (l >= 100000000000L) return 12;
if (l >= 10000000000L) return 11;
if (l >= 1000000000L) return 10;
if (l >= 100000000L) return 9;
if (l >= 10000000L) return 8;
if (l >= 1000000L) return 7;
if (l >= 100000L) return 6;
if (l >= 10000L) return 5;
if (l >= 1000L) return 4;
if (l >= 100L) return 3;
if (l >= 10L) return 2;
return 1;
}
public FastWriter write(long x) {
if (x == Long.MIN_VALUE) {
return write("" + x);
}
if (ptr + 21 >= BUF_SIZE) innerflush();
if (x < 0) {
write((byte) '-');
x = -x;
}
int d = countDigits(x);
for (int i = ptr + d - 1; i >= ptr; i--) {
buf[i] = (byte) ('0' + x % 10);
x /= 10;
}
ptr += d;
return this;
}
public FastWriter write(double x, int precision) {
if (x < 0) {
write('-');
x = -x;
}
x += Math.pow(10, -precision) / 2;
// if(x < 0){ x = 0; }
write((long) x).write(".");
x -= (long) x;
for (int i = 0; i < precision; i++) {
x *= 10;
write((char) ('0' + (int) x));
x -= (int) x;
}
return this;
}
public FastWriter writeln(char c) {
return write(c).writeln();
}
public FastWriter writeln(int x) {
return write(x).writeln();
}
public FastWriter writeln(long x) {
return write(x).writeln();
}
public FastWriter writeln(double x, int precision) {
return write(x, precision).writeln();
}
public FastWriter write(int... xs) {
boolean first = true;
for (int x : xs) {
if (!first) write(' ');
first = false;
write(x);
}
return this;
}
public FastWriter write(long... xs) {
boolean first = true;
for (long x : xs) {
if (!first) write(' ');
first = false;
write(x);
}
return this;
}
public FastWriter writeln() {
return write((byte) '\n');
}
public FastWriter writeln(int... xs) {
return write(xs).writeln();
}
public FastWriter writeln(long... xs) {
return write(xs).writeln();
}
public FastWriter writeln(char[] line) {
return write(line).writeln();
}
public FastWriter writeln(char[]... map) {
for (char[] line : map) write(line).writeln();
return this;
}
public FastWriter writeln(String s) {
return write(s).writeln();
}
private void innerflush() {
try {
out.write(buf, 0, ptr);
ptr = 0;
} catch (IOException e) {
throw new RuntimeException("innerflush");
}
}
public void flush() {
innerflush();
try {
out.flush();
} catch (IOException e) {
throw new RuntimeException("flush");
}
}
public FastWriter print(byte b) {
return write(b);
}
public FastWriter print(char c) {
return write(c);
}
public FastWriter print(char[] s) {
return write(s);
}
public FastWriter print(String s) {
return write(s);
}
public FastWriter print(int x) {
return write(x);
}
public FastWriter print(long x) {
return write(x);
}
public FastWriter print(double x, int precision) {
return write(x, precision);
}
public FastWriter println(char c) {
return writeln(c);
}
public FastWriter println(int x) {
return writeln(x);
}
public FastWriter println(long x) {
return writeln(x);
}
public FastWriter println(double x, int precision) {
return writeln(x, precision);
}
public FastWriter print(int... xs) {
return write(xs);
}
public FastWriter print(long... xs) {
return write(xs);
}
public FastWriter println(int... xs) {
return writeln(xs);
}
public FastWriter println(long... xs) {
return writeln(xs);
}
public FastWriter println(char[] line) {
return writeln(line);
}
public FastWriter println(char[]... map) {
return writeln(map);
}
public FastWriter println(String s) {
return writeln(s);
}
public FastWriter println() {
return writeln();
}
}
/********************************************************* USEFUL CODE **************************************************/
boolean[] SAPrimeGenerator(int n) {
// TC-N*LOG(LOG N)
//Create Prime Marking Array and fill it with true value
boolean[] primeMarker = new boolean[n + 1];
Arrays.fill(primeMarker, true);
primeMarker[0] = false;
primeMarker[1] = false;
for (int i = 2; i <= n; i++) {
if (primeMarker[i]) {
// we start from 2*i because i*1 must be prime
for (int j = 2 * i; j <= n; j += i) {
primeMarker[j] = false;
}
}
}
return primeMarker;
}
private void tr(Object... o) {
if (!oj) System.out.println(Arrays.deepToString(o));
}
class Pair<F, S> {
private F first;
private S second;
Pair(F first, S second) {
this.first = first;
this.second = second;
}
public F getFirst() {
return first;
}
public S getSecond() {
return second;
}
@Override
public String toString() {
return "Pair{" +
"first=" + first +
", second=" + second +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Pair<?, ?> pair = (Pair<?, ?>) o;
return Objects.equals(first, pair.first) && Objects.equals(second, pair.second);
}
}
public static void main(String[] args) throws IOException {
new Main().run();
}
public void run() throws IOException {
if (oj) {
rd = new Reader();
wr = new FastWriter(System.out);
} else {
File input = new File("input.txt");
File output = new File("output.txt");
if (input.exists() && output.exists()) {
rd = new Reader(input.getPath());
wr = new FastWriter(output.getPath());
} else {
rd = new Reader();
wr = new FastWriter(System.out);
oj = true;
}
}
long s = System.currentTimeMillis();
solve();
wr.flush();
tr(System.currentTimeMillis() - s + "ms");
}
/***************************************************************************************************************************
*********************************************************** MAIN CODE ******************************************************
****************************************************************************************************************************/
public void solve() throws IOException {
// int t = ni();
// while (t-- > 0) {
go();
// }
}
/********************************************************* MAIN LOGIC HERE ****************************************************/
public int getABC(char[] arr, int s, int e) {
int count = 0;
for (int i = s; i <= e - 2; i++) {
if (arr[i] == 'a' && arr[i + 1] == 'b' && arr[i + 2] == 'c') {
count++;
}
}
return count;
}
public void go() throws IOException {
int n = ni();
int q = ni();
String st = ns();
char[] arr = st.toCharArray();
int count = getABC(arr, 0, n - 1);
for (int i = 0; i < q; i++) {
int pos = ni();
char ch = nc();
pos = pos - 1;
if (ch != arr[pos]) {
int beforeC = 0;
if (pos <= n - 3 && arr[pos] == 'a' && arr[pos + 1] == 'b' && arr[pos + 2] == 'c') {
beforeC = 1;
} else if (pos > 0 && pos < n - 1 && arr[pos] == 'b' && arr[pos - 1] == 'a' && arr[pos + 1] == 'c') {
beforeC = 1;
} else if (pos > 1 && arr[pos] == 'c' && arr[pos - 1] == 'b' && arr[pos - 2] == 'a') {
beforeC = 1;
}
int afterC = 0;
arr[pos] = ch;
if (pos <= n - 3 && ch == 'a' && arr[pos + 1] == 'b' && arr[pos + 2] == 'c') {
afterC = 1;
} else if (pos > 0 && pos < n - 1 && ch == 'b' && arr[pos - 1] == 'a' && arr[pos + 1] == 'c') {
afterC = 1;
} else if (pos > 1 && ch == 'c' && arr[pos - 1] == 'b' && arr[pos - 2] == 'a') {
afterC = 1;
}
count += (afterC - beforeC);
}
wr.println(count);
}
}
}
| Java | ["9 10\nabcabcabc\n1 a\n1 b\n2 c\n3 a\n4 b\n5 c\n8 a\n9 b\n1 c\n4 a"] | 2 seconds | ["3\n2\n2\n2\n1\n2\n1\n1\n1\n0"] | NoteLet's consider the state of the string after each query: $$$s =$$$ "abcabcabc". In this case $$$3$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcaccabb". This string does not contain "abc" as a substring. $$$s =$$$ "bbcabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bccabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bccbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcaabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcabbcabc". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaac". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaac". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "ccabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccaaccaab". In this case the string does not contain "abc" as a substring and no replacements are needed. | Java 11 | standard input | [
"implementation",
"strings"
] | db473ad780a93983667d12b1357c6e2f | The first line contains two integers $$$n$$$ and $$$q$$$ $$$(1 \le n, q \le 10^5)$$$, the length of the string and the number of queries, respectively. The second line contains the string $$$s$$$, consisting of characters "a", "b" and "c". Each of the next $$$q$$$ lines contains an integer $$$i$$$ and character $$$c$$$ $$$(1 \le i \le n)$$$, index and the value of the new item in the string, respectively. It is guaranteed that character's $$$c$$$ value is "a", "b" or "c". | 1,100 | For each query output the minimal number of characters that would have to be replaced so that the string doesn't contain "abc" as a substring. | standard output | |
PASSED | 6c46e95906524bb9e35f6f303ef06605 | train_110.jsonl | 1638110100 | Before becoming a successful trader William got a university degree. During his education an interesting situation happened, after which William started to listen to homework assignments much more attentively. What follows is the correct formal description of the homework assignment:You are given a string $$$s$$$ of length $$$n$$$ only consisting of characters "a", "b" and "c". There are $$$q$$$ queries of format ($$$pos, c$$$), meaning replacing the element of string $$$s$$$ at position $$$pos$$$ with character $$$c$$$. After each query you must output the minimal number of characters in the string, which have to be replaced, so that the string doesn't contain string "abc" as a substring. A valid replacement of a character is replacing it with "a", "b" or "c".A string $$$x$$$ is a substring of a string $$$y$$$ if $$$x$$$ can be obtained from $$$y$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. | 256 megabytes | import java.io.*;
import java.util.*;
import java.math.BigInteger;
public class Main {
private boolean oj = System.getProperty("ONLINE_JUDGE") != null;
private static FastWriter wr;
private static Reader rd;
public final int MOD = 1000000007;
/************************************************** FAST INPUT IMPLEMENTATION *********************************************/
class Reader {
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
private int line_length;
public Reader(int ll, File filename) throws FileNotFoundException {
din = new DataInputStream(new FileInputStream(filename));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
line_length = ll;
}
public Reader(int ll) throws FileNotFoundException {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
line_length = ll;
}
public Reader(String file_name) throws IOException {
din = new DataInputStream(
new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException {
byte[] buf = new byte[line_length]; // line length
int cnt = 0, c;
while ((c = read()) != -1) {
if (c == '\n') {
if (cnt != 0) {
break;
} else {
continue;
}
}
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException {
int ret = 0;
byte c = read();
while (c <= ' ') {
c = read();
}
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException {
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException {
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (c == '.') {
while ((c = read()) >= '0' && c <= '9') {
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException {
bytesRead = din.read(buffer, bufferPointer = 0,
BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException {
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException {
if (din == null)
return;
din.close();
}
}
public int ni() throws IOException {
return rd.nextInt();
}
public long nl() throws IOException {
return rd.nextLong();
}
public String ns() throws IOException {
return rd.readLine().trim();
}
public Double nd() throws IOException {
return rd.nextDouble();
}
public int[] nai(int n) throws IOException {
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = ni();
}
return arr;
}
public long[] nal(int n) throws IOException {
long[] arr = new long[n];
for (int i = 0; i < n; i++) {
arr[i] = nl();
}
return arr;
}
/************************************************** FAST OUTPUT IMPLEMENTATION *********************************************/
public static class FastWriter {
private static final int BUF_SIZE = 1 << 13;
private final byte[] buf = new byte[BUF_SIZE];
private final OutputStream out;
private int ptr = 0;
private FastWriter() {
out = null;
}
public FastWriter(OutputStream os) {
this.out = os;
}
public FastWriter(String path) {
try {
this.out = new FileOutputStream(path);
} catch (FileNotFoundException e) {
throw new RuntimeException("FastWriter");
}
}
public FastWriter write(byte b) {
buf[ptr++] = b;
if (ptr == BUF_SIZE) innerflush();
return this;
}
public FastWriter write(char c) {
return write((byte) c);
}
public FastWriter write(char[] s) {
for (char c : s) {
buf[ptr++] = (byte) c;
if (ptr == BUF_SIZE) innerflush();
}
return this;
}
public FastWriter write(String s) {
s.chars().forEach(c -> {
buf[ptr++] = (byte) c;
if (ptr == BUF_SIZE) innerflush();
});
return this;
}
private static int countDigits(int l) {
if (l >= 1000000000) return 10;
if (l >= 100000000) return 9;
if (l >= 10000000) return 8;
if (l >= 1000000) return 7;
if (l >= 100000) return 6;
if (l >= 10000) return 5;
if (l >= 1000) return 4;
if (l >= 100) return 3;
if (l >= 10) return 2;
return 1;
}
public FastWriter write(int x) {
if (x == Integer.MIN_VALUE) {
return write((long) x);
}
if (ptr + 12 >= BUF_SIZE) innerflush();
if (x < 0) {
write((byte) '-');
x = -x;
}
int d = countDigits(x);
for (int i = ptr + d - 1; i >= ptr; i--) {
buf[i] = (byte) ('0' + x % 10);
x /= 10;
}
ptr += d;
return this;
}
private static int countDigits(long l) {
if (l >= 1000000000000000000L) return 19;
if (l >= 100000000000000000L) return 18;
if (l >= 10000000000000000L) return 17;
if (l >= 1000000000000000L) return 16;
if (l >= 100000000000000L) return 15;
if (l >= 10000000000000L) return 14;
if (l >= 1000000000000L) return 13;
if (l >= 100000000000L) return 12;
if (l >= 10000000000L) return 11;
if (l >= 1000000000L) return 10;
if (l >= 100000000L) return 9;
if (l >= 10000000L) return 8;
if (l >= 1000000L) return 7;
if (l >= 100000L) return 6;
if (l >= 10000L) return 5;
if (l >= 1000L) return 4;
if (l >= 100L) return 3;
if (l >= 10L) return 2;
return 1;
}
public FastWriter write(long x) {
if (x == Long.MIN_VALUE) {
return write("" + x);
}
if (ptr + 21 >= BUF_SIZE) innerflush();
if (x < 0) {
write((byte) '-');
x = -x;
}
int d = countDigits(x);
for (int i = ptr + d - 1; i >= ptr; i--) {
buf[i] = (byte) ('0' + x % 10);
x /= 10;
}
ptr += d;
return this;
}
public FastWriter write(double x, int precision) {
if (x < 0) {
write('-');
x = -x;
}
x += Math.pow(10, -precision) / 2;
// if(x < 0){ x = 0; }
write((long) x).write(".");
x -= (long) x;
for (int i = 0; i < precision; i++) {
x *= 10;
write((char) ('0' + (int) x));
x -= (int) x;
}
return this;
}
public FastWriter writeln(char c) {
return write(c).writeln();
}
public FastWriter writeln(int x) {
return write(x).writeln();
}
public FastWriter writeln(long x) {
return write(x).writeln();
}
public FastWriter writeln(double x, int precision) {
return write(x, precision).writeln();
}
public FastWriter write(int... xs) {
boolean first = true;
for (int x : xs) {
if (!first) write(' ');
first = false;
write(x);
}
return this;
}
public FastWriter write(long... xs) {
boolean first = true;
for (long x : xs) {
if (!first) write(' ');
first = false;
write(x);
}
return this;
}
public FastWriter writeln() {
return write((byte) '\n');
}
public FastWriter writeln(int... xs) {
return write(xs).writeln();
}
public FastWriter writeln(long... xs) {
return write(xs).writeln();
}
public FastWriter writeln(char[] line) {
return write(line).writeln();
}
public FastWriter writeln(char[]... map) {
for (char[] line : map) write(line).writeln();
return this;
}
public FastWriter writeln(String s) {
return write(s).writeln();
}
private void innerflush() {
try {
out.write(buf, 0, ptr);
ptr = 0;
} catch (IOException e) {
throw new RuntimeException("innerflush");
}
}
public void flush() {
innerflush();
try {
out.flush();
} catch (IOException e) {
throw new RuntimeException("flush");
}
}
public FastWriter print(byte b) {
return write(b);
}
public FastWriter print(char c) {
return write(c);
}
public FastWriter print(char[] s) {
return write(s);
}
public FastWriter print(String s) {
return write(s);
}
public FastWriter print(int x) {
return write(x);
}
public FastWriter print(long x) {
return write(x);
}
public FastWriter print(double x, int precision) {
return write(x, precision);
}
public FastWriter println(char c) {
return writeln(c);
}
public FastWriter println(int x) {
return writeln(x);
}
public FastWriter println(long x) {
return writeln(x);
}
public FastWriter println(double x, int precision) {
return writeln(x, precision);
}
public FastWriter print(int... xs) {
return write(xs);
}
public FastWriter print(long... xs) {
return write(xs);
}
public FastWriter println(int... xs) {
return writeln(xs);
}
public FastWriter println(long... xs) {
return writeln(xs);
}
public FastWriter println(char[] line) {
return writeln(line);
}
public FastWriter println(char[]... map) {
return writeln(map);
}
public FastWriter println(String s) {
return writeln(s);
}
public FastWriter println() {
return writeln();
}
}
/********************************************************* USEFUL CODE **************************************************/
boolean[] SAPrimeGenerator(int n) {
// TC-N*LOG(LOG N)
//Create Prime Marking Array and fill it with true value
boolean[] primeMarker = new boolean[n + 1];
Arrays.fill(primeMarker, true);
primeMarker[0] = false;
primeMarker[1] = false;
for (int i = 2; i <= n; i++) {
if (primeMarker[i]) {
// we start from 2*i because i*1 must be prime
for (int j = 2 * i; j <= n; j += i) {
primeMarker[j] = false;
}
}
}
return primeMarker;
}
private void tr(Object... o) {
if (!oj) System.out.println(Arrays.deepToString(o));
}
class Pair<F, S> {
private F first;
private S second;
Pair(F first, S second) {
this.first = first;
this.second = second;
}
public F getFirst() {
return first;
}
public S getSecond() {
return second;
}
@Override
public String toString() {
return "Pair{" +
"first=" + first +
", second=" + second +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Pair<?, ?> pair = (Pair<?, ?>) o;
return Objects.equals(first, pair.first) && Objects.equals(second, pair.second);
}
}
public static void main(String[] args) throws IOException {
new Main().run();
}
public void run() throws IOException {
if (oj) {
rd = new Reader(1000000);
wr = new FastWriter(System.out);
} else {
File input = new File("input.txt");
File output = new File("output.txt");
if (input.exists() && output.exists()) {
rd = new Reader(1000000, input);
wr = new FastWriter(output.getPath());
} else {
rd = new Reader(1000000);
wr = new FastWriter(System.out);
oj = true;
}
}
long s = System.currentTimeMillis();
solve();
wr.flush();
tr(System.currentTimeMillis() - s + "ms");
}
/***************************************************************************************************************************
*********************************************************** MAIN CODE ******************************************************
****************************************************************************************************************************/
public void solve() throws IOException {
// int t = ni();
// while (t-- > 0) {
go();
// }
}
/********************************************************* MAIN LOGIC HERE ****************************************************/
public int getABC(char[] arr,int s,int e){
int count=0;
for(int i=s;i<=e-2;i++){
if(arr[i]=='a'&&arr[i+1]=='b'&&arr[i+2]=='c'){
count++;
}
}
return count;
}
public void go() throws IOException {
int n=ni();
int q=ni();
String st=ns();
char[] arr=st.toCharArray();
int count=getABC(arr,0,n-1);
for(int i=0;i<q;i++){
int pos=ni();
char ch=(char)rd.read();
pos=pos-1;
if(ch!=arr[pos]){
int beforeC=0;
if(pos<=n-3 && arr[pos]=='a' && arr[pos+1]=='b' && arr[pos+2]=='c' ){
beforeC=1;
}else if(pos>0 && pos<n-1 && arr[pos]=='b' && arr[pos-1]=='a' && arr[pos+1]=='c'){
beforeC=1;
}else if(pos>1 && arr[pos]=='c' && arr[pos-1]=='b' && arr[pos-2]=='a'){
beforeC=1;
}
int afterC=0;
arr[pos]=ch;
if(pos<=n-3 && ch=='a' && arr[pos+1]=='b' && arr[pos+2]=='c' ){
afterC=1;
}else if(pos>0 && pos<n-1 && ch=='b' && arr[pos-1]=='a' && arr[pos+1]=='c'){
afterC=1;
}else if(pos>1 && ch=='c' && arr[pos-1]=='b' && arr[pos-2]=='a'){
afterC=1;
}
count+=(afterC-beforeC);
}
wr.println(count);
}
}
} | Java | ["9 10\nabcabcabc\n1 a\n1 b\n2 c\n3 a\n4 b\n5 c\n8 a\n9 b\n1 c\n4 a"] | 2 seconds | ["3\n2\n2\n2\n1\n2\n1\n1\n1\n0"] | NoteLet's consider the state of the string after each query: $$$s =$$$ "abcabcabc". In this case $$$3$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcaccabb". This string does not contain "abc" as a substring. $$$s =$$$ "bbcabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bccabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bccbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcaabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcabbcabc". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaac". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaac". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "ccabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccaaccaab". In this case the string does not contain "abc" as a substring and no replacements are needed. | Java 11 | standard input | [
"implementation",
"strings"
] | db473ad780a93983667d12b1357c6e2f | The first line contains two integers $$$n$$$ and $$$q$$$ $$$(1 \le n, q \le 10^5)$$$, the length of the string and the number of queries, respectively. The second line contains the string $$$s$$$, consisting of characters "a", "b" and "c". Each of the next $$$q$$$ lines contains an integer $$$i$$$ and character $$$c$$$ $$$(1 \le i \le n)$$$, index and the value of the new item in the string, respectively. It is guaranteed that character's $$$c$$$ value is "a", "b" or "c". | 1,100 | For each query output the minimal number of characters that would have to be replaced so that the string doesn't contain "abc" as a substring. | standard output | |
PASSED | 125a644f3e6f2b9c2b6de1d805354a32 | train_110.jsonl | 1638110100 | Before becoming a successful trader William got a university degree. During his education an interesting situation happened, after which William started to listen to homework assignments much more attentively. What follows is the correct formal description of the homework assignment:You are given a string $$$s$$$ of length $$$n$$$ only consisting of characters "a", "b" and "c". There are $$$q$$$ queries of format ($$$pos, c$$$), meaning replacing the element of string $$$s$$$ at position $$$pos$$$ with character $$$c$$$. After each query you must output the minimal number of characters in the string, which have to be replaced, so that the string doesn't contain string "abc" as a substring. A valid replacement of a character is replacing it with "a", "b" or "c".A string $$$x$$$ is a substring of a string $$$y$$$ if $$$x$$$ can be obtained from $$$y$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. | 256 megabytes | import java.io.*;
import java.util.*;
import java.math.BigInteger;
public class Main {
private boolean oj = System.getProperty("ONLINE_JUDGE") != null;
private static FastWriter wr;
private static Reader rd;
public final int MOD = 1000000007;
/************************************************** FAST INPUT IMPLEMENTATION *********************************************/
class Reader {
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
private int line_length;
public Reader(int ll, File filename) throws FileNotFoundException {
din = new DataInputStream(new FileInputStream(filename));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
line_length = ll;
}
public Reader(int ll) throws FileNotFoundException {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
line_length = ll;
}
public Reader(String file_name) throws IOException {
din = new DataInputStream(
new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException {
byte[] buf = new byte[line_length]; // line length
int cnt = 0, c;
while ((c = read()) != -1) {
if (c == '\n') {
if (cnt != 0) {
break;
} else {
continue;
}
}
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException {
int ret = 0;
byte c = read();
while (c <= ' ') {
c = read();
}
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException {
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException {
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (c == '.') {
while ((c = read()) >= '0' && c <= '9') {
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException {
bytesRead = din.read(buffer, bufferPointer = 0,
BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException {
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException {
if (din == null)
return;
din.close();
}
}
public int ni() throws IOException {
return rd.nextInt();
}
public long nl() throws IOException {
return rd.nextLong();
}
public String ns() throws IOException {
return rd.readLine().trim();
}
public Double nd() throws IOException {
return rd.nextDouble();
}
public int[] nai(int n) throws IOException {
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = ni();
}
return arr;
}
public long[] nal(int n) throws IOException {
long[] arr = new long[n];
for (int i = 0; i < n; i++) {
arr[i] = nl();
}
return arr;
}
/************************************************** FAST OUTPUT IMPLEMENTATION *********************************************/
public static class FastWriter {
private static final int BUF_SIZE = 1 << 13;
private final byte[] buf = new byte[BUF_SIZE];
private final OutputStream out;
private int ptr = 0;
private FastWriter() {
out = null;
}
public FastWriter(OutputStream os) {
this.out = os;
}
public FastWriter(String path) {
try {
this.out = new FileOutputStream(path);
} catch (FileNotFoundException e) {
throw new RuntimeException("FastWriter");
}
}
public FastWriter write(byte b) {
buf[ptr++] = b;
if (ptr == BUF_SIZE) innerflush();
return this;
}
public FastWriter write(char c) {
return write((byte) c);
}
public FastWriter write(char[] s) {
for (char c : s) {
buf[ptr++] = (byte) c;
if (ptr == BUF_SIZE) innerflush();
}
return this;
}
public FastWriter write(String s) {
s.chars().forEach(c -> {
buf[ptr++] = (byte) c;
if (ptr == BUF_SIZE) innerflush();
});
return this;
}
private static int countDigits(int l) {
if (l >= 1000000000) return 10;
if (l >= 100000000) return 9;
if (l >= 10000000) return 8;
if (l >= 1000000) return 7;
if (l >= 100000) return 6;
if (l >= 10000) return 5;
if (l >= 1000) return 4;
if (l >= 100) return 3;
if (l >= 10) return 2;
return 1;
}
public FastWriter write(int x) {
if (x == Integer.MIN_VALUE) {
return write((long) x);
}
if (ptr + 12 >= BUF_SIZE) innerflush();
if (x < 0) {
write((byte) '-');
x = -x;
}
int d = countDigits(x);
for (int i = ptr + d - 1; i >= ptr; i--) {
buf[i] = (byte) ('0' + x % 10);
x /= 10;
}
ptr += d;
return this;
}
private static int countDigits(long l) {
if (l >= 1000000000000000000L) return 19;
if (l >= 100000000000000000L) return 18;
if (l >= 10000000000000000L) return 17;
if (l >= 1000000000000000L) return 16;
if (l >= 100000000000000L) return 15;
if (l >= 10000000000000L) return 14;
if (l >= 1000000000000L) return 13;
if (l >= 100000000000L) return 12;
if (l >= 10000000000L) return 11;
if (l >= 1000000000L) return 10;
if (l >= 100000000L) return 9;
if (l >= 10000000L) return 8;
if (l >= 1000000L) return 7;
if (l >= 100000L) return 6;
if (l >= 10000L) return 5;
if (l >= 1000L) return 4;
if (l >= 100L) return 3;
if (l >= 10L) return 2;
return 1;
}
public FastWriter write(long x) {
if (x == Long.MIN_VALUE) {
return write("" + x);
}
if (ptr + 21 >= BUF_SIZE) innerflush();
if (x < 0) {
write((byte) '-');
x = -x;
}
int d = countDigits(x);
for (int i = ptr + d - 1; i >= ptr; i--) {
buf[i] = (byte) ('0' + x % 10);
x /= 10;
}
ptr += d;
return this;
}
public FastWriter write(double x, int precision) {
if (x < 0) {
write('-');
x = -x;
}
x += Math.pow(10, -precision) / 2;
// if(x < 0){ x = 0; }
write((long) x).write(".");
x -= (long) x;
for (int i = 0; i < precision; i++) {
x *= 10;
write((char) ('0' + (int) x));
x -= (int) x;
}
return this;
}
public FastWriter writeln(char c) {
return write(c).writeln();
}
public FastWriter writeln(int x) {
return write(x).writeln();
}
public FastWriter writeln(long x) {
return write(x).writeln();
}
public FastWriter writeln(double x, int precision) {
return write(x, precision).writeln();
}
public FastWriter write(int... xs) {
boolean first = true;
for (int x : xs) {
if (!first) write(' ');
first = false;
write(x);
}
return this;
}
public FastWriter write(long... xs) {
boolean first = true;
for (long x : xs) {
if (!first) write(' ');
first = false;
write(x);
}
return this;
}
public FastWriter writeln() {
return write((byte) '\n');
}
public FastWriter writeln(int... xs) {
return write(xs).writeln();
}
public FastWriter writeln(long... xs) {
return write(xs).writeln();
}
public FastWriter writeln(char[] line) {
return write(line).writeln();
}
public FastWriter writeln(char[]... map) {
for (char[] line : map) write(line).writeln();
return this;
}
public FastWriter writeln(String s) {
return write(s).writeln();
}
private void innerflush() {
try {
out.write(buf, 0, ptr);
ptr = 0;
} catch (IOException e) {
throw new RuntimeException("innerflush");
}
}
public void flush() {
innerflush();
try {
out.flush();
} catch (IOException e) {
throw new RuntimeException("flush");
}
}
public FastWriter print(byte b) {
return write(b);
}
public FastWriter print(char c) {
return write(c);
}
public FastWriter print(char[] s) {
return write(s);
}
public FastWriter print(String s) {
return write(s);
}
public FastWriter print(int x) {
return write(x);
}
public FastWriter print(long x) {
return write(x);
}
public FastWriter print(double x, int precision) {
return write(x, precision);
}
public FastWriter println(char c) {
return writeln(c);
}
public FastWriter println(int x) {
return writeln(x);
}
public FastWriter println(long x) {
return writeln(x);
}
public FastWriter println(double x, int precision) {
return writeln(x, precision);
}
public FastWriter print(int... xs) {
return write(xs);
}
public FastWriter print(long... xs) {
return write(xs);
}
public FastWriter println(int... xs) {
return writeln(xs);
}
public FastWriter println(long... xs) {
return writeln(xs);
}
public FastWriter println(char[] line) {
return writeln(line);
}
public FastWriter println(char[]... map) {
return writeln(map);
}
public FastWriter println(String s) {
return writeln(s);
}
public FastWriter println() {
return writeln();
}
}
/********************************************************* USEFUL CODE **************************************************/
boolean[] SAPrimeGenerator(int n) {
// TC-N*LOG(LOG N)
//Create Prime Marking Array and fill it with true value
boolean[] primeMarker = new boolean[n + 1];
Arrays.fill(primeMarker, true);
primeMarker[0] = false;
primeMarker[1] = false;
for (int i = 2; i <= n; i++) {
if (primeMarker[i]) {
// we start from 2*i because i*1 must be prime
for (int j = 2 * i; j <= n; j += i) {
primeMarker[j] = false;
}
}
}
return primeMarker;
}
private void tr(Object... o) {
if (!oj) System.out.println(Arrays.deepToString(o));
}
class Pair<F, S> {
private F first;
private S second;
Pair(F first, S second) {
this.first = first;
this.second = second;
}
public F getFirst() {
return first;
}
public S getSecond() {
return second;
}
@Override
public String toString() {
return "Pair{" +
"first=" + first +
", second=" + second +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Pair<?, ?> pair = (Pair<?, ?>) o;
return Objects.equals(first, pair.first) && Objects.equals(second, pair.second);
}
}
public static void main(String[] args) throws IOException {
new Main().run();
}
public void run() throws IOException {
if (oj) {
rd = new Reader(1000000);
wr = new FastWriter(System.out);
} else {
File input = new File("input.txt");
File output = new File("output.txt");
if (input.exists() && output.exists()) {
rd = new Reader(1000000, input);
wr = new FastWriter(output.getPath());
} else {
rd = new Reader(1000000);
wr = new FastWriter(System.out);
oj = true;
}
}
long s = System.currentTimeMillis();
solve();
wr.flush();
tr(System.currentTimeMillis() - s + "ms");
}
/***************************************************************************************************************************
*********************************************************** MAIN CODE ******************************************************
****************************************************************************************************************************/
public void solve() throws IOException {
// int t = ni();
// while (t-- > 0) {
go();
// }
}
/********************************************************* MAIN LOGIC HERE ****************************************************/
public int getABC(char[] arr,int s,int e){
int count=0;
for(int i=s;i<=e-2;i++){
if(arr[i]=='a'&&arr[i+1]=='b'&&arr[i+2]=='c'){
count++;
}
}
return count;
}
public void go() throws IOException {
int n=ni();
int q=ni();
String st=ns();
char[] arr=st.toCharArray();
int count=getABC(arr,0,n-1);
for(int i=0;i<q;i++){
int pos=ni();
char ch=(char)rd.read();
if(ch!=arr[pos-1]){
int s=(pos-1-2)<=0?0:pos-1-2;
int e=(pos+2-1)>=n-1?n-1:pos+2-1;
int beforeC=getABC(arr,s,e);
arr[pos-1]=ch;
int afterC=getABC(arr,s,e);
count+=(afterC-beforeC);
}
wr.println(count);
}
}
} | Java | ["9 10\nabcabcabc\n1 a\n1 b\n2 c\n3 a\n4 b\n5 c\n8 a\n9 b\n1 c\n4 a"] | 2 seconds | ["3\n2\n2\n2\n1\n2\n1\n1\n1\n0"] | NoteLet's consider the state of the string after each query: $$$s =$$$ "abcabcabc". In this case $$$3$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcaccabb". This string does not contain "abc" as a substring. $$$s =$$$ "bbcabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bccabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bccbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcaabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcabbcabc". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaac". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaac". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "ccabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccaaccaab". In this case the string does not contain "abc" as a substring and no replacements are needed. | Java 11 | standard input | [
"implementation",
"strings"
] | db473ad780a93983667d12b1357c6e2f | The first line contains two integers $$$n$$$ and $$$q$$$ $$$(1 \le n, q \le 10^5)$$$, the length of the string and the number of queries, respectively. The second line contains the string $$$s$$$, consisting of characters "a", "b" and "c". Each of the next $$$q$$$ lines contains an integer $$$i$$$ and character $$$c$$$ $$$(1 \le i \le n)$$$, index and the value of the new item in the string, respectively. It is guaranteed that character's $$$c$$$ value is "a", "b" or "c". | 1,100 | For each query output the minimal number of characters that would have to be replaced so that the string doesn't contain "abc" as a substring. | standard output | |
PASSED | 540b11368ed0b8a1fddfe4eb7d32ff5c | train_110.jsonl | 1638110100 | Before becoming a successful trader William got a university degree. During his education an interesting situation happened, after which William started to listen to homework assignments much more attentively. What follows is the correct formal description of the homework assignment:You are given a string $$$s$$$ of length $$$n$$$ only consisting of characters "a", "b" and "c". There are $$$q$$$ queries of format ($$$pos, c$$$), meaning replacing the element of string $$$s$$$ at position $$$pos$$$ with character $$$c$$$. After each query you must output the minimal number of characters in the string, which have to be replaced, so that the string doesn't contain string "abc" as a substring. A valid replacement of a character is replacing it with "a", "b" or "c".A string $$$x$$$ is a substring of a string $$$y$$$ if $$$x$$$ can be obtained from $$$y$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. | 256 megabytes | import java.io.*;
import java.util.*;
import java.math.BigInteger;
public class Main {
private boolean oj = System.getProperty("ONLINE_JUDGE") != null;
private static FastWriter wr;
private static Reader rd;
public final int MOD = 1000000007;
/************************************************** FAST INPUT IMPLEMENTATION *********************************************/
class Reader {
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
private int line_length;
public Reader(int ll, File filename) throws FileNotFoundException {
din = new DataInputStream(new FileInputStream(filename));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
line_length = ll;
}
public Reader(int ll) throws FileNotFoundException {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
line_length = ll;
}
public Reader(String file_name) throws IOException {
din = new DataInputStream(
new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException {
byte[] buf = new byte[line_length]; // line length
int cnt = 0, c;
while ((c = read()) != -1) {
if (c == '\n') {
if (cnt != 0) {
break;
} else {
continue;
}
}
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException {
int ret = 0;
byte c = read();
while (c <= ' ') {
c = read();
}
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException {
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException {
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (c == '.') {
while ((c = read()) >= '0' && c <= '9') {
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException {
bytesRead = din.read(buffer, bufferPointer = 0,
BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException {
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException {
if (din == null)
return;
din.close();
}
}
public int ni() throws IOException {
return rd.nextInt();
}
public long nl() throws IOException {
return rd.nextLong();
}
public String ns() throws IOException {
return rd.readLine().trim();
}
public Double nd() throws IOException {
return rd.nextDouble();
}
public int[] nai(int n) throws IOException {
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = ni();
}
return arr;
}
public long[] nal(int n) throws IOException {
long[] arr = new long[n];
for (int i = 0; i < n; i++) {
arr[i] = nl();
}
return arr;
}
/************************************************** FAST OUTPUT IMPLEMENTATION *********************************************/
public static class FastWriter {
private static final int BUF_SIZE = 1 << 13;
private final byte[] buf = new byte[BUF_SIZE];
private final OutputStream out;
private int ptr = 0;
private FastWriter() {
out = null;
}
public FastWriter(OutputStream os) {
this.out = os;
}
public FastWriter(String path) {
try {
this.out = new FileOutputStream(path);
} catch (FileNotFoundException e) {
throw new RuntimeException("FastWriter");
}
}
public FastWriter write(byte b) {
buf[ptr++] = b;
if (ptr == BUF_SIZE) innerflush();
return this;
}
public FastWriter write(char c) {
return write((byte) c);
}
public FastWriter write(char[] s) {
for (char c : s) {
buf[ptr++] = (byte) c;
if (ptr == BUF_SIZE) innerflush();
}
return this;
}
public FastWriter write(String s) {
s.chars().forEach(c -> {
buf[ptr++] = (byte) c;
if (ptr == BUF_SIZE) innerflush();
});
return this;
}
private static int countDigits(int l) {
if (l >= 1000000000) return 10;
if (l >= 100000000) return 9;
if (l >= 10000000) return 8;
if (l >= 1000000) return 7;
if (l >= 100000) return 6;
if (l >= 10000) return 5;
if (l >= 1000) return 4;
if (l >= 100) return 3;
if (l >= 10) return 2;
return 1;
}
public FastWriter write(int x) {
if (x == Integer.MIN_VALUE) {
return write((long) x);
}
if (ptr + 12 >= BUF_SIZE) innerflush();
if (x < 0) {
write((byte) '-');
x = -x;
}
int d = countDigits(x);
for (int i = ptr + d - 1; i >= ptr; i--) {
buf[i] = (byte) ('0' + x % 10);
x /= 10;
}
ptr += d;
return this;
}
private static int countDigits(long l) {
if (l >= 1000000000000000000L) return 19;
if (l >= 100000000000000000L) return 18;
if (l >= 10000000000000000L) return 17;
if (l >= 1000000000000000L) return 16;
if (l >= 100000000000000L) return 15;
if (l >= 10000000000000L) return 14;
if (l >= 1000000000000L) return 13;
if (l >= 100000000000L) return 12;
if (l >= 10000000000L) return 11;
if (l >= 1000000000L) return 10;
if (l >= 100000000L) return 9;
if (l >= 10000000L) return 8;
if (l >= 1000000L) return 7;
if (l >= 100000L) return 6;
if (l >= 10000L) return 5;
if (l >= 1000L) return 4;
if (l >= 100L) return 3;
if (l >= 10L) return 2;
return 1;
}
public FastWriter write(long x) {
if (x == Long.MIN_VALUE) {
return write("" + x);
}
if (ptr + 21 >= BUF_SIZE) innerflush();
if (x < 0) {
write((byte) '-');
x = -x;
}
int d = countDigits(x);
for (int i = ptr + d - 1; i >= ptr; i--) {
buf[i] = (byte) ('0' + x % 10);
x /= 10;
}
ptr += d;
return this;
}
public FastWriter write(double x, int precision) {
if (x < 0) {
write('-');
x = -x;
}
x += Math.pow(10, -precision) / 2;
// if(x < 0){ x = 0; }
write((long) x).write(".");
x -= (long) x;
for (int i = 0; i < precision; i++) {
x *= 10;
write((char) ('0' + (int) x));
x -= (int) x;
}
return this;
}
public FastWriter writeln(char c) {
return write(c).writeln();
}
public FastWriter writeln(int x) {
return write(x).writeln();
}
public FastWriter writeln(long x) {
return write(x).writeln();
}
public FastWriter writeln(double x, int precision) {
return write(x, precision).writeln();
}
public FastWriter write(int... xs) {
boolean first = true;
for (int x : xs) {
if (!first) write(' ');
first = false;
write(x);
}
return this;
}
public FastWriter write(long... xs) {
boolean first = true;
for (long x : xs) {
if (!first) write(' ');
first = false;
write(x);
}
return this;
}
public FastWriter writeln() {
return write((byte) '\n');
}
public FastWriter writeln(int... xs) {
return write(xs).writeln();
}
public FastWriter writeln(long... xs) {
return write(xs).writeln();
}
public FastWriter writeln(char[] line) {
return write(line).writeln();
}
public FastWriter writeln(char[]... map) {
for (char[] line : map) write(line).writeln();
return this;
}
public FastWriter writeln(String s) {
return write(s).writeln();
}
private void innerflush() {
try {
out.write(buf, 0, ptr);
ptr = 0;
} catch (IOException e) {
throw new RuntimeException("innerflush");
}
}
public void flush() {
innerflush();
try {
out.flush();
} catch (IOException e) {
throw new RuntimeException("flush");
}
}
public FastWriter print(byte b) {
return write(b);
}
public FastWriter print(char c) {
return write(c);
}
public FastWriter print(char[] s) {
return write(s);
}
public FastWriter print(String s) {
return write(s);
}
public FastWriter print(int x) {
return write(x);
}
public FastWriter print(long x) {
return write(x);
}
public FastWriter print(double x, int precision) {
return write(x, precision);
}
public FastWriter println(char c) {
return writeln(c);
}
public FastWriter println(int x) {
return writeln(x);
}
public FastWriter println(long x) {
return writeln(x);
}
public FastWriter println(double x, int precision) {
return writeln(x, precision);
}
public FastWriter print(int... xs) {
return write(xs);
}
public FastWriter print(long... xs) {
return write(xs);
}
public FastWriter println(int... xs) {
return writeln(xs);
}
public FastWriter println(long... xs) {
return writeln(xs);
}
public FastWriter println(char[] line) {
return writeln(line);
}
public FastWriter println(char[]... map) {
return writeln(map);
}
public FastWriter println(String s) {
return writeln(s);
}
public FastWriter println() {
return writeln();
}
}
/********************************************************* USEFUL CODE **************************************************/
boolean[] SAPrimeGenerator(int n) {
// TC-N*LOG(LOG N)
//Create Prime Marking Array and fill it with true value
boolean[] primeMarker = new boolean[n + 1];
Arrays.fill(primeMarker, true);
primeMarker[0] = false;
primeMarker[1] = false;
for (int i = 2; i <= n; i++) {
if (primeMarker[i]) {
// we start from 2*i because i*1 must be prime
for (int j = 2 * i; j <= n; j += i) {
primeMarker[j] = false;
}
}
}
return primeMarker;
}
private void tr(Object... o) {
if (!oj) System.out.println(Arrays.deepToString(o));
}
class Pair<F, S> {
private F first;
private S second;
Pair(F first, S second) {
this.first = first;
this.second = second;
}
public F getFirst() {
return first;
}
public S getSecond() {
return second;
}
@Override
public String toString() {
return "Pair{" +
"first=" + first +
", second=" + second +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Pair<?, ?> pair = (Pair<?, ?>) o;
return Objects.equals(first, pair.first) && Objects.equals(second, pair.second);
}
}
public static void main(String[] args) throws IOException {
new Main().run();
}
public void run() throws IOException {
if (oj) {
rd = new Reader(1000000);
wr = new FastWriter(System.out);
} else {
File input = new File("input.txt");
File output = new File("output.txt");
if (input.exists() && output.exists()) {
rd = new Reader(1000000, input);
wr = new FastWriter(output.getPath());
} else {
rd = new Reader(1000000);
wr = new FastWriter(System.out);
oj = true;
}
}
long s = System.currentTimeMillis();
solve();
wr.flush();
tr(System.currentTimeMillis() - s + "ms");
}
/***************************************************************************************************************************
*********************************************************** MAIN CODE ******************************************************
****************************************************************************************************************************/
public void solve() throws IOException {
// int t = ni();
// while (t-- > 0) {
go();
// }
}
/********************************************************* MAIN LOGIC HERE ****************************************************/
public int getABC(char[] arr,int s,int e){
int count=0;
for(int i=s;i<=e-2;i++){
if(arr[i]=='a'&&arr[i+1]=='b'&&arr[i+2]=='c'){
count++;
}
}
return count;
}
int f(char[] s, int from) {
for (int i = 0; i < 3; i++) {
if (from + i < 0 || from + i >= s.length) {
return 0;
}
if (s[from + i] != (char)('a' + i)) {
return 0;
}
}
return 1;
}
public void go() throws IOException {
int n = ni();
int q = ni();
String st = ns();
char[] s = st.toCharArray();
int ans = 0;
for (int i = 0; i < n; i++) {
ans += f(s, i);
}
while (q-- > 0) {
int idx = ni() - 1;
char nc =(char)rd.read();
for (int i = idx - 2; i <= idx; i++) {
ans -= f(s, i);
}
s[idx] = nc;
for (int i = idx - 2; i <= idx; i++) {
ans += f(s, i);
}
wr.println(ans);
}
}
} | Java | ["9 10\nabcabcabc\n1 a\n1 b\n2 c\n3 a\n4 b\n5 c\n8 a\n9 b\n1 c\n4 a"] | 2 seconds | ["3\n2\n2\n2\n1\n2\n1\n1\n1\n0"] | NoteLet's consider the state of the string after each query: $$$s =$$$ "abcabcabc". In this case $$$3$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcaccabb". This string does not contain "abc" as a substring. $$$s =$$$ "bbcabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bccabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bccbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcaabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcabbcabc". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaac". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaac". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "ccabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccaaccaab". In this case the string does not contain "abc" as a substring and no replacements are needed. | Java 11 | standard input | [
"implementation",
"strings"
] | db473ad780a93983667d12b1357c6e2f | The first line contains two integers $$$n$$$ and $$$q$$$ $$$(1 \le n, q \le 10^5)$$$, the length of the string and the number of queries, respectively. The second line contains the string $$$s$$$, consisting of characters "a", "b" and "c". Each of the next $$$q$$$ lines contains an integer $$$i$$$ and character $$$c$$$ $$$(1 \le i \le n)$$$, index and the value of the new item in the string, respectively. It is guaranteed that character's $$$c$$$ value is "a", "b" or "c". | 1,100 | For each query output the minimal number of characters that would have to be replaced so that the string doesn't contain "abc" as a substring. | standard output | |
PASSED | d5ced5b09b41d854c27fc79c7eac8dc3 | train_110.jsonl | 1638110100 | Before becoming a successful trader William got a university degree. During his education an interesting situation happened, after which William started to listen to homework assignments much more attentively. What follows is the correct formal description of the homework assignment:You are given a string $$$s$$$ of length $$$n$$$ only consisting of characters "a", "b" and "c". There are $$$q$$$ queries of format ($$$pos, c$$$), meaning replacing the element of string $$$s$$$ at position $$$pos$$$ with character $$$c$$$. After each query you must output the minimal number of characters in the string, which have to be replaced, so that the string doesn't contain string "abc" as a substring. A valid replacement of a character is replacing it with "a", "b" or "c".A string $$$x$$$ is a substring of a string $$$y$$$ if $$$x$$$ can be obtained from $$$y$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main {
static char[] s;
static int n;
public static void main(String[] args) {
FastScanner fs=new FastScanner();
/****** CODE STARTS HERE *****/
//------------------------------------------------------------------------------------------------------------
n = fs.nextInt() ; int q = fs.nextInt();
s = fs.next().toCharArray();
int cnt = 0;
for(int i=0; i<n; i++) cnt+=f(i);
for(int i=0; i<q; i++) {
int pos = fs.nextInt()-1;
char ch = fs.next().charAt(0);
cnt -= f(pos);
cnt -= f(pos-1);
cnt -= f(pos-2);
s[pos] = ch;
cnt += f(pos);
cnt += f(pos-1);
cnt += f(pos-2);
System.out.println(cnt);
}
}
static int f(int pos) {
return (pos+2<n && pos>=0 && s[pos]=='a' && s[pos+1]=='b' && s[pos+2]=='c') ? 1:0;
}
//****** CODE ENDS HERE *****
//----------------------------------------------------------------------------------------------------------------
static void sort(int[] a) {
ArrayList<Integer> l=new ArrayList<>();
for (int i:a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
//----------- FastScanner class for faster input---------------------------
static class FastScanner {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
}
} | Java | ["9 10\nabcabcabc\n1 a\n1 b\n2 c\n3 a\n4 b\n5 c\n8 a\n9 b\n1 c\n4 a"] | 2 seconds | ["3\n2\n2\n2\n1\n2\n1\n1\n1\n0"] | NoteLet's consider the state of the string after each query: $$$s =$$$ "abcabcabc". In this case $$$3$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcaccabb". This string does not contain "abc" as a substring. $$$s =$$$ "bbcabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bccabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bccbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcaabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcabbcabc". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaac". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaac". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "ccabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccaaccaab". In this case the string does not contain "abc" as a substring and no replacements are needed. | Java 11 | standard input | [
"implementation",
"strings"
] | db473ad780a93983667d12b1357c6e2f | The first line contains two integers $$$n$$$ and $$$q$$$ $$$(1 \le n, q \le 10^5)$$$, the length of the string and the number of queries, respectively. The second line contains the string $$$s$$$, consisting of characters "a", "b" and "c". Each of the next $$$q$$$ lines contains an integer $$$i$$$ and character $$$c$$$ $$$(1 \le i \le n)$$$, index and the value of the new item in the string, respectively. It is guaranteed that character's $$$c$$$ value is "a", "b" or "c". | 1,100 | For each query output the minimal number of characters that would have to be replaced so that the string doesn't contain "abc" as a substring. | standard output | |
PASSED | 70653e199117e3b5bf814bbfebc22476 | train_110.jsonl | 1638110100 | Before becoming a successful trader William got a university degree. During his education an interesting situation happened, after which William started to listen to homework assignments much more attentively. What follows is the correct formal description of the homework assignment:You are given a string $$$s$$$ of length $$$n$$$ only consisting of characters "a", "b" and "c". There are $$$q$$$ queries of format ($$$pos, c$$$), meaning replacing the element of string $$$s$$$ at position $$$pos$$$ with character $$$c$$$. After each query you must output the minimal number of characters in the string, which have to be replaced, so that the string doesn't contain string "abc" as a substring. A valid replacement of a character is replacing it with "a", "b" or "c".A string $$$x$$$ is a substring of a string $$$y$$$ if $$$x$$$ can be obtained from $$$y$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
FastScanner fs=new FastScanner();
/****** CODE STARTS HERE *****/
//------------------------------------------------------------------------------------------------------------
int cnt = 1;
int n = fs.nextInt();
int q = fs.nextInt();
String f = fs.next();
char[] s = f.toCharArray();
Set<Integer> set = new HashSet<>();
for(int i=0; i<=n-3; i++) {
if(s[i] == 'a' && s[i+1] == 'b' && s[i+2] == 'c') {
set.add(i);
}
}
for(int i=0; i<q; i++) {
int pos = fs.nextInt()-1;
char ch = fs.next().charAt(0);
if(s[pos]=='a' && set.contains(pos)) {
if(ch != 'a')set.remove(pos);
}else if(s[pos]=='b' && set.contains(pos-1)) {
if(ch != 'b')set.remove(pos-1);
}else if(s[pos]=='c' && set.contains(pos-2)) {
if(ch != 'c')set.remove(pos-2);
}
if(ch=='a' && pos+1<n && pos+2<n && s[pos+1]=='b' && s[pos+2]=='c')set.add(pos);
if(ch=='b' && pos-1>=0 && pos+1<n && s[pos-1]=='a' && s[pos+1]=='c')set.add(pos-1);
if(ch=='c' && pos-1>=0 && pos-2>=0 && s[pos-1]=='b' && s[pos-2]=='a')set.add(pos-2);
s[pos] = ch;
System.out.println(set.size());
cnt++;
}
}
//****** CODE ENDS HERE *****
//----------------------------------------------------------------------------------------------------------------
static void sort(int[] a) {
ArrayList<Integer> l=new ArrayList<>();
for (int i:a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
//----------- FastScanner class for faster input---------------------------
static class FastScanner {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
}
} | Java | ["9 10\nabcabcabc\n1 a\n1 b\n2 c\n3 a\n4 b\n5 c\n8 a\n9 b\n1 c\n4 a"] | 2 seconds | ["3\n2\n2\n2\n1\n2\n1\n1\n1\n0"] | NoteLet's consider the state of the string after each query: $$$s =$$$ "abcabcabc". In this case $$$3$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcaccabb". This string does not contain "abc" as a substring. $$$s =$$$ "bbcabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bccabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bccbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcaabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcabbcabc". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaac". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaac". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "ccabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccaaccaab". In this case the string does not contain "abc" as a substring and no replacements are needed. | Java 11 | standard input | [
"implementation",
"strings"
] | db473ad780a93983667d12b1357c6e2f | The first line contains two integers $$$n$$$ and $$$q$$$ $$$(1 \le n, q \le 10^5)$$$, the length of the string and the number of queries, respectively. The second line contains the string $$$s$$$, consisting of characters "a", "b" and "c". Each of the next $$$q$$$ lines contains an integer $$$i$$$ and character $$$c$$$ $$$(1 \le i \le n)$$$, index and the value of the new item in the string, respectively. It is guaranteed that character's $$$c$$$ value is "a", "b" or "c". | 1,100 | For each query output the minimal number of characters that would have to be replaced so that the string doesn't contain "abc" as a substring. | standard output | |
PASSED | 9ddaeb5f952ccf7a3df41dde13d33a79 | train_110.jsonl | 1638110100 | Before becoming a successful trader William got a university degree. During his education an interesting situation happened, after which William started to listen to homework assignments much more attentively. What follows is the correct formal description of the homework assignment:You are given a string $$$s$$$ of length $$$n$$$ only consisting of characters "a", "b" and "c". There are $$$q$$$ queries of format ($$$pos, c$$$), meaning replacing the element of string $$$s$$$ at position $$$pos$$$ with character $$$c$$$. After each query you must output the minimal number of characters in the string, which have to be replaced, so that the string doesn't contain string "abc" as a substring. A valid replacement of a character is replacing it with "a", "b" or "c".A string $$$x$$$ is a substring of a string $$$y$$$ if $$$x$$$ can be obtained from $$$y$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. | 256 megabytes | import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.StringTokenizer;
import java.util.function.Supplier;
import java.util.stream.Collectors;
public class codeforces {
//-----------PrintWriter for faster output---------------------------------
public static PrintWriter out;
public static int cnt;
//-----------MyScanner class for faster input----------
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine(){
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
static boolean isPrime(int n)
{
// Corner cases
if (n <= 1)
return false;
if (n <= 3)
return true;
// This is checked so that we can skip
// middle five numbers in below loop
if (n % 2 == 0 || n % 3 == 0)
return false;
for (int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
public static int reverseBits(int n)
{
int rev = 0;
// traversing bits of 'n'
// from the right
while (n > 0)
{
// bitwise left shift
// 'rev' by 1
rev <<= 1;
// if current bit is '1'
if ((int)(n & 1) == 1)
rev ^= 1;
// bitwise right shift
//'n' by 1
n >>= 1;
}
// required number
return rev;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
MyScanner sc = new MyScanner();
out = new PrintWriter(new BufferedOutputStream(System.out));
StringBuilder sb = new StringBuilder();
// int t=sc.nextInt();
// while(t-->0)
// {
int n=sc.nextInt();
int q=sc.nextInt();
String s=sc.next();
String s1=s;
s1=s1.replaceAll("abc", "");
int c=(n-s1.length())/3;
char arr[]=new char[n];
for(int i=0;i<n;i++)
{
arr[i]=s.charAt(i);
}
while(q-->0)
{
int x=sc.nextInt();
char c1=sc.next().charAt(0);
if(c1==arr[x-1])
{
}
else
{
if(c1=='a')
{
if(x>=0 && x+2<=n)
{
if(arr[x]=='b' && arr[x+1]=='c')
{
c++;
}
}
}
else if(c1=='b')
{
if(x-2>=0 && x<n)
{
if(arr[x-2]=='a' &&arr[x]=='c')
{
c++;
}
}
}
else if(c1=='c')
{
if(x-3>=0 && x-1<=n)
{
if(arr[x-3]=='a' && arr[x-2]=='b')
{
c++;
}
}
}
if(arr[x-1]=='a' && x-1>=0 && x+1<n)
{
if(arr[x]=='b' && arr[x+1]=='c')
{
c--;
}
}
else if(arr[x-1]=='b' && x-2>=0 && x<n)
{
if(arr[x-2]=='a' && arr[x]=='c')
{
c--;
}
}
else if(arr[x-1]=='c' && x-3>=0 && x-1<n)
{
if(arr[x-3]=='a' && arr[x-2]=='b')
{
c--;
}
}
}
arr[x-1]=c1;
sb.append(c+" \n");
}
out.println(sb);
// }
out.close();
}
}
| Java | ["9 10\nabcabcabc\n1 a\n1 b\n2 c\n3 a\n4 b\n5 c\n8 a\n9 b\n1 c\n4 a"] | 2 seconds | ["3\n2\n2\n2\n1\n2\n1\n1\n1\n0"] | NoteLet's consider the state of the string after each query: $$$s =$$$ "abcabcabc". In this case $$$3$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcaccabb". This string does not contain "abc" as a substring. $$$s =$$$ "bbcabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bccabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bccbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcaabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcabbcabc". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaac". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaac". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "ccabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccaaccaab". In this case the string does not contain "abc" as a substring and no replacements are needed. | Java 11 | standard input | [
"implementation",
"strings"
] | db473ad780a93983667d12b1357c6e2f | The first line contains two integers $$$n$$$ and $$$q$$$ $$$(1 \le n, q \le 10^5)$$$, the length of the string and the number of queries, respectively. The second line contains the string $$$s$$$, consisting of characters "a", "b" and "c". Each of the next $$$q$$$ lines contains an integer $$$i$$$ and character $$$c$$$ $$$(1 \le i \le n)$$$, index and the value of the new item in the string, respectively. It is guaranteed that character's $$$c$$$ value is "a", "b" or "c". | 1,100 | For each query output the minimal number of characters that would have to be replaced so that the string doesn't contain "abc" as a substring. | standard output | |
PASSED | 54550a9afcb532d9b2b44f391865738a | train_110.jsonl | 1638110100 | Before becoming a successful trader William got a university degree. During his education an interesting situation happened, after which William started to listen to homework assignments much more attentively. What follows is the correct formal description of the homework assignment:You are given a string $$$s$$$ of length $$$n$$$ only consisting of characters "a", "b" and "c". There are $$$q$$$ queries of format ($$$pos, c$$$), meaning replacing the element of string $$$s$$$ at position $$$pos$$$ with character $$$c$$$. After each query you must output the minimal number of characters in the string, which have to be replaced, so that the string doesn't contain string "abc" as a substring. A valid replacement of a character is replacing it with "a", "b" or "c".A string $$$x$$$ is a substring of a string $$$y$$$ if $$$x$$$ can be obtained from $$$y$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. | 256 megabytes |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Scanner;
import java.util.StringTokenizer;
public class B {
static class RealScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
}
static int n;
static char[] ch;
static boolean ret(int a) {
return 0 <= a && a + 2 < n && ch[a] == 'a' && ch[a + 1] == 'b' && ch[a + 2] == 'c';
}
public static void main(String[] args) {
PrintWriter out = new PrintWriter(System.out);
RealScanner sc = new RealScanner();
int q;
n = sc.nextInt();
q = sc.nextInt();
ch = sc.next().toCharArray();
int count = 0;
for (int i = 0; i < n - 2; i++) {
if (ch[i] == 'a' && ch[i + 1] == 'b' && ch[i + 2] == 'c') {
count++;
i += 2;
}
}
// System.out.println(count);
for (int i = 0; i < q; i++) {
int a = sc.nextInt();
String s = sc.next();
char my = s.charAt(0);
a--;
if (ret(a - 2)) {
count--;
}
if (ret(a - 1)) {
count--;
}
if (ret(a)) {
count--;
}
ch[a] = my;
if (ret(a - 2)) {
count++;
}
if (ret(a - 1)) {
count++;
}
if (ret(a)) {
count++;
}
out.println(count);
}
out.flush();
out.close();
}
}
| Java | ["9 10\nabcabcabc\n1 a\n1 b\n2 c\n3 a\n4 b\n5 c\n8 a\n9 b\n1 c\n4 a"] | 2 seconds | ["3\n2\n2\n2\n1\n2\n1\n1\n1\n0"] | NoteLet's consider the state of the string after each query: $$$s =$$$ "abcabcabc". In this case $$$3$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcaccabb". This string does not contain "abc" as a substring. $$$s =$$$ "bbcabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bccabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bccbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcaabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcabbcabc". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaac". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaac". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "ccabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccaaccaab". In this case the string does not contain "abc" as a substring and no replacements are needed. | Java 11 | standard input | [
"implementation",
"strings"
] | db473ad780a93983667d12b1357c6e2f | The first line contains two integers $$$n$$$ and $$$q$$$ $$$(1 \le n, q \le 10^5)$$$, the length of the string and the number of queries, respectively. The second line contains the string $$$s$$$, consisting of characters "a", "b" and "c". Each of the next $$$q$$$ lines contains an integer $$$i$$$ and character $$$c$$$ $$$(1 \le i \le n)$$$, index and the value of the new item in the string, respectively. It is guaranteed that character's $$$c$$$ value is "a", "b" or "c". | 1,100 | For each query output the minimal number of characters that would have to be replaced so that the string doesn't contain "abc" as a substring. | standard output | |
PASSED | babcd842afefb775901a9c532c0a39ab | train_110.jsonl | 1638110100 | Before becoming a successful trader William got a university degree. During his education an interesting situation happened, after which William started to listen to homework assignments much more attentively. What follows is the correct formal description of the homework assignment:You are given a string $$$s$$$ of length $$$n$$$ only consisting of characters "a", "b" and "c". There are $$$q$$$ queries of format ($$$pos, c$$$), meaning replacing the element of string $$$s$$$ at position $$$pos$$$ with character $$$c$$$. After each query you must output the minimal number of characters in the string, which have to be replaced, so that the string doesn't contain string "abc" as a substring. A valid replacement of a character is replacing it with "a", "b" or "c".A string $$$x$$$ is a substring of a string $$$y$$$ if $$$x$$$ can be obtained from $$$y$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. | 256 megabytes | import java.util.*;
import java.lang.*;
import java.io.*;
public class Main
{
static long mod = (int)1e9+7;
static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
public static void main (String[] args) throws java.lang.Exception
{
FastReader sc =new FastReader();
// int t=sc.nextInt();
int t=1;
while(t-->0)
{
int n = sc.nextInt();
int queries = sc.nextInt();
String s1 = sc.next();
char arr[] = s1.toCharArray();
HashSet<Integer> set = new HashSet<>();
for(int i=0;i<n;i++)
{
if(arr[i] == 'a')
{
if(i + 2 < n)
{
if(arr[i + 1] == 'b' && arr[i + 2] == 'c')
{
set.add(i);
}
}
}
}
while(queries --> 0)
{
int posi = sc.nextInt() - 1;
char ch = sc.next().charAt(0);
arr[posi] = ch;
for(int i=Math.max(0 , posi - 4);i<=(Math.min(n - 3 , posi + 4));i++)
{
if(arr[i] == 'a' && arr[i+1] == 'b' && arr[i+2] == 'c')
{
set.add(i);
}
else
{
set.remove(i);
}
}
out.println(set.size());
}
out.flush();
}
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
float nextFloat()
{
return Float.parseFloat(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
int[] readArray(int n) {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
long[] readArrayLong(int n) {
long[] a=new long[n];
for (int i=0; i<n; i++) a[i]=nextLong();
return a;
}
}
public static int[] radixSort2(int[] a)
{
int n = a.length;
int[] c0 = new int[0x101];
int[] c1 = new int[0x101];
int[] c2 = new int[0x101];
int[] c3 = new int[0x101];
for(int v : a) {
c0[(v&0xff)+1]++;
c1[(v>>>8&0xff)+1]++;
c2[(v>>>16&0xff)+1]++;
c3[(v>>>24^0x80)+1]++;
}
for(int i = 0;i < 0xff;i++) {
c0[i+1] += c0[i];
c1[i+1] += c1[i];
c2[i+1] += c2[i];
c3[i+1] += c3[i];
}
int[] t = new int[n];
for(int v : a)t[c0[v&0xff]++] = v;
for(int v : t)a[c1[v>>>8&0xff]++] = v;
for(int v : a)t[c2[v>>>16&0xff]++] = v;
for(int v : t)a[c3[v>>>24^0x80]++] = v;
return a;
}
static void reverse_sorted(int[] arr)
{
ArrayList<Integer> list = new ArrayList<>();
for(int i=0;i<arr.length;i++)
{
list.add(arr[i]);
}
Collections.sort(list , Collections.reverseOrder());
for(int i=0;i<arr.length;i++)
{
arr[i] = list.get(i);
}
}
static int LowerBound(int a[], int x) { // x is the target value or key
int l=-1,r=a.length;
while(l+1<r) {
int m=(l+r)>>>1;
if(a[m]>=x) r=m;
else l=m;
}
return r;
}
static int UpperBound(ArrayList<Integer> list, int x) {// x is the key or target value
int l=-1,r=list.size();
while(l+1<r) {
int m=(l+r)>>>1;
if(list.get(m)<=x) l=m;
else r=m;
}
return l+1;
}
public static HashMap<Integer, Integer> sortByValue(HashMap<Integer, Integer> hm)
{
// Create a list from elements of HashMap
List<Map.Entry<Integer, Integer> > list =
new LinkedList<Map.Entry<Integer, Integer> >(hm.entrySet());
// Sort the list
Collections.sort(list, new Comparator<Map.Entry<Integer, Integer> >() {
public int compare(Map.Entry<Integer, Integer> o1,
Map.Entry<Integer, Integer> o2)
{
return (o1.getValue()).compareTo(o2.getValue());
}
});
// put data from sorted list to hashmap
HashMap<Integer, Integer> temp = new LinkedHashMap<Integer, Integer>();
for (Map.Entry<Integer, Integer> aa : list) {
temp.put(aa.getKey(), aa.getValue());
}
return temp;
}
static class Queue_Pair implements Comparable<Queue_Pair> {
int first , second;
public Queue_Pair(int first, int second) {
this.first=first;
this.second=second;
}
public int compareTo(Queue_Pair o) {
return Integer.compare(o.first, first);
}
}
static void leftRotate(int arr[], int d, int n)
{
for (int i = 0; i < d; i++)
leftRotatebyOne(arr, n);
}
static void leftRotatebyOne(int arr[], int n)
{
int i, temp;
temp = arr[0];
for (i = 0; i < n - 1; i++)
arr[i] = arr[i + 1];
arr[n-1] = temp;
}
static boolean isPalindrome(String str)
{
// Pointers pointing to the beginning
// and the end of the string
int i = 0, j = str.length() - 1;
// While there are characters to compare
while (i < j) {
// If there is a mismatch
if (str.charAt(i) != str.charAt(j))
return false;
// Increment first pointer and
// decrement the other
i++;
j--;
}
// Given string is a palindrome
return true;
}
static boolean palindrome_array(char arr[], int n)
{
// Initialise flag to zero.
int flag = 0;
// Loop till array size n/2.
for (int i = 0; i <= n / 2 && n != 0; i++) {
// Check if first and last element are different
// Then set flag to 1.
if (arr[i] != arr[n - i - 1]) {
flag = 1;
break;
}
}
// If flag is set then print Not Palindrome
// else print Palindrome.
if (flag == 1)
return false;
else
return true;
}
static boolean allElementsEqual(int[] arr,int n)
{
int z=0;
for(int i=0;i<n-1;i++)
{
if(arr[i]==arr[i+1])
{
z++;
}
}
if(z==n-1)
{
return true;
}
else
{
return false;
}
}
static boolean allElementsDistinct(int[] arr,int n)
{
int z=0;
for(int i=0;i<n-1;i++)
{
if(arr[i]!=arr[i+1])
{
z++;
}
}
if(z==n-1)
{
return true;
}
else
{
return false;
}
}
public static void reverse(int[] array)
{
// Length of the array
int n = array.length;
// Swaping the first half elements with last half
// elements
for (int i = 0; i < n / 2; i++) {
// Storing the first half elements temporarily
int temp = array[i];
// Assigning the first half to the last half
array[i] = array[n - i - 1];
// Assigning the last half to the first half
array[n - i - 1] = temp;
}
}
public static void reverse_Long(long[] array)
{
// Length of the array
int n = array.length;
// Swaping the first half elements with last half
// elements
for (int i = 0; i < n / 2; i++) {
// Storing the first half elements temporarily
long temp = array[i];
// Assigning the first half to the last half
array[i] = array[n - i - 1];
// Assigning the last half to the first half
array[n - i - 1] = temp;
}
}
static boolean isSorted(int[] a)
{
for (int i = 0; i < a.length - 1; i++)
{
if (a[i] > a[i + 1]) {
return false;
}
}
return true;
}
static boolean isReverseSorted(int[] a)
{
for (int i = 0; i < a.length - 1; i++)
{
if (a[i] < a[i + 1]) {
return false;
}
}
return true;
}
static int[] rearrangeEvenAndOdd(int arr[], int n)
{
ArrayList<Integer> list = new ArrayList<>();
for(int i=0;i<n;i++)
{
if(arr[i]%2==0)
{
list.add(arr[i]);
}
}
for(int i=0;i<n;i++)
{
if(arr[i]%2!=0)
{
list.add(arr[i]);
}
}
int len = list.size();
int[] array = list.stream().mapToInt(i->i).toArray();
return array;
}
static long[] rearrangeEvenAndOddLong(long arr[], int n)
{
ArrayList<Long> list = new ArrayList<>();
for(int i=0;i<n;i++)
{
if(arr[i]%2==0)
{
list.add(arr[i]);
}
}
for(int i=0;i<n;i++)
{
if(arr[i]%2!=0)
{
list.add(arr[i]);
}
}
int len = list.size();
long[] array = list.stream().mapToLong(i->i).toArray();
return array;
}
static boolean isPrime(long n)
{
// Check if number is less than
// equal to 1
if (n <= 1)
return false;
// Check if number is 2
else if (n == 2)
return true;
// Check if n is a multiple of 2
else if (n % 2 == 0)
return false;
// If not, then just check the odds
for (long i = 3; i <= Math.sqrt(n); i += 2)
{
if (n % i == 0)
return false;
}
return true;
}
static long getSum(long n)
{
long sum = 0;
while (n != 0)
{
sum = sum + n % 10;
n = n/10;
}
return sum;
}
static int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
static long gcdLong(long a, long b)
{
if (b == 0)
return a;
return gcdLong(b, a % b);
}
static void swap(int i, int j)
{
int temp = i;
i = j;
j = temp;
}
static int countDigit(int n)
{
return (int)Math.floor(Math.log10(n) + 1);
}
}
class Pair
{
int first;
int second;
Pair(int first , int second)
{
this.first = first;
this.second = second;
}
}
| Java | ["9 10\nabcabcabc\n1 a\n1 b\n2 c\n3 a\n4 b\n5 c\n8 a\n9 b\n1 c\n4 a"] | 2 seconds | ["3\n2\n2\n2\n1\n2\n1\n1\n1\n0"] | NoteLet's consider the state of the string after each query: $$$s =$$$ "abcabcabc". In this case $$$3$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcaccabb". This string does not contain "abc" as a substring. $$$s =$$$ "bbcabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bccabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bccbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcaabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcabbcabc". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaac". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaac". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "ccabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccaaccaab". In this case the string does not contain "abc" as a substring and no replacements are needed. | Java 11 | standard input | [
"implementation",
"strings"
] | db473ad780a93983667d12b1357c6e2f | The first line contains two integers $$$n$$$ and $$$q$$$ $$$(1 \le n, q \le 10^5)$$$, the length of the string and the number of queries, respectively. The second line contains the string $$$s$$$, consisting of characters "a", "b" and "c". Each of the next $$$q$$$ lines contains an integer $$$i$$$ and character $$$c$$$ $$$(1 \le i \le n)$$$, index and the value of the new item in the string, respectively. It is guaranteed that character's $$$c$$$ value is "a", "b" or "c". | 1,100 | For each query output the minimal number of characters that would have to be replaced so that the string doesn't contain "abc" as a substring. | standard output | |
PASSED | f0f0c426ff6771b82e159e71f9dd607a | train_110.jsonl | 1638110100 | Before becoming a successful trader William got a university degree. During his education an interesting situation happened, after which William started to listen to homework assignments much more attentively. What follows is the correct formal description of the homework assignment:You are given a string $$$s$$$ of length $$$n$$$ only consisting of characters "a", "b" and "c". There are $$$q$$$ queries of format ($$$pos, c$$$), meaning replacing the element of string $$$s$$$ at position $$$pos$$$ with character $$$c$$$. After each query you must output the minimal number of characters in the string, which have to be replaced, so that the string doesn't contain string "abc" as a substring. A valid replacement of a character is replacing it with "a", "b" or "c".A string $$$x$$$ is a substring of a string $$$y$$$ if $$$x$$$ can be obtained from $$$y$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. | 256 megabytes | import java.io.*;
import java.util.*;
public class Solution
{
static PrintWriter out = new PrintWriter((System.out));
static Kioken sc = new Kioken();
public static void main(String[] args)
{
int t = 1;
solve();
out.close();
}
public static void solve()
{
int n = sc.nextInt();
int q = sc.nextInt();
StringBuffer s = new StringBuffer();
s.append(sc.nextLine());
int[] arr = new int[q];
char[] arr1 = new char[q];
for(int i = 0; i < q; i++){
arr[i] = sc.nextInt();
arr1[i] = sc.next().toCharArray()[0];
}
// TreeSet<Integer> set = new TreeSet<>();
int len = 2;
int cnt = 0;
for(int i = 0; i <= (n - 1 - len); i++){
String ss = s.substring(i, i + len + 1);
if(ss.equals("abc")){
cnt++;
i = i+len;
}
}
// out.println(set);
for(int i = 0; i < q; i++){
int index = arr[i]-1;
char c = arr1[i];
if(s.charAt(index) != c){
boolean flag = false;
if(index < n - 2 && s.charAt(index) == 'a' && s.charAt(index+1) == 'b' && s.charAt(index+2) == 'c'){
cnt--;
flag = true;
}
if(index > 0 && index < n-1 && s.charAt(index) == 'b' && s.charAt(index-1) == 'a' && s.charAt(index+1) == 'c'){
cnt--;
flag = true;
}
if(index > 1 && s.charAt(index) == 'c' && s.charAt(index-1) == 'b' && s.charAt(index-2) == 'a'){
cnt--;
flag = true;
}
s.replace(index, index+1, String.valueOf(c));
if(index < n - 2 && s.charAt(index) == 'a' && s.charAt(index+1) == 'b' && s.charAt(index+2) == 'c'){
cnt++;
flag = true;
}
if(index > 0 && index < n-1 && s.charAt(index) == 'b' && s.charAt(index-1) == 'a' && s.charAt(index+1) == 'c'){
cnt++;
flag = true;
}
if(index > 1 && s.charAt(index) == 'c' && s.charAt(index-1) == 'b' && s.charAt(index-2) == 'a'){
cnt++;
flag = true;
}
out.println(cnt);
}else{
out.println(cnt);
}
// out.println(s + " -- " + set);
}
// out.println(s+a);
}
public static long leftShift(long a){
return (long)Math.pow(2, a);
}
public static int lower_bound(ArrayList<Integer> ar, int k)
{
int s = 0, e = ar.size();
while (s != e)
{
int mid = s + e >> 1;
if (ar.get(mid) <= k)
{
s = mid + 1;
}
else
{
e = mid;
}
}
return Math.abs(s) - 1;
}
public static int upper_bound(ArrayList<Integer> ar, int k)
{
int s = 0;
int e = ar.size();
while (s != e)
{
int mid = s + e >> 1;
if (ar.get(mid) < k)
{
s = mid + 1;
}
else
{
e = mid;
}
}
if (s == ar.size())
{
return -1;
}
return s;
}
static class Kioken
{
// FileInputStream br = new FileInputStream("input.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
public String next()
{
while (!st.hasMoreTokens())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (Exception e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
public int nextInt()
{
return Integer.parseInt(next());
}
public long nextLong()
{
return Long.parseLong(next());
}
public double nextDouble()
{
return Double.parseDouble(next());
}
public String nextLine()
{
try
{
return br.readLine();
}
catch (Exception e)
{
e.printStackTrace();
}
return null;
}
public boolean hasNext()
{
String next = null;
try
{
next = br.readLine();
}
catch (Exception e)
{
}
if (next == null || next.length() == 0)
{
return false;
}
st = new StringTokenizer(next);
return true;
}
}
} | Java | ["9 10\nabcabcabc\n1 a\n1 b\n2 c\n3 a\n4 b\n5 c\n8 a\n9 b\n1 c\n4 a"] | 2 seconds | ["3\n2\n2\n2\n1\n2\n1\n1\n1\n0"] | NoteLet's consider the state of the string after each query: $$$s =$$$ "abcabcabc". In this case $$$3$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcaccabb". This string does not contain "abc" as a substring. $$$s =$$$ "bbcabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bccabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bccbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcaabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcabbcabc". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaac". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaac". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "ccabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccaaccaab". In this case the string does not contain "abc" as a substring and no replacements are needed. | Java 11 | standard input | [
"implementation",
"strings"
] | db473ad780a93983667d12b1357c6e2f | The first line contains two integers $$$n$$$ and $$$q$$$ $$$(1 \le n, q \le 10^5)$$$, the length of the string and the number of queries, respectively. The second line contains the string $$$s$$$, consisting of characters "a", "b" and "c". Each of the next $$$q$$$ lines contains an integer $$$i$$$ and character $$$c$$$ $$$(1 \le i \le n)$$$, index and the value of the new item in the string, respectively. It is guaranteed that character's $$$c$$$ value is "a", "b" or "c". | 1,100 | For each query output the minimal number of characters that would have to be replaced so that the string doesn't contain "abc" as a substring. | standard output | |
PASSED | 2b93516ba64826a957869fcc1d90ddbc | train_110.jsonl | 1638110100 | Before becoming a successful trader William got a university degree. During his education an interesting situation happened, after which William started to listen to homework assignments much more attentively. What follows is the correct formal description of the homework assignment:You are given a string $$$s$$$ of length $$$n$$$ only consisting of characters "a", "b" and "c". There are $$$q$$$ queries of format ($$$pos, c$$$), meaning replacing the element of string $$$s$$$ at position $$$pos$$$ with character $$$c$$$. After each query you must output the minimal number of characters in the string, which have to be replaced, so that the string doesn't contain string "abc" as a substring. A valid replacement of a character is replacing it with "a", "b" or "c".A string $$$x$$$ is a substring of a string $$$y$$$ if $$$x$$$ can be obtained from $$$y$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. | 256 megabytes | import java.io.*;
import java.util.*;
public class WilliamtheVig {
public static void main(String[] args) throws Exception {
CustomScanner sc = new CustomScanner();
CustomPrinter cp = new CustomPrinter();
int len = sc.nextInt();
int queries = sc.nextInt();
char[] chars = sc.next().toCharArray();
int i;
int count = 0;
for (i=0;i<chars.length;i++) {
if (abc(chars, i)) {
count++;
}
}
for (i=0;i<queries;i++){
int idx = sc.nextInt()-1;
char c = sc.next().charAt(0);
int j;
for (j=Math.max(idx-2,0);j<=idx;j++) {
if (abc(chars, j)) {
count--;
}
}
chars[idx] = c;
for (j=Math.max(idx-2,0);j<=idx;j++) {
if (abc(chars, j)) {
count++;
}
}
cp.print(count);
cp.print("\n");
}
cp.close();
}
public static boolean abc(char[] chars, int pos){
if (pos+2 >= chars.length)
return false;
else return chars[pos] == 'a' && chars[pos+1] == 'b' && chars[pos+2] == 'c';
}
public static class CustomScanner {
private final java.io.InputStream in;
private final byte[] buffer = new byte[1024];
private int ptr = 0;
private int buflen = 0;
private static final long LONG_MAX_TENTHS = 922337203685477580L;
private static final int LONG_MAX_LAST_DIGIT = 7;
private static final int LONG_MIN_LAST_DIGIT = 8;
public CustomScanner(java.io.InputStream in) {
this.in = in;
}
public CustomScanner() {
this(System.in);
}
private boolean hasNextByte() {
if (ptr < buflen) {
return true;
} else {
ptr = 0;
try {
buflen = in.read(buffer);
} catch (java.io.IOException e) {
e.printStackTrace();
}
if (buflen <= 0) {
return false;
}
}
return true;
}
private int readByte() {
if (hasNextByte())
return buffer[ptr++];
else
return -1;
}
private static boolean isPrintableChar(int c) {
return 33 <= c && c <= 126;
}
public boolean hasNext() {
while (hasNextByte() && !isPrintableChar(buffer[ptr]))
ptr++;
return hasNextByte();
}
public String next() {
if (!hasNext())
throw new java.util.NoSuchElementException();
StringBuilder sb = new StringBuilder();
int b = readByte();
while (isPrintableChar(b)) {
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
public long nextLong() {
if (!hasNext())
throw new java.util.NoSuchElementException();
long n = 0;
boolean minus = false;
int b = readByte();
if (b == '-') {
minus = true;
b = readByte();
}
if (b < '0' || '9' < b) {
throw new NumberFormatException();
}
while (true) {
if ('0' <= b && b <= '9') {
int digit = b - '0';
if (n >= LONG_MAX_TENTHS) {
if (n == LONG_MAX_TENTHS) {
if (minus) {
if (digit <= LONG_MIN_LAST_DIGIT) {
n = -n * 10 - digit;
b = readByte();
if (!isPrintableChar(b)) {
return n;
} else if (b < '0' || '9' < b) {
throw new NumberFormatException(
String.format("%d%s... is not number", n, Character.toString(b)));
}
}
} else {
if (digit <= LONG_MAX_LAST_DIGIT) {
n = n * 10 + digit;
b = readByte();
if (!isPrintableChar(b)) {
return n;
} else if (b < '0' || '9' < b) {
throw new NumberFormatException(
String.format("%d%s... is not number", n, Character.toString(b)));
}
}
}
}
throw new ArithmeticException(
String.format("%s%d%d... overflows long.", minus ? "-" : "", n, digit));
}
n = n * 10 + digit;
} else if (b == -1 || !isPrintableChar(b)) {
return minus ? -n : n;
} else {
throw new NumberFormatException();
}
b = readByte();
}
}
public int nextInt() {
long nl = nextLong();
if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE)
throw new NumberFormatException();
return (int) nl;
}
public double nextDouble() {
return Double.parseDouble(next());
}
public long[] nextLongArray(int length) {
long[] array = new long[length];
for (int i = 0; i < length; i++)
array[i] = this.nextLong();
return array;
}
public long[] nextLongArray(int length, java.util.function.LongUnaryOperator map) {
long[] array = new long[length];
for (int i = 0; i < length; i++)
array[i] = map.applyAsLong(this.nextLong());
return array;
}
public int[] nextIntArray(int length) {
int[] array = new int[length];
for (int i = 0; i < length; i++)
array[i] = this.nextInt();
return array;
}
public int[][] nextIntArrayMulti(int length, int width) {
int[][] arrays = new int[width][length];
for (int i = 0; i < length; i++) {
for (int j = 0; j < width; j++)
arrays[j][i] = this.nextInt();
}
return arrays;
}
public int[] nextIntArray(int length, java.util.function.IntUnaryOperator map) {
int[] array = new int[length];
for (int i = 0; i < length; i++)
array[i] = map.applyAsInt(this.nextInt());
return array;
}
public double[] nextDoubleArray(int length) {
double[] array = new double[length];
for (int i = 0; i < length; i++)
array[i] = this.nextDouble();
return array;
}
public double[] nextDoubleArray(int length, java.util.function.DoubleUnaryOperator map) {
double[] array = new double[length];
for (int i = 0; i < length; i++)
array[i] = map.applyAsDouble(this.nextDouble());
return array;
}
public long[][] nextLongMatrix(int height, int width) {
long[][] mat = new long[height][width];
for (int h = 0; h < height; h++)
for (int w = 0; w < width; w++) {
mat[h][w] = this.nextLong();
}
return mat;
}
public int[][] nextIntMatrix(int height, int width) {
int[][] mat = new int[height][width];
for (int h = 0; h < height; h++)
for (int w = 0; w < width; w++) {
mat[h][w] = this.nextInt();
}
return mat;
}
public double[][] nextDoubleMatrix(int height, int width) {
double[][] mat = new double[height][width];
for (int h = 0; h < height; h++)
for (int w = 0; w < width; w++) {
mat[h][w] = this.nextDouble();
}
return mat;
}
public char[][] nextCharMatrix(int height, int width) {
char[][] mat = new char[height][width];
for (int h = 0; h < height; h++) {
String s = this.next();
for (int w = 0; w < width; w++) {
mat[h][w] = s.charAt(w);
}
}
return mat;
}
}
static class CustomPrinter extends java.io.PrintWriter {
public CustomPrinter(java.io.PrintStream stream) {
super(stream);
}
public CustomPrinter() {
super(System.out);
}
private static String dtos(double x, int n) {
StringBuilder sb = new StringBuilder();
if (x < 0) {
sb.append('-');
x = -x;
}
x += Math.pow(10, -n) / 2;
sb.append((long) x);
sb.append(".");
x -= (long) x;
for (int i = 0; i < n; i++) {
x *= 10;
sb.append((int) x);
x -= (int) x;
}
return sb.toString();
}
@Override
public void print(float f) {
super.print(dtos(f, 20));
}
@Override
public void println(float f) {
super.println(dtos(f, 20));
}
@Override
public void print(double d) {
super.print(dtos(d, 20));
}
@Override
public void println(double d) {
super.println(dtos(d, 20));
}
public void printArray(int[] array, String separator) {
int n = array.length;
for (int i = 0; i < n - 1; i++) {
super.print(array[i]);
super.print(separator);
}
super.println(array[n - 1]);
}
public void printArray(int[] array) {
this.printArray(array, " ");
}
public void printArray(int[] array, String separator, java.util.function.IntUnaryOperator map) {
int n = array.length;
for (int i = 0; i < n - 1; i++) {
super.print(map.applyAsInt(array[i]));
super.print(separator);
}
super.println(map.applyAsInt(array[n - 1]));
}
public void printArray(int[] array, java.util.function.IntUnaryOperator map) {
this.printArray(array, " ", map);
}
public void printArray(long[] array, String separator) {
int n = array.length;
for (int i = 0; i < n - 1; i++) {
super.print(array[i]);
super.print(separator);
}
super.println(array[n - 1]);
}
public void printArray(long[] array) {
this.printArray(array, " ");
}
public void printArray(long[] array, String separator, java.util.function.LongUnaryOperator map) {
int n = array.length;
for (int i = 0; i < n - 1; i++) {
super.print(map.applyAsLong(array[i]));
super.print(separator);
}
super.println(map.applyAsLong(array[n - 1]));
}
public void printArray(long[] array, java.util.function.LongUnaryOperator map) {
this.printArray(array, " ", map);
}
}
public static void safeSort(int[] array) {
Integer[] temp = new Integer[array.length];
for (int n = 0; n < array.length; n++) {
temp[n] = array[n];
}
Arrays.sort(temp);
for (int n = 0; n < array.length; n++) {
array[n] = temp[n];
}
}
public static void safeSort(long[] array) {
Long[] temp = new Long[array.length];
for (int n = 0; n < array.length; n++) {
temp[n] = array[n];
}
Arrays.sort(temp);
for (int n = 0; n < array.length; n++) {
array[n] = temp[n];
}
}
public static void safeSort(double[] array) {
Double[] temp = new Double[array.length];
for (int n = 0; n < array.length; n++) {
temp[n] = array[n];
}
Arrays.sort(temp);
for (int n = 0; n < array.length; n++) {
array[n] = temp[n];
}
}
} | Java | ["9 10\nabcabcabc\n1 a\n1 b\n2 c\n3 a\n4 b\n5 c\n8 a\n9 b\n1 c\n4 a"] | 2 seconds | ["3\n2\n2\n2\n1\n2\n1\n1\n1\n0"] | NoteLet's consider the state of the string after each query: $$$s =$$$ "abcabcabc". In this case $$$3$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcaccabb". This string does not contain "abc" as a substring. $$$s =$$$ "bbcabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bccabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bccbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcaabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcabbcabc". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaac". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaac". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "ccabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccaaccaab". In this case the string does not contain "abc" as a substring and no replacements are needed. | Java 11 | standard input | [
"implementation",
"strings"
] | db473ad780a93983667d12b1357c6e2f | The first line contains two integers $$$n$$$ and $$$q$$$ $$$(1 \le n, q \le 10^5)$$$, the length of the string and the number of queries, respectively. The second line contains the string $$$s$$$, consisting of characters "a", "b" and "c". Each of the next $$$q$$$ lines contains an integer $$$i$$$ and character $$$c$$$ $$$(1 \le i \le n)$$$, index and the value of the new item in the string, respectively. It is guaranteed that character's $$$c$$$ value is "a", "b" or "c". | 1,100 | For each query output the minimal number of characters that would have to be replaced so that the string doesn't contain "abc" as a substring. | standard output | |
PASSED | 3d71ee2030d40e0734addc76655715d7 | train_110.jsonl | 1638110100 | Before becoming a successful trader William got a university degree. During his education an interesting situation happened, after which William started to listen to homework assignments much more attentively. What follows is the correct formal description of the homework assignment:You are given a string $$$s$$$ of length $$$n$$$ only consisting of characters "a", "b" and "c". There are $$$q$$$ queries of format ($$$pos, c$$$), meaning replacing the element of string $$$s$$$ at position $$$pos$$$ with character $$$c$$$. After each query you must output the minimal number of characters in the string, which have to be replaced, so that the string doesn't contain string "abc" as a substring. A valid replacement of a character is replacing it with "a", "b" or "c".A string $$$x$$$ is a substring of a string $$$y$$$ if $$$x$$$ can be obtained from $$$y$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. | 256 megabytes | //package Codeforces.PractiseR1100;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;
public class WilliamVigilant {
public static void main(String[] args) throws Exception {new WilliamVigilant().run();}
long mod = 1000000000 + 7;
int ans=0;
// int[][] ar;
void solve() throws Exception {
//int t=ni();
{
int n = ni();
int q = ni();
//long h = nl();
String s = ns();
char[] a = s.toCharArray();
int cnt=0;
if(a.length>=3)
for(int i=0;i<a.length-2;i++){
if(helper(a,i)){
cnt++;
}
}
//int cnt = helper(a,0);
while(q-->0){
int i = ni();
char c = nc();
i-=1;
if(a.length<3){
out.println(0);
continue;
}
for(int j= Math.max(0,i-2);j<=Math.min(i,a.length-3);j++){
if(helper(a,j)) cnt--;
}
//out.println(cnt+" hehe");
a[i]=c;
//out.println(cnt+" haha");
for(int j= Math.max(0,i-2);j<=Math.min(i,a.length-3);j++){
if(helper(a,j)) cnt++;
}
out.println(cnt);
}
}
}
boolean helper(char[] a,int i){
if(a[i]=='a' && a[i+1]=='b' && a[i+2]=='c'){
return true;
}
return false;
}
// void buildMatrix(){
//
// for(int i=1;i<=1000;i++){
//
// ar[i][1] = (i*(i+1))/2;
//
// for(int j=2;j<=1000;j++){
// ar[i][j] = ar[i][j-1]+(j-1)+i-1;
// }
// }
// }
/* FAST INPUT OUTPUT & METHODS BELOW */
private byte[] buf = new byte[1024];
private int index;
private InputStream in;
private int total;
private SpaceCharFilter filter;
PrintWriter out;
int min(int... ar) {
int min = Integer.MAX_VALUE;
for (int i : ar)
min = Math.min(min, i);
return min;
}
long min(long... ar) {
long min = Long.MAX_VALUE;
for (long i : ar)
min = Math.min(min, i);
return min;
}
int max(int... ar) {
int max = Integer.MIN_VALUE;
for (int i : ar)
max = Math.max(max, i);
return max;
}
long max(long... ar) {
long max = Long.MIN_VALUE;
for (long i : ar)
max = Math.max(max, i);
return max;
}
void shuffle(int a[]) {
ArrayList<Integer> al = new ArrayList<>();
for (int i = 0; i < a.length; i++)
al.add(a[i]);
Collections.sort(al);
for (int i = 0; i < a.length; i++)
a[i] = al.get(i);
}
long lcm(long a, long b) {
return (a * b) / (gcd(a, b));
}
int gcd(int a, int b) {
if (a == 0)
return b;
return gcd(b % a, a);
}
long gcd(long a, long b) {
if (a == 0)
return b;
return gcd(b % a, a);
}
/*
* for (1/a)%mod = ( a^(mod-2) )%mod ----> use expo to calc -->(a^(mod-2))
*/
long expo(long p, long q) /* (p^q)%mod */
{
long z = 1;
while (q > 0) {
if (q % 2 == 1) {
z = (z * p) % mod;
}
p = (p * p) % mod;
q >>= 1;
}
return z;
}
void run() throws Exception {
in = System.in;
out = new PrintWriter(System.out);
solve();
out.flush();
}
private int scan() throws IOException {
if (total < 0)
throw new InputMismatchException();
if (index >= total) {
index = 0;
total = in.read(buf);
if (total <= 0)
return -1;
}
return buf[index++];
}
private int ni() throws IOException {
int c = scan();
while (isSpaceChar(c))
c = scan();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = scan();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = scan();
} while (!isSpaceChar(c));
return res * sgn;
}
private long nl() throws IOException {
long num = 0;
int b;
boolean minus = false;
while ((b = scan()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
;
if (b == '-') {
minus = true;
b = scan();
}
while (true) {
if (b >= '0' && b <= '9') {
num = num * 10 + (b - '0');
} else {
return minus ? -num : num;
}
b = scan();
}
}
private double nd() throws IOException {
return Double.parseDouble(ns());
}
private String ns() throws IOException {
int c = scan();
while (isSpaceChar(c))
c = scan();
StringBuilder res = new StringBuilder();
do {
if (Character.isValidCodePoint(c))
res.appendCodePoint(c);
c = scan();
} while (!isSpaceChar(c));
return res.toString();
}
private String nss() throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
return br.readLine();
}
private char nc() throws IOException {
int c = scan();
while (isSpaceChar(c))
c = scan();
return (char) c;
}
private boolean isWhiteSpace(int n) {
if (n == ' ' || n == '\n' || n == '\r' || n == '\t' || n == -1)
return true;
return false;
}
private boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return isWhiteSpace(c);
}
private interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
} | Java | ["9 10\nabcabcabc\n1 a\n1 b\n2 c\n3 a\n4 b\n5 c\n8 a\n9 b\n1 c\n4 a"] | 2 seconds | ["3\n2\n2\n2\n1\n2\n1\n1\n1\n0"] | NoteLet's consider the state of the string after each query: $$$s =$$$ "abcabcabc". In this case $$$3$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcaccabb". This string does not contain "abc" as a substring. $$$s =$$$ "bbcabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bccabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bccbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcaabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcabbcabc". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaac". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaac". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "ccabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccaaccaab". In this case the string does not contain "abc" as a substring and no replacements are needed. | Java 11 | standard input | [
"implementation",
"strings"
] | db473ad780a93983667d12b1357c6e2f | The first line contains two integers $$$n$$$ and $$$q$$$ $$$(1 \le n, q \le 10^5)$$$, the length of the string and the number of queries, respectively. The second line contains the string $$$s$$$, consisting of characters "a", "b" and "c". Each of the next $$$q$$$ lines contains an integer $$$i$$$ and character $$$c$$$ $$$(1 \le i \le n)$$$, index and the value of the new item in the string, respectively. It is guaranteed that character's $$$c$$$ value is "a", "b" or "c". | 1,100 | For each query output the minimal number of characters that would have to be replaced so that the string doesn't contain "abc" as a substring. | standard output | |
PASSED | a2629ee2e96e0c50354999da1d539f4b | train_110.jsonl | 1638110100 | Before becoming a successful trader William got a university degree. During his education an interesting situation happened, after which William started to listen to homework assignments much more attentively. What follows is the correct formal description of the homework assignment:You are given a string $$$s$$$ of length $$$n$$$ only consisting of characters "a", "b" and "c". There are $$$q$$$ queries of format ($$$pos, c$$$), meaning replacing the element of string $$$s$$$ at position $$$pos$$$ with character $$$c$$$. After each query you must output the minimal number of characters in the string, which have to be replaced, so that the string doesn't contain string "abc" as a substring. A valid replacement of a character is replacing it with "a", "b" or "c".A string $$$x$$$ is a substring of a string $$$y$$$ if $$$x$$$ can be obtained from $$$y$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. | 256 megabytes | import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;
import java.io.IOException;import java.io.InputStream;import java.io.PrintWriter;
import java.security.AccessControlException;import java.util.List;import java.util.TreeSet;
public class _B {static public void main(final String[] args) throws IOException
{B._main(args);}
static class B extends Solver{public B(){singleTest=true;}@Override public void solve()throws
IOException{int n=sc.nextInt();int q=sc.nextInt();sc.nextLine();int[]s=sc.nextLine().chars().toArray();
TreeSet<Integer>set=new TreeSet<>();for(int i=0;i<n-2;i++){if(s[i]=='a'&& s[i+1]
=='b'&& s[i+2]=='c'){set.add(i);i+=2;}}for(int $i1=0;$i1<q;$i1++){int i=sc.nextInt();
int[]c=sc.nextLine().chars().toArray();i--;if(set.contains(i)&& c[1]!='a'){set.remove(i);
}else if(set.contains(i-1)&& c[1]!='b'){set.remove(i-1);}else if(set.contains(i-
2)&& c[1]!='c'){set.remove(i-2);}if(i+2<n && c[1]=='a'&& s[i+1]=='b'&& s[i+2]=='c')
{set.add(i);}else if(i>0 && i+1<n && c[1]=='b'&& s[i-1]=='a'&& s[i+1]=='c'){set.add(i
-1);}else if(i>1 && c[1]=='c'&& s[i-2]=='a'&& s[i-1]=='b'){set.add(i-2);}s[i]=c[1];
pw.println(set.size());}}static public void _main(String[]args)throws IOException
{new B().run();}}static class MyScanner{private StringBuilder cache=new StringBuilder();
int cache_pos=0;private int first_linebreak=-1;private int second_linebreak=-1;private
StringBuilder sb=new StringBuilder();private InputStream is=null;public MyScanner(final
InputStream is){this.is=is;}public String charToString(final int c){return String.format("'%s'",
c=='\n'?"\\n":(c=='\r'?"\\r":String.valueOf((char)c)));}public int get(){int res
=-1;if(cache_pos<cache.length()){res=cache.charAt(cache_pos);cache_pos++;if(cache_pos
==cache.length()){cache.delete(0,cache_pos);cache_pos=0;}}else{try{res=is.read();
}catch(IOException ex){throw new RuntimeException(ex);}}return res;}private void
unget(final int c){if(cache_pos==0){cache.insert(0,(char)c);}else{cache_pos--;}}
public String nextLine(){sb.delete(0,sb.length());int c;boolean done=false;boolean
end=false;while((c=get())!=-1){if(check_linebreak(c)){done=true;if(c==first_linebreak)
{if(!end){end=true;}else{cache.append((char)c);break;}}else if(second_linebreak!=
-1 && c==second_linebreak){break;}}if(end && c!=first_linebreak && c!=second_linebreak)
{cache.append((char)c);break;}if(!done){sb.append((char)c);}}return!done && sb.length()
==0?null:sb.toString();}private boolean check_linebreak(int c){if(c=='\n'|| c=='\r')
{if(first_linebreak==-1){first_linebreak=c;}else if(c!=first_linebreak && second_linebreak
==-1){second_linebreak=c;}return true;}return false;}public int nextInt(){return
Integer.parseInt(next());}public long nextLong(){return Long.parseLong(next());}
public boolean hasNext(){boolean res=false;int c;while((c=get())!=-1){if(!check_linebreak(c)
&& c!=' '&& c!='\t'){res=true;unget(c);break;}}return res;}public String next(){
sb.delete(0,sb.length());boolean started=false;int c;while((c=get())!=-1){if(check_linebreak(c)
|| c==' '|| c=='\t'){if(started){unget(c);break;}}else{started=true;sb.append((char)c);
}}return sb.toString();}public int nextChar(){return get();}public boolean eof()
{int c=get();boolean res=false;if(c!=-1){unget(c);}else{res=true;}return res;}public
double nextDouble(){return Double.parseDouble(next());}}static abstract class Solver
{protected String nameIn=null;protected String nameOut=null;protected boolean singleTest
=false;protected MyScanner sc=null;protected PrintWriter pw=null;private int current_test
=0;private int count_tests=0;protected int currentTest(){return current_test;}protected
int countTests(){return count_tests;}private void process()throws IOException{if(!singleTest)
{count_tests=sc.nextInt();sc.nextLine();for(current_test=1;current_test<=count_tests;
current_test++){solve();pw.flush();}}else{count_tests=1;current_test=1;solve();}
}abstract protected void solve()throws IOException;public void run()throws IOException
{boolean done=false;try{if(nameIn!=null){if(new File(nameIn).exists()){try(FileInputStream
fis=new FileInputStream(nameIn);PrintWriter pw0=select_output();){select_output();
done=true;sc=new MyScanner(fis);pw=pw0;process();}}else{throw new RuntimeException("File "
+new File(nameIn).getAbsolutePath()+" does not exist!");}}}catch(IOException | AccessControlException
ex){}if(!done){try(PrintWriter pw0=select_output();){sc=new MyScanner(System.in);
pw=pw0;process();}}}private PrintWriter select_output()throws FileNotFoundException
{if(nameOut!=null){return new PrintWriter(nameOut);}return new PrintWriter(System.out);
}}static abstract class Tester{static public int getRandomInt(final int min,final
int max){return(min+(int)Math.floor(Math.random()*(max-min+1)));}static public long
getRandomLong(final long min,final long max){return(min+(long)Math.floor(Math.random()
*(max-min+1)));}static public double getRandomDouble(final double min,final double
maxExclusive){return(min+Math.random()*(maxExclusive-min));}abstract protected void
testOutput(final List<String>output_data);abstract protected void generateInput();
abstract protected String inputDataToString();private boolean break_tester=false;
protected void beforeTesting(){}protected void breakTester(){break_tester=true;}
public boolean broken(){return break_tester;}}}
| Java | ["9 10\nabcabcabc\n1 a\n1 b\n2 c\n3 a\n4 b\n5 c\n8 a\n9 b\n1 c\n4 a"] | 2 seconds | ["3\n2\n2\n2\n1\n2\n1\n1\n1\n0"] | NoteLet's consider the state of the string after each query: $$$s =$$$ "abcabcabc". In this case $$$3$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcaccabb". This string does not contain "abc" as a substring. $$$s =$$$ "bbcabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bccabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bccbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcaabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcabbcabc". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaac". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaac". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "ccabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccaaccaab". In this case the string does not contain "abc" as a substring and no replacements are needed. | Java 11 | standard input | [
"implementation",
"strings"
] | db473ad780a93983667d12b1357c6e2f | The first line contains two integers $$$n$$$ and $$$q$$$ $$$(1 \le n, q \le 10^5)$$$, the length of the string and the number of queries, respectively. The second line contains the string $$$s$$$, consisting of characters "a", "b" and "c". Each of the next $$$q$$$ lines contains an integer $$$i$$$ and character $$$c$$$ $$$(1 \le i \le n)$$$, index and the value of the new item in the string, respectively. It is guaranteed that character's $$$c$$$ value is "a", "b" or "c". | 1,100 | For each query output the minimal number of characters that would have to be replaced so that the string doesn't contain "abc" as a substring. | standard output | |
PASSED | 797fe1a8110fe122301368dfb66dadfb | train_110.jsonl | 1638110100 | Before becoming a successful trader William got a university degree. During his education an interesting situation happened, after which William started to listen to homework assignments much more attentively. What follows is the correct formal description of the homework assignment:You are given a string $$$s$$$ of length $$$n$$$ only consisting of characters "a", "b" and "c". There are $$$q$$$ queries of format ($$$pos, c$$$), meaning replacing the element of string $$$s$$$ at position $$$pos$$$ with character $$$c$$$. After each query you must output the minimal number of characters in the string, which have to be replaced, so that the string doesn't contain string "abc" as a substring. A valid replacement of a character is replacing it with "a", "b" or "c".A string $$$x$$$ is a substring of a string $$$y$$$ if $$$x$$$ can be obtained from $$$y$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. | 256 megabytes |
import java.util.*;
import java.math.*;
import java.io.*;
import java.util.stream.Collectors;
import java.text.DecimalFormat;
public class A{
static FastScanner scan=new FastScanner();
public static PrintWriter out = new PrintWriter (new BufferedOutputStream(System.out));
public static void main(String[] args) throws Exception
{
int tt=1;
//tt=scan.nextInt();
// scan=new FastScanner("clumsy.in");
//out=new PrintWriter("clumsy.out");
outer:while(tt-->0)
{
int n=scan.nextInt();
int q=scan.nextInt();
StringBuilder s=new StringBuilder(scan.next());
int st[]=new int[n];
Arrays.fill(st,-1);
Set<Pair>set=new HashSet<Pair>();
long res=0;
for(int i=0;i<=n-3;i++)
{
if(s.charAt(i)=='a'&&s.charAt(i+1)=='b'&s.charAt(i+2)=='c')
{
//System.out.println(i);
set.add(new Pair(i,i+2));
st[i]=i+2;
res++;
}
}
// out.println(res);
for(int i=0;i<q;i++)
{
int x=scan.nextInt()-1;
char c=scan.next().charAt(0);
if(s.charAt(x)==c)
{
out.println(res);
continue ;
}
if(c=='a')
{
if(x-1>=0&&st[x-1]!=-1)
{
st[x-1]=-1;
res--;
}
else if(x-2>=0&&st[x-2]!=-1)
{
res--;
st[x-2]=-1;
}
if(x+1<n&&x+2<n&&s.charAt(x+1)=='b'&&s.charAt(x+2)=='c')
{
st[x]=x+2;
res++;
}
}
else if(c=='b')
{
// System.out.println("FUCK");
//System.out.println(st[x-1]);
if(x>=0&&st[x]!=-1)
{
//System.out.println("F");
st[x]=-1;
res--;
}
else if(x-2>=0&&st[x-2]!=-1)
{
res--;
st[x-2]=-1;
}
if(x-1>=0&&x+1<n&&s.charAt(x-1)=='a'&&s.charAt(x+1)=='c')
{
//System.out.println("F");
st[x-1]=x+1;
res++;
}
}
else {
if(x>=0&&st[x]!=-1)
{
st[x]=-1;
res--;
}
else if(x-1>=0&&st[x-1]!=-1)
{
res--;
st[x-1]=-1;
}
if(x-2>=0&&x-1>=0&&s.charAt(x-2)=='a'&&s.charAt(x-1)=='b')
{
st[x-2]=x;
res++;
}
}
s.setCharAt(x,c);
out.println(res);
}
}
out.close();
}
static class FastScanner
{
private int BS = 1 << 16;
private char NC = (char) 0;
private byte[] buf = new byte[BS];
private int bId = 0, size = 0;
private char c = NC;
private double cnt = 1;
private BufferedInputStream in;
public FastScanner() {
in = new BufferedInputStream(System.in, BS);
}
public FastScanner(String s) {
try {
in = new BufferedInputStream(new FileInputStream(new File(s)), BS);
} catch (Exception e) {
in = new BufferedInputStream(System.in, BS);
}
}
private char getChar() {
while (bId == size) {
try {
size = in.read(buf);
} catch (Exception e) {
return NC;
}
if (size == -1) return NC;
bId = 0;
}
return (char) buf[bId++];
}
public int nextInt() {
return (int) nextLong();
}
public int[] nextInts(int N) {
int[] res = new int[N];
for (int i = 0; i < N; i++) {
res[i] = (int) nextLong();
}
return res;
}
public long[] nextLongs(int N) {
long[] res = new long[N];
for (int i = 0; i < N; i++) {
res[i] = nextLong();
}
return res;
}
public long nextLong() {
cnt = 1;
boolean neg = false;
if (c == NC) c = getChar();
for (; (c < '0' || c > '9'); c = getChar()) {
if (c == '-') neg = true;
}
long res = 0;
for (; c >= '0' && c <= '9'; c = getChar()) {
res = (res << 3) + (res << 1) + c - '0';
cnt *= 10;
}
return neg ? -res : res;
}
public double nextDouble() {
double cur = nextLong();
return c != '.' ? cur : cur + nextLong() / cnt;
}
public double[] nextDoubles(int N) {
double[] res = new double[N];
for (int i = 0; i < N; i++) {
res[i] = nextDouble();
}
return res;
}
public String next() {
StringBuilder res = new StringBuilder();
while (c <= 32) c = getChar();
while (c > 32) {
res.append(c);
c = getChar();
}
return res.toString();
}
public String nextLine() {
StringBuilder res = new StringBuilder();
while (c <= 32) c = getChar();
while (c != '\n') {
res.append(c);
c = getChar();
}
return res.toString();
}
public boolean hasNext() {
if (c > 32) return true;
while (true) {
c = getChar();
if (c == NC) return false;
else if (c > 32) return true;
}
}
}
static class Pair implements Comparable<Pair>{
public long x, y,z;
public Pair(long x1, long y1,long z1) {
x=x1;
y=y1;
z=z1;
}
public Pair(long x1, long y1) {
x=x1;
y=y1;
}
@Override
public int hashCode() {
return (int)(x + 31 * y);
}
public String toString() {
return x + " " + y+" "+z;
}
@Override
public boolean equals(Object o){
if (o == this) return true;
if (o.getClass() != getClass()) return false;
Pair t = (Pair)o;
return t.x == x && t.y == y&&t.z==z;
}
public int compareTo(Pair o)
{
//if(o.x==x)
// return (int)(y-o.y);
return Long.compare(o.x,x);
}
}
}
| Java | ["9 10\nabcabcabc\n1 a\n1 b\n2 c\n3 a\n4 b\n5 c\n8 a\n9 b\n1 c\n4 a"] | 2 seconds | ["3\n2\n2\n2\n1\n2\n1\n1\n1\n0"] | NoteLet's consider the state of the string after each query: $$$s =$$$ "abcabcabc". In this case $$$3$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcaccabb". This string does not contain "abc" as a substring. $$$s =$$$ "bbcabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bccabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bccbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcaabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcabbcabc". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaac". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaac". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "ccabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccaaccaab". In this case the string does not contain "abc" as a substring and no replacements are needed. | Java 11 | standard input | [
"implementation",
"strings"
] | db473ad780a93983667d12b1357c6e2f | The first line contains two integers $$$n$$$ and $$$q$$$ $$$(1 \le n, q \le 10^5)$$$, the length of the string and the number of queries, respectively. The second line contains the string $$$s$$$, consisting of characters "a", "b" and "c". Each of the next $$$q$$$ lines contains an integer $$$i$$$ and character $$$c$$$ $$$(1 \le i \le n)$$$, index and the value of the new item in the string, respectively. It is guaranteed that character's $$$c$$$ value is "a", "b" or "c". | 1,100 | For each query output the minimal number of characters that would have to be replaced so that the string doesn't contain "abc" as a substring. | standard output | |
PASSED | 5623860878cc9043c1b2702a7e2eb609 | train_110.jsonl | 1638110100 | Before becoming a successful trader William got a university degree. During his education an interesting situation happened, after which William started to listen to homework assignments much more attentively. What follows is the correct formal description of the homework assignment:You are given a string $$$s$$$ of length $$$n$$$ only consisting of characters "a", "b" and "c". There are $$$q$$$ queries of format ($$$pos, c$$$), meaning replacing the element of string $$$s$$$ at position $$$pos$$$ with character $$$c$$$. After each query you must output the minimal number of characters in the string, which have to be replaced, so that the string doesn't contain string "abc" as a substring. A valid replacement of a character is replacing it with "a", "b" or "c".A string $$$x$$$ is a substring of a string $$$y$$$ if $$$x$$$ can be obtained from $$$y$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. | 256 megabytes |
import java.util.*;
import java.lang.*;
import java.math.*;
import java.io.*;
public class snackDown3
{
public static class FastReader {
BufferedReader b;
StringTokenizer s;
public FastReader() {
b=new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while(s==null ||!s.hasMoreElements()) {
try {
s=new StringTokenizer(b.readLine());
}
catch(IOException e) {
e.printStackTrace();
}
}
return s.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str="";
try {
str=b.readLine();
}
catch(IOException e) {
e.printStackTrace();
}
return str;
}
boolean hasNext() {
if (s != null && s.hasMoreTokens()) {
return true;
}
String tmp;
try {
b.mark(1000);
tmp = b.readLine();
if (tmp == null) {
return false;
}
b.reset();
} catch (IOException e) {
return false;
}
return true;
}
}
public static class pair{
char a;
int b;
public pair(char a,int b) {
this.a=a;
this.b=b;
}
}
public static class pair2{
int a;
int b;
public pair2(int a,int b) {
this.a=a;
this.b=b;
}
public int compareTo(pair2 b) {
return this.b-b.b;
}
}
//public static class trip
public static void main (String[] args) throws Exception
{
// your code goes here
// int a[][][]=new int[3][3][3];
FastReader sc=new FastReader();
BufferedWriter log = new BufferedWriter(new OutputStreamWriter(System.out));
// OutputStream log = new BufferedOutputStream ( System.out );
int n=sc.nextInt();
int q=sc.nextInt();
char v[]=sc.next().toCharArray();
LinkedList<Integer> a=new LinkedList<>();
HashSet<Integer> hs=new HashSet<>();
for(int i=0;i<v.length;i++) {
if(i+2<v.length) {
if(v[i]=='a' && v[i+1]=='b' && v[i+2]=='c') {
a.add(i);
hs.add(i);
}
}
}
int ans=hs.size();
while(q--!=0) {
int p=sc.nextInt();
char c=sc.next().charAt(0);
p-=1;
if(hs.contains(p) && c!='a') {
ans--;
hs.remove(p);
}
else if(hs.contains(p-1) && c!='b') {
ans--;
hs.remove(p-1);
}
else {
if(hs.contains(p-2) && c!='c') {
ans--;
hs.remove(p-2);
}
}
if(c=='a' && v[p]!='a') {
if(p+2<v.length && v[p+1]=='b' && v[p+2]=='c' ) {
if(!hs.contains(p)) {hs.add(p);
ans++;
}
}
}
else if(c=='b' && v[p]!='b') {
if((p-1>=0 && v[p-1]=='a') && (p+1<v.length && v[p+1]=='c')) {
if(!hs.contains(p-1)) {
hs.add(p-1);
ans++;
}
}
}
else {
if(c=='c' && v[p]!='c') {
if(p-2>=0 && v[p-1]=='b' && v[p-2]=='a' ) {
if(!hs.contains(p-2)) {
hs.add(p-2);
ans++;
}
}
}
}
v[p]=c;
log.write(ans+"\n");
log.flush();
}
}
}
| Java | ["9 10\nabcabcabc\n1 a\n1 b\n2 c\n3 a\n4 b\n5 c\n8 a\n9 b\n1 c\n4 a"] | 2 seconds | ["3\n2\n2\n2\n1\n2\n1\n1\n1\n0"] | NoteLet's consider the state of the string after each query: $$$s =$$$ "abcabcabc". In this case $$$3$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcaccabb". This string does not contain "abc" as a substring. $$$s =$$$ "bbcabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bccabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bccbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcaabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcabbcabc". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaac". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaac". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "ccabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccaaccaab". In this case the string does not contain "abc" as a substring and no replacements are needed. | Java 11 | standard input | [
"implementation",
"strings"
] | db473ad780a93983667d12b1357c6e2f | The first line contains two integers $$$n$$$ and $$$q$$$ $$$(1 \le n, q \le 10^5)$$$, the length of the string and the number of queries, respectively. The second line contains the string $$$s$$$, consisting of characters "a", "b" and "c". Each of the next $$$q$$$ lines contains an integer $$$i$$$ and character $$$c$$$ $$$(1 \le i \le n)$$$, index and the value of the new item in the string, respectively. It is guaranteed that character's $$$c$$$ value is "a", "b" or "c". | 1,100 | For each query output the minimal number of characters that would have to be replaced so that the string doesn't contain "abc" as a substring. | standard output | |
PASSED | a9dd0fdc8a43800ee80ddc5e909a0a34 | train_110.jsonl | 1638110100 | Before becoming a successful trader William got a university degree. During his education an interesting situation happened, after which William started to listen to homework assignments much more attentively. What follows is the correct formal description of the homework assignment:You are given a string $$$s$$$ of length $$$n$$$ only consisting of characters "a", "b" and "c". There are $$$q$$$ queries of format ($$$pos, c$$$), meaning replacing the element of string $$$s$$$ at position $$$pos$$$ with character $$$c$$$. After each query you must output the minimal number of characters in the string, which have to be replaced, so that the string doesn't contain string "abc" as a substring. A valid replacement of a character is replacing it with "a", "b" or "c".A string $$$x$$$ is a substring of a string $$$y$$$ if $$$x$$$ can be obtained from $$$y$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. | 256 megabytes | import java.io.*;
import java.util.*;
public class B {
void go() {
int n = Reader.nextInt();
int m = Reader.nextInt();
char[] s = Reader.next().toCharArray();
int tot = 0;
for(int i = 0; i < n; i++) {
if(i > 0 && i < n - 1) {
if(s[i - 1] == 'a' && s[i] == 'b' && s[i + 1] == 'c') {
tot++;
}
}
}
// abcabc
for(int i = 0; i < m; i++) {
int pos = Reader.nextInt() - 1;
char c = Reader.next().charAt(0);
if(s[pos] != c) {
if (s[pos] == 'a' && pos + 2 < n && s[pos + 1] == 'b' && s[pos + 2] == 'c') {
tot--;
}
if (s[pos] == 'b' && pos + 1 < n && pos - 1 >= 0 && s[pos - 1] == 'a' && s[pos + 1] == 'c') {
tot--;
}
if (s[pos] == 'c' && pos - 2 >= 0 && s[pos - 1] == 'b' && s[pos - 2] == 'a') {
tot--;
}
if (c == 'a' && pos + 2 < n && s[pos + 1] == 'b' && s[pos + 2] == 'c') {
tot++;
}
if (c == 'b' && pos + 1 < n && pos - 1 >= 0 && s[pos - 1] == 'a' && s[pos + 1] == 'c') {
tot++;
}
if (c == 'c' && pos - 2 >= 0 && s[pos - 1] == 'b' && s[pos - 2] == 'a') {
tot++;
}
}
s[pos] = c;
Writer.println(tot);
}
}
void solve() {
go();
}
void run() throws Exception {
Reader.init(System.in);
Writer.init(System.out);
solve();
Writer.close();
}
public static void main(String[] args) throws Exception {
new B().run();
}
public static class Reader {
public static StringTokenizer st;
public static BufferedReader br;
public static void init(InputStream in) {
br = new BufferedReader(new InputStreamReader(in));
st = new StringTokenizer("");
}
public static String next() {
while(!st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
throw new InputMismatchException();
}
}
return st.nextToken();
}
public static int nextInt() {
return Integer.parseInt(next());
}
public static long nextLong() {
return Long.parseLong(next());
}
public static double nextDouble() {
return Double.parseDouble(next());
}
}
public static class Writer {
public static PrintWriter pw;
public static void init(OutputStream os) {
pw = new PrintWriter(new BufferedOutputStream(os));
}
public static void print(String s) {
pw.print(s);
}
public static void print(int x) {
pw.print(x);
}
public static void print(long x) {
pw.print(x);
}
public static void println(String s) {
pw.println(s);
}
public static void println(int x) {
pw.println(x);
}
public static void println(long x) {
pw.println(x);
}
public static void close() {
pw.close();
}
}
} | Java | ["9 10\nabcabcabc\n1 a\n1 b\n2 c\n3 a\n4 b\n5 c\n8 a\n9 b\n1 c\n4 a"] | 2 seconds | ["3\n2\n2\n2\n1\n2\n1\n1\n1\n0"] | NoteLet's consider the state of the string after each query: $$$s =$$$ "abcabcabc". In this case $$$3$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcaccabb". This string does not contain "abc" as a substring. $$$s =$$$ "bbcabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bccabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bccbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcaabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcabbcabc". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaac". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaac". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "ccabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccaaccaab". In this case the string does not contain "abc" as a substring and no replacements are needed. | Java 11 | standard input | [
"implementation",
"strings"
] | db473ad780a93983667d12b1357c6e2f | The first line contains two integers $$$n$$$ and $$$q$$$ $$$(1 \le n, q \le 10^5)$$$, the length of the string and the number of queries, respectively. The second line contains the string $$$s$$$, consisting of characters "a", "b" and "c". Each of the next $$$q$$$ lines contains an integer $$$i$$$ and character $$$c$$$ $$$(1 \le i \le n)$$$, index and the value of the new item in the string, respectively. It is guaranteed that character's $$$c$$$ value is "a", "b" or "c". | 1,100 | For each query output the minimal number of characters that would have to be replaced so that the string doesn't contain "abc" as a substring. | standard output | |
PASSED | 02ed953274c5e11617cadf29f855f0aa | train_110.jsonl | 1638110100 | Before becoming a successful trader William got a university degree. During his education an interesting situation happened, after which William started to listen to homework assignments much more attentively. What follows is the correct formal description of the homework assignment:You are given a string $$$s$$$ of length $$$n$$$ only consisting of characters "a", "b" and "c". There are $$$q$$$ queries of format ($$$pos, c$$$), meaning replacing the element of string $$$s$$$ at position $$$pos$$$ with character $$$c$$$. After each query you must output the minimal number of characters in the string, which have to be replaced, so that the string doesn't contain string "abc" as a substring. A valid replacement of a character is replacing it with "a", "b" or "c".A string $$$x$$$ is a substring of a string $$$y$$$ if $$$x$$$ can be obtained from $$$y$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. | 256 megabytes | import java.util.*;
import java.io.*;
public class no {
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String str1 = in.readLine();
String[] spl = str1.split (" ");
int length = Integer.parseInt(spl[0]);
int queries = Integer.parseInt(spl[1]);
String str2 = in.readLine();
char[] str = str2.toCharArray();
ArrayList<String> bruh = new ArrayList<>();
for (int i = 0; i < queries; i ++) {
bruh.add(in.readLine());
}
StringBuilder b = new StringBuilder();
int numABC = 0;
//precompute
for (int i = 0; i < length - 2; i ++) {
if (str[i]=='a' && str[i+1] == 'b' && str[i+2] == 'c') {
numABC ++;
}
}
for (String query : bruh) {
String[] arr = query.split (" ");
int indexAt = Integer.parseInt(arr[0])-1;
char prevChar = str[indexAt];
str[indexAt] = arr[1].charAt(0);
if (prevChar == str[indexAt]) {
b.append(numABC+"\n");
continue;
}
if (str[indexAt] == 'a' && indexAt < length - 2 && str[indexAt + 1] == 'b' && str[indexAt + 2] == 'c') {
numABC++;
}if (str[indexAt] == 'b' && indexAt < length - 1 && indexAt > 0 && str[indexAt + 1] == 'c' && str[indexAt - 1] == 'a') {
numABC++;
}if (str[indexAt] == 'c' && indexAt > 1 && str[indexAt - 1] == 'b' && str[indexAt - 2] == 'a') {
numABC++;
}if (prevChar == 'a' && indexAt < length - 2 && str[indexAt + 1] == 'b' && str[indexAt + 2] == 'c') {
numABC--;
}if (prevChar == 'b' && indexAt < length - 1 && indexAt > 0 && str[indexAt + 1] == 'c' && str[indexAt - 1] == 'a') {
numABC--;
}if (prevChar == 'c' && indexAt > 1 && str[indexAt - 1] == 'b' && str[indexAt - 2] == 'a') {
numABC--;
}
b.append(numABC+"\n");
}
System.out.print(b);
in.close();
}
} | Java | ["9 10\nabcabcabc\n1 a\n1 b\n2 c\n3 a\n4 b\n5 c\n8 a\n9 b\n1 c\n4 a"] | 2 seconds | ["3\n2\n2\n2\n1\n2\n1\n1\n1\n0"] | NoteLet's consider the state of the string after each query: $$$s =$$$ "abcabcabc". In this case $$$3$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcaccabb". This string does not contain "abc" as a substring. $$$s =$$$ "bbcabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bccabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bccbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcaabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcabbcabc". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaac". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaac". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "ccabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccaaccaab". In this case the string does not contain "abc" as a substring and no replacements are needed. | Java 11 | standard input | [
"implementation",
"strings"
] | db473ad780a93983667d12b1357c6e2f | The first line contains two integers $$$n$$$ and $$$q$$$ $$$(1 \le n, q \le 10^5)$$$, the length of the string and the number of queries, respectively. The second line contains the string $$$s$$$, consisting of characters "a", "b" and "c". Each of the next $$$q$$$ lines contains an integer $$$i$$$ and character $$$c$$$ $$$(1 \le i \le n)$$$, index and the value of the new item in the string, respectively. It is guaranteed that character's $$$c$$$ value is "a", "b" or "c". | 1,100 | For each query output the minimal number of characters that would have to be replaced so that the string doesn't contain "abc" as a substring. | standard output | |
PASSED | bd487c0ef09ab469abdbc92b011a0775 | train_110.jsonl | 1638110100 | Before becoming a successful trader William got a university degree. During his education an interesting situation happened, after which William started to listen to homework assignments much more attentively. What follows is the correct formal description of the homework assignment:You are given a string $$$s$$$ of length $$$n$$$ only consisting of characters "a", "b" and "c". There are $$$q$$$ queries of format ($$$pos, c$$$), meaning replacing the element of string $$$s$$$ at position $$$pos$$$ with character $$$c$$$. After each query you must output the minimal number of characters in the string, which have to be replaced, so that the string doesn't contain string "abc" as a substring. A valid replacement of a character is replacing it with "a", "b" or "c".A string $$$x$$$ is a substring of a string $$$y$$$ if $$$x$$$ can be obtained from $$$y$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. | 256 megabytes | import java.util.*;
import java.io.*;
public class no {
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String str1 = in.readLine();
String[] spl = str1.split (" ");
int length = Integer.parseInt(spl[0]);
int queries = Integer.parseInt(spl[1]);
String str2 = in.readLine();
char[] str = str2.toCharArray();
ArrayList<String> bruh = new ArrayList<>();
for (int i = 0; i < queries; i ++) {
bruh.add(in.readLine());
}
int numABC = 0;
//precompute
for (int i = 0; i < length - 2; i ++) {
if (str[i]=='a' && str[i+1] == 'b' && str[i+2] == 'c') {
numABC ++;
}
}
for (String query : bruh) {
String[] arr = query.split (" ");
int indexAt = Integer.parseInt(arr[0])-1;
char prevChar = str[indexAt];
str[indexAt] = arr[1].charAt(0);
if (prevChar == str[indexAt]) {
System.out.println(numABC);
continue;
}
if (str[indexAt] == 'a' && indexAt < length - 2 && str[indexAt + 1] == 'b' && str[indexAt + 2] == 'c') {
numABC++;
}if (str[indexAt] == 'b' && indexAt < length - 1 && indexAt > 0 && str[indexAt + 1] == 'c' && str[indexAt - 1] == 'a') {
numABC++;
}if (str[indexAt] == 'c' && indexAt > 1 && str[indexAt - 1] == 'b' && str[indexAt - 2] == 'a') {
numABC++;
}if (prevChar == 'a' && indexAt < length - 2 && str[indexAt + 1] == 'b' && str[indexAt + 2] == 'c') {
numABC--;
}if (prevChar == 'b' && indexAt < length - 1 && indexAt > 0 && str[indexAt + 1] == 'c' && str[indexAt - 1] == 'a') {
numABC--;
}if (prevChar == 'c' && indexAt > 1 && str[indexAt - 1] == 'b' && str[indexAt - 2] == 'a') {
numABC--;
}
System.out.println(numABC);
}
in.close();
}
} | Java | ["9 10\nabcabcabc\n1 a\n1 b\n2 c\n3 a\n4 b\n5 c\n8 a\n9 b\n1 c\n4 a"] | 2 seconds | ["3\n2\n2\n2\n1\n2\n1\n1\n1\n0"] | NoteLet's consider the state of the string after each query: $$$s =$$$ "abcabcabc". In this case $$$3$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcaccabb". This string does not contain "abc" as a substring. $$$s =$$$ "bbcabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bccabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bccbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcaabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcabbcabc". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaac". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaac". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "ccabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccaaccaab". In this case the string does not contain "abc" as a substring and no replacements are needed. | Java 11 | standard input | [
"implementation",
"strings"
] | db473ad780a93983667d12b1357c6e2f | The first line contains two integers $$$n$$$ and $$$q$$$ $$$(1 \le n, q \le 10^5)$$$, the length of the string and the number of queries, respectively. The second line contains the string $$$s$$$, consisting of characters "a", "b" and "c". Each of the next $$$q$$$ lines contains an integer $$$i$$$ and character $$$c$$$ $$$(1 \le i \le n)$$$, index and the value of the new item in the string, respectively. It is guaranteed that character's $$$c$$$ value is "a", "b" or "c". | 1,100 | For each query output the minimal number of characters that would have to be replaced so that the string doesn't contain "abc" as a substring. | standard output | |
PASSED | b1e2d7996637d3aa174fe9c43bd4cc95 | train_110.jsonl | 1638110100 | Before becoming a successful trader William got a university degree. During his education an interesting situation happened, after which William started to listen to homework assignments much more attentively. What follows is the correct formal description of the homework assignment:You are given a string $$$s$$$ of length $$$n$$$ only consisting of characters "a", "b" and "c". There are $$$q$$$ queries of format ($$$pos, c$$$), meaning replacing the element of string $$$s$$$ at position $$$pos$$$ with character $$$c$$$. After each query you must output the minimal number of characters in the string, which have to be replaced, so that the string doesn't contain string "abc" as a substring. A valid replacement of a character is replacing it with "a", "b" or "c".A string $$$x$$$ is a substring of a string $$$y$$$ if $$$x$$$ can be obtained from $$$y$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. | 256 megabytes | import java.util.*;
import java.lang.Math;
import java.io.* ;
public class Account {
public static class Pair<Object1 ,Object2> {
Object1 key ;
Object2 val ;
Pair(Object1 key ,Object2 val) {
this.key = key ;
this.val = val ;
}
Pair() {}
}
public static void main (String[] args) {
// Scanner sc = new Scanner(System.in) ;
FastReader sc = new FastReader() ;
StringBuilder res = new StringBuilder() ;
int t= 1 ;
// t= sc.nextInt() ;
while(t-- > 0) {
int n= sc.nextInt() ;
int q= sc.nextInt() ;
String str = sc.next() ;
char[] arr = str.toCharArray() ;
int cur = 0 ;
for(int i=0 ;i<(str.length() - 2) ;i++) {
if(str.substring(i ,i+3).equals("abc")) {
cur++ ;
i += 2 ;
}
}
while(q-- > 0) {
// System.out.println(String.valueOf(arr));
int pos = sc.nextInt() ;
char ch = sc.next().charAt(0) ;
pos-- ;
// System.out.println("-->"+pos+" "+ch);
if(arr[pos] != ch) {
if(ch == 'a' && pos < n-2) {
if(arr[pos+1] == 'b' && arr[pos+2] == 'c') {
cur++ ;
}
}if(pos < n-2 && arr[pos] == 'a' && arr[pos+1] == 'b' && arr[pos+2] == 'c') {
cur-- ;
}if(ch == 'b' && pos < n-1 && pos > 0) {
// System.out.println("---------");
if(arr[pos-1] == 'a' && arr[pos+1] == 'c') {
cur++ ;
}
}if(pos < n-1 && pos > 0 && arr[pos-1] == 'a' && arr[pos] == 'b' && arr[pos+1] == 'c') {
cur-- ;
}if(ch == 'c' && pos > 1) {
// System.out.println("---------");
if(arr[pos-2] == 'a' && arr[pos-1] == 'b') {
cur++ ;
}
}if(pos > 1 && arr[pos-2] == 'a' && arr[pos-1] == 'b' && arr[pos] == 'c') {
cur-- ;
}
// System.out.println("+++++++++") ;
arr[pos] = ch ;
// str = String.valueOf(arr) ;
// str = str.substring(0 ,pos) + ch + str.substring(pos+1) ;
}
// System.out.println(cur);
res.append(cur+"\n") ;
}
System.out.println(res);
}
}
/*
* 7 5 3 3 1
* 7 5 3 9 1
* 2 4 6 10 8
*/
public static int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
public static int xor(int n)
{
if (n % 4 == 0)
return n;
if (n % 4 == 1)
return 1;
if (n % 4 == 2)
return n + 1;
return 0;
}
public static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
int [] readintarray(int n) {
int res [] = new int [n];
for(int i = 0; i<n; i++)res[i] = nextInt();
return res;
}
long [] readlongarray(int n) {
long res [] = new long [n];
for(int i = 0; i<n; i++)res[i] = nextLong();
return res;
}
}
} | Java | ["9 10\nabcabcabc\n1 a\n1 b\n2 c\n3 a\n4 b\n5 c\n8 a\n9 b\n1 c\n4 a"] | 2 seconds | ["3\n2\n2\n2\n1\n2\n1\n1\n1\n0"] | NoteLet's consider the state of the string after each query: $$$s =$$$ "abcabcabc". In this case $$$3$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcaccabb". This string does not contain "abc" as a substring. $$$s =$$$ "bbcabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bccabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bccbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcaabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcabbcabc". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaac". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaac". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "ccabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccaaccaab". In this case the string does not contain "abc" as a substring and no replacements are needed. | Java 11 | standard input | [
"implementation",
"strings"
] | db473ad780a93983667d12b1357c6e2f | The first line contains two integers $$$n$$$ and $$$q$$$ $$$(1 \le n, q \le 10^5)$$$, the length of the string and the number of queries, respectively. The second line contains the string $$$s$$$, consisting of characters "a", "b" and "c". Each of the next $$$q$$$ lines contains an integer $$$i$$$ and character $$$c$$$ $$$(1 \le i \le n)$$$, index and the value of the new item in the string, respectively. It is guaranteed that character's $$$c$$$ value is "a", "b" or "c". | 1,100 | For each query output the minimal number of characters that would have to be replaced so that the string doesn't contain "abc" as a substring. | standard output | |
PASSED | b959430a9a3d0f0b8b503c3daa6e7940 | train_110.jsonl | 1638110100 | Before becoming a successful trader William got a university degree. During his education an interesting situation happened, after which William started to listen to homework assignments much more attentively. What follows is the correct formal description of the homework assignment:You are given a string $$$s$$$ of length $$$n$$$ only consisting of characters "a", "b" and "c". There are $$$q$$$ queries of format ($$$pos, c$$$), meaning replacing the element of string $$$s$$$ at position $$$pos$$$ with character $$$c$$$. After each query you must output the minimal number of characters in the string, which have to be replaced, so that the string doesn't contain string "abc" as a substring. A valid replacement of a character is replacing it with "a", "b" or "c".A string $$$x$$$ is a substring of a string $$$y$$$ if $$$x$$$ can be obtained from $$$y$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.File;
import java.io.FileInputStream;
import java.util.*;
public class Main {
// static final File ip = new File("input.txt");
// static final File op = new File("output.txt");
// static {
// try {
// System.setOut(new PrintStream(op));
// System.setIn(new FileInputStream(ip));
// } catch (Exception e) {
// }
// }
static long countFreq(String pat, String txt) {
int M = pat.length();
int N = txt.length();
long res = 0;
for (int i = 0; i <= N - M; i++) {
int j;
for (j = 0; j < M; j++) {
if (txt.charAt(i + j) != pat.charAt(j)) {
break;
}
}
if (j == M) {
res++;
j = 0;
}
}
return res;
}
public static void main(String[] args) {
FastReader sc = new FastReader();
int test = 1;
// test = sc.nextInt();
while (test-- > 0) {
int n = sc.nextInt();
int m = sc.nextInt();
String st = sc.next();
long cnt = countFreq("abc", st);
// System.out.println(cnt);
StringBuilder s = new StringBuilder(st);
while (m-- > 0) {
int pos = sc.nextInt();
pos--;
char ch = sc.next().charAt(0);
if (n < 3) {
System.out.println("0");
continue;
}
char prev = s.charAt(pos);
s.setCharAt(pos, ch);
if (ch == prev) {
System.out.println(cnt);
continue;
} else {
if (pos + 2 < n && prev == 'a' && s.substring(pos + 1, pos + 3).equals("bc") == true)
cnt--;
if (pos - 1 >= 0 && pos + 1 < n && prev == 'b' && s.charAt(pos + 1) == 'c'
&& s.charAt(pos - 1) == 'a')
cnt--;
if (pos >= 2 && prev == 'c' && s.charAt(pos - 1) == 'b' && s.charAt(pos - 2) == 'a')
cnt--;
// if (pos + 2 < n) {
// if (s.substring(pos, pos + 3).equals("abc") == true)
// cnt++;
// }
// if (pos - 2 >= 0) {
// if (s.substring(pos - 2, pos + 1).equals("abc") == true)
// cnt++;
// }
if (pos + 2 < n && ch == 'a' && s.substring(pos + 1, pos + 3).equals("bc") == true)
cnt++;
if (pos - 1 >= 0 && pos + 1 < n && ch == 'b' && s.charAt(pos + 1) == 'c'
&& s.charAt(pos - 1) == 'a')
cnt++;
if (pos >= 2 && ch == 'c' && s.charAt(pos - 1) == 'b' && s.charAt(pos - 2) == 'a')
cnt++;
}
System.out.println(cnt);
}
}
}
static long power(long x, long y, long p) {
long res = 1;
x = x % p;
if (x == 0)
return 0;
while (y > 0) {
if ((y & 1) != 0)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
public static int countSetBits(long number) {
int count = 0;
while (number > 0) {
++count;
number &= number - 1;
}
return count;
}
static int lower_bound(long target, pair[] a, int pos) {
if (pos >= a.length)
return -1;
int low = pos, high = a.length - 1;
while (low < high) {
int mid = low + (high - low) / 2;
if (a[mid].a < target)
low = mid + 1;
else
high = mid;
}
return a[low].a >= target ? low : -1;
}
private static <T> void swap(int[] a, int i, int j) {
int tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
static class pair {
int a;
int b;
pair(int x, int y) {
this.a = x;
this.b = y;
}
}
static class first implements Comparator<pair> {
public int compare(pair p1, pair p2) {
if (p1.a > p2.a)
return -1;
else if (p1.a < p2.a)
return 1;
return 0;
}
}
static class second implements Comparator<pair> {
public int compare(pair p1, pair p2) {
if (p1.b > p2.b)
return 1;
else if (p1.b < p2.b)
return -1;
return 0;
}
}
private static long getSum(int[] array) {
long sum = 0;
for (int value : array) {
sum += value;
}
return sum;
}
private static boolean isPrime(Long x) {
if (x < 2)
return false;
for (long d = 2; d * d <= x; ++d) {
if (x % d == 0)
return false;
}
return true;
}
static int[] reverse(int a[], int n) {
int i, k, t;
for (i = 0; i < n / 2; i++) {
t = a[i];
a[i] = a[n - i - 1];
a[n - i - 1] = t;
}
return a;
}
private static boolean isPrimeInt(int x) {
if (x < 2)
return false;
for (int d = 2; d * d <= x; ++d) {
if (x % d == 0)
return false;
}
return true;
}
public static String reverse(String input) {
StringBuilder str = new StringBuilder("");
for (int i = input.length() - 1; i >= 0; i--) {
str.append(input.charAt(i));
}
return str.toString();
}
private static int[] getPrimes(int n) {
boolean[] used = new boolean[n + 1];
used[0] = used[1] = true;
int size = 0;
for (int i = 2; i <= n; ++i) {
if (!used[i]) {
++size;
for (int j = 2 * i; j <= n; j += i) {
used[j] = true;
}
}
}
int[] primes = new int[size];
for (int i = 0, cur = 0; i <= n; ++i) {
if (!used[i]) {
primes[cur++] = i;
}
}
return primes;
}
private static long lcm(long a, long b) {
return a / gcd(a, b) * b;
}
private static long gcd(long a, long b) {
return (a == 0 ? b : gcd(b % a, a));
}
static void sortI(int[] arr) {
int n = arr.length;
Random rnd = new Random();
for (int i = 0; i < n; ++i) {
int tmp = arr[i];
int randomPos = i + rnd.nextInt(n - i);
arr[i] = arr[randomPos];
arr[randomPos] = tmp;
}
Arrays.sort(arr);
}
static void shuffleList(ArrayList<Long> arr) {
int n = arr.size();
Random rnd = new Random();
for (int i = 0; i < n; ++i) {
long tmp = arr.get(i);
int randomPos = i + rnd.nextInt(n - i);
arr.set(i, arr.get(randomPos));
arr.set(randomPos, tmp);
}
}
static void factorize(long n) {
int count = 0;
while (!(n % 2 > 0)) {
n >>= 1;
count++;
}
if (count > 0) {
// System.out.println("2" + " " + count);
}
long i = 0;
for (i = 3; i <= (long) Math.sqrt(n); i += 2) {
count = 0;
while (n % i == 0) {
count++;
n = n / i;
}
if (count > 0) {
// System.out.println(i + " " + count);
}
}
if (n > 2) {
// System.out.println(i + " " + count);
}
}
static void sortL(long[] arr) {
int n = arr.length;
Random rnd = new Random();
for (int i = 0; i < n; ++i) {
long tmp = arr[i];
int randomPos = i + rnd.nextInt(n - i);
arr[i] = arr[randomPos];
arr[randomPos] = tmp;
}
Arrays.sort(arr);
}
////////////////////////////////// DSU START ///////////////////////////
static class DSU {
int[] parent, rank, total_Elements;
DSU(int n) {
parent = new int[n + 1];
rank = new int[n + 1];
// total_Elements = new int[n + 1];
for (int i = 0; i <= n; i++) {
parent[i] = i;
rank[i] = 1;
// total_Elements[i] = 1;
}
}
int find(int u) {
if (parent[u] == u)
return u;
return parent[u] = find(parent[u]);
}
void union(int u, int v) {
int pu = find(u);
int pv = find(v);
if (pu != pv) {
if (rank[pu] > rank[pv]) {
parent[pv] = pu;
// total_Elements[pu] += total_Elements[pv];
} else if (rank[pu] < rank[pv]) {
parent[pu] = pv;
// total_Elements[pv] += total_Elements[pu];
} else {
parent[pu] = pv;
// total_Elements[pv] += total_Elements[pu];
rank[pv]++;
}
}
}
}
////////////////////////////////// DSU END /////////////////////////////
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
public boolean hasNext() {
return false;
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
} | Java | ["9 10\nabcabcabc\n1 a\n1 b\n2 c\n3 a\n4 b\n5 c\n8 a\n9 b\n1 c\n4 a"] | 2 seconds | ["3\n2\n2\n2\n1\n2\n1\n1\n1\n0"] | NoteLet's consider the state of the string after each query: $$$s =$$$ "abcabcabc". In this case $$$3$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcaccabb". This string does not contain "abc" as a substring. $$$s =$$$ "bbcabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bccabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bccbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcaabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcabbcabc". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaac". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaac". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "ccabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccaaccaab". In this case the string does not contain "abc" as a substring and no replacements are needed. | Java 11 | standard input | [
"implementation",
"strings"
] | db473ad780a93983667d12b1357c6e2f | The first line contains two integers $$$n$$$ and $$$q$$$ $$$(1 \le n, q \le 10^5)$$$, the length of the string and the number of queries, respectively. The second line contains the string $$$s$$$, consisting of characters "a", "b" and "c". Each of the next $$$q$$$ lines contains an integer $$$i$$$ and character $$$c$$$ $$$(1 \le i \le n)$$$, index and the value of the new item in the string, respectively. It is guaranteed that character's $$$c$$$ value is "a", "b" or "c". | 1,100 | For each query output the minimal number of characters that would have to be replaced so that the string doesn't contain "abc" as a substring. | standard output | |
PASSED | da46879ee24cca0462cc35b9dd5c36b3 | train_110.jsonl | 1638110100 | Before becoming a successful trader William got a university degree. During his education an interesting situation happened, after which William started to listen to homework assignments much more attentively. What follows is the correct formal description of the homework assignment:You are given a string $$$s$$$ of length $$$n$$$ only consisting of characters "a", "b" and "c". There are $$$q$$$ queries of format ($$$pos, c$$$), meaning replacing the element of string $$$s$$$ at position $$$pos$$$ with character $$$c$$$. After each query you must output the minimal number of characters in the string, which have to be replaced, so that the string doesn't contain string "abc" as a substring. A valid replacement of a character is replacing it with "a", "b" or "c".A string $$$x$$$ is a substring of a string $$$y$$$ if $$$x$$$ can be obtained from $$$y$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. | 256 megabytes | import java.util.*;
import java.io.*;
public class A1{
static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
static BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(System.out));
static StringTokenizer st;
static final long mod=1000000007;
public static void Solve() throws IOException{
st=new StringTokenizer(br.readLine());
int n=Integer.parseInt(st.nextToken()),q=Integer.parseInt(st.nextToken());
char[] ch=getStr();
String s=String.valueOf(ch);
int c=0;
for(int i=0;i<n;i++){
if(i<=n-3 && s.substring(i,i+3).equals("abc")) c++;
}
while(q-->0){
st=new StringTokenizer(br.readLine());
int idx=Integer.parseInt(st.nextToken())-1;
char ca=st.nextToken().charAt(0);
if(ch[idx]!=ca){
if((idx-2>=0 && ch[idx-2]=='a' && ch[idx-1]=='b' && ch[idx]=='c') || (idx-1>=0 && idx+1<n && ch[idx-1]=='a' && ch[idx]=='b' && ch[idx+1]=='c') || (idx+2<n && ch[idx]=='a' && ch[idx+1]=='b' && ch[idx+2]=='c')) c--;
ch[idx]=ca;
if((idx-2>=0 && ch[idx-2]=='a' && ch[idx-1]=='b' && ch[idx]=='c') || (idx-1>=0 && idx+1<n && ch[idx-1]=='a' && ch[idx]=='b' && ch[idx+1]=='c') || (idx+2<n && ch[idx]=='a' && ch[idx+1]=='b' && ch[idx+2]=='c')) c++;
}
bw.write(c+"\n");
}
}
/** Main Method**/
public static void main(String[] YDSV) throws IOException{
int t=1;
//int t=Integer.parseInt(br.readLine());
while(t-->0) Solve();
bw.flush();
}
/** Helpers**/
private static char[] getStr()throws IOException{
return br.readLine().toCharArray();
}
private static int Gcd(int a,int b){
if(b==0) return a;
return Gcd(b,a%b);
}
private static long Gcd(long a,long b){
if(b==0) return a;
return Gcd(b,a%b);
}
private static int[] getArrIn(int n) throws IOException{
st=new StringTokenizer(br.readLine());
int[] ar=new int[n];
for(int i=0;i<n;i++) ar[i]=Integer.parseInt(st.nextToken());
return ar;
}
private static Integer[] getArrInP(int n) throws IOException{
st=new StringTokenizer(br.readLine());
Integer[] ar=new Integer[n];
for(int i=0;i<n;i++) ar[i]=Integer.parseInt(st.nextToken());
return ar;
}
private static long[] getArrLo(int n) throws IOException{
st=new StringTokenizer(br.readLine());
long[] ar=new long[n];
for(int i=0;i<n;i++) ar[i]=Long.parseLong(st.nextToken());
return ar;
}
private static List<Integer> getListIn(int n) throws IOException{
st=new StringTokenizer(br.readLine());
List<Integer> al=new ArrayList<>();
for(int i=0;i<n;i++) al.add(Integer.parseInt(st.nextToken()));
return al;
}
private static List<Long> getListLo(int n) throws IOException{
st=new StringTokenizer(br.readLine());
List<Long> al=new ArrayList<>();
for(int i=0;i<n;i++) al.add(Long.parseLong(st.nextToken()));
return al;
}
private static long pow_mod(long a,long b) {
long result=1;
while(b!=0){
if((b&1)!=0) result=(result*a)%mod;
a=(a*a)%mod;
b>>=1;
}
return result;
}
private static int lower_bound(int a[],int x){
int l=-1,r=a.length;
while(l+1<r){
int m=(l+r)>>>1;
if(a[m]>=x) r=m;
else l=m;
}
return r+1;
}
private static long lower_bound(long a[],long x){
int l=-1,r=a.length;
while(l+1<r){
int m=(l+r)>>>1;
if(a[m]>=x) r=m;
else l=m;
}
return r+1;
}
private static int upper_bound(int a[],int x){
int l=-1,r=a.length;
while(l+1<r){
int m=(l+r)>>>1;
if(a[m]<=x) l=m;
else r=m;
}
return l+1;
}
private static long upper_bound(long a[],long x){
int l=-1,r=a.length;
while(l+1<r){
int m=(l+r)>>>1;
if(a[m]<=x) l=m;
else r=m;
}
return l+1;
}
private static boolean Sqrt(int x){
int a=(int)Math.sqrt(x);
return a*a==x;
}
private static boolean Sqrt(long x){
long a=(long)Math.sqrt(x);
return a*a==x;
}
} | Java | ["9 10\nabcabcabc\n1 a\n1 b\n2 c\n3 a\n4 b\n5 c\n8 a\n9 b\n1 c\n4 a"] | 2 seconds | ["3\n2\n2\n2\n1\n2\n1\n1\n1\n0"] | NoteLet's consider the state of the string after each query: $$$s =$$$ "abcabcabc". In this case $$$3$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcaccabb". This string does not contain "abc" as a substring. $$$s =$$$ "bbcabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bccabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bccbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcaabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcabbcabc". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaac". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaac". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "ccabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccaaccaab". In this case the string does not contain "abc" as a substring and no replacements are needed. | Java 11 | standard input | [
"implementation",
"strings"
] | db473ad780a93983667d12b1357c6e2f | The first line contains two integers $$$n$$$ and $$$q$$$ $$$(1 \le n, q \le 10^5)$$$, the length of the string and the number of queries, respectively. The second line contains the string $$$s$$$, consisting of characters "a", "b" and "c". Each of the next $$$q$$$ lines contains an integer $$$i$$$ and character $$$c$$$ $$$(1 \le i \le n)$$$, index and the value of the new item in the string, respectively. It is guaranteed that character's $$$c$$$ value is "a", "b" or "c". | 1,100 | For each query output the minimal number of characters that would have to be replaced so that the string doesn't contain "abc" as a substring. | standard output | |
PASSED | f65cdb391cb6c8ab83ae504719377e30 | train_110.jsonl | 1638110100 | Before becoming a successful trader William got a university degree. During his education an interesting situation happened, after which William started to listen to homework assignments much more attentively. What follows is the correct formal description of the homework assignment:You are given a string $$$s$$$ of length $$$n$$$ only consisting of characters "a", "b" and "c". There are $$$q$$$ queries of format ($$$pos, c$$$), meaning replacing the element of string $$$s$$$ at position $$$pos$$$ with character $$$c$$$. After each query you must output the minimal number of characters in the string, which have to be replaced, so that the string doesn't contain string "abc" as a substring. A valid replacement of a character is replacing it with "a", "b" or "c".A string $$$x$$$ is a substring of a string $$$y$$$ if $$$x$$$ can be obtained from $$$y$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. | 256 megabytes | import java.io.*;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class B_William_the_Vigilant {
static final int MOD = (int) 1e9 + 7;
static final int INT_POSITIVE_INFINITY = Integer.MAX_VALUE;
static final long LONG_POSITIVE_INFINITY = Long.MAX_VALUE;
static final int INT_NEGATIVE_INFINITY = Integer.MIN_VALUE;
static final long LONG_NEGATIVE_INFINITY = Long.MIN_VALUE;
static StringBuilder result = new StringBuilder();
public static void main(String args[]) throws IOException {
FastReader fr = new FastReader();
int tc = 1;
while (tc-- > 0) {
int n = fr.nextInt();
int m = fr.nextInt();
char[] a = fr.nextLine().toCharArray();
Matcher matcher = Pattern.compile("abc").matcher(String.valueOf(a));
int cnt = 0;
while (matcher.find())
cnt++;
while (m-- > 0) {
String[] s = fr.nextLine().split(" ");
int k = Integer.parseInt(s[0]) - 1;
char c = s[1].charAt(0);
for (int i = Math.max(0, k - 2); i <= Math.min(n - 3, k); i++) {
if (isABC(a, i))
cnt--;
}
a[k] = c;
for (int i = Math.max(0, k - 2); i <= Math.min(n - 3, k); i++) {
if (isABC(a, i))
cnt++;
}
result.append(cnt + "\n");
}
}
System.out.print(result);
}
static boolean isABC(char[] a, int i) {
return a[i] == 'a' && a[i + 1] == 'b' && a[i + 2] == 'c';
}
static class Pair {
int a;
int b;
public Pair(int a, int b) {
this.a = a;
this.b = b;
}
}
static class Tair {
int a;
int b;
int c;
public Tair(int a, int b, int c) {
this.a = a;
this.b = b;
this.c = c;
}
}
static void reverse(int[] nums, int i, int j) {
while (i < j) {
swap(nums, i++, j--);
}
}
static void swap(int[] nums, int i, int j) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
static int fastPow(long b, long e) {
long curr = b;
long res = 1;
while (e != 0) {
if ((e & 1) != 0) {
res = (res * curr) % MOD;
}
curr = (curr * curr) % MOD;
e >>= 1;
}
return (int) res;
}
static long lcm(long a, long b) {
return a * b / gcd(a, b);
}
static long gcd(long a, long b) {
if (b > a) {
return gcd(b, a);
}
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
static int LIS(int[] arr, int i, int j) {
TreeSet<Integer> set = new TreeSet<Integer>();
for (int k = i; k <= j; k++) {
int x = arr[k];
if (set.isEmpty() || set.last() < x) {
set.add(x);
} else {
set.remove(set.ceiling(x));
set.add(x);
}
}
return set.size();
}
static int LDS(int[] arr, int i, int j) {
reverse(arr, i, j);
int res = LIS(arr, i, j);
reverse(arr, i, j);
return res;
}
static int lowerBound(int[] arr, int target) {
int idx = -1;
int l = 0;
int r = arr.length - 1;
while (l <= r) {
int mid = l + (r - l) / 2;
if (arr[mid] >= target) {
r = mid - 1;
} else {
idx = mid;
l = mid + 1;
}
}
return idx;
}
static int upperBound(int[] arr, int target) {
int idx = arr.length;
int l = 0;
int r = arr.length - 1;
while (l <= r) {
int mid = l + (r - l) / 2;
if (arr[mid] <= target) {
l = mid + 1;
} else {
idx = mid;
r = mid - 1;
}
}
return idx;
}
static boolean isPrime(long x) {
for (long i = 2; i * i <= x; i++) {
if (x % i == 0)
return false;
}
return true;
}
static boolean isFibonacci(long x) {
return isPerfectSquare(5 * x * x + 4);
}
static boolean isPerfectSquare(long x) {
return Math.sqrt(x) * Math.sqrt(x) == x;
}
static class FastReader {
InputStreamReader ir;
BufferedReader br;
StringTokenizer st;
public FastReader() {
ir = new InputStreamReader(System.in);
br = new BufferedReader(ir);
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
} | Java | ["9 10\nabcabcabc\n1 a\n1 b\n2 c\n3 a\n4 b\n5 c\n8 a\n9 b\n1 c\n4 a"] | 2 seconds | ["3\n2\n2\n2\n1\n2\n1\n1\n1\n0"] | NoteLet's consider the state of the string after each query: $$$s =$$$ "abcabcabc". In this case $$$3$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcaccabb". This string does not contain "abc" as a substring. $$$s =$$$ "bbcabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bccabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bccbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcaabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcabbcabc". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaac". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaac". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "ccabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccaaccaab". In this case the string does not contain "abc" as a substring and no replacements are needed. | Java 11 | standard input | [
"implementation",
"strings"
] | db473ad780a93983667d12b1357c6e2f | The first line contains two integers $$$n$$$ and $$$q$$$ $$$(1 \le n, q \le 10^5)$$$, the length of the string and the number of queries, respectively. The second line contains the string $$$s$$$, consisting of characters "a", "b" and "c". Each of the next $$$q$$$ lines contains an integer $$$i$$$ and character $$$c$$$ $$$(1 \le i \le n)$$$, index and the value of the new item in the string, respectively. It is guaranteed that character's $$$c$$$ value is "a", "b" or "c". | 1,100 | For each query output the minimal number of characters that would have to be replaced so that the string doesn't contain "abc" as a substring. | standard output | |
PASSED | ade96b5f409360133fa14e4909deff6c | train_110.jsonl | 1638110100 | Before becoming a successful trader William got a university degree. During his education an interesting situation happened, after which William started to listen to homework assignments much more attentively. What follows is the correct formal description of the homework assignment:You are given a string $$$s$$$ of length $$$n$$$ only consisting of characters "a", "b" and "c". There are $$$q$$$ queries of format ($$$pos, c$$$), meaning replacing the element of string $$$s$$$ at position $$$pos$$$ with character $$$c$$$. After each query you must output the minimal number of characters in the string, which have to be replaced, so that the string doesn't contain string "abc" as a substring. A valid replacement of a character is replacing it with "a", "b" or "c".A string $$$x$$$ is a substring of a string $$$y$$$ if $$$x$$$ can be obtained from $$$y$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. | 256 megabytes | import java.io.*;
import java.util.*;
import java.math.*;
public class Solutionb {
public static void main(String arg[]) throws IOException {
CustomInputReader sc = new CustomInputReader();
PrintWriter pw = new PrintWriter(System.out);
int in[] = sc.nextIntArr(2);
int n = in[0], q = in[1];
char s[] = sc.next().toCharArray();
boolean flag[] = new boolean[n];
int counter = 0;
for(int i=0;i<n;++i) {
if(s[i] == 'a' && i+2<n && s[i+1] == 'b' && s[i+2] == 'c') {
counter++;
for(int j=i;j<=i+2;++j) flag[j] = true;
}
}
while(q-->0) {
String inp[] = sc.next().split(" ");
int index = Integer.parseInt(inp[0]) - 1;
char ch = inp[1].charAt(0);
if(flag[index]) {
if(ch == s[index]) {
pw.println(counter);
}
else {
if(s[index] == 'a') {
for(int i=index;i<=index+2;++i) flag[i] = false;
}
else if(s[index] == 'b') {
for(int i=index-1;i<=index+1;++i) flag[i] = false;
}
else {
for(int i=index-2;i<=index;++i) flag[i] = false;
}
s[index] = ch;
counter--;
if(ch == 'a') {
if(index+2<n && s[index+1] == 'b' && s[index+2] == 'c') {
counter++;
for(int i=index;i<=index+2;++i) flag[i] = true;
}
}
else if(ch == 'b') {
if(index-1>=0 && index+1<n && s[index-1] == 'a' && s[index+1] == 'c') {
for(int i=index-1;i<=index+1;++i) flag[i] = true;
counter++;
}
}
else {
if(index-2>=0 && s[index-1] == 'b' && s[index-2] == 'a') {
for(int i=index-2;i<=index;++i) flag[i] = true;
counter++;
}
}
pw.println(counter);
}
}
else {
if(ch == 'a') {
if(index+2<n && s[index+1] == 'b' && s[index+2] == 'c') {
counter++;
for(int i=index;i<=index+2;++i) flag[i] = true;
}
}
else if(ch == 'b') {
if(index-1>=0 && index+1<n && s[index-1] == 'a' && s[index+1] == 'c') {
for(int i=index-1;i<=index+1;++i) flag[i] = true;
counter++;
}
}
else {
if(index-2>=0 && s[index-1] == 'b' && s[index-2] == 'a') {
for(int i=index-2;i<=index;++i) flag[i] = true;
counter++;
}
}
s[index] = ch;
pw.println(counter);
}
}
//flush output
pw.flush();
pw.close();
}
static class CustomInputReader {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public int nextInt() throws IOException {
return Integer.parseInt(br.readLine());
}
public int[] nextIntArr(int n) throws IOException {
String in[] = br.readLine().split(" ");
int arr[] = new int[n];
for(int i=0;i<n;++i) arr[i]=Integer.parseInt(in[i]);
return arr;
}
public long nextLong() throws IOException {
return Long.parseLong(br.readLine());
}
public long[] nextLongArr(int n) throws IOException {
String in[] = br.readLine().split(" ");
long arr[] = new long[n];
for(int i=0;i<n;++i) arr[i]=Long.parseLong(in[i]);
return arr;
}
public String next() throws IOException {
return br.readLine();
}
}
}
| Java | ["9 10\nabcabcabc\n1 a\n1 b\n2 c\n3 a\n4 b\n5 c\n8 a\n9 b\n1 c\n4 a"] | 2 seconds | ["3\n2\n2\n2\n1\n2\n1\n1\n1\n0"] | NoteLet's consider the state of the string after each query: $$$s =$$$ "abcabcabc". In this case $$$3$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcaccabb". This string does not contain "abc" as a substring. $$$s =$$$ "bbcabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bccabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bccbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcaabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcabbcabc". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaac". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaac". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "ccabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccaaccaab". In this case the string does not contain "abc" as a substring and no replacements are needed. | Java 11 | standard input | [
"implementation",
"strings"
] | db473ad780a93983667d12b1357c6e2f | The first line contains two integers $$$n$$$ and $$$q$$$ $$$(1 \le n, q \le 10^5)$$$, the length of the string and the number of queries, respectively. The second line contains the string $$$s$$$, consisting of characters "a", "b" and "c". Each of the next $$$q$$$ lines contains an integer $$$i$$$ and character $$$c$$$ $$$(1 \le i \le n)$$$, index and the value of the new item in the string, respectively. It is guaranteed that character's $$$c$$$ value is "a", "b" or "c". | 1,100 | For each query output the minimal number of characters that would have to be replaced so that the string doesn't contain "abc" as a substring. | standard output | |
PASSED | 08619db01929b43eba0a0c6f22493d82 | train_110.jsonl | 1638110100 | Before becoming a successful trader William got a university degree. During his education an interesting situation happened, after which William started to listen to homework assignments much more attentively. What follows is the correct formal description of the homework assignment:You are given a string $$$s$$$ of length $$$n$$$ only consisting of characters "a", "b" and "c". There are $$$q$$$ queries of format ($$$pos, c$$$), meaning replacing the element of string $$$s$$$ at position $$$pos$$$ with character $$$c$$$. After each query you must output the minimal number of characters in the string, which have to be replaced, so that the string doesn't contain string "abc" as a substring. A valid replacement of a character is replacing it with "a", "b" or "c".A string $$$x$$$ is a substring of a string $$$y$$$ if $$$x$$$ can be obtained from $$$y$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. | 256 megabytes |
// Working program with FastReader
import java.util.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class B_William_the_Vigilant {
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static void main(String[] args) {
FastReader sc = new FastReader();
int n = sc.nextInt();
int q = sc.nextInt();
String str = sc.next();
char c[] = str.toCharArray();
Set<Integer> set = new HashSet<>();
long count = 0;
int i = 0;
while (i + 2 < n) {
String temp = "" + c[i] + c[i + 1] + c[i + 2];
if (temp.equals("abc")) {
set.add(i);
++count;
i = i + 3;
} else {
++i;
}
}
while (q-- > 0) {
// String query = sc.next();
int pos = sc.nextInt();
--pos;
char rep = sc.next().charAt(0);
if (set.contains(pos)) {
--count;
set.remove(pos);
}
if (pos - 1 >= 0 && set.contains(pos - 1)) {
--count;
set.remove(pos - 1);
}
if (pos - 2 >= 0 && set.contains(pos - 2)) {
--count;
set.remove(pos - 2);
}
c[pos] = rep;
if (pos + 2 < n) {
String s = "" + c[pos] + c[pos + 1] + c[pos + 2];
if (s.equals("abc")) {
++count;
set.add(pos);
}
}
if (pos - 1 >= 0 && pos + 1 < n) {
String s = "" + c[pos - 1] + c[pos] + c[pos + 1];
if (s.equals("abc")) {
++count;
set.add(pos - 1);
}
}
if (pos - 2 >= 0) {
String s = "" + c[pos - 2] + c[pos - 1] + c[pos];
if (s.equals("abc")) {
++count;
set.add(pos - 2);
}
}
System.out.println(count);
}
}
} | Java | ["9 10\nabcabcabc\n1 a\n1 b\n2 c\n3 a\n4 b\n5 c\n8 a\n9 b\n1 c\n4 a"] | 2 seconds | ["3\n2\n2\n2\n1\n2\n1\n1\n1\n0"] | NoteLet's consider the state of the string after each query: $$$s =$$$ "abcabcabc". In this case $$$3$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcaccabb". This string does not contain "abc" as a substring. $$$s =$$$ "bbcabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bccabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bccbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcaabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcabbcabc". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaac". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaac". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "ccabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccaaccaab". In this case the string does not contain "abc" as a substring and no replacements are needed. | Java 11 | standard input | [
"implementation",
"strings"
] | db473ad780a93983667d12b1357c6e2f | The first line contains two integers $$$n$$$ and $$$q$$$ $$$(1 \le n, q \le 10^5)$$$, the length of the string and the number of queries, respectively. The second line contains the string $$$s$$$, consisting of characters "a", "b" and "c". Each of the next $$$q$$$ lines contains an integer $$$i$$$ and character $$$c$$$ $$$(1 \le i \le n)$$$, index and the value of the new item in the string, respectively. It is guaranteed that character's $$$c$$$ value is "a", "b" or "c". | 1,100 | For each query output the minimal number of characters that would have to be replaced so that the string doesn't contain "abc" as a substring. | standard output | |
PASSED | 12d00a2aa7fa32624f1a6a534207c072 | train_110.jsonl | 1638110100 | Before becoming a successful trader William got a university degree. During his education an interesting situation happened, after which William started to listen to homework assignments much more attentively. What follows is the correct formal description of the homework assignment:You are given a string $$$s$$$ of length $$$n$$$ only consisting of characters "a", "b" and "c". There are $$$q$$$ queries of format ($$$pos, c$$$), meaning replacing the element of string $$$s$$$ at position $$$pos$$$ with character $$$c$$$. After each query you must output the minimal number of characters in the string, which have to be replaced, so that the string doesn't contain string "abc" as a substring. A valid replacement of a character is replacing it with "a", "b" or "c".A string $$$x$$$ is a substring of a string $$$y$$$ if $$$x$$$ can be obtained from $$$y$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. | 256 megabytes | import java.util.*;
import java.lang.*;
import java.io.*;
public class Main
{
static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
public static void main (String[] args) throws java.lang.Exception
{
FastReader sc = new FastReader();
//int t = sc.nextInt();
//while(t-->0){
solve(sc);
//}
}
public static void solve(FastReader sc){
int n = sc.nextInt();
int q = sc.nextInt();
String str = sc.next();
char [] arr = str.toCharArray();
int ans = 0;
for(int i = 0;i<n-2;++i){
if(arr[i]=='a'&&arr[i+1]=='b'&&arr[i+2]=='c' ){
ans++;
}
}
if(arr.length<3){
while(q-->0){
int pos = sc.nextInt();
char rep = sc.next().charAt(0);
out.println(0);
}
out.flush();return;
}
while(q-->0){
int pos = sc.nextInt();
char rep = sc.next().charAt(0);
int i=pos-1;
if(rep==arr[i]){
out.println(ans);continue;
}
if(partofabc(arr, i)){
ans--;
}
arr[i] = rep;
if(partofabc(arr, i)){
ans++;
}
out.println(ans);
}
//out.println(ans);
out.flush();
}
/*
int [] arr = new int[n];
for(int i = 0;i<n;++i){
arr[i] = sc.nextInt();
}
*/
static boolean partofabc(char[] arr, int ind){
int len = arr.length;
if(arr[ind]=='a'){
if(ind+2<len && arr[ind+1]=='b' && arr[ind+2]=='c'){
return true;
}
return false;
}else if(arr[ind]=='b'){
if(ind-1>=0 && arr[ind-1]=='a' && ind+1<len && arr[ind+1]=='c'){
return true;
}
return false;
}else{
if(ind-2>=0 && arr[ind-2]=='a' && arr[ind-1]=='b'){
return true;
}
return false;
}
}
static void mysort(int[] arr) {
for(int i=0;i<arr.length;i++) {
int rand = (int) (Math.random() * arr.length);
int loc = arr[rand];
arr[rand] = arr[i];
arr[i] = loc;
}
Arrays.sort(arr);
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
} | Java | ["9 10\nabcabcabc\n1 a\n1 b\n2 c\n3 a\n4 b\n5 c\n8 a\n9 b\n1 c\n4 a"] | 2 seconds | ["3\n2\n2\n2\n1\n2\n1\n1\n1\n0"] | NoteLet's consider the state of the string after each query: $$$s =$$$ "abcabcabc". In this case $$$3$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcaccabb". This string does not contain "abc" as a substring. $$$s =$$$ "bbcabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bccabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bccbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcaabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcabbcabc". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaac". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaac". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "ccabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccaaccaab". In this case the string does not contain "abc" as a substring and no replacements are needed. | Java 11 | standard input | [
"implementation",
"strings"
] | db473ad780a93983667d12b1357c6e2f | The first line contains two integers $$$n$$$ and $$$q$$$ $$$(1 \le n, q \le 10^5)$$$, the length of the string and the number of queries, respectively. The second line contains the string $$$s$$$, consisting of characters "a", "b" and "c". Each of the next $$$q$$$ lines contains an integer $$$i$$$ and character $$$c$$$ $$$(1 \le i \le n)$$$, index and the value of the new item in the string, respectively. It is guaranteed that character's $$$c$$$ value is "a", "b" or "c". | 1,100 | For each query output the minimal number of characters that would have to be replaced so that the string doesn't contain "abc" as a substring. | standard output | |
PASSED | 7f079b64c977080367f2d64ee1a0b39b | train_110.jsonl | 1638110100 | Before becoming a successful trader William got a university degree. During his education an interesting situation happened, after which William started to listen to homework assignments much more attentively. What follows is the correct formal description of the homework assignment:You are given a string $$$s$$$ of length $$$n$$$ only consisting of characters "a", "b" and "c". There are $$$q$$$ queries of format ($$$pos, c$$$), meaning replacing the element of string $$$s$$$ at position $$$pos$$$ with character $$$c$$$. After each query you must output the minimal number of characters in the string, which have to be replaced, so that the string doesn't contain string "abc" as a substring. A valid replacement of a character is replacing it with "a", "b" or "c".A string $$$x$$$ is a substring of a string $$$y$$$ if $$$x$$$ can be obtained from $$$y$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Scanner;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
Scanner in = new Scanner(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskB solver = new TaskB();
solver.solve(1, in, out);
out.close();
}
static class TaskB {
public void solve(int testNumber, Scanner in, PrintWriter out) {
int T = 1;
while (T-- > 0) {
solveOne(in, out);
}
}
private void solveOne(Scanner in, PrintWriter out) {
int N = in.nextInt();
int Q = in.nextInt();
char[] chars = in.next().toCharArray();
int times = replace(chars);
while (Q-- > 0) {
int pos = in.nextInt() - 1;
char c = in.next().charAt(0);
if (chars[pos] != c) {
if (c == 'a') {
if (pos <= N - 3 && chars[pos + 1] == 'b' && chars[pos + 2] == 'c') times++;
if (chars[pos] == 'b' && pos >= 1 && pos <= N - 2 && chars[pos - 1] == 'a' && chars[pos + 1] == 'c')
times--;
if (pos >= 2 && chars[pos - 2] == 'a' && chars[pos - 1] == 'b' && chars[pos] == 'c') times--;
}
if (c == 'b') {
if (pos <= N - 3 && chars[pos] == 'a' && chars[pos + 1] == 'b' && chars[pos + 2] == 'c')
times--;
if (pos >= 1 && pos <= N - 2 && chars[pos - 1] == 'a' && chars[pos + 1] == 'c') times++;
if (pos >= 2 && chars[pos - 2] == 'a' && chars[pos - 1] == 'b' && chars[pos] == 'c') times--;
}
if (c == 'c') {
if (pos <= N - 3 && chars[pos + 1] == 'b' && chars[pos + 2] == 'c' && chars[pos] == 'a')
times--;
if (pos >= 1 && pos <= N - 2 && chars[pos - 1] == 'a' && chars[pos + 1] == 'c' && chars[pos] == 'b')
times--;
if (pos >= 2 && chars[pos - 2] == 'a' && chars[pos - 1] == 'b') times++;
}
}
chars[pos] = c;
out.println(times);
}
}
private int replace(char chars[]) {
int replace = 0;
for (int i = 0; i < chars.length - 2; i++) {
if (chars[i] == 'a' && chars[i + 1] == 'b' && chars[i + 2] == 'c') {
replace++;
}
}
return replace;
}
}
}
| Java | ["9 10\nabcabcabc\n1 a\n1 b\n2 c\n3 a\n4 b\n5 c\n8 a\n9 b\n1 c\n4 a"] | 2 seconds | ["3\n2\n2\n2\n1\n2\n1\n1\n1\n0"] | NoteLet's consider the state of the string after each query: $$$s =$$$ "abcabcabc". In this case $$$3$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcaccabb". This string does not contain "abc" as a substring. $$$s =$$$ "bbcabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bccabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bccbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcaabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcabbcabc". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaac". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaac". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "ccabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccaaccaab". In this case the string does not contain "abc" as a substring and no replacements are needed. | Java 11 | standard input | [
"implementation",
"strings"
] | db473ad780a93983667d12b1357c6e2f | The first line contains two integers $$$n$$$ and $$$q$$$ $$$(1 \le n, q \le 10^5)$$$, the length of the string and the number of queries, respectively. The second line contains the string $$$s$$$, consisting of characters "a", "b" and "c". Each of the next $$$q$$$ lines contains an integer $$$i$$$ and character $$$c$$$ $$$(1 \le i \le n)$$$, index and the value of the new item in the string, respectively. It is guaranteed that character's $$$c$$$ value is "a", "b" or "c". | 1,100 | For each query output the minimal number of characters that would have to be replaced so that the string doesn't contain "abc" as a substring. | standard output | |
PASSED | e08a377cd788d30b997a9e9941f1b2b9 | train_110.jsonl | 1638110100 | Before becoming a successful trader William got a university degree. During his education an interesting situation happened, after which William started to listen to homework assignments much more attentively. What follows is the correct formal description of the homework assignment:You are given a string $$$s$$$ of length $$$n$$$ only consisting of characters "a", "b" and "c". There are $$$q$$$ queries of format ($$$pos, c$$$), meaning replacing the element of string $$$s$$$ at position $$$pos$$$ with character $$$c$$$. After each query you must output the minimal number of characters in the string, which have to be replaced, so that the string doesn't contain string "abc" as a substring. A valid replacement of a character is replacing it with "a", "b" or "c".A string $$$x$$$ is a substring of a string $$$y$$$ if $$$x$$$ can be obtained from $$$y$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. | 256 megabytes | import java.util.*;
import java.lang.*;
import java.io.*;
public class Main
{
static long mod = (int)1e9+7;
static PrintWriter out=new PrintWriter(new BufferedOutputStream(System.out));
public static void main (String[] args) throws java.lang.Exception
{
FastReader sc =new FastReader();
// int t=sc.nextInt();
int t=1;
while(t-->0)
{
int n=sc.nextInt();
int m=sc.nextInt();
char a[]=sc.next().toCharArray();
int count=0;
for(int i=0;i<n-2;i++)
{
if(a[i]=='a' && a[i+1]=='b' && a[i+2]=='c')
{
count++;
}
}
while(m-- > 0)
{
int pos=sc.nextInt();
pos--;
char c=sc.next().charAt(0);
if( pos-2 >= 0 && a[pos-2] == 'a' && a[pos-1] == 'b' && a[pos] == 'c' )
{
count--;
}
else if( pos - 1 >= 0 && pos + 1 < n && a[pos-1] == 'a' && a[pos] == 'b' && a[pos+1] == 'c')
{
count--;
}
else if( pos+2 < n && a[pos] == 'a' && a[pos+1] == 'b' &&a[pos+2] == 'c')
{
count--;
}
a[pos] = c;
if( pos-2 >= 0 && a[pos-2] == 'a' && a[pos-1] == 'b' && a[pos] == 'c' )
{
count++;
}
else if( pos - 1 >= 0 && pos + 1 < n && a[pos-1] == 'a' && a[pos] == 'b' && a[pos+1] == 'c')
{
count++;
}
else if( pos+2 < n && a[pos] == 'a' && a[pos+1] == 'b' && a[pos+2] == 'c')
{
count++;
}
out.println(count);
}
}
out.flush();
}
static void frequency(char a[],int pos,int count)
{
int nm=count;
if(pos-2>=0 && pos+2<a.length)
{
boolean flag=true;
for(int i=pos-2;i<pos+1;i++)
{
if(a[i]=='a' && a[i+1]=='b' && a[i+2]=='c')
{
flag=false;
break;
}
}
if(flag==false)
{
count++;
}
else
{
count--;
}
}
else if(pos-1>=0 && pos+2<a.length)
{
boolean flag=true;
for(int i=pos-1;i<pos+1;i++)
{
if(a[i]=='a' && a[i+1]=='b' && a[i+2]=='c')
{
flag=false;
break;
}
}
if(flag==false)
{
count++;
}
else
{
count--;
}
}
else if(pos>=0 && pos+2<a.length)
{
boolean flag=true;
if(a[pos]=='a' && a[pos+1]=='b' && a[pos+2]=='c')
{
count++;
}
else
{
count--;
}
}
if(count>nm)
out.println(nm);
else
{
out.println(count);
}
}
static int findfrequencies(int a[],int n)
{
int count=0;
for(int i=0;i<a.length;i++)
{
if(a[i]==n)
{
count++;
}
}
return count;
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
float nextFloat()
{
return Float.parseFloat(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
int[] readArray(int n) {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
long[] readArrayLong(int n) {
long[] a=new long[n];
for (int i=0; i<n; i++) a[i]=nextLong();
return a;
}
}
public static int[] radixSort2(int[] a)
{
int n = a.length;
int[] c0 = new int[0x101];
int[] c1 = new int[0x101];
int[] c2 = new int[0x101];
int[] c3 = new int[0x101];
for(int v : a) {
c0[(v&0xff)+1]++;
c1[(v>>>8&0xff)+1]++;
c2[(v>>>16&0xff)+1]++;
c3[(v>>>24^0x80)+1]++;
}
for(int i = 0;i < 0xff;i++) {
c0[i+1] += c0[i];
c1[i+1] += c1[i];
c2[i+1] += c2[i];
c3[i+1] += c3[i];
}
int[] t = new int[n];
for(int v : a)t[c0[v&0xff]++] = v;
for(int v : t)a[c1[v>>>8&0xff]++] = v;
for(int v : a)t[c2[v>>>16&0xff]++] = v;
for(int v : t)a[c3[v>>>24^0x80]++] = v;
return a;
}
static int[] EvenOddArragement(int a[])
{
ArrayList<Integer> list=new ArrayList<>();
for(int i=0;i<a.length;i++)
{
if(a[i]%2==0)
{
list.add(a[i]);
}
}
for(int i=0;i<a.length;i++)
{
if(a[i]%2!=0)
{
list.add(a[i]);
}
}
for(int i=0;i<a.length;i++)
{
a[i]=list.get(i);
}
return a;
}
static int gcd(int a, int b) {
while (b != 0) {
int t = a;
a = b;
b = t % b;
}
return a;
}
static int lcm(int a, int b)
{
return (a / gcd(a, b)) * b;
}
public static HashMap<Integer, Integer> sortByValue(HashMap<Integer, Integer> hm)
{
// Create a list from elements of HashMap
List<Map.Entry<Integer, Integer> > list =
new LinkedList<Map.Entry<Integer, Integer> >(hm.entrySet());
// Sort the list
Collections.sort(list, new Comparator<Map.Entry<Integer, Integer> >() {
public int compare(Map.Entry<Integer, Integer> o1,
Map.Entry<Integer, Integer> o2)
{
return (o1.getValue()).compareTo(o2.getValue());
}
});
// put data from sorted list to hashmap
HashMap<Integer, Integer> temp = new LinkedHashMap<Integer, Integer>();
for (Map.Entry<Integer, Integer> aa : list) {
temp.put(aa.getKey(), aa.getValue());
}
return temp;
}
static int DigitSum(int n)
{
int r=0,sum=0;
while(n>=0)
{
r=n%10;
sum=sum+r;
n=n/10;
}
return sum;
}
static boolean checkPerfectSquare(int number)
{
double sqrt=Math.sqrt(number);
return ((sqrt - Math.floor(sqrt)) == 0);
}
static boolean isPowerOfTwo(int n)
{
if(n==0)
return false;
return (int)(Math.ceil((Math.log(n) / Math.log(2)))) == (int)(Math.floor(((Math.log(n) / Math.log(2)))));
}
static boolean isPrime2(int n)
{
if (n <= 1)
{
return false;
}
if (n == 2)
{
return true;
}
if (n % 2 == 0)
{
return false;
}
for (int i = 3; i <= Math.sqrt(n) + 1; i = i + 2)
{
if (n % i == 0)
{
return false;
}
}
return true;
}
static String minLexRotation(String str)
{
int n = str.length();
String arr[] = new String[n];
String concat = str + str;
for(int i=0;i<n;i++)
{
arr[i] = concat.substring(i, i + n);
}
Arrays.sort(arr);
return arr[0];
}
static String maxLexRotation(String str)
{
int n = str.length();
String arr[] = new String[n];
String concat = str + str;
for (int i = 0; i < n; i++)
{
arr[i] = concat.substring(i, i + n);
}
Arrays.sort(arr);
return arr[arr.length-1];
}
static class P implements Comparable<P> {
int i, j;
public P(int i, int j) {
this.i=i;
this.j=j;
}
public int compareTo(P o) {
return Integer.compare(i, o.i);
}
}
} | Java | ["9 10\nabcabcabc\n1 a\n1 b\n2 c\n3 a\n4 b\n5 c\n8 a\n9 b\n1 c\n4 a"] | 2 seconds | ["3\n2\n2\n2\n1\n2\n1\n1\n1\n0"] | NoteLet's consider the state of the string after each query: $$$s =$$$ "abcabcabc". In this case $$$3$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcaccabb". This string does not contain "abc" as a substring. $$$s =$$$ "bbcabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bccabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bccbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcaabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcabbcabc". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaac". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaac". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "ccabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccaaccaab". In this case the string does not contain "abc" as a substring and no replacements are needed. | Java 11 | standard input | [
"implementation",
"strings"
] | db473ad780a93983667d12b1357c6e2f | The first line contains two integers $$$n$$$ and $$$q$$$ $$$(1 \le n, q \le 10^5)$$$, the length of the string and the number of queries, respectively. The second line contains the string $$$s$$$, consisting of characters "a", "b" and "c". Each of the next $$$q$$$ lines contains an integer $$$i$$$ and character $$$c$$$ $$$(1 \le i \le n)$$$, index and the value of the new item in the string, respectively. It is guaranteed that character's $$$c$$$ value is "a", "b" or "c". | 1,100 | For each query output the minimal number of characters that would have to be replaced so that the string doesn't contain "abc" as a substring. | standard output | |
PASSED | 4f32f32a36a50582e9cb102b4520729f | train_110.jsonl | 1638110100 | Before becoming a successful trader William got a university degree. During his education an interesting situation happened, after which William started to listen to homework assignments much more attentively. What follows is the correct formal description of the homework assignment:You are given a string $$$s$$$ of length $$$n$$$ only consisting of characters "a", "b" and "c". There are $$$q$$$ queries of format ($$$pos, c$$$), meaning replacing the element of string $$$s$$$ at position $$$pos$$$ with character $$$c$$$. After each query you must output the minimal number of characters in the string, which have to be replaced, so that the string doesn't contain string "abc" as a substring. A valid replacement of a character is replacing it with "a", "b" or "c".A string $$$x$$$ is a substring of a string $$$y$$$ if $$$x$$$ can be obtained from $$$y$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. | 256 megabytes | import java.io.*;
import java.util.*;
public class William {
static BufferedReader br;
static PrintWriter pw;
static StringTokenizer token;
static int length;
static int numQueries;
static char[] s;
static int numABC;
public static void main(String args[]) throws IOException {
br = new BufferedReader(new InputStreamReader(System.in));
pw = new PrintWriter(System.out);
token = new StringTokenizer(br.readLine());
length = Integer.parseInt(token.nextToken());
numQueries = Integer.parseInt(token.nextToken());
if (length <= 2) {
while (numQueries-- > 0) {
pw.println(0);
}
pw.close();
return;
}
s = br.readLine().toCharArray();
numABC = 0;
for (int i = 0; i < length-2; i++) {
if (s[i] == 'a' && s[i+1] == 'b' && s[i+2] == 'c') {
numABC++;
i += 2;
}
}
while (numQueries-- > 0) solve();
pw.close();
}
public static void solve() throws IOException {
token = new StringTokenizer(br.readLine());
int zeroedIndex = Integer.parseInt(token.nextToken())-1;
for (int i = zeroedIndex-2; i <= zeroedIndex; i++) {
if (i >= 0 && i+2 < length) {
if (s[i] == 'a' && s[i+1] == 'b' && s[i+2] == 'c') {
numABC--;
break;
}
}
}
s[zeroedIndex] = token.nextToken().charAt(0);
for (int i = zeroedIndex-2; i <= zeroedIndex; i++) {
if (i >= 0 && i+2 < length) {
if (s[i] == 'a' && s[i+1] == 'b' && s[i+2] == 'c') {
numABC++;
break;
}
}
}
pw.println(numABC);
}
} | Java | ["9 10\nabcabcabc\n1 a\n1 b\n2 c\n3 a\n4 b\n5 c\n8 a\n9 b\n1 c\n4 a"] | 2 seconds | ["3\n2\n2\n2\n1\n2\n1\n1\n1\n0"] | NoteLet's consider the state of the string after each query: $$$s =$$$ "abcabcabc". In this case $$$3$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcaccabb". This string does not contain "abc" as a substring. $$$s =$$$ "bbcabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bccabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bccbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcaabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcabbcabc". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaac". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaac". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "ccabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccaaccaab". In this case the string does not contain "abc" as a substring and no replacements are needed. | Java 11 | standard input | [
"implementation",
"strings"
] | db473ad780a93983667d12b1357c6e2f | The first line contains two integers $$$n$$$ and $$$q$$$ $$$(1 \le n, q \le 10^5)$$$, the length of the string and the number of queries, respectively. The second line contains the string $$$s$$$, consisting of characters "a", "b" and "c". Each of the next $$$q$$$ lines contains an integer $$$i$$$ and character $$$c$$$ $$$(1 \le i \le n)$$$, index and the value of the new item in the string, respectively. It is guaranteed that character's $$$c$$$ value is "a", "b" or "c". | 1,100 | For each query output the minimal number of characters that would have to be replaced so that the string doesn't contain "abc" as a substring. | standard output | |
PASSED | 987430812c0892a95e0959e98c9430ce | train_110.jsonl | 1638110100 | Before becoming a successful trader William got a university degree. During his education an interesting situation happened, after which William started to listen to homework assignments much more attentively. What follows is the correct formal description of the homework assignment:You are given a string $$$s$$$ of length $$$n$$$ only consisting of characters "a", "b" and "c". There are $$$q$$$ queries of format ($$$pos, c$$$), meaning replacing the element of string $$$s$$$ at position $$$pos$$$ with character $$$c$$$. After each query you must output the minimal number of characters in the string, which have to be replaced, so that the string doesn't contain string "abc" as a substring. A valid replacement of a character is replacing it with "a", "b" or "c".A string $$$x$$$ is a substring of a string $$$y$$$ if $$$x$$$ can be obtained from $$$y$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. | 256 megabytes | import java.io.*;
import java.util.*;
public class William {
static BufferedReader br;
static PrintWriter pw;
static StringTokenizer token;
static int length;
static int numQueries;
static char[] s;
static int numABC;
public static void main(String args[]) throws IOException {
br = new BufferedReader(new InputStreamReader(System.in));
pw = new PrintWriter(System.out);
token = new StringTokenizer(br.readLine());
length = Integer.parseInt(token.nextToken());
numQueries = Integer.parseInt(token.nextToken());
if (length <= 2) {
while (numQueries-- > 0) {
System.out.println(0);
}
return;
}
s = br.readLine().toCharArray();
numABC = 0;
for (int i = 0; i < length-2; i++) {
if (s[i] == 'a' && s[i+1] == 'b' && s[i+2] == 'c') {
numABC++;
i += 2;
}
}
while (numQueries-- > 0) solve();
}
public static void solve() throws IOException {
token = new StringTokenizer(br.readLine());
int zeroedIndex = Integer.parseInt(token.nextToken())-1;
for (int i = zeroedIndex-2; i <= zeroedIndex; i++) {
if (i >= 0 && i+2 < length) {
if (s[i] == 'a' && s[i+1] == 'b' && s[i+2] == 'c') {
numABC--;
break;
}
}
}
s[zeroedIndex] = token.nextToken().charAt(0);
for (int i = zeroedIndex-2; i <= zeroedIndex; i++) {
if (i >= 0 && i+2 < length) {
if (s[i] == 'a' && s[i+1] == 'b' && s[i+2] == 'c') {
numABC++;
break;
}
}
}
System.out.println(numABC);
}
} | Java | ["9 10\nabcabcabc\n1 a\n1 b\n2 c\n3 a\n4 b\n5 c\n8 a\n9 b\n1 c\n4 a"] | 2 seconds | ["3\n2\n2\n2\n1\n2\n1\n1\n1\n0"] | NoteLet's consider the state of the string after each query: $$$s =$$$ "abcabcabc". In this case $$$3$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcaccabb". This string does not contain "abc" as a substring. $$$s =$$$ "bbcabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bccabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bccbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcaabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcabbcabc". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaac". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaac". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "ccabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccaaccaab". In this case the string does not contain "abc" as a substring and no replacements are needed. | Java 11 | standard input | [
"implementation",
"strings"
] | db473ad780a93983667d12b1357c6e2f | The first line contains two integers $$$n$$$ and $$$q$$$ $$$(1 \le n, q \le 10^5)$$$, the length of the string and the number of queries, respectively. The second line contains the string $$$s$$$, consisting of characters "a", "b" and "c". Each of the next $$$q$$$ lines contains an integer $$$i$$$ and character $$$c$$$ $$$(1 \le i \le n)$$$, index and the value of the new item in the string, respectively. It is guaranteed that character's $$$c$$$ value is "a", "b" or "c". | 1,100 | For each query output the minimal number of characters that would have to be replaced so that the string doesn't contain "abc" as a substring. | standard output | |
PASSED | 3eaeeaa605f67458b2ae5c672cfd40f5 | train_110.jsonl | 1638110100 | Before becoming a successful trader William got a university degree. During his education an interesting situation happened, after which William started to listen to homework assignments much more attentively. What follows is the correct formal description of the homework assignment:You are given a string $$$s$$$ of length $$$n$$$ only consisting of characters "a", "b" and "c". There are $$$q$$$ queries of format ($$$pos, c$$$), meaning replacing the element of string $$$s$$$ at position $$$pos$$$ with character $$$c$$$. After each query you must output the minimal number of characters in the string, which have to be replaced, so that the string doesn't contain string "abc" as a substring. A valid replacement of a character is replacing it with "a", "b" or "c".A string $$$x$$$ is a substring of a string $$$y$$$ if $$$x$$$ can be obtained from $$$y$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. | 256 megabytes |
import static java.lang.Integer.parseInt;
import static java.lang.Long.parseLong;
import static java.lang.Double.parseDouble;
import static java.lang.Math.PI;
import static java.lang.Math.min;
import static java.lang.System.arraycopy;
import static java.lang.System.exit;
import static java.util.Arrays.copyOf;
import java.util.LinkedList;
import java.util.List;
import java.util.Iterator;
import java.io.FileReader;
import java.io.FileWriter;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.Comparator;
import java.lang.StringBuilder;
import java.util.Collections;
import java.text.DecimalFormat;
public class Solution {
static BufferedReader in;
static PrintWriter out;
static StringTokenizer tok;
static long mod= (long)1e9+7;
static boolean isPrime[];
private static void solve() throws IOException{
int n = scanInt(), q = scanInt();
char[] arr = scanString().toCharArray();
int count = 0;
for(int i = 0 ; i<n-2; ++i){
char temp = arr[i];
boolean flag = true;
for(int j = 1; j<=2 ;++j){
if(temp+j != arr[j+i]){
flag = false;
break;
}
}
if(flag){
++count;
}
}
//out.println(count);
while(--q >=0){
int pos = scanInt()-1; char ch = scanString().charAt(0);
if(arr[pos] == ch){
out.println(count);
continue;
}
boolean happened = false;
for(int j = pos - 2; j <= pos+2 && j<n-2; ++j){
if(j<0)
continue;
if((arr[j]+1 == arr[j+1]) && (arr[j+1]+1 == arr[j+2])){
--count;
happened = true;
//break;
}
}
arr[pos] = ch;
for(int j = pos - 2; j <= pos+2 && j<n-2; ++j){
if(j<0)
continue;
if((arr[j]+1 == arr[j+1]) && (arr[j+1]+1 == arr[j+2])){
++count;
//break;
}
}
out.println(count);
}
}
public static void main(String[] args) {
try {
long startTime = System.currentTimeMillis();
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
//in = new BufferedReader(new FileReader("input.txt"));
//out = new PrintWriter(new FileWriter("output.txt"));
// int test=scanInt();
// for(int t=1; t<=test; t++){
// out.print("Case #"+t+": ");
solve();
// }
long endTime = System.currentTimeMillis();
long totalTime = endTime - startTime;
//out.println(totalTime+" "+System.currentTimeMillis() );
in.close();
out.close();
} catch (Throwable e) {
e.printStackTrace();
exit(1);
}
}
static int scanInt() throws IOException {
return parseInt(scanString());
}
static long scanLong() throws IOException {
return parseLong(scanString());
}
static double scanDouble() throws IOException {
return parseDouble(scanString());
}
static String scanString() throws IOException {
if (tok == null || !tok.hasMoreTokens()) {
tok = new StringTokenizer(in.readLine());
}
return tok.nextToken();
}
static String scanLine() throws IOException {
return in.readLine();
}
}
| Java | ["9 10\nabcabcabc\n1 a\n1 b\n2 c\n3 a\n4 b\n5 c\n8 a\n9 b\n1 c\n4 a"] | 2 seconds | ["3\n2\n2\n2\n1\n2\n1\n1\n1\n0"] | NoteLet's consider the state of the string after each query: $$$s =$$$ "abcabcabc". In this case $$$3$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcaccabb". This string does not contain "abc" as a substring. $$$s =$$$ "bbcabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bccabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bccbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcaabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcabbcabc". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaac". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaac". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "ccabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccaaccaab". In this case the string does not contain "abc" as a substring and no replacements are needed. | Java 11 | standard input | [
"implementation",
"strings"
] | db473ad780a93983667d12b1357c6e2f | The first line contains two integers $$$n$$$ and $$$q$$$ $$$(1 \le n, q \le 10^5)$$$, the length of the string and the number of queries, respectively. The second line contains the string $$$s$$$, consisting of characters "a", "b" and "c". Each of the next $$$q$$$ lines contains an integer $$$i$$$ and character $$$c$$$ $$$(1 \le i \le n)$$$, index and the value of the new item in the string, respectively. It is guaranteed that character's $$$c$$$ value is "a", "b" or "c". | 1,100 | For each query output the minimal number of characters that would have to be replaced so that the string doesn't contain "abc" as a substring. | standard output | |
PASSED | 72de052c23dc6be2b03a2a93c393aaa4 | train_110.jsonl | 1638110100 | Before becoming a successful trader William got a university degree. During his education an interesting situation happened, after which William started to listen to homework assignments much more attentively. What follows is the correct formal description of the homework assignment:You are given a string $$$s$$$ of length $$$n$$$ only consisting of characters "a", "b" and "c". There are $$$q$$$ queries of format ($$$pos, c$$$), meaning replacing the element of string $$$s$$$ at position $$$pos$$$ with character $$$c$$$. After each query you must output the minimal number of characters in the string, which have to be replaced, so that the string doesn't contain string "abc" as a substring. A valid replacement of a character is replacing it with "a", "b" or "c".A string $$$x$$$ is a substring of a string $$$y$$$ if $$$x$$$ can be obtained from $$$y$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. | 256 megabytes | import java.util.*; import java.io.*; import java.math.*;
public class Main{
//見なくていいよ ここから------------------------------------------
static class InputIterator{
ArrayList<String> inputLine = new ArrayList<>(buf);
int index = 0; int max; String read;
InputIterator(){
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try{
while((read = br.readLine()) != null){
inputLine.addAll(Arrays.asList(read.split(" ")));
}
}catch(IOException e){}
max = inputLine.size();
}
boolean hasNext(){return (index < max);}
String next(){
if(hasNext()){
return inputLine.get(index++);
}else{
throw new IndexOutOfBoundsException("There is no more input");
}
}
}
//入力バッファサイズのつもり。入力点数(スペース区切りでの要素数)が100万を超える場合はバッファサイズを100万にすること
static int buf = 1024;
static HashMap<Integer, String> CONVSTR = new HashMap<>();
static InputIterator ii = new InputIterator();//This class cannot be used in reactive problem.
static PrintWriter out = new PrintWriter(System.out);
static void flush(){out.flush();}
static void myout(Object t){out.println(t);}
static void myerr(Object t){System.err.print("debug:");System.err.println(t);}
static String next(){return ii.next();}
static boolean hasNext(){return ii.hasNext();}
static int nextInt(){return Integer.parseInt(next());}
static long nextLong(){return Long.parseLong(next());}
static double nextDouble(){return Double.parseDouble(next());}
static ArrayList<String> nextCharArray(){return myconv(next(), 0);}
static ArrayList<String> nextStrArray(int size){
ArrayList<String> ret = new ArrayList<>(size);
for(int i = 0; i < size; i++){
ret.add(next());
}
return ret;
}
static ArrayList<Integer> nextIntArray(int size){
ArrayList<Integer> ret = new ArrayList<>(size);
for(int i = 0; i < size; i++){
ret.add(Integer.parseInt(next()));
}
return ret;
}
static ArrayList<Long> nextLongArray(int size){
ArrayList<Long> ret = new ArrayList<>(size);
for(int i = 0; i < size; i++){
ret.add(Long.parseLong(next()));
}
return ret;
}
@SuppressWarnings("unchecked")
static String myconv(Object list, int no){//only join
StringBuilder sb = new StringBuilder("");
String joinString = CONVSTR.get(no);
if(list instanceof String[]){
return String.join(joinString, (String[])list);
}else if(list instanceof long[]){
long[] tmp = (long[])list;
if(tmp.length == 0){
return "";
}
sb.append(String.valueOf(tmp[0]));
for(int i = 1; i < tmp.length; i++){
sb.append(joinString).append(String.valueOf(tmp[i]));
}
return sb.toString();
}else if(list instanceof int[]){
int[] tmp = (int[])list;
if(tmp.length == 0){
return "";
}
sb.append(String.valueOf(tmp[0]));
for(int i = 1; i < tmp.length; i++){
sb.append(joinString).append(String.valueOf(tmp[i]));
}
return sb.toString();
}else if(list instanceof ArrayList){
ArrayList tmp = (ArrayList)list;
if(tmp.size() == 0){
return "";
}
sb.append(tmp.get(0));
for(int i = 1; i < tmp.size(); i++){
sb.append(joinString).append(tmp.get(i));
}
return sb.toString();
}else{
throw new ClassCastException("Don't join");
}
}
static ArrayList<String> myconv(String str, int no){//only split
String splitString = CONVSTR.get(no);
return new ArrayList<String>(Arrays.asList(str.split(splitString)));
}
static ArrayList<String> myconv(String str, String no){
return new ArrayList<String>(Arrays.asList(str.split(no)));
}
public static void main(String[] args){
CONVSTR.put(8, " "); CONVSTR.put(9, "\n"); CONVSTR.put(0, "");
solve();flush();
}
//見なくていいよ ここまで------------------------------------------
//このコードをコンパイルするときは、「-encoding UTF-8」を指定すること
static void solve(){//ここがメイン関数
int N = nextInt();
int Q = nextInt();
String[] s = new String[N];
String v = next();
for(int i = 0; i < N; i++){
s[i] = v.substring(i, i + 1);
}
int now = 0;
for(int i = 0; i < N - 2; i++){
if(s[i].equals("a") && s[i + 1].equals("b") && s[i + 2].equals("c")){
now++;
}
}
for(int i = 0; i < Q; i++){
int index = nextInt() - 1;
String g = next();
myerr(index + " " + g);
if(!s[index].equals(g)){
if(3 <= N){
if(index <= N - 3){
if(s[index].equals("a") && s[index + 1].equals("b") && s[index + 2].equals("c")){
now--;
}
}
if(1 <= index && index < N - 1){
if(s[index - 1].equals("a") && s[index].equals("b") && s[index + 1].equals("c")){
now--;
}
}
if(2 <= index){
if(s[index - 2].equals("a") && s[index - 1].equals("b") && s[index].equals("c")){
now--;
}
}
}
s[index] = g;
if(3 <= N){
if(index <= N - 3){
if(s[index].equals("a") && s[index + 1].equals("b") && s[index + 2].equals("c")){
now++;
}
}
if(1 <= index && index < N - 1){
if(s[index - 1].equals("a") && s[index].equals("b") && s[index + 1].equals("c")){
now++;
}
}
if(2 <= index){
if(s[index - 2].equals("a") && s[index - 1].equals("b") && s[index].equals("c")){
now++;
}
}
}
}
myout(now);
}
}
//メソッド追加エリア ここから
//メソッド追加エリア ここまで
}
| Java | ["9 10\nabcabcabc\n1 a\n1 b\n2 c\n3 a\n4 b\n5 c\n8 a\n9 b\n1 c\n4 a"] | 2 seconds | ["3\n2\n2\n2\n1\n2\n1\n1\n1\n0"] | NoteLet's consider the state of the string after each query: $$$s =$$$ "abcabcabc". In this case $$$3$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcaccabb". This string does not contain "abc" as a substring. $$$s =$$$ "bbcabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bccabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bccbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcaabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcabbcabc". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaac". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaac". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "ccabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccaaccaab". In this case the string does not contain "abc" as a substring and no replacements are needed. | Java 11 | standard input | [
"implementation",
"strings"
] | db473ad780a93983667d12b1357c6e2f | The first line contains two integers $$$n$$$ and $$$q$$$ $$$(1 \le n, q \le 10^5)$$$, the length of the string and the number of queries, respectively. The second line contains the string $$$s$$$, consisting of characters "a", "b" and "c". Each of the next $$$q$$$ lines contains an integer $$$i$$$ and character $$$c$$$ $$$(1 \le i \le n)$$$, index and the value of the new item in the string, respectively. It is guaranteed that character's $$$c$$$ value is "a", "b" or "c". | 1,100 | For each query output the minimal number of characters that would have to be replaced so that the string doesn't contain "abc" as a substring. | standard output | |
PASSED | d378b8c99268f2deddcf8a7aa9b2d3e1 | train_110.jsonl | 1638110100 | Before becoming a successful trader William got a university degree. During his education an interesting situation happened, after which William started to listen to homework assignments much more attentively. What follows is the correct formal description of the homework assignment:You are given a string $$$s$$$ of length $$$n$$$ only consisting of characters "a", "b" and "c". There are $$$q$$$ queries of format ($$$pos, c$$$), meaning replacing the element of string $$$s$$$ at position $$$pos$$$ with character $$$c$$$. After each query you must output the minimal number of characters in the string, which have to be replaced, so that the string doesn't contain string "abc" as a substring. A valid replacement of a character is replacing it with "a", "b" or "c".A string $$$x$$$ is a substring of a string $$$y$$$ if $$$x$$$ can be obtained from $$$y$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. | 256 megabytes | import java.util.*; import java.io.*; import java.math.*;
public class Main{
//見なくていいよ ここから------------------------------------------
static class InputIterator{
ArrayList<String> inputLine = new ArrayList<>(buf);
int index = 0; int max; String read;
InputIterator(){
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try{
while((read = br.readLine()) != null){
inputLine.addAll(Arrays.asList(read.split(" ")));
}
}catch(IOException e){}
max = inputLine.size();
}
boolean hasNext(){return (index < max);}
String next(){
if(hasNext()){
return inputLine.get(index++);
}else{
throw new IndexOutOfBoundsException("There is no more input");
}
}
}
//入力バッファサイズのつもり。入力点数(スペース区切りでの要素数)が100万を超える場合はバッファサイズを100万にすること
static int buf = 1024;
static HashMap<Integer, String> CONVSTR = new HashMap<>();
static InputIterator ii = new InputIterator();//This class cannot be used in reactive problem.
static PrintWriter out = new PrintWriter(System.out);
static void flush(){out.flush();}
static void myout(Object t){out.println(t);}
static void myerr(Object t){System.err.print("debug:");System.err.println(t);}
static String next(){return ii.next();}
static boolean hasNext(){return ii.hasNext();}
static int nextInt(){return Integer.parseInt(next());}
static long nextLong(){return Long.parseLong(next());}
static double nextDouble(){return Double.parseDouble(next());}
static ArrayList<String> nextCharArray(){return myconv(next(), 0);}
static ArrayList<String> nextStrArray(int size){
ArrayList<String> ret = new ArrayList<>(size);
for(int i = 0; i < size; i++){
ret.add(next());
}
return ret;
}
static ArrayList<Integer> nextIntArray(int size){
ArrayList<Integer> ret = new ArrayList<>(size);
for(int i = 0; i < size; i++){
ret.add(Integer.parseInt(next()));
}
return ret;
}
static ArrayList<Long> nextLongArray(int size){
ArrayList<Long> ret = new ArrayList<>(size);
for(int i = 0; i < size; i++){
ret.add(Long.parseLong(next()));
}
return ret;
}
@SuppressWarnings("unchecked")
static String myconv(Object list, int no){//only join
StringBuilder sb = new StringBuilder("");
String joinString = CONVSTR.get(no);
if(list instanceof String[]){
return String.join(joinString, (String[])list);
}else if(list instanceof long[]){
long[] tmp = (long[])list;
if(tmp.length == 0){
return "";
}
sb.append(String.valueOf(tmp[0]));
for(int i = 1; i < tmp.length; i++){
sb.append(joinString).append(String.valueOf(tmp[i]));
}
return sb.toString();
}else if(list instanceof int[]){
int[] tmp = (int[])list;
if(tmp.length == 0){
return "";
}
sb.append(String.valueOf(tmp[0]));
for(int i = 1; i < tmp.length; i++){
sb.append(joinString).append(String.valueOf(tmp[i]));
}
return sb.toString();
}else if(list instanceof ArrayList){
ArrayList tmp = (ArrayList)list;
if(tmp.size() == 0){
return "";
}
sb.append(tmp.get(0));
for(int i = 1; i < tmp.size(); i++){
sb.append(joinString).append(tmp.get(i));
}
return sb.toString();
}else{
throw new ClassCastException("Don't join");
}
}
static ArrayList<String> myconv(String str, int no){//only split
String splitString = CONVSTR.get(no);
return new ArrayList<String>(Arrays.asList(str.split(splitString)));
}
static ArrayList<String> myconv(String str, String no){
return new ArrayList<String>(Arrays.asList(str.split(no)));
}
public static void main(String[] args){
CONVSTR.put(8, " "); CONVSTR.put(9, "\n"); CONVSTR.put(0, "");
solve();flush();
}
//見なくていいよ ここまで------------------------------------------
//このコードをコンパイルするときは、「-encoding UTF-8」を指定すること
static void solve(){//ここがメイン関数
int N = nextInt();
int Q = nextInt();
String[] s = new String[N];
String v = next();
for(int i = 0; i < N; i++){
s[i] = v.substring(i, i + 1);
}
int now = 0;
for(int i = 0; i < N - 2; i++){
if(s[i].equals("a") && s[i + 1].equals("b") && s[i + 2].equals("c")){
now++;
}
}
for(int i = 0; i < Q; i++){
int index = nextInt() - 1;
String g = next();
//myerr(index + " " + g);
if(!s[index].equals(g)){
if(3 <= N){
if(index <= N - 3){
if(s[index].equals("a") && s[index + 1].equals("b") && s[index + 2].equals("c")){
now--;
}
}
if(1 <= index && index < N - 1){
if(s[index - 1].equals("a") && s[index].equals("b") && s[index + 1].equals("c")){
now--;
}
}
if(2 <= index){
if(s[index - 2].equals("a") && s[index - 1].equals("b") && s[index].equals("c")){
now--;
}
}
}
s[index] = g;
if(3 <= N){
if(index <= N - 3){
if(s[index].equals("a") && s[index + 1].equals("b") && s[index + 2].equals("c")){
now++;
}
}
if(1 <= index && index < N - 1){
if(s[index - 1].equals("a") && s[index].equals("b") && s[index + 1].equals("c")){
now++;
}
}
if(2 <= index){
if(s[index - 2].equals("a") && s[index - 1].equals("b") && s[index].equals("c")){
now++;
}
}
}
}
myout(now);
}
}
//メソッド追加エリア ここから
//メソッド追加エリア ここまで
}
| Java | ["9 10\nabcabcabc\n1 a\n1 b\n2 c\n3 a\n4 b\n5 c\n8 a\n9 b\n1 c\n4 a"] | 2 seconds | ["3\n2\n2\n2\n1\n2\n1\n1\n1\n0"] | NoteLet's consider the state of the string after each query: $$$s =$$$ "abcabcabc". In this case $$$3$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcaccabb". This string does not contain "abc" as a substring. $$$s =$$$ "bbcabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bccabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bccbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcaabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcabbcabc". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaac". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaac". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "ccabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccaaccaab". In this case the string does not contain "abc" as a substring and no replacements are needed. | Java 11 | standard input | [
"implementation",
"strings"
] | db473ad780a93983667d12b1357c6e2f | The first line contains two integers $$$n$$$ and $$$q$$$ $$$(1 \le n, q \le 10^5)$$$, the length of the string and the number of queries, respectively. The second line contains the string $$$s$$$, consisting of characters "a", "b" and "c". Each of the next $$$q$$$ lines contains an integer $$$i$$$ and character $$$c$$$ $$$(1 \le i \le n)$$$, index and the value of the new item in the string, respectively. It is guaranteed that character's $$$c$$$ value is "a", "b" or "c". | 1,100 | For each query output the minimal number of characters that would have to be replaced so that the string doesn't contain "abc" as a substring. | standard output | |
PASSED | 7495222642a615debde2b2c8a3225291 | train_110.jsonl | 1638110100 | Before becoming a successful trader William got a university degree. During his education an interesting situation happened, after which William started to listen to homework assignments much more attentively. What follows is the correct formal description of the homework assignment:You are given a string $$$s$$$ of length $$$n$$$ only consisting of characters "a", "b" and "c". There are $$$q$$$ queries of format ($$$pos, c$$$), meaning replacing the element of string $$$s$$$ at position $$$pos$$$ with character $$$c$$$. After each query you must output the minimal number of characters in the string, which have to be replaced, so that the string doesn't contain string "abc" as a substring. A valid replacement of a character is replacing it with "a", "b" or "c".A string $$$x$$$ is a substring of a string $$$y$$$ if $$$x$$$ can be obtained from $$$y$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main
{
static class InputReader {
BufferedReader reader;
StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
String next() { // reads in the next string
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() { // reads in the next int
return Integer.parseInt(next());
}
public long nextLong() { // reads in the next long
return Long.parseLong(next());
}
public double nextDouble() { // reads in the next double
return Double.parseDouble(next());
}
}
static InputReader r = new InputReader(System.in);
static PrintWriter pw = new PrintWriter(System.out);
public static void main(String[] args)
{
int n = r.nextInt();
int q = r.nextInt();
String s = r.next();
int[] a = new int[n];
int[] good = new int[n];
int num = 0;
for (int i = 0; i < n; i ++)
{
a[i] = (int) s.charAt(i) - (int) 'a';
if (i-2 >= 0 && ok(a,i-2) == 1)
{
num ++;
}
}
for (int in = 0; in < q; in ++)
{
int i = r.nextInt()-1;
String s1 = r.next();
char c1 = s1.charAt(0);
int c = (int) c1 - (int) 'a';
if (a[i] == c)
{
pw.println(num);
continue;
}
for (int x = 0; x < 3; x ++)
{
if (i-x >= 0 && i-x+2 < n)
{
num-=ok(a,i-x);
}
}
a[i] = c;
for (int x = 0; x < 3; x ++)
{
if (i - x >= 0 && i-x+2 < n)
{
num+=ok(a,i-x);
}
}
pw.println(num);
}
pw.close();
}
static int ok(int[] a, int i)
{
return ((a[i] == 0 && a[i+1] == 1 && a[i+2] == 2)?1:0);
}
} | Java | ["9 10\nabcabcabc\n1 a\n1 b\n2 c\n3 a\n4 b\n5 c\n8 a\n9 b\n1 c\n4 a"] | 2 seconds | ["3\n2\n2\n2\n1\n2\n1\n1\n1\n0"] | NoteLet's consider the state of the string after each query: $$$s =$$$ "abcabcabc". In this case $$$3$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcaccabb". This string does not contain "abc" as a substring. $$$s =$$$ "bbcabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bccabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bccbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcaabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcabbcabc". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaac". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaac". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "ccabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccaaccaab". In this case the string does not contain "abc" as a substring and no replacements are needed. | Java 11 | standard input | [
"implementation",
"strings"
] | db473ad780a93983667d12b1357c6e2f | The first line contains two integers $$$n$$$ and $$$q$$$ $$$(1 \le n, q \le 10^5)$$$, the length of the string and the number of queries, respectively. The second line contains the string $$$s$$$, consisting of characters "a", "b" and "c". Each of the next $$$q$$$ lines contains an integer $$$i$$$ and character $$$c$$$ $$$(1 \le i \le n)$$$, index and the value of the new item in the string, respectively. It is guaranteed that character's $$$c$$$ value is "a", "b" or "c". | 1,100 | For each query output the minimal number of characters that would have to be replaced so that the string doesn't contain "abc" as a substring. | standard output | |
PASSED | f0a608337faf886a3766bc4b83dc3d5b | train_110.jsonl | 1638110100 | Before becoming a successful trader William got a university degree. During his education an interesting situation happened, after which William started to listen to homework assignments much more attentively. What follows is the correct formal description of the homework assignment:You are given a string $$$s$$$ of length $$$n$$$ only consisting of characters "a", "b" and "c". There are $$$q$$$ queries of format ($$$pos, c$$$), meaning replacing the element of string $$$s$$$ at position $$$pos$$$ with character $$$c$$$. After each query you must output the minimal number of characters in the string, which have to be replaced, so that the string doesn't contain string "abc" as a substring. A valid replacement of a character is replacing it with "a", "b" or "c".A string $$$x$$$ is a substring of a string $$$y$$$ if $$$x$$$ can be obtained from $$$y$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. | 256 megabytes | import java.util.*;
import java.io.*;
public class Main{
public static void main (String[] args){
Scanner in=new Scanner(System.in);
int n=in.nextInt(),q=in.nextInt(),i,r=0;
char s[]=in.next().toCharArray();
char c;
for(int j=0;j<n-2;j++)
if(s[j]=='a' && s[j+1]=='b' && s[j+2]=='c')
r++;
while(q--!=0){
i=in.nextInt();c=in.next().charAt(0);
i--;
if(c=='a'){
if(i-1>=0 && i+1<n && s[i-1]=='a' && s[i]=='b' && s[i+1]=='c') r--;
else if(i-2>=0 && s[i-2]=='a' && s[i-1]=='b' && s[i]=='c') r--;
if(i+2<n && s[i+1]=='b' && s[i+2]=='c' && s[i]!='a') r++;
}else if(c=='b'){
if(i+2<n && s[i]=='a' && s[i+1]=='b' && s[i+2]=='c') r--;
else if(i-2>=0 && s[i-2]=='a' && s[i-1]=='b' && s[i]=='c') r--;
if(i-1>=0 && i+1<n && s[i-1]=='a' && s[i+1]=='c' && s[i]!='b') r++;
}else if(c=='c'){
if(i+2<n && s[i]=='a' && s[i+1]=='b' && s[i+2]=='c') r--;
else if(i-1>=0 && i+1<n && s[i-1]=='a' && s[i]=='b' && s[i+1]=='c') r--;
if(i-2>=0 && s[i-2]=='a' && s[i-1]=='b' && s[i]!='c') r++;
}
s[i]=c;
System.out.println(r);
}
}
} | Java | ["9 10\nabcabcabc\n1 a\n1 b\n2 c\n3 a\n4 b\n5 c\n8 a\n9 b\n1 c\n4 a"] | 2 seconds | ["3\n2\n2\n2\n1\n2\n1\n1\n1\n0"] | NoteLet's consider the state of the string after each query: $$$s =$$$ "abcabcabc". In this case $$$3$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcaccabb". This string does not contain "abc" as a substring. $$$s =$$$ "bbcabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bccabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bccbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcaabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcabbcabc". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaac". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaac". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "ccabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccaaccaab". In this case the string does not contain "abc" as a substring and no replacements are needed. | Java 11 | standard input | [
"implementation",
"strings"
] | db473ad780a93983667d12b1357c6e2f | The first line contains two integers $$$n$$$ and $$$q$$$ $$$(1 \le n, q \le 10^5)$$$, the length of the string and the number of queries, respectively. The second line contains the string $$$s$$$, consisting of characters "a", "b" and "c". Each of the next $$$q$$$ lines contains an integer $$$i$$$ and character $$$c$$$ $$$(1 \le i \le n)$$$, index and the value of the new item in the string, respectively. It is guaranteed that character's $$$c$$$ value is "a", "b" or "c". | 1,100 | For each query output the minimal number of characters that would have to be replaced so that the string doesn't contain "abc" as a substring. | standard output | |
PASSED | 0219bc19c90c131a445ea356986f0b87 | train_110.jsonl | 1638110100 | Before becoming a successful trader William got a university degree. During his education an interesting situation happened, after which William started to listen to homework assignments much more attentively. What follows is the correct formal description of the homework assignment:You are given a string $$$s$$$ of length $$$n$$$ only consisting of characters "a", "b" and "c". There are $$$q$$$ queries of format ($$$pos, c$$$), meaning replacing the element of string $$$s$$$ at position $$$pos$$$ with character $$$c$$$. After each query you must output the minimal number of characters in the string, which have to be replaced, so that the string doesn't contain string "abc" as a substring. A valid replacement of a character is replacing it with "a", "b" or "c".A string $$$x$$$ is a substring of a string $$$y$$$ if $$$x$$$ can be obtained from $$$y$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. | 256 megabytes | import java.util.Scanner;
public class ProblemB {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int stringLine = scanner.nextInt(), queriesCount = scanner.nextInt();
scanner.nextLine();
char[] str = scanner.nextLine().toCharArray();
int count = getSubStringCount(str);
for (int i=0; i<queriesCount; i++) {
int index = scanner.nextInt();
char newSymbol = scanner.next().charAt(0);
if (newSymbol != str[index-1]) {
if (isInsideABC(str, index-1)) {
count--;
}
str[index - 1] = newSymbol;
if (isInsideABC(str, index-1)) {
count++;
}
}
System.out.println(count);
}
}
public static int getSubStringCount(char[] s) {
int count = 0;
for (int i = 0; i < s.length-2;) {
if (s[i] == 'a' && s[i+1] == 'b' && s[i+2] == 'c') {
count++;
i+=3;
} else {
i++;
}
}
return count;
}
public static boolean isInsideABC(char[] s, int index) {
if ((index>1 && s[index-2]=='a' && s[index-1]=='b' && s[index] == 'c') ||
(index>0 && index<s.length-1 && s[index-1]=='a' && s[index]=='b' && s[index+1] == 'c') ||
(index<s.length-2 && s[index]=='a' && s[index+1]=='b' && s[index+2]=='c')) {
return true;
}
return false;
}
}
| Java | ["9 10\nabcabcabc\n1 a\n1 b\n2 c\n3 a\n4 b\n5 c\n8 a\n9 b\n1 c\n4 a"] | 2 seconds | ["3\n2\n2\n2\n1\n2\n1\n1\n1\n0"] | NoteLet's consider the state of the string after each query: $$$s =$$$ "abcabcabc". In this case $$$3$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcaccabb". This string does not contain "abc" as a substring. $$$s =$$$ "bbcabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bccabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bccbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcaabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcabbcabc". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaac". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaac". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "ccabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccaaccaab". In this case the string does not contain "abc" as a substring and no replacements are needed. | Java 11 | standard input | [
"implementation",
"strings"
] | db473ad780a93983667d12b1357c6e2f | The first line contains two integers $$$n$$$ and $$$q$$$ $$$(1 \le n, q \le 10^5)$$$, the length of the string and the number of queries, respectively. The second line contains the string $$$s$$$, consisting of characters "a", "b" and "c". Each of the next $$$q$$$ lines contains an integer $$$i$$$ and character $$$c$$$ $$$(1 \le i \le n)$$$, index and the value of the new item in the string, respectively. It is guaranteed that character's $$$c$$$ value is "a", "b" or "c". | 1,100 | For each query output the minimal number of characters that would have to be replaced so that the string doesn't contain "abc" as a substring. | standard output | |
PASSED | 1be13f005d997193c82905c8b5f1103f | train_110.jsonl | 1638110100 | Before becoming a successful trader William got a university degree. During his education an interesting situation happened, after which William started to listen to homework assignments much more attentively. What follows is the correct formal description of the homework assignment:You are given a string $$$s$$$ of length $$$n$$$ only consisting of characters "a", "b" and "c". There are $$$q$$$ queries of format ($$$pos, c$$$), meaning replacing the element of string $$$s$$$ at position $$$pos$$$ with character $$$c$$$. After each query you must output the minimal number of characters in the string, which have to be replaced, so that the string doesn't contain string "abc" as a substring. A valid replacement of a character is replacing it with "a", "b" or "c".A string $$$x$$$ is a substring of a string $$$y$$$ if $$$x$$$ can be obtained from $$$y$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. | 256 megabytes | import java.util.*;
import java.io.*;
import java.math.*;
public class Main {
private static boolean arr[] = sieve(1000001);
private static ArrayList<Long> primes = new ArrayList<>();
private static int freq[] = new int[200005];
private static int mod = (int) (1e9 + 7);
private static final int IMAX = 2147483647;
private static final int IMIN = -2147483648;
private static final long LMAX = 9223372036854775807L;
private static final long LMIN = -9223372036854775808L;
private static FastScanner c;
private static PrintWriter pw;
// **********************************Code Begins From
// Here***************************************
// DecimalFormat df = new DecimalFormat("#.000000000000");
// map.put(key,map.getOrDefault(key,0)+1);
static char[] a;
public static void solve() {
int n = c.i(), q = c.i(), ans = 0;
a = c.next().toCharArray();
for (int i = 0; i <= n; i++)
ans += is(i) ? 1 : 0;
for (int Q = 0; Q < q; Q++) {
int x = c.i() - 1;
char ch = c.next().charAt(0);
if (is(x - 2))
ans--;
if (is(x - 1))
ans--;
if (is(x))
ans--;
a[x] = ch;
if (is(x - 2))
ans++;
if (is(x - 1))
ans++;
if (is(x))
ans++;
pn(ans);
}
}
static boolean is(int i) {
if (i >= 0 && i + 2 < a.length) {
if (a[i] == 'a' && a[i + 1] == 'b' && a[i + 2] == 'c')
return true;
}
return false;
}
public static void main(String[] args) throws FileNotFoundException {
// Scanner sc = new Scanner(System.in);
c = new FastScanner();
pw = new PrintWriter(System.out);
int tc = 1;
// tc = c.i();
long start = System.currentTimeMillis();
for (int t = 0; t < tc; t++) {
// p("Case #" + (t + 1) + ": ");
solve();
}
long end = System.currentTimeMillis();
if (System.getProperty("os.name").equals("Mac OS X")) {
pn("The Program takes " + (end - start) + "ms");
}
// for (long i = 0; i < arr.length; i++) {
// if (arr[(int) i]) {
// primes.add(i);
// }
// }
pw.close();
}
// ArrayList<Integer> al = new ArrayList<>();
// Set<Integer> set = new TreeSet<>();
// Pair<Integer, Integer> pair;
// Map<Integer, Integer> map = new HashMap<>();
// for(Map.Entry<Integer,Integer> e:map.entrySet())pn(e.getKey()+"
// "+e.getValue());
// LinkedList<Integer> ll = new LinkedList<>();
// math util
private static int cei(double d) {
return (int) Math.ceil(d);
}
private static int gcd(int a, int b) {
return b == 0 ? a : gcd(b, a % b);
}
private static long gcd(long a, long b) {
return b == 0 ? a : gcd(b, a % b);
}
private static int abs(int a, int b) {
return (int) Math.abs(a - b);
}
private static int abs(int a) {
return (int) Math.abs(a);
}
private static long abs(long a) {
return (long) Math.abs(a);
}
private static long abs(long a, long b) {
return (long) Math.abs(a - b);
}
private static int max(int a, int b) {
if (a > b) {
return a;
} else {
return b;
}
}
private static int min(int a, int b) {
if (a > b) {
return b;
} else {
return a;
}
}
private static long max(long a, long b) {
if (a > b) {
return a;
} else {
return b;
}
}
private static long min(long a, long b) {
if (a > b) {
return b;
} else {
return a;
}
}
private static int pow(int base, int exp) {
int result = 1;
while (exp != 0) {
if ((exp & 1) == 1)
result *= base;
exp >>= 1;
base *= base;
}
return result;
}
// array util
private static void sortList(ArrayList<Integer> al) {
Collections.sort(al);
}
private static void sort(int[] a) {
Arrays.parallelSort(a);
}
private static void sort(long[] a) {
Arrays.parallelSort(a);
}
private static int[] copy(int[] a) {
int[] ans = new int[a.length];
for (int i = 0; i < a.length; ++i)
ans[i] = a[i];
return ans;
}
private static char[] copy(char[] a) {
char[] ans = new char[a.length];
for (int i = 0; i < a.length; ++i)
ans[i] = a[i];
return ans;
}
private static long[] copy(long[] a) {
long[] ans = new long[a.length];
for (int i = 0; i < a.length; ++i)
ans[i] = a[i];
return ans;
}
// output
private static void pn(Object o) {
pw.println(o);
}
private static void p(Object o) {
pw.print(o);
}
private static void pry() {
pn("Yes");
}
private static void pY() {
pn("YES");
}
private static void prn() {
pn("No");
}
private static void pN() {
pn("NO");
}
private static void flush() {
pw.flush();
}
private static void watch(int[] a) {
for (int e : a)
p(e + " ");
pn("");
}
private static void watchList(ArrayList<Integer> al) {
for (int e : al)
p(e + " ");
pn("");
}
private static Set<Integer> putSet(int[] a) {
Set<Integer> set = new TreeSet<>();
for (int e : a)
set.add(e);
return set;
}
private static boolean[] sieve(int n) {
boolean[] isPrime = new boolean[n + 1];
Arrays.fill(isPrime, true);
isPrime[0] = false;
isPrime[1] = false;
for (int i = 2; i * i <= n; i++) {
for (int j = i * i; j <= n; j += i) {
isPrime[j] = false;
}
}
return isPrime;
}
private static class Pair<U, V> implements Comparable<Pair<U, V>> {
public final U first;
public V second;
public <U, V> Pair<U, V> makePair(U first, V second) {
return new Pair<U, V>(first, second);
}
public Pair(U first, V second) {
this.first = first;
this.second = second;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Pair pair = (Pair) o;
return !(first != null ? !first.equals(pair.first) : pair.first != null)
&& !(second != null ? !second.equals(pair.second) : pair.second != null);
}
@Override
public int hashCode() {
int result = first != null ? first.hashCode() : 0;
result = 31 * result + (second != null ? second.hashCode() : 0);
return result;
}
public Pair<V, U> swap() {
return makePair(second, first);
}
@Override
public String toString() {
return "(" + first + "," + second + ")";
}
@SuppressWarnings({ "unchecked" })
public int compareTo(Pair<U, V> o) {
int value = ((Comparable<U>) first).compareTo(o.first);
if (value != 0) {
return value;
}
return ((Comparable<V>) second).compareTo(o.second);
}
}
private static class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner() throws FileNotFoundException {
if (System.getProperty("os.name").equals("Mac OS X")) {
// Input is a file
br = new BufferedReader(new FileReader("input.txt"));
// PrintWriter class prints formatted representations
// of objects to a text-output stream.
PrintStream pw = new PrintStream(new FileOutputStream("output.txt"));
System.setOut(pw);
} else {
// Input is System.in
br = new BufferedReader(new InputStreamReader(System.in), 32768);
st = null;
}
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int i() {
return Integer.parseInt(next());
}
int[] intArray(int n) {
int[] ret = new int[n];
for (int i = 0; i < n; i++)
ret[i] = i();
return ret;
}
long l() {
return Long.parseLong(next());
}
long[] longArray(int n) {
long[] ret = new long[n];
for (int i = 0; i < n; i++)
ret[i] = l();
return ret;
}
double d() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
} | Java | ["9 10\nabcabcabc\n1 a\n1 b\n2 c\n3 a\n4 b\n5 c\n8 a\n9 b\n1 c\n4 a"] | 2 seconds | ["3\n2\n2\n2\n1\n2\n1\n1\n1\n0"] | NoteLet's consider the state of the string after each query: $$$s =$$$ "abcabcabc". In this case $$$3$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcaccabb". This string does not contain "abc" as a substring. $$$s =$$$ "bbcabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bccabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bccbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcaabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcabbcabc". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaac". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaac". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "ccabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccaaccaab". In this case the string does not contain "abc" as a substring and no replacements are needed. | Java 11 | standard input | [
"implementation",
"strings"
] | db473ad780a93983667d12b1357c6e2f | The first line contains two integers $$$n$$$ and $$$q$$$ $$$(1 \le n, q \le 10^5)$$$, the length of the string and the number of queries, respectively. The second line contains the string $$$s$$$, consisting of characters "a", "b" and "c". Each of the next $$$q$$$ lines contains an integer $$$i$$$ and character $$$c$$$ $$$(1 \le i \le n)$$$, index and the value of the new item in the string, respectively. It is guaranteed that character's $$$c$$$ value is "a", "b" or "c". | 1,100 | For each query output the minimal number of characters that would have to be replaced so that the string doesn't contain "abc" as a substring. | standard output | |
PASSED | bde75a57d0f1a55a32f3b87e21d5c47f | train_110.jsonl | 1638110100 | Before becoming a successful trader William got a university degree. During his education an interesting situation happened, after which William started to listen to homework assignments much more attentively. What follows is the correct formal description of the homework assignment:You are given a string $$$s$$$ of length $$$n$$$ only consisting of characters "a", "b" and "c". There are $$$q$$$ queries of format ($$$pos, c$$$), meaning replacing the element of string $$$s$$$ at position $$$pos$$$ with character $$$c$$$. After each query you must output the minimal number of characters in the string, which have to be replaced, so that the string doesn't contain string "abc" as a substring. A valid replacement of a character is replacing it with "a", "b" or "c".A string $$$x$$$ is a substring of a string $$$y$$$ if $$$x$$$ can be obtained from $$$y$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. | 256 megabytes | import java.util.*;
import java.io.*;
import java.math.*;
public class Main {
private static boolean arr[] = sieve(1000001);
private static ArrayList<Long> primes = new ArrayList<>();
private static int freq[] = new int[200005];
private static int mod = (int) (1e9 + 7);
private static final int IMAX = 2147483647;
private static final int IMIN = -2147483648;
private static final long LMAX = 9223372036854775807L;
private static final long LMIN = -9223372036854775808L;
private static FastScanner c;
private static PrintWriter pw;
private static class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner() throws FileNotFoundException {
if (System.getProperty("os.name").equals("Mac OS X")) {
// Input is a file
br = new BufferedReader(new FileReader("input.txt"));
// PrintWriter class prints formatted representations
// of objects to a text-output stream.
PrintStream pw = new PrintStream(new FileOutputStream("output.txt"));
System.setOut(pw);
} else {
// Input is System.in
br = new BufferedReader(new InputStreamReader(System.in), 32768);
st = null;
}
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int i() {
return Integer.parseInt(next());
}
int[] intArray(int n) {
int[] ret = new int[n];
for (int i = 0; i < n; i++)
ret[i] = i();
return ret;
}
long l() {
return Long.parseLong(next());
}
long[] longArray(int n) {
long[] ret = new long[n];
for (int i = 0; i < n; i++)
ret[i] = l();
return ret;
}
double d() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
// **********************************Code Begins From
// Here***************************************
// DecimalFormat df = new DecimalFormat("#.000000000000");
// map.put(key,map.getOrDefault(key,0)+1);
public static void solve() {
int n = c.i(), q = c.i();
String s = c.next();
char a[] = s.toCharArray();
ArrayList<Integer> al = new ArrayList<>();
for (int i = 0; i < n - 2; i++) {
String x = a[i] + "" + a[i + 1] + "" + a[i + 2] + "";
if (x.equals("abc"))
al.add(i);
}
int m = al.size();
for (int Q = 1; Q <= q; Q++) {
int pos = c.i() - 1;
char ch = c.next().charAt(0);
if (pos - 2 >= 0) {
String next1 = a[pos - 2] + "" + a[pos - 1] + "" + a[pos],
next2 = a[pos - 2] + "" + a[pos - 1] + "" + ch;
if (next1.equals("abc") && !next2.equals("abc")) {
m--;
// pn("1");
}
}
if (pos > 0 && pos < n - 1) {
String next1 = a[pos - 1] + "" + a[pos] + "" + a[pos + 1],
next2 = a[pos - 1] + "" + ch + "" + a[pos + 1];
if (next1.equals("abc") && !next2.equals("abc")) {
m--;
// pn("2");
}
}
if (pos < n - 2) {
String next1 = a[pos] + "" + a[pos + 1] + "" + a[pos + 2],
next2 = ch + "" + a[pos + 1] + "" + a[pos + 2];
if (next1.equals("abc") && !next2.equals("abc")) {
m--;
// pn("3");
}
}
if (pos - 2 >= 0) {
String prev1 = a[pos - 2] + "" + a[pos - 1] + "" + a[pos],
prev2 = a[pos - 2] + "" + a[pos - 1] + "" + ch;
if (!prev1.equals("abc") && prev2.equals("abc")) {
m++;
// pn("4");
}
}
if (pos > 0 && pos < n - 1) {
String prev1 = a[pos - 1] + "" + a[pos] + "" + a[pos + 1],
prev2 = a[pos - 1] + "" + ch + "" + a[pos + 1];
if (!prev1.equals("abc") && prev2.equals("abc")) {
m++;
// pn("5");
}
}
if (pos < n - 2) {
String prev1 = a[pos] + "" + a[pos + 1] + "" + a[pos + 2],
prev2 = ch + "" + a[pos + 1] + "" + a[pos + 2];
if (!prev1.equals("abc") && prev2.equals("abc")) {
m++;
// pn("6");
}
}
a[pos] = ch;
// pn(Q + " " + Arrays.toString(a) + " " + m);
pn(m);
}
}
public static void main(String[] args) throws FileNotFoundException {
// Scanner sc = new Scanner(System.in);
c = new FastScanner();
pw = new PrintWriter(System.out);
int tc = 1;
// tc = c.i();
long start = System.currentTimeMillis();
for (int t = 0; t < tc; t++) {
// p("Case #" + (t + 1) + ": ");
solve();
}
long end = System.currentTimeMillis();
if (System.getProperty("os.name").equals("Mac OS X")) {
pn("The Program takes " + (end - start) + "ms");
}
// for (long i = 0; i < arr.length; i++) {
// if (arr[(int) i]) {
// primes.add(i);
// }
// }
pw.close();
}
// ArrayList<Integer> al = new ArrayList<>();
// Set<Integer> set = new TreeSet<>();
// Pair<Integer, Integer> pair;
// Map<Integer, Integer> map = new HashMap<>();
// for(Map.Entry<Integer,Integer> e:map.entrySet())pn(e.getKey()+"
// "+e.getValue());
// LinkedList<Integer> ll = new LinkedList<>();
// math util
private static int cei(double d) {
return (int) Math.ceil(d);
}
private static int gcd(int a, int b) {
return b == 0 ? a : gcd(b, a % b);
}
private static long gcd(long a, long b) {
return b == 0 ? a : gcd(b, a % b);
}
private static int abs(int a, int b) {
return (int) Math.abs(a - b);
}
private static int abs(int a) {
return (int) Math.abs(a);
}
private static long abs(long a) {
return (long) Math.abs(a);
}
private static long abs(long a, long b) {
return (long) Math.abs(a - b);
}
private static int max(int a, int b) {
if (a > b) {
return a;
} else {
return b;
}
}
private static int min(int a, int b) {
if (a > b) {
return b;
} else {
return a;
}
}
private static long max(long a, long b) {
if (a > b) {
return a;
} else {
return b;
}
}
private static long min(long a, long b) {
if (a > b) {
return b;
} else {
return a;
}
}
private static int pow(int base, int exp) {
int result = 1;
while (exp != 0) {
if ((exp & 1) == 1)
result *= base;
exp >>= 1;
base *= base;
}
return result;
}
// array util
private static void sortList(ArrayList<Integer> al) {
Collections.sort(al);
}
private static void sort(int[] a) {
Arrays.parallelSort(a);
}
private static void sort(long[] a) {
Arrays.parallelSort(a);
}
private static int[] copy(int[] a) {
int[] ans = new int[a.length];
for (int i = 0; i < a.length; ++i)
ans[i] = a[i];
return ans;
}
private static long[] copy(long[] a) {
long[] ans = new long[a.length];
for (int i = 0; i < a.length; ++i)
ans[i] = a[i];
return ans;
}
// output
private static void pn(Object o) {
pw.println(o);
}
private static void p(Object o) {
pw.print(o);
}
private static void pry() {
pn("Yes");
}
private static void pY() {
pn("YES");
}
private static void prn() {
pn("No");
}
private static void pN() {
pn("NO");
}
private static void flush() {
pw.flush();
}
private static void watch(int[] a) {
for (int e : a)
p(e + " ");
pn("");
}
private static void watchList(ArrayList<Integer> al) {
for (int e : al)
p(e + " ");
pn("");
}
private static Set<Integer> putSet(int[] a) {
Set<Integer> set = new TreeSet<>();
for (int e : a)
set.add(e);
return set;
}
private static boolean[] sieve(int n) {
boolean[] isPrime = new boolean[n + 1];
Arrays.fill(isPrime, true);
isPrime[0] = false;
isPrime[1] = false;
for (int i = 2; i * i <= n; i++) {
for (int j = i * i; j <= n; j += i) {
isPrime[j] = false;
}
}
return isPrime;
}
private static class Pair<U, V> implements Comparable<Pair<U, V>> {
public final U first;
public V second;
public <U, V> Pair<U, V> makePair(U first, V second) {
return new Pair<U, V>(first, second);
}
public Pair(U first, V second) {
this.first = first;
this.second = second;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Pair pair = (Pair) o;
return !(first != null ? !first.equals(pair.first) : pair.first != null)
&& !(second != null ? !second.equals(pair.second) : pair.second != null);
}
@Override
public int hashCode() {
int result = first != null ? first.hashCode() : 0;
result = 31 * result + (second != null ? second.hashCode() : 0);
return result;
}
public Pair<V, U> swap() {
return makePair(second, first);
}
@Override
public String toString() {
return "(" + first + "," + second + ")";
}
@SuppressWarnings({ "unchecked" })
public int compareTo(Pair<U, V> o) {
int value = ((Comparable<U>) first).compareTo(o.first);
if (value != 0) {
return value;
}
return ((Comparable<V>) second).compareTo(o.second);
}
}
} | Java | ["9 10\nabcabcabc\n1 a\n1 b\n2 c\n3 a\n4 b\n5 c\n8 a\n9 b\n1 c\n4 a"] | 2 seconds | ["3\n2\n2\n2\n1\n2\n1\n1\n1\n0"] | NoteLet's consider the state of the string after each query: $$$s =$$$ "abcabcabc". In this case $$$3$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcaccabb". This string does not contain "abc" as a substring. $$$s =$$$ "bbcabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bccabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bccbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcaabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcabbcabc". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaac". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaac". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "ccabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccaaccaab". In this case the string does not contain "abc" as a substring and no replacements are needed. | Java 11 | standard input | [
"implementation",
"strings"
] | db473ad780a93983667d12b1357c6e2f | The first line contains two integers $$$n$$$ and $$$q$$$ $$$(1 \le n, q \le 10^5)$$$, the length of the string and the number of queries, respectively. The second line contains the string $$$s$$$, consisting of characters "a", "b" and "c". Each of the next $$$q$$$ lines contains an integer $$$i$$$ and character $$$c$$$ $$$(1 \le i \le n)$$$, index and the value of the new item in the string, respectively. It is guaranteed that character's $$$c$$$ value is "a", "b" or "c". | 1,100 | For each query output the minimal number of characters that would have to be replaced so that the string doesn't contain "abc" as a substring. | standard output | |
PASSED | 44be8f52e296e69bb0109d39c33093b7 | train_110.jsonl | 1638110100 | Before becoming a successful trader William got a university degree. During his education an interesting situation happened, after which William started to listen to homework assignments much more attentively. What follows is the correct formal description of the homework assignment:You are given a string $$$s$$$ of length $$$n$$$ only consisting of characters "a", "b" and "c". There are $$$q$$$ queries of format ($$$pos, c$$$), meaning replacing the element of string $$$s$$$ at position $$$pos$$$ with character $$$c$$$. After each query you must output the minimal number of characters in the string, which have to be replaced, so that the string doesn't contain string "abc" as a substring. A valid replacement of a character is replacing it with "a", "b" or "c".A string $$$x$$$ is a substring of a string $$$y$$$ if $$$x$$$ can be obtained from $$$y$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. | 256 megabytes | import java.io.*;
import java.util.Arrays;
import java.util.StringTokenizer;
public class DeltixRound2 {
static FastScanner fs;
static FastWriter fw;
static boolean checkOnlineJudge = System.getProperty("ONLINE_JUDGE") == null;
private static final int mod = (int) (1e9 + 7);
private static final long lMax = (int) (1e15), lMin = (int) (-1e15);
public static void main(String[] args) throws IOException {
fs = new FastScanner();
fw = new FastWriter();
int t = 1;
//t = fs.nextInt();
while (t-- > 0) {
solve();
}
fw.out.close();
}
static void solve() {
int n = fs.nextInt(), q = fs.nextInt();
char[] charArr = fs.nextLine().toCharArray();
int substrings = 0;
int i = 0;
while (i < n) {
if (i + 2 < n && charArr[i] == 'a' && charArr[i + 1] == 'b' && charArr[i + 2] == 'c') {
substrings += 1;
i += 3;
continue;
}
i += 1;
}
//fw.out.println(substrings);
while (q-- > 0) {
int idx = fs.nextInt() - 1;
char replaceBy = fs.next().charAt(0);
if (charArr[idx] == replaceBy) {
fw.out.println(substrings);
continue;
}
if (isInSubstring(idx, charArr, n)) {
substrings--;
}
charArr[idx] = replaceBy;
if (isInSubstring(idx, charArr, n))
substrings++;
fw.out.println(substrings);
}
}
private static boolean isInSubstring(int idx, char[] charArr, int n) {
if (charArr[idx] == 'a') {
return (idx + 2 < n) && (charArr[idx + 1] == 'b') && (charArr[idx + 2] == 'c');
} else if (charArr[idx] == 'b') {
return (idx - 1 >= 0) && (idx + 1 < n) && (charArr[idx - 1] == 'a') && (charArr[idx + 1] == 'c');
} else
return (idx - 2 >= 0) && (charArr[idx - 2] == 'a') && (charArr[idx - 1] == 'b');
}
private static class FastScanner {
BufferedReader br;
StringTokenizer st;
FastScanner() throws IOException {
if (checkOnlineJudge)
this.br = new BufferedReader(new FileReader("src/input.txt"));
else
this.br = new BufferedReader(new InputStreamReader(System.in));
this.st = new StringTokenizer("");
}
public String next() {
while (!st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException err) {
err.printStackTrace();
}
}
return st.nextToken();
}
public String nextLine() {
if (st.hasMoreTokens()) {
return st.nextToken("").trim();
}
try {
return br.readLine().trim();
} catch (IOException err) {
err.printStackTrace();
}
return "";
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
}
private static class FastWriter {
PrintWriter out;
FastWriter() throws IOException {
if (checkOnlineJudge)
out = new PrintWriter(new FileWriter("src/output.txt"));
else
out = new PrintWriter(System.out);
}
}
}
| Java | ["9 10\nabcabcabc\n1 a\n1 b\n2 c\n3 a\n4 b\n5 c\n8 a\n9 b\n1 c\n4 a"] | 2 seconds | ["3\n2\n2\n2\n1\n2\n1\n1\n1\n0"] | NoteLet's consider the state of the string after each query: $$$s =$$$ "abcabcabc". In this case $$$3$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcaccabb". This string does not contain "abc" as a substring. $$$s =$$$ "bbcabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bccabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bccbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcaabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcabbcabc". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaac". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaac". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "ccabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccaaccaab". In this case the string does not contain "abc" as a substring and no replacements are needed. | Java 11 | standard input | [
"implementation",
"strings"
] | db473ad780a93983667d12b1357c6e2f | The first line contains two integers $$$n$$$ and $$$q$$$ $$$(1 \le n, q \le 10^5)$$$, the length of the string and the number of queries, respectively. The second line contains the string $$$s$$$, consisting of characters "a", "b" and "c". Each of the next $$$q$$$ lines contains an integer $$$i$$$ and character $$$c$$$ $$$(1 \le i \le n)$$$, index and the value of the new item in the string, respectively. It is guaranteed that character's $$$c$$$ value is "a", "b" or "c". | 1,100 | For each query output the minimal number of characters that would have to be replaced so that the string doesn't contain "abc" as a substring. | standard output | |
PASSED | 0c87dfef44e70b337552126c2c17fe68 | train_110.jsonl | 1638110100 | Before becoming a successful trader William got a university degree. During his education an interesting situation happened, after which William started to listen to homework assignments much more attentively. What follows is the correct formal description of the homework assignment:You are given a string $$$s$$$ of length $$$n$$$ only consisting of characters "a", "b" and "c". There are $$$q$$$ queries of format ($$$pos, c$$$), meaning replacing the element of string $$$s$$$ at position $$$pos$$$ with character $$$c$$$. After each query you must output the minimal number of characters in the string, which have to be replaced, so that the string doesn't contain string "abc" as a substring. A valid replacement of a character is replacing it with "a", "b" or "c".A string $$$x$$$ is a substring of a string $$$y$$$ if $$$x$$$ can be obtained from $$$y$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. | 256 megabytes | import java.util.*;
import java.io.*;
public class Main {
static long mod = 1000000007;
static long max ;
static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
public static void main(String[] args) throws IOException {
FastReader sc = new FastReader();
int t = 1;
while( t-- > 0) {
int n = sc.nextInt();
int q = sc.nextInt();
char arr[] = sc.next().toCharArray();
int ans = 0;
for( int i = 0 ; i< n ;i++) {
if( i+2 < n && arr[i] == 'a' && arr[i+1] == 'b' && arr[i+2] == 'c') {
ans++;
i+=2;
}
}
while( q-- > 0) {
int pos = sc.nextInt()-1;
char a = sc.next().charAt(0);
if( pos-2 >= 0 && arr[pos-2] == 'a' && arr[pos-1] == 'b' && arr[pos] == 'c' ) {
ans--;
}
else if( pos - 1 >= 0 && pos + 1 < n && arr[pos-1] == 'a' && arr[pos] == 'b' && arr[pos+1] == 'c') {
ans--;
}
else if( pos+2 < n && arr[pos] == 'a' && arr[pos+1] == 'b' && arr[pos+2] == 'c') {
ans--;
}
arr[pos] = a;
if( pos-2 >= 0 && arr[pos-2] == 'a' && arr[pos-1] == 'b' && arr[pos] == 'c' ) {
ans++;
}
else if( pos - 1 >= 0 && pos + 1 < n && arr[pos-1] == 'a' && arr[pos] == 'b' && arr[pos+1] == 'c') {
ans++;
}
else if( pos+2 < n && arr[pos] == 'a' && arr[pos+1] == 'b' && arr[pos+2] == 'c') {
ans++;
}
out.println(ans);
}
}
out.flush();
}
/*
* FIRST AND VERY IMP -> READ AND UNDERSTAND THE QUESTION VERY CAREFULLY.
* WARNING -> DONT CODE BULLSHIT , ALWAYS CHECK THE LOGIC ON MULTIPLE TESTCASES AND EDGECASES BEFORE.
* SECOND -> TRY TO FIND RELEVENT PATTERN SMARTLY.
* WARNING -> IF YOU THINK YOUR SOLUION IS JUST DUMB DONT SUBMIT IT BEFORE RECHECKING ON YOUR END.
*/
public static boolean ifpowof2(long n ) {
return ((n&(n-1)) == 0);
}
public static int[] nextLargerElement(int[] arr, int n) {
Stack<Integer> stack = new Stack<>();
int rtrn[] = new int[n];
rtrn[n-1] = -1;
stack.push( n-1);
for( int i = n-2 ;i >= 0 ; i--){
int temp = arr[i];
int lol = -1;
while( !stack.isEmpty() && arr[stack.peek()] <= temp){
if(arr[stack.peek()] == temp ) {
lol = stack.peek();
}
stack.pop();
}
if( stack.isEmpty()){
if( lol != -1) {
rtrn[i] = lol;
}
else {
rtrn[i] = -1;
}
}
else{
rtrn[i] = stack.peek();
}
stack.push( i);
}
return rtrn;
}
static void mysort(int[] arr) {
for(int i=0;i<arr.length;i++) {
int rand = (int) (Math.random() * arr.length);
int loc = arr[rand];
arr[rand] = arr[i];
arr[i] = loc;
}
Arrays.sort(arr);
}
static void mySort(long[] arr) {
for(int i=0;i<arr.length;i++) {
int rand = (int) (Math.random() * arr.length);
long loc = arr[rand];
arr[rand] = arr[i];
arr[i] = loc;
}
Arrays.sort(arr);
}
static long gcd(long a, long b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
static long lcm(long a, long b)
{
return (a / gcd(a, b)) * b;
}
static long rightmostsetbit(long n) {
return n&-n;
}
static long leftmostsetbit(long n)
{
long k = (long)(Math.log(n) / Math.log(2));
return 1 << k;
}
static HashMap<Long,Long> primefactor( long n){
HashMap<Long ,Long> hm = new HashMap<>();
long temp = 0;
while( n%2 == 0) {
temp++;
n/=2;
}
if( temp!= 0) {
hm.put( 2L, temp);
}
long c = (long)Math.sqrt(n);
for( long i = 3 ; i <= c ; i+=2) {
temp = 0;
while( n% i == 0) {
temp++;
n/=i;
}
if( temp!= 0) {
hm.put( i, temp);
}
}
if( n!= 1) {
hm.put( n , 1L);
}
return hm;
}
static ArrayList<Integer> allfactors(int abs) {
HashMap<Integer,Integer> hm = new HashMap<>();
ArrayList<Integer> rtrn = new ArrayList<>();
for( int i = 2 ;i*i <= abs; i++) {
if( abs% i == 0) {
hm.put( i , 0);
hm.put(abs/i, 0);
}
}
for( int x : hm.keySet()) {
rtrn.add(x);
}
if( abs != 0) {
rtrn.add(abs);
}
return rtrn;
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
| Java | ["9 10\nabcabcabc\n1 a\n1 b\n2 c\n3 a\n4 b\n5 c\n8 a\n9 b\n1 c\n4 a"] | 2 seconds | ["3\n2\n2\n2\n1\n2\n1\n1\n1\n0"] | NoteLet's consider the state of the string after each query: $$$s =$$$ "abcabcabc". In this case $$$3$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcaccabb". This string does not contain "abc" as a substring. $$$s =$$$ "bbcabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bccabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bccbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcaabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcabbcabc". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaac". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaac". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "ccabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccaaccaab". In this case the string does not contain "abc" as a substring and no replacements are needed. | Java 11 | standard input | [
"implementation",
"strings"
] | db473ad780a93983667d12b1357c6e2f | The first line contains two integers $$$n$$$ and $$$q$$$ $$$(1 \le n, q \le 10^5)$$$, the length of the string and the number of queries, respectively. The second line contains the string $$$s$$$, consisting of characters "a", "b" and "c". Each of the next $$$q$$$ lines contains an integer $$$i$$$ and character $$$c$$$ $$$(1 \le i \le n)$$$, index and the value of the new item in the string, respectively. It is guaranteed that character's $$$c$$$ value is "a", "b" or "c". | 1,100 | For each query output the minimal number of characters that would have to be replaced so that the string doesn't contain "abc" as a substring. | standard output | |
PASSED | 958388c1935eff6edde806b1ac4ebcd4 | train_110.jsonl | 1638110100 | Before becoming a successful trader William got a university degree. During his education an interesting situation happened, after which William started to listen to homework assignments much more attentively. What follows is the correct formal description of the homework assignment:You are given a string $$$s$$$ of length $$$n$$$ only consisting of characters "a", "b" and "c". There are $$$q$$$ queries of format ($$$pos, c$$$), meaning replacing the element of string $$$s$$$ at position $$$pos$$$ with character $$$c$$$. After each query you must output the minimal number of characters in the string, which have to be replaced, so that the string doesn't contain string "abc" as a substring. A valid replacement of a character is replacing it with "a", "b" or "c".A string $$$x$$$ is a substring of a string $$$y$$$ if $$$x$$$ can be obtained from $$$y$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. | 256 megabytes |
/* 🅻🅴🅰🆁🅽🅸🅽🅶 */
//✅//
import java.util.*;
import java.io.*;
import java.lang.*;
public class B {
static FastReader sc = new FastReader();
static PrintWriter out = new PrintWriter(System.out);
public static void main(String[] args) throws Exception {
// int t = sc.nextInt();
// while (t-- > 0)
B.go();
out.flush();
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Code Starts <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<//
static int mod=(int)1e9+7;
static class helper{
int x;int y;
helper(int x,int y){
this.x=x;
this.y=y;
}
}
static void go() throws Exception {
int n=sc.nextInt();
int q=sc.nextInt();
char a[]=sc.next().toCharArray();
int c=0;
for(int i=0;i<n;i++) {
String s="";
for(int j=i;j<i+3&& i+2<n;j++) {
s=s+a[j];
}
if(s.equals("abc")) {
c++;
}
}
// out.println("->"+c);
String match="abc";
for(int i=0;i<q;i++) {
int pos=sc.nextInt()-1;
char ch=sc.next().charAt(0);
String x="";
int chk=0;
for(int j=pos;j<Math.min(n,pos+3);j++) {
x+=a[j];
}
if(x.equals(match)) {
c--;
}
x="";
for(int j=Math.max(0,pos-1);j<Math.min(n, pos+2);j++) {
x+=a[j];
}
if(x.equals(match)) {
c--;
}
x="";
for(int j=Math.max(0,pos-2);j<=pos;j++) {
x+=a[j];
}
if(x.equals(match)) {
c--;
}
x="";
a[pos]=ch;
for(int j=pos;j<Math.min(n,pos+3);j++) {
x+=a[j];
}
if(x.equals(match)) {
chk++;
}
x="";
for(int j=Math.max(0,pos-1);j<Math.min(n, pos+2);j++) {
x+=a[j];
}
if(x.equals(match)) {
chk++;
}
x="";
for(int j=Math.max(0,pos-2);j<=pos;j++) {
x+=a[j];
}
if(x.equals(match)) {
chk++;
}
// out.println(c+" "+chk);
int tot=chk+c;
c=tot;
out.println(tot);
}
}
static boolean check(long mid,helper a[][]) {
for(helper[] i:a) {
for(int j=0;j<i.length-1;j++) {
if(mid>i[j].x) {
mid+=1;
}else {
return false;
}
}
}
return true;
}
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Code Ends <<<<<<<<<<<<<<<<<<<<<<<<<<<<< //
static long gcd(long a,long b){
if(b==0){
return a;
}return gcd(b,a%b);
}
static long fact[];
static long invfact[];
static long ncr(int n,int k) {
if(k<0||k>n) {
return 0;
}
long x=fact[n]%mod;
long y=invfact[k]%mod;
long yy=invfact[n-k]%mod;
long ans=(x*y)%mod;
ans=(ans*yy)%mod;
return ans;
}
static int prime[]=new int[40006];
static void sieve() {
Arrays.fill(prime, 1);
prime[0]=0;
prime[1]=0;
for(int i=2;i*i<=40005;i++) {
if(prime[i]==1) {
for(int j=i*i;j<=40005;j+=i) {
prime[j]=0;
}
}
}
// for(int i=1;i<1000005;i++) {prime[i]=prime[i]+prime[i-1];}
}
static void sort(long [] a) {
ArrayList<Long> aa = new ArrayList<>();
for (long i : a) {
aa.add(i);
}Collections.sort(aa); for (int i = 0; i < a.length; i++)
a[i] = aa.get(i); }
static void sort(int [] a) {
ArrayList<Integer> aa = new ArrayList<>();
for (int i : a) {
aa.add(i);
} Collections.sort(aa); for (int i = 0; i < a.length; i++)
a[i] = aa.get(i); }
static long pow(long x, long y) {
long res = 1l;
while (y != 0) {
if (y % 2 == 1) {
res = (x%mod * res%mod)%mod; } y /= 2; x = (x%mod * x%mod)%mod;
} return res%mod; }
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Fast IO <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< //
static class FastReader {
BufferedReader br;
StringTokenizer st;public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
} String next() {
while (st == null || !st.hasMoreElements()) {
try { st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
} } return st.nextToken();
} int nextInt() {
return Integer.parseInt(next());
} long nextLong() {
return Long.parseLong(next());
}double nextDouble() {
return Double.parseDouble(next());
}
int[] intArray(int n) {
int a[]=new int[n];
for(int i=0;i<n;i++)a[i]=sc.nextInt();
return a;
}
long[] longArray(int n) {
long a[]=new long[n];
for(int i=0;i<n;i++)a[i]=sc.nextLong();
return a;
}String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}}
}
/*
* imp log method using math function is somewhat inaccurate to check power of
* two
*
*/ | Java | ["9 10\nabcabcabc\n1 a\n1 b\n2 c\n3 a\n4 b\n5 c\n8 a\n9 b\n1 c\n4 a"] | 2 seconds | ["3\n2\n2\n2\n1\n2\n1\n1\n1\n0"] | NoteLet's consider the state of the string after each query: $$$s =$$$ "abcabcabc". In this case $$$3$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcaccabb". This string does not contain "abc" as a substring. $$$s =$$$ "bbcabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bccabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bccbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcaabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcabbcabc". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaac". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaac". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "ccabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccaaccaab". In this case the string does not contain "abc" as a substring and no replacements are needed. | Java 11 | standard input | [
"implementation",
"strings"
] | db473ad780a93983667d12b1357c6e2f | The first line contains two integers $$$n$$$ and $$$q$$$ $$$(1 \le n, q \le 10^5)$$$, the length of the string and the number of queries, respectively. The second line contains the string $$$s$$$, consisting of characters "a", "b" and "c". Each of the next $$$q$$$ lines contains an integer $$$i$$$ and character $$$c$$$ $$$(1 \le i \le n)$$$, index and the value of the new item in the string, respectively. It is guaranteed that character's $$$c$$$ value is "a", "b" or "c". | 1,100 | For each query output the minimal number of characters that would have to be replaced so that the string doesn't contain "abc" as a substring. | standard output | |
PASSED | 4dba95af1cd0bd44ffcd52fef77bbe22 | train_110.jsonl | 1638110100 | Before becoming a successful trader William got a university degree. During his education an interesting situation happened, after which William started to listen to homework assignments much more attentively. What follows is the correct formal description of the homework assignment:You are given a string $$$s$$$ of length $$$n$$$ only consisting of characters "a", "b" and "c". There are $$$q$$$ queries of format ($$$pos, c$$$), meaning replacing the element of string $$$s$$$ at position $$$pos$$$ with character $$$c$$$. After each query you must output the minimal number of characters in the string, which have to be replaced, so that the string doesn't contain string "abc" as a substring. A valid replacement of a character is replacing it with "a", "b" or "c".A string $$$x$$$ is a substring of a string $$$y$$$ if $$$x$$$ can be obtained from $$$y$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. | 256 megabytes | import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class ProblemB {
public static void main(String[] args) throws Exception {
BufferedReader f = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(f.readLine());
int n = Integer.parseInt(st.nextToken());
int q = Integer.parseInt(st.nextToken());
st = new StringTokenizer(f.readLine());
String s = st.nextToken();
int count = 0;
for (int j = 0; j < s.length() - 2; j++) {
if (s.substring(j, j + 3).equals("abc")) {
count++;
}
}
StringBuilder sb = new StringBuilder();
sb.append(s);
int[] query = new int[q];
String[] ch = new String[q];
for (int j = 0; j < q; j++) {
st = new StringTokenizer(f.readLine());
query[j] = Integer.parseInt(st.nextToken())-1;
ch[j] = st.nextToken();
int add = 0;
if (query[j] > 1 && query[j] < n - 2) {
for (int m = query[j] - 2; m >= 0 && m < n - 2 && m <= query[j]; m++) {
if (sb.substring(m, m + 3).equals("abc")) {
add++;
}
}
} else if(query[j] <= 1 && query[j] < n - 2) {
for (int m = 0; m >= 0 && m < n - 2 && m <= query[j]; m++) {
if (sb.substring(m, m + 3).equals("abc")) {
add++;
}
}
}
else if(query[j] > 1 && query[j] >= n - 2) {
for (int m = query[j]-2; m >= 0 && m < n - 2 && m <= query[j]; m++) {
if (sb.substring(m, m + 3).equals("abc")) {
add++;
}
}
}
sb.replace(query[j], query[j]+1, ch[j]);
int total = 0;
if (query[j] > 1 && query[j] < n - 2) {
for (int m = query[j] - 2; m >= 0 && m < n - 2 && m <= query[j]; m++) {
if (sb.substring(m, m + 3).equals("abc")) {
total++;
}
}
} else if(query[j] <= 1 && query[j] < n - 2) {
for (int m = 0; m >= 0 && m < n - 2 && m <= query[j]; m++) {
if (sb.substring(m, m + 3).equals("abc")) {
total++;
}
}
}
else if(query[j] > 1 && query[j] >= n - 2) {
for (int m = query[j]-2; m >= 0 && m < n - 2 && m <= query[j]; m++) {
if (sb.substring(m, m + 3).equals("abc")) {
total++;
}
}
}
if (total > add) {
count++;
} else if (total < add) {
count--;
}
//System.out.println(add + " " + total);
//System.out.println(s+" "+add+" "+total);
System.out.println(count);
}
}
} | Java | ["9 10\nabcabcabc\n1 a\n1 b\n2 c\n3 a\n4 b\n5 c\n8 a\n9 b\n1 c\n4 a"] | 2 seconds | ["3\n2\n2\n2\n1\n2\n1\n1\n1\n0"] | NoteLet's consider the state of the string after each query: $$$s =$$$ "abcabcabc". In this case $$$3$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcaccabb". This string does not contain "abc" as a substring. $$$s =$$$ "bbcabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bccabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bccbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcaabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcabbcabc". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaac". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaac". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "ccabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccaaccaab". In this case the string does not contain "abc" as a substring and no replacements are needed. | Java 11 | standard input | [
"implementation",
"strings"
] | db473ad780a93983667d12b1357c6e2f | The first line contains two integers $$$n$$$ and $$$q$$$ $$$(1 \le n, q \le 10^5)$$$, the length of the string and the number of queries, respectively. The second line contains the string $$$s$$$, consisting of characters "a", "b" and "c". Each of the next $$$q$$$ lines contains an integer $$$i$$$ and character $$$c$$$ $$$(1 \le i \le n)$$$, index and the value of the new item in the string, respectively. It is guaranteed that character's $$$c$$$ value is "a", "b" or "c". | 1,100 | For each query output the minimal number of characters that would have to be replaced so that the string doesn't contain "abc" as a substring. | standard output | |
PASSED | 63a81800e76456b5e0f40a8f512f5867 | train_110.jsonl | 1638110100 | Before becoming a successful trader William got a university degree. During his education an interesting situation happened, after which William started to listen to homework assignments much more attentively. What follows is the correct formal description of the homework assignment:You are given a string $$$s$$$ of length $$$n$$$ only consisting of characters "a", "b" and "c". There are $$$q$$$ queries of format ($$$pos, c$$$), meaning replacing the element of string $$$s$$$ at position $$$pos$$$ with character $$$c$$$. After each query you must output the minimal number of characters in the string, which have to be replaced, so that the string doesn't contain string "abc" as a substring. A valid replacement of a character is replacing it with "a", "b" or "c".A string $$$x$$$ is a substring of a string $$$y$$$ if $$$x$$$ can be obtained from $$$y$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. | 256 megabytes | import java.util.*;
import java.io.*;
import java.math.*;
import java.lang.*;
public class b {
// static int mod = 998244353;
static int mod = 1000000007;
private static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
public static void main(String[] args) throws Exception {
FastReader scn = new FastReader();
PrintWriter pw = new PrintWriter(System.out);
int t = 1;
outer : while(t-->0){
int n = scn.nextInt();
int q = scn.nextInt();
String s = scn.nextLine();
int ss = countSS(s);
StringBuilder sb = new StringBuilder(s);
int prev = ss;
while(q-->0){
String str = scn.nextLine();
String[] parts = str.split(" ");
int idx = Integer.parseInt(parts[0]) - 1;
char ch = parts[1].charAt(0);
if(sb.charAt(idx) == ch){
pw.println(prev);
}else{
boolean res = isPart(sb, idx);
sb.setCharAt(idx, ch);
boolean ans = isPart(sb, idx);
if(res == false && ans == false){
pw.println(prev);
}else if(res == false && ans == true){
prev++;
pw.println(prev);
}else if(res == true && ans == false){
prev--;
pw.println(prev);
}else if(res == true && ans == true){
pw.println(prev);
}
}
}
}
pw.close();
}
private static boolean isPart(StringBuilder sb, int idx){
if(idx - 2 >= 0){
if(sb.charAt(idx-2) == 'a' && sb.charAt(idx-1) == 'b' && sb.charAt(idx) == 'c'){
return true;
}
}
if(idx-1 >= 0 && idx + 1 < sb.length()){
if(sb.charAt(idx-1) == 'a' && sb.charAt(idx) == 'b' && sb.charAt(idx+1) == 'c'){
return true;
}
}
if(idx + 2 < sb.length()){
if(sb.charAt(idx) == 'a' && sb.charAt(idx+1) == 'b' && sb.charAt(idx+2) == 'c'){
return true;
}
}
return false;
}
private static int countSS(String s){
int count = 0;
for(int i=0; i<s.length()-2; i++){
if(s.charAt(i) == 'a' && s.charAt(i+1) == 'b' && s.charAt(i+2) == 'c'){
count++;
}
}
return count;
}
public static class Pair{
int x;
int y;
Pair(int x, int y){
this.x = x;
this.y = y;
}
}
private static void sort(int[] arr) {
List<Integer> list = new ArrayList<>();
for (int i=0; i<arr.length; i++){
list.add(arr[i]);
}
Collections.sort(list); // collections.sort uses nlogn in backend
for (int i = 0; i < arr.length; i++){
arr[i] = list.get(i);
}
}
private static void sort(long[] arr) {
List<Long> list = new ArrayList<>();
for(int i=0; i<arr.length; i++){
list.add(arr[i]);
}
Collections.sort(list); // collections.sort uses nlogn in backend
for (int i = 0; i < arr.length; i++){
arr[i] = list.get(i);
}
}
// private static void sort(Pair[] arr) {
// List<Pair> list = new ArrayList<>();
// for(int i=0; i<arr.length; i++){
// list.add(arr[i]);
// }
// Collections.sort(list); // collections.sort uses nlogn in backend
// for (int i = 0; i < arr.length; i++){
// arr[i] = list.get(i);
// }
// }
private static void reverseSort(int[] arr){
List<Integer> list = new ArrayList<>();
for (int i=0; i<arr.length; i++){
list.add(arr[i]);
}
Collections.sort(list, Collections.reverseOrder()); // collections.sort uses nlogn in backend
for (int i = 0; i < arr.length; i++){
arr[i] = list.get(i);
}
}
private static void reverseSort(long[] arr){
List<Long> list = new ArrayList<>();
for (int i=0; i<arr.length; i++){
list.add(arr[i]);
}
Collections.sort(list, Collections.reverseOrder()); // collections.sort uses nlogn in backend
for (int i = 0; i < arr.length; i++){
arr[i] = list.get(i);
}
}
private static void reverseSort(ArrayList<Integer> list){
Collections.sort(list, Collections.reverseOrder());
}
private static int lowerBound(int[] arr, int x){
int n = arr.length, si = 0, ei = n - 1;
while(si <= ei){
int mid = si + (ei - si)/2;
if(arr[mid] == x){
if(mid-1 >= 0 && arr[mid-1] == arr[mid]){
ei = mid-1;
}else{
return mid;
}
}else if(arr[mid] > x){
ei = mid - 1;
}else{
si = mid+1;
}
}
return si;
}
private static int upperBound(int[] arr, int x){
int n = arr.length, si = 0, ei = n - 1;
while(si <= ei){
int mid = si + (ei - si)/2;
if(arr[mid] == x){
if(mid+1 < n && arr[mid+1] == arr[mid]){
si = mid+1;
}else{
return mid + 1;
}
}else if(arr[mid] > x){
ei = mid - 1;
}else{
si = mid+1;
}
}
return si;
}
private static int upperBound(ArrayList<Integer> list, int x){
int n = list.size(), si = 0, ei = n - 1;
while(si <= ei){
int mid = si + (ei - si)/2;
if(list.get(mid) == x){
if(mid+1 < n && list.get(mid+1) == list.get(mid)){
si = mid+1;
}else{
return mid + 1;
}
}else if(list.get(mid) > x){
ei = mid - 1;
}else{
si = mid+1;
}
}
return si;
}
// (x^y)%p in O(logy)
private static long power(long x, long y){
long res = 1;
x = x % mod;
while(y > 0){
if ((y & 1) == 1){
res = (res * x) % mod;
}
y = y >> 1;
x = (x * x) % mod;
}
return res;
}
public static boolean nextPermutation(int[] arr) {
if(arr == null || arr.length <= 1){
return false;
}
int last = arr.length-2;
while(last >= 0){
if(arr[last] < arr[last+1]){
break;
}
last--;
}
if (last < 0){
return false;
}
if(last >= 0){
int nextGreater = arr.length-1;
for(int i=arr.length-1; i>last; i--){
if(arr[i] > arr[last]){
nextGreater = i;
break;
}
}
swap(arr, last, nextGreater);
}
reverse(arr, last+1, arr.length-1);
return true;
}
private static void swap(int[] arr, int i, int j){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
private static void reverse(int[] arr, int i, int j){
while(i < j){
swap(arr, i++, j--);
}
}
private static String reverseStr(String s){
StringBuilder sb = new StringBuilder(s);
return sb.reverse().toString();
}
// TC- O(logmax(a,b))
private static int gcd(int a, int b) {
if(a == 0){
return b;
}
return gcd(b%a, a);
}
private static long gcd(long a, long b) {
if(a == 0L){
return b;
}
return gcd(b%a, a);
}
private static long lcm(long a, long b) {
return a / gcd(a, b) * b;
}
// TC- O(logmax(a,b))
private static int lcm(int a, int b) {
return a / gcd(a, b) * b;
}
private static long inv(long x){
return power(x, mod - 2);
}
private static long summation(long n){
return (n * (n + 1L)) >> 1;
}
} | Java | ["9 10\nabcabcabc\n1 a\n1 b\n2 c\n3 a\n4 b\n5 c\n8 a\n9 b\n1 c\n4 a"] | 2 seconds | ["3\n2\n2\n2\n1\n2\n1\n1\n1\n0"] | NoteLet's consider the state of the string after each query: $$$s =$$$ "abcabcabc". In this case $$$3$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcaccabb". This string does not contain "abc" as a substring. $$$s =$$$ "bbcabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bccabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bccbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcaabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcabbcabc". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaac". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaac". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "ccabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccaaccaab". In this case the string does not contain "abc" as a substring and no replacements are needed. | Java 11 | standard input | [
"implementation",
"strings"
] | db473ad780a93983667d12b1357c6e2f | The first line contains two integers $$$n$$$ and $$$q$$$ $$$(1 \le n, q \le 10^5)$$$, the length of the string and the number of queries, respectively. The second line contains the string $$$s$$$, consisting of characters "a", "b" and "c". Each of the next $$$q$$$ lines contains an integer $$$i$$$ and character $$$c$$$ $$$(1 \le i \le n)$$$, index and the value of the new item in the string, respectively. It is guaranteed that character's $$$c$$$ value is "a", "b" or "c". | 1,100 | For each query output the minimal number of characters that would have to be replaced so that the string doesn't contain "abc" as a substring. | standard output | |
PASSED | 7e0690629acd31b455f9cf60089a35b7 | train_110.jsonl | 1638110100 | Before becoming a successful trader William got a university degree. During his education an interesting situation happened, after which William started to listen to homework assignments much more attentively. What follows is the correct formal description of the homework assignment:You are given a string $$$s$$$ of length $$$n$$$ only consisting of characters "a", "b" and "c". There are $$$q$$$ queries of format ($$$pos, c$$$), meaning replacing the element of string $$$s$$$ at position $$$pos$$$ with character $$$c$$$. After each query you must output the minimal number of characters in the string, which have to be replaced, so that the string doesn't contain string "abc" as a substring. A valid replacement of a character is replacing it with "a", "b" or "c".A string $$$x$$$ is a substring of a string $$$y$$$ if $$$x$$$ can be obtained from $$$y$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. | 256 megabytes | /*
Codeforces
Problem 1609B
*/
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
public class WilliamVigilant {
public static void main(String[] args) throws IOException {
FastIO in = new FastIO(System.in);
int n = in.nextInt();
int q = in.nextInt();
StringBuilder s = new StringBuilder(in.next());
int count = 0;
for (int i = 0; i < n-2; i++) {
if (s.substring(i, i+3).equals("abc")) count++;
}
while (q-- > 0) {
int pos = in.nextInt() - 1;
char c = (char) in.nextByte();
if (pos+3 <= n && s.substring(pos, pos+3).equals("abc")) count--;
if (pos-1 >= 0 && pos+2 <= n && s.substring(pos-1, pos+2).equals("abc")) count--;
if (pos-2 >= 0 && pos+1 <= n && s.substring(pos-2, pos+1).equals("abc")) count--;
s.setCharAt(pos, c);
if (pos+3 <= n && s.substring(pos, pos+3).equals("abc")) count++;
if (pos-1 >= 0 && pos+2 <= n && s.substring(pos-1, pos+2).equals("abc")) count++;
if (pos-2 >= 0 && pos+1 <= n && s.substring(pos-2, pos+1).equals("abc")) count++;
System.out.println(count);
}
in.close();
}
public static class FastIO {
private InputStream dis;
private byte[] buffer = new byte[1 << 17];
private int pointer = 0;
public FastIO(String fileName) throws IOException {
dis = new FileInputStream(fileName);
}
public FastIO(InputStream is) throws IOException {
dis = is;
}
public int nextInt() throws IOException {
int ret = 0;
byte b;
do {
b = nextByte();
} while (b <= ' ');
boolean negative = false;
if (b == '-') {
negative = true;
b = nextByte();
}
while (b >= '0' && b <= '9') {
ret = 10 * ret + b - '0';
b = nextByte();
}
return (negative) ? -ret : ret;
}
public long nextLong() throws IOException {
long ret = 0;
byte b;
do {
b = nextByte();
} while (b <= ' ');
boolean negative = false;
if (b == '-') {
negative = true;
b = nextByte();
}
while (b >= '0' && b <= '9') {
ret = 10 * ret + b - '0';
b = nextByte();
}
return (negative) ? -ret : ret;
}
public byte nextByte() throws IOException {
if (pointer == buffer.length) {
dis.read(buffer, 0, buffer.length);
pointer = 0;
}
return buffer[pointer++];
}
public String next() throws IOException {
StringBuffer ret = new StringBuffer();
byte b;
do {
b = nextByte();
} while (b <= ' ');
while (b > ' ') {
ret.appendCodePoint(b);
b = nextByte();
}
return ret.toString();
}
public void close() throws IOException {
dis.close();
}
}
} | Java | ["9 10\nabcabcabc\n1 a\n1 b\n2 c\n3 a\n4 b\n5 c\n8 a\n9 b\n1 c\n4 a"] | 2 seconds | ["3\n2\n2\n2\n1\n2\n1\n1\n1\n0"] | NoteLet's consider the state of the string after each query: $$$s =$$$ "abcabcabc". In this case $$$3$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcaccabb". This string does not contain "abc" as a substring. $$$s =$$$ "bbcabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bccabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bccbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcaabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcabbcabc". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaac". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaac". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "ccabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccaaccaab". In this case the string does not contain "abc" as a substring and no replacements are needed. | Java 11 | standard input | [
"implementation",
"strings"
] | db473ad780a93983667d12b1357c6e2f | The first line contains two integers $$$n$$$ and $$$q$$$ $$$(1 \le n, q \le 10^5)$$$, the length of the string and the number of queries, respectively. The second line contains the string $$$s$$$, consisting of characters "a", "b" and "c". Each of the next $$$q$$$ lines contains an integer $$$i$$$ and character $$$c$$$ $$$(1 \le i \le n)$$$, index and the value of the new item in the string, respectively. It is guaranteed that character's $$$c$$$ value is "a", "b" or "c". | 1,100 | For each query output the minimal number of characters that would have to be replaced so that the string doesn't contain "abc" as a substring. | standard output | |
PASSED | 9e445ff53f22f06f7e6c256d54b8622f | train_110.jsonl | 1638110100 | Before becoming a successful trader William got a university degree. During his education an interesting situation happened, after which William started to listen to homework assignments much more attentively. What follows is the correct formal description of the homework assignment:You are given a string $$$s$$$ of length $$$n$$$ only consisting of characters "a", "b" and "c". There are $$$q$$$ queries of format ($$$pos, c$$$), meaning replacing the element of string $$$s$$$ at position $$$pos$$$ with character $$$c$$$. After each query you must output the minimal number of characters in the string, which have to be replaced, so that the string doesn't contain string "abc" as a substring. A valid replacement of a character is replacing it with "a", "b" or "c".A string $$$x$$$ is a substring of a string $$$y$$$ if $$$x$$$ can be obtained from $$$y$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. | 256 megabytes | import java.util.*;
import java.io.*;
public class xorspecialist {
static Scanner sc=new Scanner(System.in);
static PrintWriter out=new PrintWriter(System.out);
public static void main(String[] args) {
long t=1;
while(t-->0){
solve();
}
out.close();
}
private static void solve() {
int n=sc.nextInt();
int q=sc.nextInt();
String s=sc.next();
int count=0;
for(int i=0;i<n;i++){
if(s.charAt(i)=='a'){
if(i<n-2&&s.charAt(i+1)=='b'&&s.charAt(i+2)=='c'){
count++;
}
}
}
StringBuilder sb=new StringBuilder(s);
int temp=count;
// out.println(count);
while(q-->0){
int x=sc.nextInt();
x--;
char ch=sc.next().charAt(0);
if(s.charAt(x)==ch){
out.println(count);
}
else{
if(s.charAt(x)=='a'){
if(x<n-2&&s.charAt(x+1)=='b'&&s.charAt(x+2)=='c'){
count--;
}
}
else if(s.charAt(x)=='b'){
if(x>0&&x<n-1&&s.charAt(x-1)=='a'&&s.charAt(x+1)=='c'){
count--;
}
}
else if(s.charAt(x)=='c'){
if(x>1&&x<n&&s.charAt(x-1)=='b'&&s.charAt(x-2)=='a'){
count--;
}
}
//now we gor new character
if(ch=='a'){
if(x<n-2&&s.charAt(x+1)=='b'&&s.charAt(x+2)=='c'){
count++;
}
}
else if(ch=='b'){
if(x>0&&x<n-1&&s.charAt(x-1)=='a'&&s.charAt(x+1)=='c'){
count++;
}
}
else if(ch=='c'){
if(x>1&&x<n&&s.charAt(x-1)=='b'&&s.charAt(x-2)=='a'){
count++;
}
}
sb.setCharAt(x, ch);
s=sb.toString();
out.println(count);
}
}
}
}
| Java | ["9 10\nabcabcabc\n1 a\n1 b\n2 c\n3 a\n4 b\n5 c\n8 a\n9 b\n1 c\n4 a"] | 2 seconds | ["3\n2\n2\n2\n1\n2\n1\n1\n1\n0"] | NoteLet's consider the state of the string after each query: $$$s =$$$ "abcabcabc". In this case $$$3$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcaccabb". This string does not contain "abc" as a substring. $$$s =$$$ "bbcabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bccabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bccbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcaabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcabbcabc". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaac". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaac". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "ccabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccaaccaab". In this case the string does not contain "abc" as a substring and no replacements are needed. | Java 11 | standard input | [
"implementation",
"strings"
] | db473ad780a93983667d12b1357c6e2f | The first line contains two integers $$$n$$$ and $$$q$$$ $$$(1 \le n, q \le 10^5)$$$, the length of the string and the number of queries, respectively. The second line contains the string $$$s$$$, consisting of characters "a", "b" and "c". Each of the next $$$q$$$ lines contains an integer $$$i$$$ and character $$$c$$$ $$$(1 \le i \le n)$$$, index and the value of the new item in the string, respectively. It is guaranteed that character's $$$c$$$ value is "a", "b" or "c". | 1,100 | For each query output the minimal number of characters that would have to be replaced so that the string doesn't contain "abc" as a substring. | standard output | |
PASSED | 96bb08a15a5847646b04443b89a7aaa2 | train_110.jsonl | 1638110100 | Before becoming a successful trader William got a university degree. During his education an interesting situation happened, after which William started to listen to homework assignments much more attentively. What follows is the correct formal description of the homework assignment:You are given a string $$$s$$$ of length $$$n$$$ only consisting of characters "a", "b" and "c". There are $$$q$$$ queries of format ($$$pos, c$$$), meaning replacing the element of string $$$s$$$ at position $$$pos$$$ with character $$$c$$$. After each query you must output the minimal number of characters in the string, which have to be replaced, so that the string doesn't contain string "abc" as a substring. A valid replacement of a character is replacing it with "a", "b" or "c".A string $$$x$$$ is a substring of a string $$$y$$$ if $$$x$$$ can be obtained from $$$y$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. | 256 megabytes | import java.util.*;
import java.io.*;
public class practice {
static char vowels[] = new char[]{'a', 'e', 'i', 'o', 'u'};
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(new BufferedWriter(new PrintWriter(System.out)));
StringBuilder sb = new StringBuilder();
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int q = Integer.parseInt(st.nextToken());
char arr[] = br.readLine().toCharArray();
int tot = 0;
for (int i = 0; i < n - 2; i++) {
if (arr[i] == 'a') {
if (arr[i + 1] == 'b' && arr[i + 2] == 'c') tot++;
}
}
while (q --> 0) {
st = new StringTokenizer(br.readLine());
int i = Integer.parseInt(st.nextToken()) - 1;
char c = st.nextToken().charAt(0);
if (c == 'a') {
if (arr[i] == 'b' && i - 1 >= 0 && arr[i - 1] == 'a' && i + 1 < n && arr[i + 1] == 'c') tot--;
else if (arr[i] == 'c' && i - 1 >= 0 && arr[i - 1] == 'b' && i - 2 >= 0 && arr[i - 2] == 'a') tot--;
if (arr[i] != 'a' && i + 1 < n && arr[i + 1] == 'b' && i + 2 < n && arr[i + 2] == 'c') tot++;
} else if (c == 'b') {
if (arr[i] == 'a' && i + 1 < n && arr[i + 1] == 'b' && i + 2 < n && arr[i + 2] == 'c') tot--;
else if (arr[i] == 'c' && i - 1 >= 0 && arr[i - 1] == 'b' && i - 2 >= 0 && arr[i - 2] == 'a') tot--;
if (arr[i] != 'b' && i - 1 >= 0 && arr[i - 1] == 'a' && i + 1 < n && arr[i + 1] == 'c') tot++;
} else {
if (arr[i] == 'a' && i + 1 < n && arr[i + 1] == 'b' && i + 2 < n && arr[i + 2] == 'c') tot--;
else if (arr[i] == 'b' && i - 1 >= 0 && arr[i - 1] == 'a' && i + 1 < n && arr[i + 1] == 'c') tot--;
if (arr[i] != 'c' && i - 1 >= 0 && arr[i - 1] == 'b' && i - 2 >= 0 && arr[i - 2] == 'a') tot++;
}
arr[i] = c;
sb.append(tot).append("\n");
}
pw.println(sb.toString().trim());
pw.close();
br.close();
}
} | Java | ["9 10\nabcabcabc\n1 a\n1 b\n2 c\n3 a\n4 b\n5 c\n8 a\n9 b\n1 c\n4 a"] | 2 seconds | ["3\n2\n2\n2\n1\n2\n1\n1\n1\n0"] | NoteLet's consider the state of the string after each query: $$$s =$$$ "abcabcabc". In this case $$$3$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcaccabb". This string does not contain "abc" as a substring. $$$s =$$$ "bbcabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bccabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bccbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcaabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcabbcabc". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaac". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaac". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "ccabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccaaccaab". In this case the string does not contain "abc" as a substring and no replacements are needed. | Java 11 | standard input | [
"implementation",
"strings"
] | db473ad780a93983667d12b1357c6e2f | The first line contains two integers $$$n$$$ and $$$q$$$ $$$(1 \le n, q \le 10^5)$$$, the length of the string and the number of queries, respectively. The second line contains the string $$$s$$$, consisting of characters "a", "b" and "c". Each of the next $$$q$$$ lines contains an integer $$$i$$$ and character $$$c$$$ $$$(1 \le i \le n)$$$, index and the value of the new item in the string, respectively. It is guaranteed that character's $$$c$$$ value is "a", "b" or "c". | 1,100 | For each query output the minimal number of characters that would have to be replaced so that the string doesn't contain "abc" as a substring. | standard output | |
PASSED | c0c715c958c68ce5c2b77d2f905d5bb0 | train_110.jsonl | 1638110100 | Before becoming a successful trader William got a university degree. During his education an interesting situation happened, after which William started to listen to homework assignments much more attentively. What follows is the correct formal description of the homework assignment:You are given a string $$$s$$$ of length $$$n$$$ only consisting of characters "a", "b" and "c". There are $$$q$$$ queries of format ($$$pos, c$$$), meaning replacing the element of string $$$s$$$ at position $$$pos$$$ with character $$$c$$$. After each query you must output the minimal number of characters in the string, which have to be replaced, so that the string doesn't contain string "abc" as a substring. A valid replacement of a character is replacing it with "a", "b" or "c".A string $$$x$$$ is a substring of a string $$$y$$$ if $$$x$$$ can be obtained from $$$y$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Random;
import java.util.StringTokenizer;
/*
*/
public class acmp {
public static void main(String[] args) {
FastScanner fs=new FastScanner();
PrintWriter out=new PrintWriter(System.out);
int T=1;
// int T=fs.nextInt();
outer: for (int tt=0; tt<T; tt++) {
int n = fs.nextInt(), q = fs.nextInt();
int ans = 0;
char[] s = fs.next().toCharArray();
for (int i = 0; i < s.length - 2; i++) {
if (s[i] == 'a' && s[i + 1] == 'b' && s[i + 2] == 'c') ans++;
}
while (q-- > 0) {
int pos = fs.nextInt();
char c = fs.next().charAt(0);
pos--;
if (pos - 2 >= 0 && s[pos - 2] == 'a' && s[pos - 1] == 'b' && s[pos] == 'c') ans--;
else if (pos + 1 < n && pos - 1 >= 0 && s[pos - 1] == 'a' && s[pos] == 'b' && s[pos + 1] == 'c') ans--;
else if (pos + 2 < n && s[pos] == 'a' && s[pos + 1] == 'b' && s[pos + 2] == 'c') ans--;
s[pos] = c;
if (pos - 2 >= 0 && s[pos - 2] == 'a' && s[pos - 1] == 'b' && s[pos] == 'c') ans++;
else if (pos + 1 < n && pos - 1 >= 0 && s[pos - 1] == 'a' && s[pos] == 'b' && s[pos + 1] == 'c') ans++;
else if (pos + 2 < n && s[pos] == 'a' && s[pos + 1] == 'b' && s[pos + 2] == 'c') ans++;
out.println(ans);
}
}
out.close();
}
static final int INF = 2_000_000_000;
// static Random random = new Random(5);
static final int mod = 998244353;
// static final int LOGN = 15;
//
// static void ruffleSort(int[] a) {
// int n=a.lengh;//shuffle, then sort
// for (int i=0; i<n; i++) {
// int oi=random.nextInt(n), temp=a[oi];
// a[oi]=a[i]; a[i]=temp;
// }
// Arrays.sort(a);
// }
static long add(long a, long b) {
return (a+b)%mod;
}
// static long sub(long a, long b) {
// return ((a-b)%mod+mod)%mod;
// }
// static long mul(long a, long b) {
// return (a*b)%mod;
// }
// static long exp(long base, long exp) {
// if (exp==0) return 1;
// long half=exp(base, exp/2);
// if (exp%2==0) return mul(half, half);
// return mul(half, mul(half, base));
// }
// static long[] factorials=new long[2_000_001];
// static long[] invFactorials=new long[2_000_001];
// static void precompFacts() {
// factorials[0]=invFactorials[0]=1;
// for (int i=1; i<factorials.length; i++) factorials[i]=mul(factorials[i-1], i);
// invFactorials[factorials.length-1]=exp(factorials[factorials.length-1], mod-2);
// for (int i=invFactorials.length-2; i>=0; i--)
// invFactorials[i]=mul(invFactorials[i+1], i+1);
// }
//
// static long nCk(int n, int k) {
// return mul(factorials[n], mul(invFactorials[k], invFactorials[n-k]));
// }
static void sort(int[] b) {
ArrayList<Integer> l=new ArrayList<>();
for (int i:b) l.add(i);
Collections.sort(l);
for (int i=0; i<b.length; i++) b[i]=l.get(i);
}
static class FastScanner {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
}
}
| Java | ["9 10\nabcabcabc\n1 a\n1 b\n2 c\n3 a\n4 b\n5 c\n8 a\n9 b\n1 c\n4 a"] | 2 seconds | ["3\n2\n2\n2\n1\n2\n1\n1\n1\n0"] | NoteLet's consider the state of the string after each query: $$$s =$$$ "abcabcabc". In this case $$$3$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcaccabb". This string does not contain "abc" as a substring. $$$s =$$$ "bbcabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bccabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bccbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcaabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcabbcabc". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaac". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaac". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "ccabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccaaccaab". In this case the string does not contain "abc" as a substring and no replacements are needed. | Java 11 | standard input | [
"implementation",
"strings"
] | db473ad780a93983667d12b1357c6e2f | The first line contains two integers $$$n$$$ and $$$q$$$ $$$(1 \le n, q \le 10^5)$$$, the length of the string and the number of queries, respectively. The second line contains the string $$$s$$$, consisting of characters "a", "b" and "c". Each of the next $$$q$$$ lines contains an integer $$$i$$$ and character $$$c$$$ $$$(1 \le i \le n)$$$, index and the value of the new item in the string, respectively. It is guaranteed that character's $$$c$$$ value is "a", "b" or "c". | 1,100 | For each query output the minimal number of characters that would have to be replaced so that the string doesn't contain "abc" as a substring. | standard output | |
PASSED | 088e4fa3ec8a495bd450daf851508702 | train_110.jsonl | 1638110100 | Before becoming a successful trader William got a university degree. During his education an interesting situation happened, after which William started to listen to homework assignments much more attentively. What follows is the correct formal description of the homework assignment:You are given a string $$$s$$$ of length $$$n$$$ only consisting of characters "a", "b" and "c". There are $$$q$$$ queries of format ($$$pos, c$$$), meaning replacing the element of string $$$s$$$ at position $$$pos$$$ with character $$$c$$$. After each query you must output the minimal number of characters in the string, which have to be replaced, so that the string doesn't contain string "abc" as a substring. A valid replacement of a character is replacing it with "a", "b" or "c".A string $$$x$$$ is a substring of a string $$$y$$$ if $$$x$$$ can be obtained from $$$y$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. | 256 megabytes | //package cp;
import java.io.*;
import java.util.*;
public class ProblemA{
static long mod = 1000000007L;
// map.put(a[i],map.getOrDefault(a[i],0)+1);
// map.putIfAbsent;
static MyScanner sc = new MyScanner();
//<----------------------------------------------WRITE HERE------------------------------------------->
static void solve(){
int n = sc.nextInt();
int q = sc.nextInt();
char[] ca = sc.next().toCharArray();
int ans = 0;
for(int i=0;i<n;i++) {
if(ca[i] == 'a' && i<n-2) {
if(ca[i+1] == 'b' && ca[i+2] == 'c') {
ans++;
}
}
}
for(int k=0;k<q;k++) {
int pos = sc.nextInt()-1;
char c = sc.next().charAt(0);
if(ca[pos] != c) {
if(check(ca,pos,c)) ans--;
int i = pos;
if(c == 'a') {
if(i<n-2 && ca[i+1] == 'b' && ca[i+2] == 'c') {
ans++;
}
}
else if(c == 'b') {
if(i>0 && i<n-1 && ca[i-1] == 'a' && ca[i+1] == 'c') {
ans++;
}
}else {
if(i>1 && ca[i-2] == 'a' && ca[i-1] == 'b') {
ans++;
}
}
ca[pos] = c;
}
out.println(ans);
}
}
static boolean check(char[] ca, int i , char c) {
for(int j=Math.max(i-2, 0);j<=i;j++) {
if(ca[j] == 'a' && j<ca.length-2) {
if(ca[j+1] == 'b' && ca[j+2] == 'c') {
return true;
}
}
}
return false;
}
//<----------------------------------------------WRITE HERE------------------------------------------->
static void swap(char[] a,int i,int j) {
char temp = a[i];
a[i] = a[j];
a[j] = temp;
}
static int LowerBound(int a[], int x) {
int l = -1, r = a.length;
while (l + 1 < r) {
int m = (l + r) >>> 1;
if (a[m] >= x)
r = m;
else
l = m;
}
return r;
}
static int UpperBound(int a[], int x) {
int l = -1, r = a.length;
while (l + 1 < r) {
int m = (l + r) >>> 1;
if (a[m] <= x)
l = m;
else
r = m;
}
return l + 1;
}
static String sortString(String inputString) {
char tempArray[] = inputString.toCharArray();
Arrays.sort(tempArray);
return new String(tempArray);
}
static boolean isPali(String str){
int i = 0;
int j = str.length()-1;
while(i<j){
if(str.charAt(i)!=str.charAt(j)){
return false;
}
i++;
j--;
}
return true;
}
static void priArr(int[] a) {
for(int i=0;i<a.length;i++) {
out.print(a[i] + " ");
}
out.println();
}
static long gcd(long a,long b){
if(b==0) return a;
return gcd(b,a%b);
}
static String reverse(String str){
char arr[] = str.toCharArray();
int i = 0;
int j = arr.length-1;
while(i<j){
char temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++;
j--;
}
String st = new String(arr);
return st;
}
static void reverse(int[] arr){
int i = 0;
int j = arr.length-1;
while(i<j){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++;
j--;
}
}
static boolean isprime(int n){
if(n==1) return false;
if(n==3 || n==2) return true;
if(n%2==0 || n%3==0) return false;
for(int i = 5;i*i<=n;i+= 6){
if(n%i== 0 || n%(i+2)==0){
return false;
}
}
return true;
}
static class Pair implements Comparable<Pair>{
int val;
int ind;
Pair(int v,int f){
val = v;
ind = f;
}
public int compareTo(Pair p){
return p.val-this.val; // decreasing
}
}
public static void main(String[] args) {
out = new PrintWriter(new BufferedOutputStream(System.out));
// int t = sc.nextInt();
int t= 1;
while(t-- >0){
solve();
}
// Stop writing your solution here. -------------------------------------
out.close();
}
//-----------PrintWriter for faster output---------------------------------
public static PrintWriter out;
//-----------MyScanner class for faster input----------
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
public int[] readIntArray(int n){
int arr[] = new int[n];
for(int i = 0;i<n;i++){
arr[i] = Integer.parseInt(next());
}
return arr;
}
int[] reverse(int arr[]){
int n= arr.length;
int i = 0;
int j = n-1;
while(i<j){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
j--;i++;
}
return arr;
}
long[] readLongArray(int n){
long arr[] = new long[n];
for(int i = 0;i<n;i++){
arr[i] = Long.parseLong(next());
}
return arr;
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
private static void sort(int[] arr) {
List<Integer> list = new ArrayList<>();
for (int i=0; i<arr.length; i++){
list.add(arr[i]);
}
Collections.sort(list); // collections.sort uses nlogn in backend
for (int i = 0; i < arr.length; i++){
arr[i] = list.get(i);
}
}
} | Java | ["9 10\nabcabcabc\n1 a\n1 b\n2 c\n3 a\n4 b\n5 c\n8 a\n9 b\n1 c\n4 a"] | 2 seconds | ["3\n2\n2\n2\n1\n2\n1\n1\n1\n0"] | NoteLet's consider the state of the string after each query: $$$s =$$$ "abcabcabc". In this case $$$3$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcaccabb". This string does not contain "abc" as a substring. $$$s =$$$ "bbcabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bccabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bccbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcaabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcabbcabc". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaac". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaac". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "ccabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccaaccaab". In this case the string does not contain "abc" as a substring and no replacements are needed. | Java 11 | standard input | [
"implementation",
"strings"
] | db473ad780a93983667d12b1357c6e2f | The first line contains two integers $$$n$$$ and $$$q$$$ $$$(1 \le n, q \le 10^5)$$$, the length of the string and the number of queries, respectively. The second line contains the string $$$s$$$, consisting of characters "a", "b" and "c". Each of the next $$$q$$$ lines contains an integer $$$i$$$ and character $$$c$$$ $$$(1 \le i \le n)$$$, index and the value of the new item in the string, respectively. It is guaranteed that character's $$$c$$$ value is "a", "b" or "c". | 1,100 | For each query output the minimal number of characters that would have to be replaced so that the string doesn't contain "abc" as a substring. | standard output | |
PASSED | 18c7974a53d8db0f16f3831b9103975f | train_110.jsonl | 1638110100 | Before becoming a successful trader William got a university degree. During his education an interesting situation happened, after which William started to listen to homework assignments much more attentively. What follows is the correct formal description of the homework assignment:You are given a string $$$s$$$ of length $$$n$$$ only consisting of characters "a", "b" and "c". There are $$$q$$$ queries of format ($$$pos, c$$$), meaning replacing the element of string $$$s$$$ at position $$$pos$$$ with character $$$c$$$. After each query you must output the minimal number of characters in the string, which have to be replaced, so that the string doesn't contain string "abc" as a substring. A valid replacement of a character is replacing it with "a", "b" or "c".A string $$$x$$$ is a substring of a string $$$y$$$ if $$$x$$$ can be obtained from $$$y$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. | 256 megabytes |
import java.io.BufferedReader;
//import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Algorithm {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
// BufferedReader bufferedReader = new BufferedReader(new FileReader("C:\\Users\\USER\\AndroidStudioProjects\\MyApplication\\lib\\src\\main\\java\\com\\example\\lib\\inpu"));
String [] lenAndQue= bufferedReader.readLine().split(" ");
String inp = bufferedReader.readLine();
int currentCount=0;
for (int i = 0; i < inp.length(); i++) {
if(i< inp.length()-2 && inp.substring(i, i+3).equals("abc")){
currentCount++;
}
}
char [] inpArray= inp.toCharArray();
StringBuilder stringBuilder= new StringBuilder();
for (int i = 0; i < Integer.valueOf(lenAndQue[1]); i++) {
String [] posAndChar= bufferedReader.readLine().split(" ");
int pos= Integer.valueOf(posAndChar[0]);
char ch= posAndChar[1].charAt(0);
--pos;
if( ch == 'a' ){
if(inpArray[pos] =='a'){
stringBuilder.append(currentCount +"\n");
continue;
}
else if(inpArray[pos] == 'b'){
if(pos!=0 && pos< inpArray.length-1 && inpArray[pos-1] == 'a' && inpArray[pos+1] =='c'){
currentCount--;
}
}else{
if(pos >1 && inpArray[pos-1] == 'b' && inpArray[pos-2] =='a'){
currentCount--;
}
}
inpArray[pos] = 'a';
if( pos< inpArray.length-2 && inpArray[pos+2] == 'c' && inpArray[pos+1] =='b'){
currentCount++;
}
}else if( ch =='b'){
if(inpArray[pos] =='b'){
stringBuilder.append(currentCount +"\n");
continue;
}
else if(inpArray[pos] == 'a'){
if( pos< inpArray.length-2 && inpArray[pos+2] == 'c' && inpArray[pos+1] =='b'){
currentCount--;
}
}else{
if(pos >1 && inpArray[pos-1] == 'b' && inpArray[pos-2] =='a'){
currentCount--;
}
}
inpArray[pos] = 'b';
if(pos!=0 && pos< inpArray.length-1 && inpArray[pos-1] == 'a' && inpArray[pos+1] =='c'){
currentCount++;
}
}else {
if(inpArray[pos] =='c'){
stringBuilder.append(currentCount +"\n");
continue;
}
else if(inpArray[pos] == 'a'){
if( pos< inpArray.length-2 && inpArray[pos+2] == 'c' && inpArray[pos+1] =='b'){
currentCount--;
}
}else{
if(pos!=0 && pos< inpArray.length-1 && inpArray[pos-1] == 'a' && inpArray[pos+1] =='c'){
currentCount--;
}
}
inpArray[pos] = 'c';
if(pos >1 && inpArray[pos-1] == 'b' && inpArray[pos-2] =='a'){
currentCount++;
}
}
stringBuilder.append(currentCount +"\n");
}
System.out.println(stringBuilder);
}
} | Java | ["9 10\nabcabcabc\n1 a\n1 b\n2 c\n3 a\n4 b\n5 c\n8 a\n9 b\n1 c\n4 a"] | 2 seconds | ["3\n2\n2\n2\n1\n2\n1\n1\n1\n0"] | NoteLet's consider the state of the string after each query: $$$s =$$$ "abcabcabc". In this case $$$3$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcaccabb". This string does not contain "abc" as a substring. $$$s =$$$ "bbcabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bccabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bccbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcaabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcabbcabc". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaac". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaac". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "ccabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccaaccaab". In this case the string does not contain "abc" as a substring and no replacements are needed. | Java 11 | standard input | [
"implementation",
"strings"
] | db473ad780a93983667d12b1357c6e2f | The first line contains two integers $$$n$$$ and $$$q$$$ $$$(1 \le n, q \le 10^5)$$$, the length of the string and the number of queries, respectively. The second line contains the string $$$s$$$, consisting of characters "a", "b" and "c". Each of the next $$$q$$$ lines contains an integer $$$i$$$ and character $$$c$$$ $$$(1 \le i \le n)$$$, index and the value of the new item in the string, respectively. It is guaranteed that character's $$$c$$$ value is "a", "b" or "c". | 1,100 | For each query output the minimal number of characters that would have to be replaced so that the string doesn't contain "abc" as a substring. | standard output | |
PASSED | da74597c97212dec09702e679dac7b24 | train_110.jsonl | 1638110100 | Before becoming a successful trader William got a university degree. During his education an interesting situation happened, after which William started to listen to homework assignments much more attentively. What follows is the correct formal description of the homework assignment:You are given a string $$$s$$$ of length $$$n$$$ only consisting of characters "a", "b" and "c". There are $$$q$$$ queries of format ($$$pos, c$$$), meaning replacing the element of string $$$s$$$ at position $$$pos$$$ with character $$$c$$$. After each query you must output the minimal number of characters in the string, which have to be replaced, so that the string doesn't contain string "abc" as a substring. A valid replacement of a character is replacing it with "a", "b" or "c".A string $$$x$$$ is a substring of a string $$$y$$$ if $$$x$$$ can be obtained from $$$y$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. | 256 megabytes | import static java.lang.Math.max;
import static java.lang.Math.min;
import static java.lang.Math.abs;
import java.util.*;
import java.io.*;
import java.math.*;
public class B_William_the_Vigilant {
public static void main(String[] args) {
OutputStream outputStream = System.out;
PrintWriter out = new PrintWriter(outputStream);
FastReader f = new FastReader();
int t = 1;
while(t-- > 0){
solve(f, out);
}
out.close();
}
public static void solve(FastReader f, PrintWriter out) {
int n = f.nextInt();
int q = f.nextInt();
StringBuilder sb = new StringBuilder(f.nextLine());
String repeat = "abc";
int count = countRepetation(sb.toString(), repeat);
while(q-- > 0) {
int ind = f.nextInt()-1;
char ch = f.next().charAt(0);
if(sb.charAt(ind) == ch) {
out.println(count);
continue;
}
int x = countRepetation(sb.substring(max(0, ind-2), min(n, ind+3)), repeat);
sb.setCharAt(ind, ch);
int y = countRepetation(sb.substring(max(0, ind-2), min(n, ind+3)), repeat);
if(y > x) {
count++;
} else if(y < x) {
count--;
}
out.println(count);
}
}
public static int countRepetation(String s, String repeat) {
int n = s.length();
int i = 0;
int count = 0;
while(i+3 <= n) {
if(s.substring(i, i+3).equals(repeat)) {
count++;
i += 3;
} else {
i++;
}
}
return count;
}
public static void sort(int arr[]) {
ArrayList<Integer> al = new ArrayList<>();
for(int i: arr) {
al.add(i);
}
Collections.sort(al);
for(int i = 0; i < arr.length; i++) {
arr[i] = al.get(i);
}
}
public static void allDivisors(int n) {
for(int i = 1; i*i <= n; i++) {
if(n%i == 0) {
System.out.println(i + " ");
if(i != n/i) {
System.out.println(n/i + " ");
}
}
}
}
public static boolean isPrime(int n) {
if(n < 1) return false;
if(n == 2 || n == 3) return true;
if(n % 2 == 0 || n % 3 == 0) return false;
for(int i = 5; i*i <= n; i += 6) {
if(n % i == 0 || n % (i+2) == 0) {
return false;
}
}
return true;
}
public static int gcd(int a, int b) {
int dividend = a > b ? a : b;
int divisor = a < b ? a : b;
while(divisor > 0) {
int reminder = dividend % divisor;
dividend = divisor;
divisor = reminder;
}
return dividend;
}
public static int lcm(int a, int b) {
int lcm = gcd(a, b);
int hcf = (a * b) / lcm;
return hcf;
}
public static String sortString(String inputString) {
char tempArray[] = inputString.toCharArray();
Arrays.sort(tempArray);
return new String(tempArray);
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
float nextFloat() {
return Float.parseFloat(next());
}
boolean nextBoolean() {
return Boolean.parseBoolean(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
int[] nextArray(int n) {
int[] a = new int[n];
for(int i=0; i<n; i++) {
a[i] = nextInt();
}
return a;
}
}
}
/**
Dec Char Dec Char Dec Char Dec Char
--------- --------- --------- ----------
0 NUL (null) 32 SPACE 64 @ 96 `
1 SOH (start of heading) 33 ! 65 A 97 a
2 STX (start of text) 34 " 66 B 98 b
3 ETX (end of text) 35 # 67 C 99 c
4 EOT (end of transmission) 36 $ 68 D 100 d
5 ENQ (enquiry) 37 % 69 E 101 e
6 ACK (acknowledge) 38 & 70 F 102 f
7 BEL (bell) 39 ' 71 G 103 g
8 BS (backspace) 40 ( 72 H 104 h
9 TAB (horizontal tab) 41 ) 73 I 105 i
10 LF (NL line feed, new line) 42 * 74 J 106 j
11 VT (vertical tab) 43 + 75 K 107 k
12 FF (NP form feed, new page) 44 , 76 L 108 l
13 CR (carriage return) 45 - 77 M 109 m
14 SO (shift out) 46 . 78 N 110 n
15 SI (shift in) 47 / 79 O 111 o
16 DLE (data link escape) 48 0 80 P 112 p
17 DC1 (device control 1) 49 1 81 Q 113 q
18 DC2 (device control 2) 50 2 82 R 114 r
19 DC3 (device control 3) 51 3 83 S 115 s
20 DC4 (device control 4) 52 4 84 T 116 t
21 NAK (negative acknowledge) 53 5 85 U 117 u
22 SYN (synchronous idle) 54 6 86 V 118 v
23 ETB (end of trans. block) 55 7 87 W 119 w
24 CAN (cancel) 56 8 88 X 120 x
25 EM (end of medium) 57 9 89 Y 121 y
26 SUB (substitute) 58 : 90 Z 122 z
27 ESC (escape) 59 ; 91 [ 123 {
28 FS (file separator) 60 < 92 \ 124 |
29 GS (group separator) 61 = 93 ] 125 }
30 RS (record separator) 62 > 94 ^ 126 ~
31 US (unit separator) 63 ? 95 _ 127 DEL
*/
| Java | ["9 10\nabcabcabc\n1 a\n1 b\n2 c\n3 a\n4 b\n5 c\n8 a\n9 b\n1 c\n4 a"] | 2 seconds | ["3\n2\n2\n2\n1\n2\n1\n1\n1\n0"] | NoteLet's consider the state of the string after each query: $$$s =$$$ "abcabcabc". In this case $$$3$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcaccabb". This string does not contain "abc" as a substring. $$$s =$$$ "bbcabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bccabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bccbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcaabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcabbcabc". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaac". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaac". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "ccabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccaaccaab". In this case the string does not contain "abc" as a substring and no replacements are needed. | Java 11 | standard input | [
"implementation",
"strings"
] | db473ad780a93983667d12b1357c6e2f | The first line contains two integers $$$n$$$ and $$$q$$$ $$$(1 \le n, q \le 10^5)$$$, the length of the string and the number of queries, respectively. The second line contains the string $$$s$$$, consisting of characters "a", "b" and "c". Each of the next $$$q$$$ lines contains an integer $$$i$$$ and character $$$c$$$ $$$(1 \le i \le n)$$$, index and the value of the new item in the string, respectively. It is guaranteed that character's $$$c$$$ value is "a", "b" or "c". | 1,100 | For each query output the minimal number of characters that would have to be replaced so that the string doesn't contain "abc" as a substring. | standard output | |
PASSED | a3750f98fbbe8d891fb7bf1d1f1716ef | train_110.jsonl | 1638110100 | Before becoming a successful trader William got a university degree. During his education an interesting situation happened, after which William started to listen to homework assignments much more attentively. What follows is the correct formal description of the homework assignment:You are given a string $$$s$$$ of length $$$n$$$ only consisting of characters "a", "b" and "c". There are $$$q$$$ queries of format ($$$pos, c$$$), meaning replacing the element of string $$$s$$$ at position $$$pos$$$ with character $$$c$$$. After each query you must output the minimal number of characters in the string, which have to be replaced, so that the string doesn't contain string "abc" as a substring. A valid replacement of a character is replacing it with "a", "b" or "c".A string $$$x$$$ is a substring of a string $$$y$$$ if $$$x$$$ can be obtained from $$$y$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. | 256 megabytes | //package com.company;//comment this line while submitting the answer otherwise you'll get WA;
//not all import are used to solve every problem .. but these are the most important one
import java.io.DataInputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Objects;
import java.util.Random;
public class WilliamTheVigilant {
@SuppressWarnings("FieldCanBeLocal")
private static Reader in;
private static PrintWriter out;
private static void solve() throws IOException{
//Your Code Goes Here;
int n= in.nextInt();
int q= in.nextInt();
char[] s= in.next().toCharArray();
int count=0;
for(int i=0; i<n-2; i++){
if(s[i]=='a' && s[i+1]=='b' && s[i+2]=='c'){
count++;
}
}
while(q--!=0) {
int i = in.nextInt() - 1;
char c = in.nc();
if (s[i] == c) {
out.println(count);
} else {
int change = 0;
for (int j = Math.max(0, i - 2); j <= Math.min(n - 3, i); j++) {
if (s[j] == 'a' && s[j + 1] == 'b' && s[j + 2] == 'c') {
change--;
}
}
s[i] = c;
for (int j = Math.max(0, i - 2); j <= Math.min(n - 3, i); j++) {
if (s[j] == 'a' && s[j + 1] == 'b' && s[j + 2] == 'c') {
change++;
}
}
count += change;
out.println(count);
}
}
}
public static void main(String[] args) throws IOException {
//FastScaner fs = new FastScaner();//There is a class below .. uncomment that class to instantiate an object from that class;
//int t = fs.nextInt();//uncomment this line when you're using the fastscaner class;
in = new Reader();
out = new PrintWriter(new OutputStreamWriter(System.out));
int T = 1;
while(T-->0){
solve();
}
out.flush();
in.close();
out.close();
}
static final Random random = new Random();
static final int mod = 1_000_000_007;
//Taken From "Second Thread"
static void ruffleSort(int[] a){
int n = a.length;//shuffles, then sort;
for(int i=0; i<n; i++){
int oi = random.nextInt(n), temp = a[oi];
a[oi] = a[i]; a[i] = temp;
}
sort(a);
}
public static boolean primeFinder(int n){
int m = 0;
int flag = 0;
m = n / 2;
if(n == 0 ||n == 1){
return false;
}
else{
for(int i=2;i<=m;i++){
if(n % i == 0){
return false;
}
}
return true;
}
}
private static boolean[] sieveOfEratosthenes(long n) {
boolean prime[] = new boolean[(int)n + 1];
for (int i = 0; i <= n; i++) {
prime[i] = true;
}
for (long p = 2; p * p <= n; p++) {
if (prime[(int)p] == true) {
for (long i = p * p; i <= n; i += p)
prime[(int)i] = false;
}
}
return prime;
}
private static long add(long a, long b){
return (a + b) % mod;
}
private static int gcd(int a, int b) {
if (a == 0 || b == 0)
return 0;
while (b != 0) {
int tmp;
tmp = a % b;
a = b;
b = tmp;
}
return a;
}
private static long lcm(long a, long b){
return (a / gcd((int) a, (int) b) * b);
}
private static void sort(int[] a){
ArrayList<Integer> l = new ArrayList<>();
for(int i:a) l.add(i);
Collections.sort(l);
for(int i=0;i<a.length;i++)a[i] = l.get(i);
}
public static int[][] prefixsum( int n , int m , int arr[][] ){
int prefixsum[][] = new int[n+1][m+1];
for( int i = 1 ;i <= n ;i++) {
for( int j = 1 ; j<= m ; j++) {
int toadd = 0;
if( arr[i-1][j-1] == 1) {
toadd = 1;
}
prefixsum[i][j] = toadd + prefixsum[i][j-1] + prefixsum[i-1][j] - prefixsum[i-1][j-1];
}
}
return prefixsum;
}
//call this method when you want to read an integer array;
private static int[] readArray(int len) throws IOException{
int[] a = new int[len];
for(int i=0;i<len;i++)a[i] = in.nextInt();
return a;
}
//call this method when you want to read an Long array;
private static long[] readLongArray(int len) throws IOException{
long[] a = new long[len];
for(int i=0;i<len;i++) a[i] = in.nextLong();
return a;
}
//call this method to print the integer array;
private static void printArray(int[] array){
for(int now : array) out.print(now);out.print(' ');out.close();
}
//call this method to print the long array;
private static void printLongArray(long[] array){
for(long now:array) out.print(now); out.print(' '); out.close();
}
/*Another way of Reading input...collected from a online blog
from here => https://codeforces.com/contest/1625/submission/144334744;*/
static class Reader {
private static final int BUFFER_SIZE = 1 << 16;
private final DataInputStream din;
private final byte[] buffer;
private int bufferPointer, bytesRead;
Reader() {//Constructor;
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException {//To take user input for String values;
final byte[] buf = new byte[1024]; // line length
int cnt = 0, c;
while ((c = read()) != -1) {
if (c == '\n') {
break;
}
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public int nextSign() throws IOException {//For taking the signs like plus or minus ...
byte c = read();
while ('+' != c && '-' != c) {
c = read();
}
return '+' == c ? 0 : 1;
}
private static boolean isSpaceChar(int c) {
return !(c >= 33 && c <= 126);
}
public int skip() throws IOException {
int b;
// noinspection ALL
while ((b = read()) != -1 && isSpaceChar(b)) {
;
}
return b;
}
public char nc() throws IOException {
return (char) skip();
}
public String next() throws IOException {
int b = skip();
final StringBuilder sb = new StringBuilder();
while (!isSpaceChar(b)) { // when nextLine, (isSpaceChar(b) && b != ' ')
sb.appendCodePoint(b);
b = read();
}
return sb.toString();
}
public int nextInt() throws IOException {
int ret = 0;
byte c = read();
while (c <= ' ') {
c = read();
}
final boolean neg = c == '-';
if (neg) {
c = read();
}
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg) {
return -ret;
}
return ret;
}
public long nextLong() throws IOException {
long ret = 0;
byte c = read();
while (c <= ' ') {
c = read();
}
final boolean neg = c == '-';
if (neg) {
c = read();
}
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (neg) {
return -ret;
}
return ret;
}
public double nextDouble() throws IOException {
double ret = 0, div = 1;
byte c = read();
while (c <= ' ') {
c = read();
}
final boolean neg = c == '-';
if (neg) {
c = read();
}
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (c == '.') {
while ((c = read()) >= '0' && c <= '9') {
ret += (c - '0') / (div *= 10);
}
}
if (neg) {
return -ret;
}
return ret;
}
private void fillBuffer() throws IOException {
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1) {
buffer[0] = -1;
}
}
private byte read() throws IOException {
if (bufferPointer == bytesRead) {
fillBuffer();
}
return buffer[bufferPointer++];
}
public void close() throws IOException {
din.close();
}
}
}
| Java | ["9 10\nabcabcabc\n1 a\n1 b\n2 c\n3 a\n4 b\n5 c\n8 a\n9 b\n1 c\n4 a"] | 2 seconds | ["3\n2\n2\n2\n1\n2\n1\n1\n1\n0"] | NoteLet's consider the state of the string after each query: $$$s =$$$ "abcabcabc". In this case $$$3$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcaccabb". This string does not contain "abc" as a substring. $$$s =$$$ "bbcabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bccabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bccbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcaabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcabbcabc". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaac". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaac". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "ccabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccaaccaab". In this case the string does not contain "abc" as a substring and no replacements are needed. | Java 11 | standard input | [
"implementation",
"strings"
] | db473ad780a93983667d12b1357c6e2f | The first line contains two integers $$$n$$$ and $$$q$$$ $$$(1 \le n, q \le 10^5)$$$, the length of the string and the number of queries, respectively. The second line contains the string $$$s$$$, consisting of characters "a", "b" and "c". Each of the next $$$q$$$ lines contains an integer $$$i$$$ and character $$$c$$$ $$$(1 \le i \le n)$$$, index and the value of the new item in the string, respectively. It is guaranteed that character's $$$c$$$ value is "a", "b" or "c". | 1,100 | For each query output the minimal number of characters that would have to be replaced so that the string doesn't contain "abc" as a substring. | standard output | |
PASSED | ac4a6b81ae643e917f2d16c390ab58d6 | train_110.jsonl | 1638110100 | Before becoming a successful trader William got a university degree. During his education an interesting situation happened, after which William started to listen to homework assignments much more attentively. What follows is the correct formal description of the homework assignment:You are given a string $$$s$$$ of length $$$n$$$ only consisting of characters "a", "b" and "c". There are $$$q$$$ queries of format ($$$pos, c$$$), meaning replacing the element of string $$$s$$$ at position $$$pos$$$ with character $$$c$$$. After each query you must output the minimal number of characters in the string, which have to be replaced, so that the string doesn't contain string "abc" as a substring. A valid replacement of a character is replacing it with "a", "b" or "c".A string $$$x$$$ is a substring of a string $$$y$$$ if $$$x$$$ can be obtained from $$$y$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. | 256 megabytes | /**
* @author -- Sourav Joshi
*/
import java.io.*;
import java.util.*;
import static java.lang.Math.*;
public class Solution {
/*----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
static FastScanner s = new FastScanner();
static FastWriter out = new FastWriter();
final static int mod = (int)1e9 + 7;
final static int INT_MAX = Integer.MAX_VALUE;
final static int INT_MIN = Integer.MIN_VALUE;
final static long LONG_MAX = Long.MAX_VALUE;
final static long LONG_MIN = Long.MIN_VALUE;
final static double DOUBLE_MAX = Double.MAX_VALUE;
final static double DOUBLE_MIN = Double.MIN_VALUE;
final static float FLOAT_MAX = Float.MAX_VALUE;
final static float FLOAT_MIN = Float.MIN_VALUE;
/*----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
static class FastScanner{BufferedReader br;StringTokenizer st;
public FastScanner() {br = new BufferedReader(new InputStreamReader(System.in));}
String next(){while (st == null || !st.hasMoreElements()){try{st = new StringTokenizer(br.readLine());}catch (IOException e){e.printStackTrace();}}return st.nextToken();}
int nextInt(){return Integer.parseInt(next());}
long nextLong(){return Long.parseLong(next());}
double nextDouble(){return Double.parseDouble(next());}
List<Integer> readIntList(int n){List<Integer> arr = new ArrayList<>(); for(int i = 0; i < n; i++) arr.add(s.nextInt()); return arr;}
List<Long> readLongList(int n){List<Long> arr = new ArrayList<>(); for(int i = 0; i < n; i++) arr.add(s.nextLong()); return arr;}
int[] readIntArr(int n){int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = s.nextInt(); return arr;}
long[] readLongArr(int n){long[] arr = new long[n]; for (int i = 0; i < n; i++) arr[i] = s.nextLong(); return arr;}
String nextLine(){String str = "";try{str = br.readLine();}catch (IOException e){e.printStackTrace();}return str;}}
static class FastWriter{private final BufferedWriter bw;public FastWriter() {this.bw = new BufferedWriter(new OutputStreamWriter(System.out));}
public void print(Object object) throws IOException{bw.append(""+ object);}
public void println(Object object) throws IOException{print(object);bw.append("\n");}
public void debug(int object[]) throws IOException{bw.append("["); for(int i = 0; i < object.length; i++){if(i != object.length-1){print(object[i]+", ");}else{print(object[i]);}}bw.append("]\n");}
public void debug(long object[]) throws IOException{bw.append("["); for(int i = 0; i < object.length; i++){if(i != object.length-1){print(object[i]+", ");}else{print(object[i]);}}bw.append("]\n");}
public void close() throws IOException{bw.close();}}
/*----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
public static ArrayList<Integer> seive(int n){ArrayList<Integer> list = new ArrayList<>();int arr[] = new int[n+1];for(int i = 2; i <= n; i++) {if(arr[i] == 1) {continue;}else {list.add(i);for(int j = i*i; j <= n; j = j + i) {arr[j] = 1;}}}return list;}
public static long gcd(long a, long b){if(a > b) {a = (a+b)-(b=a);}if(a == 0L){return b;}return gcd(b%a, a);}
public static void swap(int[] arr, int i, int j) {arr[i] = arr[i] ^ arr[j]; arr[j] = arr[j] ^ arr[i]; arr[i] = arr[i] ^ arr[j];}
public static boolean isPrime(long n){if(n < 2){return false;}if(n == 2 || n == 3){return true;}if(n%2 == 0 || n%3 == 0){return false;}long sqrtN = (long)Math.sqrt(n)+1;for(long i = 6L; i <= sqrtN; i += 6) {if(n%(i-1) == 0 || n%(i+1) == 0) return false;}return true;}
public static long mod_add(long a, long b){ return (a%mod + b%mod)%mod;}
public static long mod_sub(long a, long b){ return (a%mod - b%mod + mod)%mod;}
public static long mod_mul(long a, long b){ return (a%mod * b%mod)%mod;}
public static long modInv(long a, long b){ return expo(a, b-2)%b;}
public static long mod_div(long a, long b){return mod_mul(a, modInv(b, mod));}
public static long expo (long a, long n){if(n == 0){return 1;}long recAns = expo(mod_mul(a,a), n/2);if(n % 2 == 0){return recAns;}else{return mod_mul(a, recAns);}}
/*----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
// Pair class
public static class Pair<X extends Comparable<X>,Y extends Comparable<Y>> implements Comparable<Pair<X, Y>>{
X first;
Y second;
public Pair(X first, Y second){
this.first = first;
this.second = second;
}
public String toString(){
return "( " + first+" , "+second+" )";
}
@Override
public int compareTo(Pair<X, Y> o) {
int t = first.compareTo(o.first);
if(t == 0) return -1*second.compareTo(o.second);
return t;
}
}
/*----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
// Code begins
public static void solve() throws IOException {
int n = s.nextInt();
int q = s.nextInt();
char arr[] = s.nextLine().toCharArray();
Set<Integer> abc = new HashSet<>();
for(int i = 0; i < n-2;){
if(arr[i] == 'a' && arr[i+1] == 'b' && arr[i+2] == 'c'){
abc.add(i);
i += 3;
}else{
i++;
}
}
for(int query = 0; query < q; query++){
String input[] = s.nextLine().split(" ");
int pos = Integer.parseInt(input[0])-1;
char ch = input[1].charAt(0);
// System.out.println(pos+" : pos");
// System.out.println(ch+" : ch");
if(abc.contains(pos) || abc.contains(pos-1) || abc.contains(pos-2)){
if(arr[pos] == ch){
out.println(abc.size());
}else{
if(abc.contains(pos)){
abc.remove(pos);
}
if(abc.contains(pos-1)){
abc.remove(pos-1);
}
if(abc.contains(pos-2)){
abc.remove(pos-2);
}
arr[pos] = ch;
if(pos+2 < n){
if(arr[pos] =='a' && arr[pos+1] =='b' && arr[pos+2] == 'c'){
abc.add(pos);
}
}
if(pos - 1 >= 0 && pos + 1 < n){
if(arr[pos-1] =='a' && arr[pos] =='b' && arr[pos+1] == 'c'){
abc.add(pos-1);
}
}
if(pos-2 >= 0){
if(arr[pos-2] =='a' && arr[pos-1] =='b' && arr[pos] == 'c'){
abc.add(pos-2);
}
}
out.println(abc.size());
}
}else{
arr[pos] = ch;
if(pos+2 < n){
if(arr[pos] =='a' && arr[pos+1] =='b' && arr[pos+2] == 'c'){
abc.add(pos);
}
}
if(pos - 1 >= 0 && pos + 1 < n){
if(arr[pos-1] =='a' && arr[pos] =='b' && arr[pos+1] == 'c'){
abc.add(pos-1);
}
}
if(pos-2 >= 0){
if(arr[pos-2] =='a' && arr[pos-1] =='b' && arr[pos] == 'c'){
abc.add(pos-2);
}
}
out.println(abc.size());
}
}
}
/*----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
public static void main(String[] args) throws IOException {
int test = 1;
for(int tt = 1; tt <= test; tt++) {
solve();
}
out.close();
}
}
| Java | ["9 10\nabcabcabc\n1 a\n1 b\n2 c\n3 a\n4 b\n5 c\n8 a\n9 b\n1 c\n4 a"] | 2 seconds | ["3\n2\n2\n2\n1\n2\n1\n1\n1\n0"] | NoteLet's consider the state of the string after each query: $$$s =$$$ "abcabcabc". In this case $$$3$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcaccabb". This string does not contain "abc" as a substring. $$$s =$$$ "bbcabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bccabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bccbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcaabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcabbcabc". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaac". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaac". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "ccabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccaaccaab". In this case the string does not contain "abc" as a substring and no replacements are needed. | Java 11 | standard input | [
"implementation",
"strings"
] | db473ad780a93983667d12b1357c6e2f | The first line contains two integers $$$n$$$ and $$$q$$$ $$$(1 \le n, q \le 10^5)$$$, the length of the string and the number of queries, respectively. The second line contains the string $$$s$$$, consisting of characters "a", "b" and "c". Each of the next $$$q$$$ lines contains an integer $$$i$$$ and character $$$c$$$ $$$(1 \le i \le n)$$$, index and the value of the new item in the string, respectively. It is guaranteed that character's $$$c$$$ value is "a", "b" or "c". | 1,100 | For each query output the minimal number of characters that would have to be replaced so that the string doesn't contain "abc" as a substring. | standard output | |
PASSED | 83c3dc06b01a1ae1b1ce8ca6e93d2b8a | train_110.jsonl | 1638110100 | Before becoming a successful trader William got a university degree. During his education an interesting situation happened, after which William started to listen to homework assignments much more attentively. What follows is the correct formal description of the homework assignment:You are given a string $$$s$$$ of length $$$n$$$ only consisting of characters "a", "b" and "c". There are $$$q$$$ queries of format ($$$pos, c$$$), meaning replacing the element of string $$$s$$$ at position $$$pos$$$ with character $$$c$$$. After each query you must output the minimal number of characters in the string, which have to be replaced, so that the string doesn't contain string "abc" as a substring. A valid replacement of a character is replacing it with "a", "b" or "c".A string $$$x$$$ is a substring of a string $$$y$$$ if $$$x$$$ can be obtained from $$$y$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. | 256 megabytes | import java.util.*;
public class William_The_Vigilant {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int q = sc.nextInt();
String str = "abc";
Pair arr[] = new Pair[q];
sc.nextLine();
String s = sc.nextLine();
for(int i=0;i<q;i++) {
int a = sc.nextInt();
char c = sc.next().charAt(0);
arr[i] = new Pair(a,c);
}
int count = 0;
for(int i=0;i<n-2;i++) {
if(s.substring(i, i+3).equals(str))
count++;
}
char[] ar = s.toCharArray();
for(Pair p:arr) {
int index = p.ind-1;
char c = p.ch;
if(c==ar[index]) {
System.out.println(count);
continue;
}
int rep=0;
int i= index-2,j=index;
while(rep<3) {
rep++;
if(i>=0&&j+1<=s.length()){
String temp = "";
for(int l=0;l<3;l++)
temp += ar[i+l];
if(temp.equals(str))
count--;
}
i++;j++;
}
ar[index] = c;;
rep=0;
i= index-2;j=index;
while(rep<3) {
rep++;
if(i>=0&&j+1<=s.length()){
String temp = "";
for(int l=0;l<3;l++)
temp += ar[i+l];
if(temp.equals(str))
count++;
}
i++;j++;
}
System.out.println(count);
}
}
static class Pair{
int ind;
char ch;
Pair(int i,char x){
this.ind = i;
this.ch = x;
}
}
}
| Java | ["9 10\nabcabcabc\n1 a\n1 b\n2 c\n3 a\n4 b\n5 c\n8 a\n9 b\n1 c\n4 a"] | 2 seconds | ["3\n2\n2\n2\n1\n2\n1\n1\n1\n0"] | NoteLet's consider the state of the string after each query: $$$s =$$$ "abcabcabc". In this case $$$3$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcaccabb". This string does not contain "abc" as a substring. $$$s =$$$ "bbcabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bccabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bccbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcaabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcabbcabc". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaac". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaac". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "ccabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccaaccaab". In this case the string does not contain "abc" as a substring and no replacements are needed. | Java 11 | standard input | [
"implementation",
"strings"
] | db473ad780a93983667d12b1357c6e2f | The first line contains two integers $$$n$$$ and $$$q$$$ $$$(1 \le n, q \le 10^5)$$$, the length of the string and the number of queries, respectively. The second line contains the string $$$s$$$, consisting of characters "a", "b" and "c". Each of the next $$$q$$$ lines contains an integer $$$i$$$ and character $$$c$$$ $$$(1 \le i \le n)$$$, index and the value of the new item in the string, respectively. It is guaranteed that character's $$$c$$$ value is "a", "b" or "c". | 1,100 | For each query output the minimal number of characters that would have to be replaced so that the string doesn't contain "abc" as a substring. | standard output | |
PASSED | c0ac7a26785e88793259d2f96e6ed961 | train_110.jsonl | 1638110100 | Before becoming a successful trader William got a university degree. During his education an interesting situation happened, after which William started to listen to homework assignments much more attentively. What follows is the correct formal description of the homework assignment:You are given a string $$$s$$$ of length $$$n$$$ only consisting of characters "a", "b" and "c". There are $$$q$$$ queries of format ($$$pos, c$$$), meaning replacing the element of string $$$s$$$ at position $$$pos$$$ with character $$$c$$$. After each query you must output the minimal number of characters in the string, which have to be replaced, so that the string doesn't contain string "abc" as a substring. A valid replacement of a character is replacing it with "a", "b" or "c".A string $$$x$$$ is a substring of a string $$$y$$$ if $$$x$$$ can be obtained from $$$y$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. | 256 megabytes | import java.util.*;
public class HelloWorld{
public static void main(String []args){
Scanner sc=new Scanner(System.in);
// while(t-->0){
int n=sc.nextInt();
int q=sc.nextInt();
String str=sc.next();
int ans=0;
StringBuilder sb=new StringBuilder(str);
for(int i=0;i<sb.length();i++){
char ch1=sb.charAt(i);
if(ch1=='a'&& i+1<sb.length() && sb.charAt(i+1)=='b'&&i+2<sb.length() && sb.charAt(i+2)=='c'){
ans++;
}
}
int first=0;
while(q-->0){
int pos=sc.nextInt();
pos=pos-1;
char currch=sc.next().charAt(0);
char prevch=sb.charAt(pos);
sb.setCharAt(pos,currch);
if(prevch==currch){
System.out.println(ans);
}else{
if(prevch=='b'){
if(pos-1>=0 && sb.charAt(pos-1)=='a' && pos+1<sb.length()&&sb.charAt(pos+1)=='c'){
ans--;
}
}else if(prevch=='a'){
if(pos+1<sb.length() && sb.charAt(pos+1)=='b' && pos+2<sb.length()&&sb.charAt(pos+2)=='c'){
ans--;
}
}else if(prevch=='c'){
if(pos-2>=0&&sb.charAt(pos-2)=='a'&&pos-1>=0 && sb.charAt(pos-1)=='b'){
ans--;
}
}
if(currch=='b'){
if(pos-1>=0 && sb.charAt(pos-1)=='a' && pos+1<sb.length()&&sb.charAt(pos+1)=='c'){
ans++;
}
}else if(currch=='a'){
if(pos+1<sb.length() && sb.charAt(pos+1)=='b' && pos+2<sb.length()&&sb.charAt(pos+2)=='c'){
ans++;
}
}else if(currch=='c'){
if(pos-2>=0&&sb.charAt(pos-2)=='a'&&pos-1>=0 && sb.charAt(pos-1)=='b'){
ans++;
}
}
System.out.println(ans);
}
// System.out.println(currch +" "+prevch);
}
// }
}
} | Java | ["9 10\nabcabcabc\n1 a\n1 b\n2 c\n3 a\n4 b\n5 c\n8 a\n9 b\n1 c\n4 a"] | 2 seconds | ["3\n2\n2\n2\n1\n2\n1\n1\n1\n0"] | NoteLet's consider the state of the string after each query: $$$s =$$$ "abcabcabc". In this case $$$3$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcaccabb". This string does not contain "abc" as a substring. $$$s =$$$ "bbcabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bccabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bccbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcaabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcabbcabc". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaac". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaac". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "ccabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccaaccaab". In this case the string does not contain "abc" as a substring and no replacements are needed. | Java 11 | standard input | [
"implementation",
"strings"
] | db473ad780a93983667d12b1357c6e2f | The first line contains two integers $$$n$$$ and $$$q$$$ $$$(1 \le n, q \le 10^5)$$$, the length of the string and the number of queries, respectively. The second line contains the string $$$s$$$, consisting of characters "a", "b" and "c". Each of the next $$$q$$$ lines contains an integer $$$i$$$ and character $$$c$$$ $$$(1 \le i \le n)$$$, index and the value of the new item in the string, respectively. It is guaranteed that character's $$$c$$$ value is "a", "b" or "c". | 1,100 | For each query output the minimal number of characters that would have to be replaced so that the string doesn't contain "abc" as a substring. | standard output | |
PASSED | 9eacb7d8c51d98bd4dc75acf855b47f1 | train_110.jsonl | 1638110100 | Before becoming a successful trader William got a university degree. During his education an interesting situation happened, after which William started to listen to homework assignments much more attentively. What follows is the correct formal description of the homework assignment:You are given a string $$$s$$$ of length $$$n$$$ only consisting of characters "a", "b" and "c". There are $$$q$$$ queries of format ($$$pos, c$$$), meaning replacing the element of string $$$s$$$ at position $$$pos$$$ with character $$$c$$$. After each query you must output the minimal number of characters in the string, which have to be replaced, so that the string doesn't contain string "abc" as a substring. A valid replacement of a character is replacing it with "a", "b" or "c".A string $$$x$$$ is a substring of a string $$$y$$$ if $$$x$$$ can be obtained from $$$y$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. | 256 megabytes | import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
public class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt(), m = sc.nextInt();
String s = sc.next();
String sub = "abc";
int cnt =0,ind =0;
while(true){
ind = s.indexOf(sub,ind);
if(ind!=-1){
ind+=3;
cnt++;
}else break;
}
char [] cc = s.toCharArray();
while(m!=0){
m--;
int pos = sc.nextInt();
char d = sc.next().charAt(0);
char x = cc[pos-1];pos--;
if(x!=d){
if(x=='a' && pos+2<n && cc[pos+1]=='b' && cc[pos+2]=='c')cnt--;
if(x=='b' && pos+1<n && pos-1>=0 && cc[pos-1]=='a' && cc[pos+1]=='c')cnt--;
if(x=='c' && pos-2>=0 && cc[pos-2]=='a' && cc[pos-1]=='b')cnt--;
if(d=='a' && pos+2<n && cc[pos+1]=='b' && cc[pos+2]=='c')cnt++;
if(d=='b' && pos+1<n && pos-1>=0 && cc[pos-1]=='a' && cc[pos+1]=='c')cnt++;
if(d=='c' && pos-2>=0 && cc[pos-2]=='a' && cc[pos-1]=='b')cnt++;
cc[pos] = d;
}System.out.println(cnt);
}
}
}
| Java | ["9 10\nabcabcabc\n1 a\n1 b\n2 c\n3 a\n4 b\n5 c\n8 a\n9 b\n1 c\n4 a"] | 2 seconds | ["3\n2\n2\n2\n1\n2\n1\n1\n1\n0"] | NoteLet's consider the state of the string after each query: $$$s =$$$ "abcabcabc". In this case $$$3$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcaccabb". This string does not contain "abc" as a substring. $$$s =$$$ "bbcabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bccabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bccbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcaabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcabbcabc". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaac". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaac". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "ccabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccaaccaab". In this case the string does not contain "abc" as a substring and no replacements are needed. | Java 11 | standard input | [
"implementation",
"strings"
] | db473ad780a93983667d12b1357c6e2f | The first line contains two integers $$$n$$$ and $$$q$$$ $$$(1 \le n, q \le 10^5)$$$, the length of the string and the number of queries, respectively. The second line contains the string $$$s$$$, consisting of characters "a", "b" and "c". Each of the next $$$q$$$ lines contains an integer $$$i$$$ and character $$$c$$$ $$$(1 \le i \le n)$$$, index and the value of the new item in the string, respectively. It is guaranteed that character's $$$c$$$ value is "a", "b" or "c". | 1,100 | For each query output the minimal number of characters that would have to be replaced so that the string doesn't contain "abc" as a substring. | standard output | |
PASSED | 25fdb3fbc6b02566a6df18e86f885af4 | train_110.jsonl | 1638110100 | Before becoming a successful trader William got a university degree. During his education an interesting situation happened, after which William started to listen to homework assignments much more attentively. What follows is the correct formal description of the homework assignment:You are given a string $$$s$$$ of length $$$n$$$ only consisting of characters "a", "b" and "c". There are $$$q$$$ queries of format ($$$pos, c$$$), meaning replacing the element of string $$$s$$$ at position $$$pos$$$ with character $$$c$$$. After each query you must output the minimal number of characters in the string, which have to be replaced, so that the string doesn't contain string "abc" as a substring. A valid replacement of a character is replacing it with "a", "b" or "c".A string $$$x$$$ is a substring of a string $$$y$$$ if $$$x$$$ can be obtained from $$$y$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.text.DecimalFormat;
import java.util.*;
public class Codeforces {
static int mod= 1000000007;
static int ans=0;
public static void main(String[] args) throws Exception {
PrintWriter out=new PrintWriter(System.out);
FastScanner fs=new FastScanner();
int t=1;
outer:while(t-->0) {
int n=fs.nextInt(), q=fs.nextInt();
char arr[]=fs.next().toCharArray();
ans=0;
find(arr);
while(q-->0) {
int pos=fs.nextInt();
char c=fs.next().toCharArray()[0];
query(arr,pos-1,c);
out.println(ans);
}
}
out.close();
}
static void query(char arr[],int pos,char c) {
if(arr[pos]==c) {
return ;
}
if((pos-2>=0&&pos-1>=0&&arr[pos-2]=='a'&&arr[pos-1]=='b'&&arr[pos]=='c')||
(pos-1>=0&&pos+1<arr.length&&arr[pos-1]=='a'&&arr[pos]=='b'&&arr[pos+1]=='c')||
(pos+1<arr.length&&pos+2<arr.length&&arr[pos]=='a'&&arr[pos+1]=='b'&&arr[pos+2]=='c')) {
ans--;
arr[pos]=c;
}
arr[pos]=c;
if((pos-2>=0&&pos-1>=0&&arr[pos-2]=='a'&&arr[pos-1]=='b'&&arr[pos]=='c')||
(pos-1>=0&&pos+1<arr.length&&arr[pos-1]=='a'&&arr[pos]=='b'&&arr[pos+1]=='c')||
(pos+1<arr.length&&pos+2<arr.length&&arr[pos]=='a'&&arr[pos+1]=='b'&&arr[pos+2]=='c')) {
ans++;
}
}
static void find(char arr[]) {
int i=0;
while(i<arr.length-2) {
if(arr[i]=='a'&&arr[i+1]=='b'&&arr[i+2]=='c') {
ans++;
i+=3;
}
else i++;
}
}
static long pow(long a,long b) {
if(b<=0) return 1;
long res=1;
while(b!=0) {
if((b&1)!=0) {
res*=a;
res%=mod;
}
a*=a;
a%=mod;
b=b>>1;
}
return res;
}
static int gcd(int a,int b) {
if(b==0) return a;
return gcd(b,a%b);
}
static long nck(int n,int k) {
if(k>n) return 0;
long res=1;
res*=fact(n);
res%=mod;
res*=modInv(fact(k));
res%=mod;
res*=modInv(fact(n-k));
res%=mod;
return res;
}
static long fact(long n) {
// return fact[(int)n];
long res=1;
for(int i=2;i<=n;i++) {
res*=i;
res%=mod;
}
return res;
}
static long modInv(long n) {
return pow(n,mod-2);
}
static void sort(int[] a) {
//suffle
int n=a.length;
Random r=new Random();
for (int i=0; i<a.length; i++) {
int oi=r.nextInt(n);
int temp=a[i];
a[i]=a[oi];
a[oi]=temp;
}
//then sort
Arrays.sort(a);
}
// Use this to input code since it is faster than a Scanner
static class FastScanner {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long[] lreadArray(int n) {
long a[]=new long[n];
for(int i=0;i<n;i++) a[i]=nextLong();
return a;
}
int[] readArray(int n) {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
}
} | Java | ["9 10\nabcabcabc\n1 a\n1 b\n2 c\n3 a\n4 b\n5 c\n8 a\n9 b\n1 c\n4 a"] | 2 seconds | ["3\n2\n2\n2\n1\n2\n1\n1\n1\n0"] | NoteLet's consider the state of the string after each query: $$$s =$$$ "abcabcabc". In this case $$$3$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcaccabb". This string does not contain "abc" as a substring. $$$s =$$$ "bbcabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bccabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bccbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcaabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcabbcabc". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaac". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaac". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "ccabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccaaccaab". In this case the string does not contain "abc" as a substring and no replacements are needed. | Java 11 | standard input | [
"implementation",
"strings"
] | db473ad780a93983667d12b1357c6e2f | The first line contains two integers $$$n$$$ and $$$q$$$ $$$(1 \le n, q \le 10^5)$$$, the length of the string and the number of queries, respectively. The second line contains the string $$$s$$$, consisting of characters "a", "b" and "c". Each of the next $$$q$$$ lines contains an integer $$$i$$$ and character $$$c$$$ $$$(1 \le i \le n)$$$, index and the value of the new item in the string, respectively. It is guaranteed that character's $$$c$$$ value is "a", "b" or "c". | 1,100 | For each query output the minimal number of characters that would have to be replaced so that the string doesn't contain "abc" as a substring. | standard output | |
PASSED | 2a3125a4286dd7488cfb9057d9d48088 | train_110.jsonl | 1638110100 | Before becoming a successful trader William got a university degree. During his education an interesting situation happened, after which William started to listen to homework assignments much more attentively. What follows is the correct formal description of the homework assignment:You are given a string $$$s$$$ of length $$$n$$$ only consisting of characters "a", "b" and "c". There are $$$q$$$ queries of format ($$$pos, c$$$), meaning replacing the element of string $$$s$$$ at position $$$pos$$$ with character $$$c$$$. After each query you must output the minimal number of characters in the string, which have to be replaced, so that the string doesn't contain string "abc" as a substring. A valid replacement of a character is replacing it with "a", "b" or "c".A string $$$x$$$ is a substring of a string $$$y$$$ if $$$x$$$ can be obtained from $$$y$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.util.HashSet;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author saikat021
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
TaskB solver = new TaskB();
solver.solve(1, in, out);
out.close();
}
static class TaskB {
public void solve(int testNumber, InputReader in, OutputWriter out) {
int n = in.nextInt(), q = in.nextInt();
StringBuilder s = new StringBuilder(in.readLine().trim());
HashSet<Integer> set = new HashSet<>();
for (int i = 0; i < n; i += 1) {
if (s.charAt(i) == 'a' && i + 1 < n && s.charAt(i + 1) == 'b' && i + 2 < n && s.charAt(i + 2) == 'c') {
set.add(i);
}
}
while (q-- > 0) {
int pos = in.nextInt() - 1;
char ch = in.nextCharacter();
if (s.charAt(pos) == ch) {
out.println(set.size());
} else {
if (s.charAt(pos) == 'a' && set.contains(pos)) {
set.remove(pos);
} else if (s.charAt(pos) == 'b' && set.contains(pos - 1)) {
set.remove(pos - 1);
} else if (s.charAt(pos) == 'c' && set.contains(pos - 2)) {
set.remove(pos - 2);
}
if (ch == 'a' && pos + 1 < n && s.charAt(pos + 1) == 'b' && pos + 2 < n && s.charAt(pos + 2) == 'c') {
set.add(pos);
} else if (ch == 'b' && pos - 1 >= 0 && s.charAt(pos - 1) == 'a' && pos + 1 < n && s.charAt(pos + 1) == 'c') {
set.add(pos - 1);
} else if (ch == 'c' && pos - 1 >= 0 && s.charAt(pos - 1) == 'b' && pos - 2 >= 0 && s.charAt(pos - 2) == 'a') {
set.add(pos - 2);
}
s.replace(pos, pos + 1, "" + ch);
out.println(set.size());
}
}
}
}
static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void close() {
writer.close();
}
public void println(int i) {
writer.println(i);
}
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private InputReader.SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1) {
throw new InputMismatchException();
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0) {
return -1;
}
}
return buf[curChar++];
}
public int nextInt() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public boolean isSpaceChar(int c) {
if (filter != null) {
return filter.isSpaceChar(c);
}
return isWhitespace(c);
}
public static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private String readLine0() {
StringBuilder buf = new StringBuilder();
int c = read();
while (c != '\n' && c != -1) {
if (c != '\r') {
buf.appendCodePoint(c);
}
c = read();
}
return buf.toString();
}
public String readLine() {
String s = readLine0();
while (s.trim().length() == 0) {
s = readLine0();
}
return s;
}
public char nextCharacter() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
return (char) c;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
}
| Java | ["9 10\nabcabcabc\n1 a\n1 b\n2 c\n3 a\n4 b\n5 c\n8 a\n9 b\n1 c\n4 a"] | 2 seconds | ["3\n2\n2\n2\n1\n2\n1\n1\n1\n0"] | NoteLet's consider the state of the string after each query: $$$s =$$$ "abcabcabc". In this case $$$3$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcaccabb". This string does not contain "abc" as a substring. $$$s =$$$ "bbcabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bccabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bccbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcaabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcabbcabc". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaac". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaac". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "ccabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccaaccaab". In this case the string does not contain "abc" as a substring and no replacements are needed. | Java 11 | standard input | [
"implementation",
"strings"
] | db473ad780a93983667d12b1357c6e2f | The first line contains two integers $$$n$$$ and $$$q$$$ $$$(1 \le n, q \le 10^5)$$$, the length of the string and the number of queries, respectively. The second line contains the string $$$s$$$, consisting of characters "a", "b" and "c". Each of the next $$$q$$$ lines contains an integer $$$i$$$ and character $$$c$$$ $$$(1 \le i \le n)$$$, index and the value of the new item in the string, respectively. It is guaranteed that character's $$$c$$$ value is "a", "b" or "c". | 1,100 | For each query output the minimal number of characters that would have to be replaced so that the string doesn't contain "abc" as a substring. | standard output | |
PASSED | 7a083ebed48645f0371e02ccec834dbd | train_110.jsonl | 1638110100 | Before becoming a successful trader William got a university degree. During his education an interesting situation happened, after which William started to listen to homework assignments much more attentively. What follows is the correct formal description of the homework assignment:You are given a string $$$s$$$ of length $$$n$$$ only consisting of characters "a", "b" and "c". There are $$$q$$$ queries of format ($$$pos, c$$$), meaning replacing the element of string $$$s$$$ at position $$$pos$$$ with character $$$c$$$. After each query you must output the minimal number of characters in the string, which have to be replaced, so that the string doesn't contain string "abc" as a substring. A valid replacement of a character is replacing it with "a", "b" or "c".A string $$$x$$$ is a substring of a string $$$y$$$ if $$$x$$$ can be obtained from $$$y$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. | 256 megabytes |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
import java.util.*;
public class D {
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next(){
while (st == null || !st.hasMoreElements()){
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e){
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt(){
return Integer.parseInt(next());
}
long nextLong(){
return Long.parseLong(next());
}
double nextDouble(){
return Double.parseDouble(next());
}
String nextLine(){
String str = "";
try{
str = br.readLine();
}
catch (IOException e){
e.printStackTrace();
}
return str;
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
FastReader sc = new FastReader();
int n = sc.nextInt();
int k= sc.nextInt();
String st = sc.next();
char[] s= st.toCharArray();
int total = 0;
char c;
int i;
for(int j=0;j<n-2;j++) {
if(s[j]=='a' && s[j+1]=='b' && s[j+2]=='c')total++;
}
while(k--!=0) {
i=sc.nextInt();c=sc.next().charAt(0);
i--;
if(c=='a'){
if(i-1>=0 && i+1<n && s[i-1]=='a' && s[i]=='b' && s[i+1]=='c') total--;
else if(i-2>=0 && s[i-2]=='a' && s[i-1]=='b' && s[i]=='c') total--;
if(i+2<n && s[i+1]=='b' && s[i+2]=='c' && s[i]!='a') total++;
}else if(c=='b'){
if(i+2<n && s[i]=='a' && s[i+1]=='b' && s[i+2]=='c') total--;
else if(i-2>=0 && s[i-2]=='a' && s[i-1]=='b' && s[i]=='c') total--;
if(i-1>=0 && i+1<n && s[i-1]=='a' && s[i+1]=='c' && s[i]!='b') total++;
}else if(c=='c'){
if(i+2<n && s[i]=='a' && s[i+1]=='b' && s[i+2]=='c') total--;
else if(i-1>=0 && i+1<n && s[i-1]=='a' && s[i]=='b' && s[i+1]=='c') total--;
if(i-2>=0 && s[i-2]=='a' && s[i-1]=='b' && s[i]!='c') total++;
}
// System.out.println(str);
s[i]=c;
System.out.println(total);
}
}
// System.out.println(s);
// System.out.println(total);
}
| Java | ["9 10\nabcabcabc\n1 a\n1 b\n2 c\n3 a\n4 b\n5 c\n8 a\n9 b\n1 c\n4 a"] | 2 seconds | ["3\n2\n2\n2\n1\n2\n1\n1\n1\n0"] | NoteLet's consider the state of the string after each query: $$$s =$$$ "abcabcabc". In this case $$$3$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcaccabb". This string does not contain "abc" as a substring. $$$s =$$$ "bbcabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bccabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bccbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcaabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcabbcabc". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaac". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaac". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "ccabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccaaccaab". In this case the string does not contain "abc" as a substring and no replacements are needed. | Java 11 | standard input | [
"implementation",
"strings"
] | db473ad780a93983667d12b1357c6e2f | The first line contains two integers $$$n$$$ and $$$q$$$ $$$(1 \le n, q \le 10^5)$$$, the length of the string and the number of queries, respectively. The second line contains the string $$$s$$$, consisting of characters "a", "b" and "c". Each of the next $$$q$$$ lines contains an integer $$$i$$$ and character $$$c$$$ $$$(1 \le i \le n)$$$, index and the value of the new item in the string, respectively. It is guaranteed that character's $$$c$$$ value is "a", "b" or "c". | 1,100 | For each query output the minimal number of characters that would have to be replaced so that the string doesn't contain "abc" as a substring. | standard output | |
PASSED | 47aa7381337ab2ada961da458184cc3e | train_110.jsonl | 1638110100 | Before becoming a successful trader William got a university degree. During his education an interesting situation happened, after which William started to listen to homework assignments much more attentively. What follows is the correct formal description of the homework assignment:You are given a string $$$s$$$ of length $$$n$$$ only consisting of characters "a", "b" and "c". There are $$$q$$$ queries of format ($$$pos, c$$$), meaning replacing the element of string $$$s$$$ at position $$$pos$$$ with character $$$c$$$. After each query you must output the minimal number of characters in the string, which have to be replaced, so that the string doesn't contain string "abc" as a substring. A valid replacement of a character is replacing it with "a", "b" or "c".A string $$$x$$$ is a substring of a string $$$y$$$ if $$$x$$$ can be obtained from $$$y$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author Manav
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
BWilliamTheVigilant solver = new BWilliamTheVigilant();
solver.solve(1, in, out);
out.close();
}
static class BWilliamTheVigilant {
public void solve(int testNumber, InputReader in, OutputWriter out) {
int n = in.nextInt(), q = in.nextInt();
char[] a = in.next().toCharArray();
int count = 0;
for (int i = 0; i < n - 2; i++) {
if (a[i] == 'a' && a[i + 1] == 'b' && a[i + 2] == 'c') {
count++;
}
}
for (int i = 0; i < q; i++) {
int index = in.nextInt() - 1;
char x = in.nextCharacter();
String s = "";
for (int j = -2; j < 3; j++) {
if (index + j >= 0 && index + j < n) {
s += a[index + j];
}
}
if (s.indexOf("abc") >= 0) {
count--;
}
a[index] = x;
s = "";
for (int j = -2; j < 3; j++) {
if (index + j >= 0 && index + j < n) {
s += a[index + j];
}
}
if (s.indexOf("abc") >= 0) {
count++;
}
out.println(count);
}
}
}
static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void close() {
writer.close();
}
public void println(int i) {
writer.println(i);
}
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private InputReader.SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1) {
throw new InputMismatchException();
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0) {
return -1;
}
}
return buf[curChar++];
}
public int nextInt() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public String nextString() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
StringBuilder res = new StringBuilder();
do {
if (Character.isValidCodePoint(c)) {
res.appendCodePoint(c);
}
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null) {
return filter.isSpaceChar(c);
}
return isWhitespace(c);
}
public static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public char nextCharacter() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
return (char) c;
}
public String next() {
return nextString();
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
}
| Java | ["9 10\nabcabcabc\n1 a\n1 b\n2 c\n3 a\n4 b\n5 c\n8 a\n9 b\n1 c\n4 a"] | 2 seconds | ["3\n2\n2\n2\n1\n2\n1\n1\n1\n0"] | NoteLet's consider the state of the string after each query: $$$s =$$$ "abcabcabc". In this case $$$3$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcaccabb". This string does not contain "abc" as a substring. $$$s =$$$ "bbcabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bccabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bccbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcaabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcabbcabc". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaac". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaac". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "ccabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccaaccaab". In this case the string does not contain "abc" as a substring and no replacements are needed. | Java 11 | standard input | [
"implementation",
"strings"
] | db473ad780a93983667d12b1357c6e2f | The first line contains two integers $$$n$$$ and $$$q$$$ $$$(1 \le n, q \le 10^5)$$$, the length of the string and the number of queries, respectively. The second line contains the string $$$s$$$, consisting of characters "a", "b" and "c". Each of the next $$$q$$$ lines contains an integer $$$i$$$ and character $$$c$$$ $$$(1 \le i \le n)$$$, index and the value of the new item in the string, respectively. It is guaranteed that character's $$$c$$$ value is "a", "b" or "c". | 1,100 | For each query output the minimal number of characters that would have to be replaced so that the string doesn't contain "abc" as a substring. | standard output | |
PASSED | fa6e4740dbb03f422e3db961ac52c35b | train_110.jsonl | 1638110100 | Before becoming a successful trader William got a university degree. During his education an interesting situation happened, after which William started to listen to homework assignments much more attentively. What follows is the correct formal description of the homework assignment:You are given a string $$$s$$$ of length $$$n$$$ only consisting of characters "a", "b" and "c". There are $$$q$$$ queries of format ($$$pos, c$$$), meaning replacing the element of string $$$s$$$ at position $$$pos$$$ with character $$$c$$$. After each query you must output the minimal number of characters in the string, which have to be replaced, so that the string doesn't contain string "abc" as a substring. A valid replacement of a character is replacing it with "a", "b" or "c".A string $$$x$$$ is a substring of a string $$$y$$$ if $$$x$$$ can be obtained from $$$y$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. | 256 megabytes | import java.util.*;
public class abcary
{
public static void main(String arg[])
{
Scanner Sc=new Scanner(System.in);
int n=Sc.nextInt();
int q=Sc.nextInt();
Sc.nextLine();
String S=Sc.next();
int abc=0;
char ar[]=new char[n];
for(int i=0;i<n;i++)
{
ar[i]=S.charAt(i);
}
for(int i=0;i<n-2;i++)
{
if(ar[i] == 'a')
{
if(ar[i+1]=='b' && ar[i+2]=='c')
{
abc++;
i+=2;
}
}
}
for(int i=0;i<q;i++)
{
int index=Sc.nextInt()-1;
char change=Sc.next().charAt(0);
if(change == ar[index])
{
}
else{
switch(ar[index])
{
case 'a':
if(index<n-2)
{
if(ar[index+1]=='b' &&ar[index+2]=='c')
abc--;
}
break;
case 'b':
if(index>0 && index< n-1)
{
if(ar[index-1]=='a' && ar[index+1]=='c')
abc--;
}
break;
case'c':
if(index>1)
{
if(ar[index-2]=='a' && ar[index-1]=='b')
abc--;
}
break;
}
ar[index]=change;
switch(change)
{
case 'a':
if(index<n-2)
{
if(ar[index+1]=='b' &&ar[index+2]=='c')
abc++;
}
break;
case 'b':
if(index>0 && index< n-1)
{
if(ar[index-1]=='a' && ar[index+1]=='c')
abc++;
}
break;
case'c':
if(index>1)
{
if(ar[index-2]=='a' && ar[index-1]=='b')
abc++;
}
break;
}
}
System.out.println(abc);
}
}
} | Java | ["9 10\nabcabcabc\n1 a\n1 b\n2 c\n3 a\n4 b\n5 c\n8 a\n9 b\n1 c\n4 a"] | 2 seconds | ["3\n2\n2\n2\n1\n2\n1\n1\n1\n0"] | NoteLet's consider the state of the string after each query: $$$s =$$$ "abcabcabc". In this case $$$3$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcaccabb". This string does not contain "abc" as a substring. $$$s =$$$ "bbcabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bccabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bccbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcaabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcabbcabc". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaac". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaac". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "ccabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccaaccaab". In this case the string does not contain "abc" as a substring and no replacements are needed. | Java 11 | standard input | [
"implementation",
"strings"
] | db473ad780a93983667d12b1357c6e2f | The first line contains two integers $$$n$$$ and $$$q$$$ $$$(1 \le n, q \le 10^5)$$$, the length of the string and the number of queries, respectively. The second line contains the string $$$s$$$, consisting of characters "a", "b" and "c". Each of the next $$$q$$$ lines contains an integer $$$i$$$ and character $$$c$$$ $$$(1 \le i \le n)$$$, index and the value of the new item in the string, respectively. It is guaranteed that character's $$$c$$$ value is "a", "b" or "c". | 1,100 | For each query output the minimal number of characters that would have to be replaced so that the string doesn't contain "abc" as a substring. | standard output | |
PASSED | c36d2185c228cd9aea3ae21df21cf3da | train_110.jsonl | 1638110100 | Before becoming a successful trader William got a university degree. During his education an interesting situation happened, after which William started to listen to homework assignments much more attentively. What follows is the correct formal description of the homework assignment:You are given a string $$$s$$$ of length $$$n$$$ only consisting of characters "a", "b" and "c". There are $$$q$$$ queries of format ($$$pos, c$$$), meaning replacing the element of string $$$s$$$ at position $$$pos$$$ with character $$$c$$$. After each query you must output the minimal number of characters in the string, which have to be replaced, so that the string doesn't contain string "abc" as a substring. A valid replacement of a character is replacing it with "a", "b" or "c".A string $$$x$$$ is a substring of a string $$$y$$$ if $$$x$$$ can be obtained from $$$y$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. | 256 megabytes | import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class WilliamTheVigilant1609B {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
StringTokenizer st = new StringTokenizer(br.readLine());
/* int n = */Integer.parseInt(st.nextToken());
int q = Integer.parseInt(st.nextToken());
char[] c = br.readLine().toCharArray();
// count the amount of "abc"s in the original string
int cnt = abcCount(c, 0, c.length);
// queries
for (int i = 0; i < q; i++) {
st = new StringTokenizer(br.readLine());
int index = Integer.parseInt(st.nextToken()) - 1;
char newChar = st.nextToken().charAt(0);
// do the replacement and see the effects on the "abc" count on the adjacent 5 characters
int before = abcCount(c, index - 2, index + 2);
c[index] = newChar;
int after = abcCount(c, index - 2, index + 2);
cnt += after - before;
pw.println(cnt);
}
br.close();
pw.close();
}
// counts number of abcs in a char[] from start to end
static int abcCount(char[] c, int start, int end) {
int cnt = 0;
start = Math.max(start, 0);
end = Math.min(end, c.length - 2);
for (int i = start; i < end; i++) {
if (c[i] == 'a') {
if (c[i + 1] == 'b' && c[i + 2] == 'c') {
cnt++;
}
}
}
return cnt;
}
} | Java | ["9 10\nabcabcabc\n1 a\n1 b\n2 c\n3 a\n4 b\n5 c\n8 a\n9 b\n1 c\n4 a"] | 2 seconds | ["3\n2\n2\n2\n1\n2\n1\n1\n1\n0"] | NoteLet's consider the state of the string after each query: $$$s =$$$ "abcabcabc". In this case $$$3$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcaccabb". This string does not contain "abc" as a substring. $$$s =$$$ "bbcabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bccabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bccbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcaabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcabbcabc". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaac". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaac". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "ccabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccaaccaab". In this case the string does not contain "abc" as a substring and no replacements are needed. | Java 11 | standard input | [
"implementation",
"strings"
] | db473ad780a93983667d12b1357c6e2f | The first line contains two integers $$$n$$$ and $$$q$$$ $$$(1 \le n, q \le 10^5)$$$, the length of the string and the number of queries, respectively. The second line contains the string $$$s$$$, consisting of characters "a", "b" and "c". Each of the next $$$q$$$ lines contains an integer $$$i$$$ and character $$$c$$$ $$$(1 \le i \le n)$$$, index and the value of the new item in the string, respectively. It is guaranteed that character's $$$c$$$ value is "a", "b" or "c". | 1,100 | For each query output the minimal number of characters that would have to be replaced so that the string doesn't contain "abc" as a substring. | standard output | |
PASSED | 91d25ddf8366911a2bcb2e8bfd5903da | train_110.jsonl | 1638110100 | Before becoming a successful trader William got a university degree. During his education an interesting situation happened, after which William started to listen to homework assignments much more attentively. What follows is the correct formal description of the homework assignment:You are given a string $$$s$$$ of length $$$n$$$ only consisting of characters "a", "b" and "c". There are $$$q$$$ queries of format ($$$pos, c$$$), meaning replacing the element of string $$$s$$$ at position $$$pos$$$ with character $$$c$$$. After each query you must output the minimal number of characters in the string, which have to be replaced, so that the string doesn't contain string "abc" as a substring. A valid replacement of a character is replacing it with "a", "b" or "c".A string $$$x$$$ is a substring of a string $$$y$$$ if $$$x$$$ can be obtained from $$$y$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. | 256 megabytes | // package cf_1609b;
import java.util.Scanner;
/**
*
* @author MRayhanFerdousFaisal
*/
public class Cf_1609b {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
int n = cin.nextInt();
int m = cin.nextInt();
char str[] = cin.next().toCharArray();
int cnt = 0;
for(int i=0; i<n-2; ++i){
if(str[i]=='a' && str[i+1]=='b' && str[i+2]=='c')
++cnt;
}
// System.out.println("cnt = "+ cnt);
for(int q=1; q<=m; ++q){
int idx = cin.nextInt();
char ch = cin.next().charAt(0);
--idx;
int before = 0, after = 0;
int st = idx, ed = idx+2;
if(st>=0 && ed <n && str[idx]=='a'&&str[idx+1]=='b' && str[idx+2]=='c') ++before;
if(st-1>=0 && ed-1 <n && str[idx-1]=='a' && str[idx]=='b' && str[idx+1]=='c') ++before;
if(st-2>=0 && ed-2 <n && str[idx-2]=='a' && str[idx-1]=='b' && str[idx]=='c') ++before;
str[idx] = ch;
if(st>=0 && ed <n && str[idx]=='a' && str[idx+1]=='b' && str[idx+2]=='c') ++after;
if(st-1>=0 && ed-1 <n && str[idx-1]=='a' && str[idx]=='b' && str[idx+1]=='c') ++after;
if(st-2>=0 && ed-2 <n && str[idx-2]=='a' && str[idx-1]=='b' && str[idx]=='c') ++after;
if(before > after) --cnt;
else if(before < after) ++cnt;
System.out.println(cnt);
}
cin.close();
}
}
| Java | ["9 10\nabcabcabc\n1 a\n1 b\n2 c\n3 a\n4 b\n5 c\n8 a\n9 b\n1 c\n4 a"] | 2 seconds | ["3\n2\n2\n2\n1\n2\n1\n1\n1\n0"] | NoteLet's consider the state of the string after each query: $$$s =$$$ "abcabcabc". In this case $$$3$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcaccabb". This string does not contain "abc" as a substring. $$$s =$$$ "bbcabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bccabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bccbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcaabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcabbcabc". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaac". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaac". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "ccabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccaaccaab". In this case the string does not contain "abc" as a substring and no replacements are needed. | Java 11 | standard input | [
"implementation",
"strings"
] | db473ad780a93983667d12b1357c6e2f | The first line contains two integers $$$n$$$ and $$$q$$$ $$$(1 \le n, q \le 10^5)$$$, the length of the string and the number of queries, respectively. The second line contains the string $$$s$$$, consisting of characters "a", "b" and "c". Each of the next $$$q$$$ lines contains an integer $$$i$$$ and character $$$c$$$ $$$(1 \le i \le n)$$$, index and the value of the new item in the string, respectively. It is guaranteed that character's $$$c$$$ value is "a", "b" or "c". | 1,100 | For each query output the minimal number of characters that would have to be replaced so that the string doesn't contain "abc" as a substring. | standard output | |
PASSED | 294cfc8d7a3fd73bd59e3525a2d53f86 | train_110.jsonl | 1638110100 | Before becoming a successful trader William got a university degree. During his education an interesting situation happened, after which William started to listen to homework assignments much more attentively. What follows is the correct formal description of the homework assignment:You are given a string $$$s$$$ of length $$$n$$$ only consisting of characters "a", "b" and "c". There are $$$q$$$ queries of format ($$$pos, c$$$), meaning replacing the element of string $$$s$$$ at position $$$pos$$$ with character $$$c$$$. After each query you must output the minimal number of characters in the string, which have to be replaced, so that the string doesn't contain string "abc" as a substring. A valid replacement of a character is replacing it with "a", "b" or "c".A string $$$x$$$ is a substring of a string $$$y$$$ if $$$x$$$ can be obtained from $$$y$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. | 256 megabytes | import java.util.*;
import java.io.*;
public class Sol {
public static boolean solve(char c[],int pos) {
if(pos+2>=c.length) return false;
if(c[pos]=='a' && c[pos+1]=='b' && c[pos+2]=='c') return true;
else return false;
}
public static void main(String[] args) throws IOException {
Scanner sc=new Scanner(System.in);
//while(t--> 0) {
int t=sc.nextInt();
int n=sc.nextInt();
int cnt=0;
char s[]=sc.next().toCharArray();
for(int i=0;i<s.length;i++)
if(solve(s,i)) cnt++;
for(int i=0;i<n;i++) {
int pos=sc.nextInt()-1;
char c=sc.next().charAt(0);
for(int j=Math.max(pos-2,0);j<=pos;j++)
if(solve(s,j)) cnt--;
s[pos]=c;
for(int j=Math.max(pos-2,0);j<=pos;j++)
if(solve(s,j)) cnt++;
System.out.println(cnt);
}
}
} | Java | ["9 10\nabcabcabc\n1 a\n1 b\n2 c\n3 a\n4 b\n5 c\n8 a\n9 b\n1 c\n4 a"] | 2 seconds | ["3\n2\n2\n2\n1\n2\n1\n1\n1\n0"] | NoteLet's consider the state of the string after each query: $$$s =$$$ "abcabcabc". In this case $$$3$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcaccabb". This string does not contain "abc" as a substring. $$$s =$$$ "bbcabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bccabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bccbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcaabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcabbcabc". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaac". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaac". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "ccabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccaaccaab". In this case the string does not contain "abc" as a substring and no replacements are needed. | Java 11 | standard input | [
"implementation",
"strings"
] | db473ad780a93983667d12b1357c6e2f | The first line contains two integers $$$n$$$ and $$$q$$$ $$$(1 \le n, q \le 10^5)$$$, the length of the string and the number of queries, respectively. The second line contains the string $$$s$$$, consisting of characters "a", "b" and "c". Each of the next $$$q$$$ lines contains an integer $$$i$$$ and character $$$c$$$ $$$(1 \le i \le n)$$$, index and the value of the new item in the string, respectively. It is guaranteed that character's $$$c$$$ value is "a", "b" or "c". | 1,100 | For each query output the minimal number of characters that would have to be replaced so that the string doesn't contain "abc" as a substring. | standard output | |
PASSED | 8f2fed9641d94c2196723e36ecbc7217 | train_110.jsonl | 1638110100 | Before becoming a successful trader William got a university degree. During his education an interesting situation happened, after which William started to listen to homework assignments much more attentively. What follows is the correct formal description of the homework assignment:You are given a string $$$s$$$ of length $$$n$$$ only consisting of characters "a", "b" and "c". There are $$$q$$$ queries of format ($$$pos, c$$$), meaning replacing the element of string $$$s$$$ at position $$$pos$$$ with character $$$c$$$. After each query you must output the minimal number of characters in the string, which have to be replaced, so that the string doesn't contain string "abc" as a substring. A valid replacement of a character is replacing it with "a", "b" or "c".A string $$$x$$$ is a substring of a string $$$y$$$ if $$$x$$$ can be obtained from $$$y$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. | 256 megabytes | // हर हर महादेव
import java.util.*;
import java.lang.*;
import java.io.*;
import java.math.BigInteger;
import java.text.DecimalFormat;
public final class Solution {
static int inf = Integer.MAX_VALUE;
static long mod = 1000000000 + 7;
static int count=0;
static void ne(Scanner sc, BufferedWriter op) throws Exception {
int n=sc.nextInt();
int q=sc.nextInt();
int[] arr= new int[n];
String s=sc.next();
for(int i=0;i<n;i++){
arr[i]=s.charAt(i)-'a';
}
count=check(arr,0,n-1);
while(q-->0){
int pos=sc.nextInt();
String ch=sc.next();
updateCount(arr,pos-1,ch.charAt(0));
op.write(count+"\n");
}
}
static void updateCount(int[] arr, int id, char c){
int n=arr.length;
int l=Math.max(0,id-2);
int r=Math.min(n-1,id+2);
int cc=check(arr,l,r);
count-=cc;
arr[id]=c-'a';
cc=check(arr,l,r);
count+=cc;
}
static int check(int[] arr, int l,int r){
int count=0;
int n=arr.length;
for(int i=l;i<=r;i++){
if(i+2<=r){
if(arr[i]==0 && arr[i+1]==1 && arr[i+2]==2){
count++;
}
}
}
return count;
}
static long gcd(long a, long b){
if(a==0L) return b;
return gcd(b%a,a);
}
public static void main(String[] args) throws Exception {
BufferedWriter op = new BufferedWriter(new OutputStreamWriter(System.out));
// Reader sc = new Reader();
Scanner sc= new Scanner(System.in);
// int t = sc.nextInt();
// while (t-->0){ ne(sc, op); }
ne(sc,op);
op.flush();
}
static void print(Object o) {
System.out.println(String.valueOf(o));
}
static int[] toIntArr(String s){
int[] val= new int[s.length()];
for(int i=0;i<s.length();i++){
val[i]=s.charAt(i)-'a';
}
return val;
}
static void sort(int[] arr){
ArrayList<Integer> list= new ArrayList<>();
for(int i=0;i<arr.length;i++){
list.add(arr[i]);
}
Collections.sort(list);
for(int i=0;i<arr.length;i++){
arr[i]=list.get(i);
}
}
static void sort(long[] arr){
ArrayList<Long> list= new ArrayList<>();
for(int i=0;i<arr.length;i++){
list.add(arr[i]);
}
Collections.sort(list);
for(int i=0;i<arr.length;i++){
arr[i]=list.get(i);
}
}
}
// return -1 to put no ahed in array
class pair {
long xx;
int yy;
pair(long xx, int yy ) {
this.xx = xx;
this.yy = yy;
}
}
class sortY implements Comparator<pair> {
public int compare(pair p1, pair p2) {
if (p1.yy > p2.yy) {
return 1;
} else if (p1.yy == p2.yy) {
if (p1.xx > p2.xx) {
return 1;
} else if (p1.xx < p2.xx) {
return -1;
}
return 0;
}
return -1;
}
}
class sortX implements Comparator<pair> {
public int compare(pair p1, pair p2) {
if (p1.xx > p2.xx) {
return 1;
} else if (p1.xx == p2.xx) {
if (p1.yy > p2.yy) {
return 1;
} else if (p1.yy < p2.yy) {
return -1;
}
return 0;
}
return -1;
}
}
class debug {
static void print1d(long[] arr) {
System.out.println();
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
static void print1d(int[] arr) {
System.out.println();
for (int i = 0; i < arr.length; i++) {
System.out.println(i+" = "+arr[i]);
}
}
static void print1d(boolean[] arr) {
System.out.println();
for (int i = 0; i < arr.length; i++) {
System.out.println(i + "= " + arr[i]);
}
}
static void print2d(int[][] arr) {
System.out.println();
int n = arr.length;
int n2 = arr[0].length;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n2; j++) {
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
static void print2d(long[][] arr) {
System.out.println();
int n = arr.length;
int n2 = arr[0].length;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n2; j++) {
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
static void printPair(ArrayList<pair> list) {
if(list.size()==0){
System.out.println("empty list");
return;
}
System.out.println();
for(int i=0;i<list.size();i++){
System.out.println(list.get(i).xx+"-"+list.get(i).yy);
}
}
}
class Reader {
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader() {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException {
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException {
byte[] buf = new byte[64]; // line length
int cnt = 0, c;
while ((c = read()) != -1) {
if (c == '\n') {
if (cnt != 0) {
break;
} else {
continue;
}
}
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException {
int ret = 0;
byte c = read();
while (c <= ' ') {
c = read();
}
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException {
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException {
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (c == '.') {
while ((c = read()) >= '0' && c <= '9') {
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException {
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException {
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException {
if (din == null)
return;
din.close();
}
}
| Java | ["9 10\nabcabcabc\n1 a\n1 b\n2 c\n3 a\n4 b\n5 c\n8 a\n9 b\n1 c\n4 a"] | 2 seconds | ["3\n2\n2\n2\n1\n2\n1\n1\n1\n0"] | NoteLet's consider the state of the string after each query: $$$s =$$$ "abcabcabc". In this case $$$3$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcaccabb". This string does not contain "abc" as a substring. $$$s =$$$ "bbcabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bccabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bccbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcaabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcabbcabc". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaac". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaac". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "ccabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccaaccaab". In this case the string does not contain "abc" as a substring and no replacements are needed. | Java 11 | standard input | [
"implementation",
"strings"
] | db473ad780a93983667d12b1357c6e2f | The first line contains two integers $$$n$$$ and $$$q$$$ $$$(1 \le n, q \le 10^5)$$$, the length of the string and the number of queries, respectively. The second line contains the string $$$s$$$, consisting of characters "a", "b" and "c". Each of the next $$$q$$$ lines contains an integer $$$i$$$ and character $$$c$$$ $$$(1 \le i \le n)$$$, index and the value of the new item in the string, respectively. It is guaranteed that character's $$$c$$$ value is "a", "b" or "c". | 1,100 | For each query output the minimal number of characters that would have to be replaced so that the string doesn't contain "abc" as a substring. | standard output | |
PASSED | 2faae4aa95cc114fb96f116de9531a9b | train_110.jsonl | 1638110100 | Before becoming a successful trader William got a university degree. During his education an interesting situation happened, after which William started to listen to homework assignments much more attentively. What follows is the correct formal description of the homework assignment:You are given a string $$$s$$$ of length $$$n$$$ only consisting of characters "a", "b" and "c". There are $$$q$$$ queries of format ($$$pos, c$$$), meaning replacing the element of string $$$s$$$ at position $$$pos$$$ with character $$$c$$$. After each query you must output the minimal number of characters in the string, which have to be replaced, so that the string doesn't contain string "abc" as a substring. A valid replacement of a character is replacing it with "a", "b" or "c".A string $$$x$$$ is a substring of a string $$$y$$$ if $$$x$$$ can be obtained from $$$y$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. | 256 megabytes |
import java.io.*;
import java.math.*;
import java.util.*;
// @author : Dinosparton
public class test {
static class Pair{
long x;
long y;
Pair(long x,long y){
this.x = x;
this.y = y;
}
}
static class Sort implements Comparator<Pair>
{
@Override
public int compare(Pair a, Pair b)
{
if(a.x!=b.x)
{
return (int)(a.x - b.x);
}
else
{
return (int)(a.y-b.y);
}
}
}
static class Compare {
void compare(Pair arr[], int n)
{
// Comparator to sort the pair according to second element
Arrays.sort(arr, new Comparator<Pair>() {
@Override public int compare(Pair p1, Pair p2)
{
if(p1.x!=p2.x) {
return (int)(p1.x - p2.x);
}
else {
return (int)(p1.y - p2.y);
}
}
});
// for (int i = 0; i < n; i++) {
// System.out.print(arr[i].x + " " + arr[i].y + " ");
// }
// System.out.println();
}
}
static class Scanner {
BufferedReader br;
StringTokenizer st;
public Scanner()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static void main(String args[]) throws Exception {
Scanner sc = new Scanner();
StringBuilder res = new StringBuilder();
int tc = 1;
while(tc-->0) {
int n = sc.nextInt();
int q = sc.nextInt();
String st = sc.next();
char s[] = st.toCharArray();
int cnt = 0;
for(int i=0;i<s.length-2;i++) {
if(s[i]=='a' && s[i+1]=='b' && s[i+2]=='c') {
cnt++;
}
}
while(q-->0) {
int num = sc.nextInt()-1;
char c = sc.next().charAt(0);
if(s[num]==c) {
res.append(cnt+"\n");
continue;
}
if(num+2<s.length) {
if(s[num]=='a' && s[num+1]=='b' && s[num+2]=='c') {
cnt--;
}
}
if(num-2>=0) {
if(s[num-2]=='a' && s[num-1]=='b' && s[num]=='c') {
cnt--;
}
}
if(num-1>=0 && num+1<s.length) {
if(s[num-1]=='a' && s[num]=='b' && s[num+1]=='c') {
cnt--;
}
}
s[num] = c;
if(num+2<s.length) {
if(s[num]=='a' && s[num+1]=='b' && s[num+2]=='c') {
cnt++;
}
}
if(num-2>=0) {
if(s[num-2]=='a' && s[num-1]=='b' && s[num]=='c') {
cnt++;
}
}
if(num-1>=0 && num+1<s.length) {
if(s[num-1]=='a' && s[num]=='b' && s[num+1]=='c') {
cnt++;
}
}
res.append(cnt+"\n");
}
}
System.out.println(res);
}
}
| Java | ["9 10\nabcabcabc\n1 a\n1 b\n2 c\n3 a\n4 b\n5 c\n8 a\n9 b\n1 c\n4 a"] | 2 seconds | ["3\n2\n2\n2\n1\n2\n1\n1\n1\n0"] | NoteLet's consider the state of the string after each query: $$$s =$$$ "abcabcabc". In this case $$$3$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcaccabb". This string does not contain "abc" as a substring. $$$s =$$$ "bbcabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bccabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bccbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcaabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcabbcabc". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaac". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaac". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "ccabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccaaccaab". In this case the string does not contain "abc" as a substring and no replacements are needed. | Java 11 | standard input | [
"implementation",
"strings"
] | db473ad780a93983667d12b1357c6e2f | The first line contains two integers $$$n$$$ and $$$q$$$ $$$(1 \le n, q \le 10^5)$$$, the length of the string and the number of queries, respectively. The second line contains the string $$$s$$$, consisting of characters "a", "b" and "c". Each of the next $$$q$$$ lines contains an integer $$$i$$$ and character $$$c$$$ $$$(1 \le i \le n)$$$, index and the value of the new item in the string, respectively. It is guaranteed that character's $$$c$$$ value is "a", "b" or "c". | 1,100 | For each query output the minimal number of characters that would have to be replaced so that the string doesn't contain "abc" as a substring. | standard output | |
PASSED | e3da8fa5e9f5de8abc6ac59bc9bd2a73 | train_110.jsonl | 1638110100 | Before becoming a successful trader William got a university degree. During his education an interesting situation happened, after which William started to listen to homework assignments much more attentively. What follows is the correct formal description of the homework assignment:You are given a string $$$s$$$ of length $$$n$$$ only consisting of characters "a", "b" and "c". There are $$$q$$$ queries of format ($$$pos, c$$$), meaning replacing the element of string $$$s$$$ at position $$$pos$$$ with character $$$c$$$. After each query you must output the minimal number of characters in the string, which have to be replaced, so that the string doesn't contain string "abc" as a substring. A valid replacement of a character is replacing it with "a", "b" or "c".A string $$$x$$$ is a substring of a string $$$y$$$ if $$$x$$$ can be obtained from $$$y$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. | 256 megabytes | import java.io.*;
import java.lang.reflect.Array;
import java.math.BigInteger;
import java.util.*;
public class Main {
static class Pair {
int l,r,x;
public Pair(int l,int r,int x) {
this.l=l;
this.r=r;
this.x=x;
}
}
static int mod=1000000007;
public static void main(String[] args) throws IOException
{
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
TaskC solver = new TaskC();
solver.solve(1, in, out);
out.close();
}
static class TaskC
{
public static void solve(int testNumber, InputReader in, OutputWriter out)
{
int n=in.readInt();
int q=in.readInt();
StringBuilder s=new StringBuilder(in.readString());
int cnt=0;
for(int i=0;i<n;i++)
{
if(s.charAt(i)=='a' && i+1<n && s.charAt(i+1)=='b' && i+2<n && s.charAt(i+2)=='c')
{
++cnt;
}
}
int pos=0;
char ch=0;
while(q-->0)
{
pos=in.readInt();
ch=in.readString().charAt(0);
--pos;
if(ch=='a')
{
if(s.charAt(pos)=='b')
{
if((pos-1>=0 && s.charAt(pos-1)=='a')&&(pos+1<n && s.charAt(pos+1)=='c'))
{
--cnt;
}
}
else if(s.charAt(pos)=='c')
{
if((pos-1>=0 && s.charAt(pos-1)=='b')&&(pos-2>=0 && s.charAt(pos-2)=='a'))
{
--cnt;
}
}
if(s.charAt(pos)!=ch && pos+1<n && s.charAt(pos+1)=='b' && pos+2<n && s.charAt(pos+2)=='c')
{
++cnt;
}
}
else if(ch=='b')
{
if(s.charAt(pos)=='a')
{
if((pos+1<n && s.charAt(pos+1)=='b')&&(pos+2<n && s.charAt(pos+2)=='c'))
{
--cnt;
}
}
else if(s.charAt(pos)=='c')
{
if((pos-1>=0 && s.charAt(pos-1)=='b')&&(pos-2>=0 && s.charAt(pos-2)=='a'))
{
--cnt;
}
}
if(s.charAt(pos)!=ch && pos-1>=0 && s.charAt(pos-1)=='a' && pos+1<n && s.charAt(pos+1)=='c')
{
++cnt;
}
}
else
{
if(s.charAt(pos)=='a')
{
if((pos+1<n && s.charAt(pos+1)=='b')&&(pos+2<n && s.charAt(pos+2)=='c'))
{
--cnt;
}
}
else if(s.charAt(pos)=='b')
{
if((pos-1>=0 && s.charAt(pos-1)=='a')&&(pos+1<n && s.charAt(pos+1)=='c'))
{
--cnt;
}
}
if(s.charAt(pos)!=ch && pos-2>=0 && s.charAt(pos-2)=='a' && pos-1>=0 && s.charAt(pos-1)=='b')
{
++cnt;
}
}
s.setCharAt(pos,ch);
out.printLine(cnt);
}
}
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private InputReader.SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1) {
throw new InputMismatchException();
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0) {
return -1;
}
}
return buf[curChar++];
}
public int readInt() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public int[] nextIntArray(int arraySize) {
int[] array = new int[arraySize];
for (int i = 0; i < arraySize; i++) {
array[i] = readInt();
}
return array;
}
public String readString() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
StringBuilder res = new StringBuilder();
do {
if (Character.isValidCodePoint(c)) {
res.appendCodePoint(c);
}
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public boolean isNewLine(int c) {
return c == '\n';
}
public String nextLine() {
int c = read();
StringBuilder result = new StringBuilder();
do {
result.appendCodePoint(c);
c = read();
} while (!isNewLine(c));
return result.toString();
}
public long nextLong() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sign = 1;
if (c == '-') {
sign = -1;
c = read();
}
long result = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
result *= 10;
result += c & 15;
c = read();
} while (!isSpaceChar(c));
return result * sign;
}
public long[] nextLongArray(int arraySize) {
long array[] = new long[arraySize];
for (int i = 0; i < arraySize; i++) {
array[i] = nextLong();
}
return array;
}
public double nextDouble() {
double ret = 0, div = 1;
byte c = (byte) read();
while (c <= ' ') {
c = (byte) read();
}
boolean neg = (c == '-');
if (neg) {
c = (byte) read();
}
do {
ret = ret * 10 + c - '0';
} while ((c = (byte) read()) >= '0' && c <= '9');
if (c == '.') {
while ((c = (byte) read()) >= '0' && c <= '9') {
ret += (c - '0') / (div *= 10);
}
}
if (neg) {
return -ret;
}
return ret;
}
public boolean isSpaceChar(int c) {
if (filter != null) {
return filter.isSpaceChar(c);
}
return isWhitespace(c);
}
public static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
static class CP {
static boolean isPrime(long n) {
if (n <= 1)
return false;
if (n == 2 || n == 3)
return true;
if (n % 2 == 0 || n % 3 == 0)
return false;
for (int i = 5; (long) i * i <= n; i += 6) {
if (n % i == 0 || n % (i + 2) == 0)
return false;
}
return true;
}
static String addChar(String s, int n, String ch) {
String res =s+String.join("", Collections.nCopies(n, ch));
return res;
}
static int ifnotPrime(int[] prime, int x) {
return (prime[x / 64] & (1 << ((x >> 1) & 31)));
}
static void makeComposite(int[] prime, int x) {
prime[x / 64] |= (1 << ((x >> 1) & 31));
}
static ArrayList<Integer> bitWiseSieve(int n) {
ArrayList<Integer> al = new ArrayList<>();
int prime[] = new int[n / 64 + 1];
for (int i = 3; i * i <= n; i += 2) {
if (ifnotPrime(prime, i) == 0)
for (int j = i * i, k = i << 1;
j < n; j += k)
makeComposite(prime, j);
}
al.add(2);
for (int i = 3; i <= n; i += 2)
if (ifnotPrime(prime, i) == 0)
al.add(i);
return al;
}
public static long[] sort(long arr[]){
List<Long> list = new ArrayList<>();
for(long n : arr){list.add(n);}
Collections.sort(list);
for(int i=0;i<arr.length;i++){
arr[i] = list.get(i);
}
return arr;
}
static ArrayList<Integer> sieve(long size) {
ArrayList<Integer> pr = new ArrayList<Integer>();
boolean prime[] = new boolean[(int) size];
for (int i = 2; i < prime.length; i++) prime[i] = true;
for (int i = 2; i * i < prime.length; i++) {
if (prime[i]) {
for (int j = i * i; j < prime.length; j += i) {
prime[j] = false;
}
}
}
for (int i = 2; i < prime.length; i++) if (prime[i]) pr.add(i);
return pr;
}
static ArrayList<Integer> segmented_sieve(int l, int r, ArrayList<Integer> primes) {
ArrayList<Integer> al=new ArrayList<>();
if (l == 1) ++l;
int max = r - l + 1;
int arr[] = new int[max];
for (int p : primes) {
if (p * p <= r) {
int i = (l / p) * p;
if (i < l) i += p;
for (; i <= r; i += p) {
if (i != p) {
arr[i - l] = 1;
}
}
}
}
for (int i = 0; i < max; ++i) {
if (arr[i] == 0) {
al.add(l+i);
}
}
return al;
}
static boolean isfPrime(long n, int iteration) {
if (n == 0 || n == 1)
return false;
if (n == 2)
return true;
if (n % 2 == 0)
return false;
Random rand = new Random();
for (int i = 0; i < iteration; i++) {
long r = Math.abs(rand.nextLong());
long a = r % (n - 1) + 1;
if (modPow(a, n - 1, n) != 1)
return false;
}
return true;
}
static long modPow(long a, long b, long c) {
long res = 1;
for (int i = 0; i < b; i++) {
res *= a;
res %= c;
}
return res % c;
}
private static long binPower(long a, long l, long mod) {
long res = 0;
while (l > 0) {
if ((l & 1) == 1) {
res = mulmod(res, a, mod);
;
l >>= 1;
}
a = mulmod(a, a, mod);
}
return res;
}
private static long mulmod(long a, long b, long c) {
long x = 0, y = a % c;
while (b > 0) {
if (b % 2 == 1) {
x = (x + y) % c;
}
y = (y * 2L) % c;
b /= 2;
}
return x % c;
}
static long binary_Expo(long a, long b) {
long res = 1;
while (b != 0) {
if ((b & 1) == 1) {
res *= a;
--b;
}
a *= a;
b /= 2;
}
return res;
}
static long Modular_Expo(long a, long b) {
long res = 1;
while (b != 0) {
if ((b & 1) == 1) {
res = (res * a) % 1000000007 ;
--b;
}
a = (a * a) % 1000000007;
b /= 2;
}
return res%1000000007;
}
static int i_gcd(int a, int b) {
while (true) {
if (b == 0)
return a;
int c = a;
a = b;
b = c % b;
}
}
static long gcd(long a, long b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
static long ceil_div(long a, long b) {
return (a + b - 1) / b;
}
static int getIthBitFromInt(int bits, int i) {
return (bits >> (i - 1)) & 1;
}
private static TreeMap<Long, Long> primeFactorize(long n) {
TreeMap<Long, Long> pf = new TreeMap<>(Collections.reverseOrder());
long cnt = 0;
long total = 1;
for (long i = 2; (long) i * i <= n; ++i) {
if (n % i == 0) {
cnt = 0;
while (n % i == 0) {
++cnt;
n /= i;
}
pf.put(cnt, i);
//total*=(cnt+1);
}
}
if (n > 1) {
pf.put(1L, n);
//total*=2;
}
return pf;
}
static long upper_Bound(long a[], long x) {
long l = -1, r = a.length;
while (l + 1 < r) {
long m = (l + r) >>> 1;
if (a[(int) m] <= x)
l = m;
else
r = m;
}
return l + 1;
}
static int lower_Bound(ArrayList<Long> a, long x) {
int l = 0, r = a.size()-1,ans=-1,mid=0;
while(l<=r)
{
mid=(l+r)>>1;
if(a.get(mid)<=x)
{
ans=mid;
l=mid+1;
}
else
{
r=mid-1;
}
}
return ans;
}
static int lower_Bound(int a[],int x) {
int l = -1, r = a.length;
while (l + 1 < r) {
int m = (l + r) >>> 1;
if (a[(int) m] >= x)
r = m;
else
l = m;
}
return r;
}
static int upperBound(int a[], int x) {// x is the key or target value
int l=-1,r=a.length;
while(l+1<r) {
int m=(l+r)>>>1;
if(a[m]<=x) l=m;
else r=m;
}
return l+1;
}
static int bsh(int a[],int t) {
int ans =-1;
int i = 0, j = a.length - 1;
while (i <= j) {
int mid = i + (j - i) / 2;
if (a[mid] > t) {
ans = mid;
//System.out.print("uppr "+a[ans]);
j=mid-1;
}
else{
//System.out.print("less "+a[mid]);
i=mid+1;
}
}
return ans;
}
static int bsl(int a[],int t) {
int ans =-1;
int i = 0, j = a.length-1;
while (i <= j) {
int mid = i + (j - i) / 2;
if (a[mid] == t) {
ans = mid;
break;
} else if (a[mid] > t) {
j = mid - 1;
} else {
ans=mid;
i = mid + 1;
}
}
return ans;
}
static int upper_Bound(ArrayList<Long> a, long x) //closest to the right
{
int l = 0, r = a.size()-1,ans=-1,mid=0;
while(l<=r)
{
mid=(l+r)>>1;
if(a.get(mid)>=x)
{
ans=mid;
r=mid-1;
}
else
{
l=mid+1;
}
}
return ans;
}
static boolean isSquarefactor(int x, int factor, int target) {
int s = (int) Math.round(Math.sqrt(x));
return factor * s * s == target;
}
static boolean isSquare(int x) {
int s = (int) Math.round(Math.sqrt(x));
return x * x == s;
}
static void sort(int a[]) // heap sort
{
PriorityQueue<Integer> q = new PriorityQueue<>();
for (int i = 0; i < a.length; i++)
q.add(a[i]);
for (int i = 0; i < a.length; i++)
a[i] = q.poll();
}
static void shuffle(int[] in) {
for (int i = 0; i < in.length; i++) {
int idx = (int) (Math.random() * in.length);
fast_swap(in, idx, i);
}
}
static boolean isPalindrome(String s)
{
StringBuilder sb=new StringBuilder(s);
sb.reverse();
if(s.equals(sb.toString()))
{
return true;
}
return false;
}
static int[] computeLps(String pat) {
int len = 0, i = 1, m = pat.length();
int lps[] = new int[m];
lps[0] = 0;
while (i < m) {
if (pat.charAt(i) == pat.charAt(len)) {
++len;
lps[i] = len;
++i;
} else {
if (len != 0) {
len = lps[len - 1];
} else {
lps[i] = len;
++i;
}
}
}
return lps;
}
static void kmp(String s, String pat,int[] prf,int[] st) {
int n = s.length(), m = pat.length();
int lps[] = computeLps(pat);
int i = 0, j = 0;
while (i < n) {
if (s.charAt(i) == pat.charAt(j)) {
i++;
j++;
if (j == m) {
++prf[(i-j)+m];
++st[(i-j)+1];
j=lps[j - 1];
}
} else {
if (j != 0) {
j = lps[j - 1];
} else {
i++;
}
}
}
}
static void reverse_ruffle_sort(int a[]) {
shuffle(a);
Arrays.sort(a);
for (int l = 0, r = a.length - 1; l < r; ++l, --r)
fast_swap(a, l, r);
}
static void ruffle_sort(int a[]) {
shuffle(a);
Arrays.sort(a);
}
static int getMax(int arr[], int n) {
int mx = arr[0];
for (int i = 1; i < n; i++)
if (arr[i] > mx)
mx = arr[i];
return mx;
}
static ArrayList<Long> primeFactors(long n) {
ArrayList<Long> al = new ArrayList<>();
al.add(1L);
while (n % 2 == 0) {
if(!al.contains(2L))
{
al.add(2L);
}
n /= 2L;
}
for (int i = 3; (long) i * i <= n; i += 2) {
while ((n % i == 0)) {
if(!al.contains((long)i))
{
al.add((long) i);
}
n /= i;
}
}
if (n > 2) {
if(!al.contains(n))
{
al.add(n);
}
}
return al;
}
static int[] z_function(String s) {
int n = s.length(), z[] = new int[n];
for (int i = 1, l = 0, r = 0; i < n; ++i) {
if (i <= r)
z[i] = Math.min(z[i - l], r - i + 1);
while (i + z[i] < n && s.charAt(z[i]) == s.charAt(i + z[i]))
++z[i];
if (i + z[i] - 1 > r) {
l = i;
r = i + z[i] - 1;
}
}
return z;
}
static void swap(int a[], int idx1, int idx2) {
a[idx1] += a[idx2];
a[idx2] = a[idx1] - a[idx2];
a[idx1] -= a[idx2];
}
static void fast_swap(int[] a, int idx1, int idx2) {
if (a[idx1] == a[idx2])
return;
a[idx1] ^= a[idx2];
a[idx2] ^= a[idx1];
a[idx1] ^= a[idx2];
}
public static void fast_sort(long[] array) {
ArrayList<Long> copy = new ArrayList<>();
for (long i : array)
copy.add(i);
Collections.sort(copy);
for (int i = 0; i < array.length; i++)
array[i] = copy.get(i);
}
static int divCount(int n) {
boolean hash[] = new boolean[n + 1];
Arrays.fill(hash, true);
for (int p = 2; p * p < n; p++)
if (hash[p] == true)
for (int i = p * 2; i < n; i += p)
hash[i] = false;
int total = 1;
for (int p = 2; p <= n; p++) {
if (hash[p]) {
int count = 0;
if (n % p == 0) {
while (n % p == 0) {
n = n / p;
count++;
}
total = total * (count + 1);
}
}
}
return total;
}
static long binomialCoeff(long n,long k)
{
long res = 1;
// Since C(n, k) = C(n, n-k)
if (k > n - k)
k = n - k;
// Calculate value of
// [n * (n-1) *---* (n-k+1)] / [k * (k-1) *----* 1]
for (int i = 0; i < k; ++i) {
res = (res*(n - i));
res /= (i + 1);
}
return res;
}
static long c(long fact[],long n,long k)
{
if(k>n) return 0;
long res=fact[(int)n];
res= (int) ((res * Modular_Expo(fact[(int)k], mod - 2))%mod);
res= (int) ((res * Modular_Expo(fact[(int)n - (int)k], mod - 2))%mod);
return res%mod;
}
public static ArrayList<Long> getFactors(long x) {
ArrayList<Long> facts=new ArrayList<>();
for(long i=2;i*i<=x;++i)
{
if(x%i==0)
{
facts.add(i);
if(i!=x/i)
{
facts.add(x/i);
}
}
}
return facts;
}
public static HashMap<Integer, Integer> sortMap(HashMap<Integer, Integer> map) {
List<Map.Entry<Integer,Integer>> list=new LinkedList<>(map.entrySet());
Collections.sort(list,(o1,o2)->o2.getValue()-o1.getValue());
HashMap<Integer,Integer> temp=new LinkedHashMap<>();
for(Map.Entry<Integer,Integer> i:list)
{
temp.put(i.getKey(),i.getValue());
}
return temp;
}
public static long lcm(long l,long l2) {
long val=CP.gcd(l,l2);
return (l*l2)/val;
}
}
static void py() {
System.out.println("YES");
}
static void pn() {
System.out.println("NO");
}
static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(int[] array) {
for (int i = 0; i < array.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(array[i]);
}
}
public void printLine(int[] array) {
print(array);
writer.println();
}
public void close() {
writer.close();
}
public void printLine(long i) {
writer.println(i);
}
public void printLine(int i) {
writer.println(i);
}
public void printLine(char c)
{
writer.println(c);
}
public void print(Object... objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(objects[i]);
}
}
public void printLine(Object... objects) {
print(objects);
writer.println();
}
}
}
| Java | ["9 10\nabcabcabc\n1 a\n1 b\n2 c\n3 a\n4 b\n5 c\n8 a\n9 b\n1 c\n4 a"] | 2 seconds | ["3\n2\n2\n2\n1\n2\n1\n1\n1\n0"] | NoteLet's consider the state of the string after each query: $$$s =$$$ "abcabcabc". In this case $$$3$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcaccabb". This string does not contain "abc" as a substring. $$$s =$$$ "bbcabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bccabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bccbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcaabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcabbcabc". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaac". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaac". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "ccabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccaaccaab". In this case the string does not contain "abc" as a substring and no replacements are needed. | Java 11 | standard input | [
"implementation",
"strings"
] | db473ad780a93983667d12b1357c6e2f | The first line contains two integers $$$n$$$ and $$$q$$$ $$$(1 \le n, q \le 10^5)$$$, the length of the string and the number of queries, respectively. The second line contains the string $$$s$$$, consisting of characters "a", "b" and "c". Each of the next $$$q$$$ lines contains an integer $$$i$$$ and character $$$c$$$ $$$(1 \le i \le n)$$$, index and the value of the new item in the string, respectively. It is guaranteed that character's $$$c$$$ value is "a", "b" or "c". | 1,100 | For each query output the minimal number of characters that would have to be replaced so that the string doesn't contain "abc" as a substring. | standard output | |
PASSED | 6b5a3a727868ffdd6e23b1e8649e17fe | train_110.jsonl | 1638110100 | Before becoming a successful trader William got a university degree. During his education an interesting situation happened, after which William started to listen to homework assignments much more attentively. What follows is the correct formal description of the homework assignment:You are given a string $$$s$$$ of length $$$n$$$ only consisting of characters "a", "b" and "c". There are $$$q$$$ queries of format ($$$pos, c$$$), meaning replacing the element of string $$$s$$$ at position $$$pos$$$ with character $$$c$$$. After each query you must output the minimal number of characters in the string, which have to be replaced, so that the string doesn't contain string "abc" as a substring. A valid replacement of a character is replacing it with "a", "b" or "c".A string $$$x$$$ is a substring of a string $$$y$$$ if $$$x$$$ can be obtained from $$$y$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. | 256 megabytes | import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class B {
private static boolean hasString(int index, char[] s)
{
for(int i = index-2; i <= index; i++)
{
if(i >= 0 && i < s.length-2)
{
if(s[i] == 'a' && s[i+1] == 'b' && s[i+2] == 'c')
return true;
}
}
return false;
}
public static void main(String[] args) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] nq = br.readLine().split(" ");
int n = Integer.parseInt(nq[0]);
int q = Integer.parseInt(nq[1]);
char[] s = br.readLine().toCharArray();
int count = 0;
for(int i = 0; i < n-2; i++)
{
if(s[i] == 'a' && s[i+1] == 'b' && s[i+2] == 'c')
count++;
}
for(int i = 0; i < q; i++)
{
String[] query = br.readLine().split(" ");
int index = Integer.parseInt(query[0]) - 1;
char c = query[1].toCharArray()[0];
if(hasString(index, s))
count--;
s[index] = c;
if(hasString(index, s))
count++;
System.out.println(count);
}
br.close();
}
}
| Java | ["9 10\nabcabcabc\n1 a\n1 b\n2 c\n3 a\n4 b\n5 c\n8 a\n9 b\n1 c\n4 a"] | 2 seconds | ["3\n2\n2\n2\n1\n2\n1\n1\n1\n0"] | NoteLet's consider the state of the string after each query: $$$s =$$$ "abcabcabc". In this case $$$3$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcaccabb". This string does not contain "abc" as a substring. $$$s =$$$ "bbcabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bccabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bccbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcaabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcabbcabc". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaac". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaac". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "ccabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccaaccaab". In this case the string does not contain "abc" as a substring and no replacements are needed. | Java 11 | standard input | [
"implementation",
"strings"
] | db473ad780a93983667d12b1357c6e2f | The first line contains two integers $$$n$$$ and $$$q$$$ $$$(1 \le n, q \le 10^5)$$$, the length of the string and the number of queries, respectively. The second line contains the string $$$s$$$, consisting of characters "a", "b" and "c". Each of the next $$$q$$$ lines contains an integer $$$i$$$ and character $$$c$$$ $$$(1 \le i \le n)$$$, index and the value of the new item in the string, respectively. It is guaranteed that character's $$$c$$$ value is "a", "b" or "c". | 1,100 | For each query output the minimal number of characters that would have to be replaced so that the string doesn't contain "abc" as a substring. | standard output | |
PASSED | 897edad5af0dadbae0ab5f8a964a5dce | train_110.jsonl | 1638110100 | Before becoming a successful trader William got a university degree. During his education an interesting situation happened, after which William started to listen to homework assignments much more attentively. What follows is the correct formal description of the homework assignment:You are given a string $$$s$$$ of length $$$n$$$ only consisting of characters "a", "b" and "c". There are $$$q$$$ queries of format ($$$pos, c$$$), meaning replacing the element of string $$$s$$$ at position $$$pos$$$ with character $$$c$$$. After each query you must output the minimal number of characters in the string, which have to be replaced, so that the string doesn't contain string "abc" as a substring. A valid replacement of a character is replacing it with "a", "b" or "c".A string $$$x$$$ is a substring of a string $$$y$$$ if $$$x$$$ can be obtained from $$$y$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. | 256 megabytes | import java.io.PrintWriter;
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner obj = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
int len = 1;
while (len-- != 0) {
int n = obj.nextInt();
int q = obj.nextInt();
String s = obj.next();
char[] chh = s.toCharArray();
int c = 0;
for (int i = 0; i < s.length(); i++) {
if (chh[i] == 'c' && i - 2 >= 0) {
if (chh[i - 1] == 'b' && chh[i - 2] == 'a') c++;
}
}
for (int i = 0; i < q; i++) {
int index = obj.nextInt() - 1;
char ch = obj.next().charAt(0);
if(chh[index]==ch)
{
out.println(c);
}
else {
if (chh[index] == 'a' && index + 2 <= s.length() - 1) {
if (chh[index + 1] == 'b' && chh[index + 2] == 'c') if (chh[index] != ch) c--;
} else if (chh[index] == 'b' && index - 1 >= 0 && index + 1 <= s.length() - 1) {
if (chh[index - 1] == 'a' && chh[index + 1] == 'c') if (chh[index] != ch) c--;
} else if (chh[index] == 'c' && index - 2 >= 0) {
if (chh[index - 2] == 'a' && chh[index - 1] == 'b') if (chh[index] != ch) c--;
}
chh[index] = ch;
if (chh[index] == 'a' && index + 2 <= s.length() - 1) {
if (chh[index + 1] == 'b' && chh[index + 2] == 'c') c++;
} else if (chh[index] == 'b' && index - 1 >= 0 && index + 1 <= s.length() - 1) {
if (chh[index - 1] == 'a' && chh[index + 1] == 'c') c++;
} else if (chh[index] == 'c' && index - 2 >= 0) {
if (chh[index - 2] == 'a' && chh[index - 1] == 'b') c++;
}
out.println(c);
}
}
}
out.flush();
}
} | Java | ["9 10\nabcabcabc\n1 a\n1 b\n2 c\n3 a\n4 b\n5 c\n8 a\n9 b\n1 c\n4 a"] | 2 seconds | ["3\n2\n2\n2\n1\n2\n1\n1\n1\n0"] | NoteLet's consider the state of the string after each query: $$$s =$$$ "abcabcabc". In this case $$$3$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcaccabb". This string does not contain "abc" as a substring. $$$s =$$$ "bbcabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bccabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bccbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcaabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcabbcabc". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaac". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaac". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "ccabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccaaccaab". In this case the string does not contain "abc" as a substring and no replacements are needed. | Java 11 | standard input | [
"implementation",
"strings"
] | db473ad780a93983667d12b1357c6e2f | The first line contains two integers $$$n$$$ and $$$q$$$ $$$(1 \le n, q \le 10^5)$$$, the length of the string and the number of queries, respectively. The second line contains the string $$$s$$$, consisting of characters "a", "b" and "c". Each of the next $$$q$$$ lines contains an integer $$$i$$$ and character $$$c$$$ $$$(1 \le i \le n)$$$, index and the value of the new item in the string, respectively. It is guaranteed that character's $$$c$$$ value is "a", "b" or "c". | 1,100 | For each query output the minimal number of characters that would have to be replaced so that the string doesn't contain "abc" as a substring. | standard output | |
PASSED | 9f556316666b55c6a3bb3091a5cc9d9b | train_110.jsonl | 1638110100 | Before becoming a successful trader William got a university degree. During his education an interesting situation happened, after which William started to listen to homework assignments much more attentively. What follows is the correct formal description of the homework assignment:You are given a string $$$s$$$ of length $$$n$$$ only consisting of characters "a", "b" and "c". There are $$$q$$$ queries of format ($$$pos, c$$$), meaning replacing the element of string $$$s$$$ at position $$$pos$$$ with character $$$c$$$. After each query you must output the minimal number of characters in the string, which have to be replaced, so that the string doesn't contain string "abc" as a substring. A valid replacement of a character is replacing it with "a", "b" or "c".A string $$$x$$$ is a substring of a string $$$y$$$ if $$$x$$$ can be obtained from $$$y$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. | 256 megabytes | /* package codechef; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
public class Codechef
{static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
// static int mod = 998244353 ;
// static int N = 200005;
// static long factorial_num_inv[] = new long[N+1];
// static long natual_num_inv[] = new long[N+1];
// static long fact[] = new long[N+1];
// static void InverseofNumber()
//{
// natual_num_inv[0] = 1;
// natual_num_inv[1] = 1;
// for (int i = 2; i <= N; i++)
// natual_num_inv[i] = natual_num_inv[mod % i] * (mod - mod / i) % mod;
//}
//static void InverseofFactorial()
//{
// factorial_num_inv[0] = factorial_num_inv[1] = 1;
// for (int i = 2; i <= N; i++)
// factorial_num_inv[i] = (natual_num_inv[i] * factorial_num_inv[i - 1]) % mod;
//}
//static long nCrModP(long N, long R)
//{
// long ans = ((fact[(int)N] * factorial_num_inv[(int)R]) % mod * factorial_num_inv[(int)(N - R)]) % mod;
// return ans%mod;
//}
//static boolean prime[];
//static void sieveOfEratosthenes(int n)
//{
// prime = new boolean[n+1];
// for (int i = 0; i <= n; i++)
// prime[i] = true;
// for (int p = 2; p * p <= n; p++)
// {
// // If prime[p] is not changed, then it is a
// // prime
// if (prime[p] == true)
// {
// // Update all multiples of p
// for (int i = p * p; i <= n; i += p)
// prime[i] = false;
// }
// }
//}
public static void main (String[] args) throws java.lang.Exception
{
// InverseofNumber();
// InverseofFactorial();
// fact[0] = 1;
// for (long i = 1; i <= 2*100000; i++)
// {
// fact[(int)i] = (fact[(int)i - 1] * i) % mod;
// }
FastReader scan = new FastReader();
PrintWriter pw = new PrintWriter(System.out);
int n = scan.nextInt();
int q = scan.nextInt();
char a[] = scan.next().toCharArray();
int count = 0;
for(int i=0;i<n-2;i++){
if(a[i]=='a' && a[i+1]=='b' && a[i+2]=='c')
count++;
}
while(q-->0){
int index = scan.nextInt();
index--;
char ch = scan.next().charAt(0);
if(ch=='a'){
if(a[index]=='a'){
}
else if(a[index]=='b'){
if((index-1)>=0 && (index+1)<n && a[index-1]=='a' && a[index+1]=='c'){
count--;
}
if((index+2)<n && a[index+1]=='b' && a[index+2]=='c')
count++;
a[index] = 'a';
}
else{
if((index-2)>=0 && a[index-2]=='a' && a[index-1]=='b'){
count--;
}
if((index+2)<n && a[index+1]=='b' && a[index+2]=='c')
count++;
a[index]='a';
}
}
else if(ch=='b'){
if(a[index]=='b'){
}
else if(a[index]=='a'){
if(index+2<n && a[index+1]=='b' && a[index+2]=='c'){
count--;
}
if((index-1)>=0 && (index+1)<n && a[index-1]=='a' && a[index+1]=='c')
count++;
a[index] = 'b';
}
else{
if((index-2)>=0 && a[index-2]=='a' && a[index-1]=='b'){
count--;
}
if((index-1)>=0 && (index+1)<n && a[index-1]=='a' && a[index+1]=='c')
count++;
a[index]='b';
}
}
else{
if(a[index]=='c'){
}
else if(a[index]=='a'){
if(index+2<n && a[index+1]=='b' && a[index+2]=='c'){
count--;
}
if((index-2)>=0 && a[index-2]=='a' && a[index-1]=='b')
count++;
a[index] = 'c';
}
else{
if((index-1)>=0 && (index+1)<n && a[index-1]=='a' && a[index+1]=='c'){
count--;
}
if((index-2)>=0 && a[index-2]=='a' && a[index-1]=='b')
count++;
a[index]='c';
}
}
pw.println(count);
pw.flush();
}
}
//static long bin_exp_mod(long a,long n){
// long res = 1;
// if(a==0)
// return 0;
// while(n!=0){
// if(n%2==1){
// res = ((res)*(a));
// }
// n = n/2;
// a = ((a)*(a));
// }
// return res;
//}
//static long bin_exp_mod(long a,long n){
// long mod = 998244353;
// long res = 1;
// a = a%mod;
// if(a==0)
// return 0;
// while(n!=0){
// if(n%2==1){
// res = ((res%mod)*(a%mod))%mod;
// }
// n = n/2;
// a = ((a%mod)*(a%mod))%mod;
// }
// res = res%mod;
// return res;
//}
// static long gcd(long a,long b){
// if(a==0)
// return b;
// return gcd(b%a,a);
// }
// static long lcm(long a,long b){
// return (a/gcd(a,b))*b;
// }
}
//class Pair{
// Integer x,y;
// Pair(int x,int y){
// this.x = x;
// this.y = y;
// }
// public boolean equals(Object obj) {
// // TODO Auto-generated method stub
// if(obj instanceof Pair)
// {
// Pair temp = (Pair) obj;
// if(this.x.equals(temp.x) && this.y.equals(temp.y))
// return true;
// }
// return false;
// }
// @Override
// public int hashCode() {
// // TODO Auto-generated method stub
// return (this.x.hashCode() + this.y.hashCode());
// }
//}
//class Compar implements Comparator<Pair>{
// public int compare(Pair p1,Pair p2){
// return p1.x-p2.x;
// }
//} | Java | ["9 10\nabcabcabc\n1 a\n1 b\n2 c\n3 a\n4 b\n5 c\n8 a\n9 b\n1 c\n4 a"] | 2 seconds | ["3\n2\n2\n2\n1\n2\n1\n1\n1\n0"] | NoteLet's consider the state of the string after each query: $$$s =$$$ "abcabcabc". In this case $$$3$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcaccabb". This string does not contain "abc" as a substring. $$$s =$$$ "bbcabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bccabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bccbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcaabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcabbcabc". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaac". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaac". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "ccabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccaaccaab". In this case the string does not contain "abc" as a substring and no replacements are needed. | Java 11 | standard input | [
"implementation",
"strings"
] | db473ad780a93983667d12b1357c6e2f | The first line contains two integers $$$n$$$ and $$$q$$$ $$$(1 \le n, q \le 10^5)$$$, the length of the string and the number of queries, respectively. The second line contains the string $$$s$$$, consisting of characters "a", "b" and "c". Each of the next $$$q$$$ lines contains an integer $$$i$$$ and character $$$c$$$ $$$(1 \le i \le n)$$$, index and the value of the new item in the string, respectively. It is guaranteed that character's $$$c$$$ value is "a", "b" or "c". | 1,100 | For each query output the minimal number of characters that would have to be replaced so that the string doesn't contain "abc" as a substring. | standard output | |
PASSED | ed0d2b6e17107f2dc7817a1b7ab4bba8 | train_110.jsonl | 1638110100 | Before becoming a successful trader William got a university degree. During his education an interesting situation happened, after which William started to listen to homework assignments much more attentively. What follows is the correct formal description of the homework assignment:You are given a string $$$s$$$ of length $$$n$$$ only consisting of characters "a", "b" and "c". There are $$$q$$$ queries of format ($$$pos, c$$$), meaning replacing the element of string $$$s$$$ at position $$$pos$$$ with character $$$c$$$. After each query you must output the minimal number of characters in the string, which have to be replaced, so that the string doesn't contain string "abc" as a substring. A valid replacement of a character is replacing it with "a", "b" or "c".A string $$$x$$$ is a substring of a string $$$y$$$ if $$$x$$$ can be obtained from $$$y$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. | 256 megabytes | // package c1609;
import java.io.File;
import java.lang.invoke.MethodHandles;
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
//
// Deltix Round, Autumn 2021 (open for everyone, rated, Div. 1 + Div. 2) 2021-11-28 06:35
// B. William the Vigilant
// https://codeforces.com/contest/1609/problem/B
// time limit per test 2 seconds; memory limit per test 256 megabytes
//
// Before becoming a successful trader William got a university degree. During his education an
// interesting situation happened, after which William started to listen to homework assignments
// much more attentively. What follows is the correct formal description of the homework assignment:
//
// You are given a string s of length n only consisting of characters "a", "b" and "c". There are q
// queries of format (pos,c), meaning replacing the element of string s at position pos with
// character c. After each query you must output the minimal number of characters in the string,
// which have to be replaced, so that the string doesn't contain string "abc" as a substring. A
// valid replacement of a character is replacing it with "a", "b" or "c".
//
// A string x is a substring of a string y if x can be obtained from y by deletion of several
// (possibly, zero or all) characters from the beginning and several (possibly, zero or all)
// characters from the end.
//
// Input
//
// The first line contains two integers n and q (1<=n,q<=10^5), the length of the string and the
// number of queries, respectively.
//
// The second line contains the string s, consisting of characters "a", "b" and "c".
//
// Each of the next q lines contains an integer i and character c (1<=i<=n), index and the value of
// the new item in the string, respectively. It is guaranteed that character's c value is "a", "b"
// or "c".
//
// Output
//
// For each query output the minimal number of characters that would have to be replaced so that the
// string doesn't contain "abc" as a substring.
//
// Example
/*
input:
9 10
abcabcabc
1 a
1 b
2 c
3 a
4 b
5 c
8 a
9 b
1 c
4 a
output:
3
2
2
2
1
2
1
1
1
0
*/
// Note
//
public class C1609B {
static final int MOD = (int)1e9+7;
static final Random RAND = new Random();
static int[] solve(String s, int[] pos, char[] val) {
int n = s.length();
int q = pos.length;
int[] ans = new int[q];
char[] ca = s.toCharArray();
int count = countOfAbc(ca);
for (int i = 0; i < q; i++) {
int idx = pos[i] - 1;
char c = val[i];
count += delta(ca, idx, c);
ca[idx] = c;
ans[i] = count;
}
return ans;
}
static int delta(char[] ca, int idx, char c) {
int ans = 0;
if (c == ca[idx]) {
return ans;
}
int n = ca.length;
// Note that it is possible to eliminate abs and introduce abc at the same time. For example:
// in "abcbcbaabb", change character at index 2 to be a.
if (ca[idx] == 'a' && idx + 2 < n && ca[idx+1] == 'b' && ca[idx+2] == 'c') {
ans--;
} else if (ca[idx] == 'b' && idx - 1 >= 0 && idx + 1 < n && ca[idx-1] == 'a' && ca[idx+1] == 'c') {
ans--;
} else if (ca[idx] == 'c' && idx - 2 >= 0 && ca[idx-2] == 'a' && ca[idx-1] == 'b') {
ans--;
}
if (c == 'a' && idx + 2 < n && ca[idx+1] == 'b' && ca[idx+2] == 'c') {
ans++;
} else if (c == 'b' && idx - 1 >= 0 && idx + 1 < n && ca[idx-1] == 'a' && ca[idx+1] == 'c') {
ans++;
} else if (c == 'c' && idx - 2 >= 0 && ca[idx-2] == 'a' && ca[idx-1] == 'b') {
ans++;
}
return ans;
}
static int countOfAbc(char[] ca) {
int n = ca.length;
int ans = 0;
for (int i = 0; i < n - 2; i++) {
if (ca[i] == 'a' && ca[i+1] == 'b' && ca[i+2] == 'c') {
ans++;
}
}
return ans;
}
static void doTest() {
int n = 1000;
int q = 100;
char[] ca = new char[n];
for (int i = 0; i < n; i++) {
char c = (char) ('a' + RAND.nextInt(3));
ca[i] = c;
}
int[] pos = new int[q];
char[] val = new char[q];
int[] exp = new int[q];
String s = new String(ca);
for (int i = 0; i < q; i++) {
pos[i] = 1 + RAND.nextInt(n);
char c = (char) ('a' + RAND.nextInt(3));
val[i] = c;
ca[pos[i] - 1] = c;
exp[i] = countOfAbc(ca);
}
System.out.println(s);
int[] ans = solve(s, pos, val);
for (int i = 0; i < q; i++) {
if (ans[i] != exp[i]) {
System.out.format(" i:%d idx:%2d c:%c ans:%d exp:%d\n", i, pos[i]-1, val[i], ans[i], exp[i]);
}
}
System.out.println(Arrays.toString(ans));
System.out.println(Arrays.toString(exp));
System.exit(0);
}
public static void main(String[] args) {
// doTest();
Scanner in = getInputScanner();
int n = in.nextInt();
int q = in.nextInt();
String s = in.next();
int[] pos = new int[q];
char[] val = new char[q];
for (int i = 0; i < q; i++) {
pos[i] = in.nextInt();
val[i] = in.next().charAt(0);
}
int[] ans = solve(s, pos, val);
StringBuilder sb = new StringBuilder();
for (int v : ans) {
sb.append(v);
sb.append('\n');
}
System.out.println(sb.toString().trim());
in.close();
}
static Scanner getInputScanner() {
try {
final String USERDIR = System.getProperty("user.dir");
final String CNAME = MethodHandles.lookup().lookupClass().getSimpleName();
final File fin = new File(USERDIR + "/io/c" + CNAME.substring(1,5) + "/" + CNAME + ".in");
return fin.exists() ? new Scanner(fin) : new Scanner(System.in);
} catch (Exception e) {
return new Scanner(System.in);
}
}
}
| Java | ["9 10\nabcabcabc\n1 a\n1 b\n2 c\n3 a\n4 b\n5 c\n8 a\n9 b\n1 c\n4 a"] | 2 seconds | ["3\n2\n2\n2\n1\n2\n1\n1\n1\n0"] | NoteLet's consider the state of the string after each query: $$$s =$$$ "abcabcabc". In this case $$$3$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcaccabb". This string does not contain "abc" as a substring. $$$s =$$$ "bbcabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bccabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bccbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcaabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcabbcabc". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaac". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaac". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "ccabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccaaccaab". In this case the string does not contain "abc" as a substring and no replacements are needed. | Java 11 | standard input | [
"implementation",
"strings"
] | db473ad780a93983667d12b1357c6e2f | The first line contains two integers $$$n$$$ and $$$q$$$ $$$(1 \le n, q \le 10^5)$$$, the length of the string and the number of queries, respectively. The second line contains the string $$$s$$$, consisting of characters "a", "b" and "c". Each of the next $$$q$$$ lines contains an integer $$$i$$$ and character $$$c$$$ $$$(1 \le i \le n)$$$, index and the value of the new item in the string, respectively. It is guaranteed that character's $$$c$$$ value is "a", "b" or "c". | 1,100 | For each query output the minimal number of characters that would have to be replaced so that the string doesn't contain "abc" as a substring. | standard output | |
PASSED | b608d0339f3054869531778c69dbfeb3 | train_110.jsonl | 1638110100 | Before becoming a successful trader William got a university degree. During his education an interesting situation happened, after which William started to listen to homework assignments much more attentively. What follows is the correct formal description of the homework assignment:You are given a string $$$s$$$ of length $$$n$$$ only consisting of characters "a", "b" and "c". There are $$$q$$$ queries of format ($$$pos, c$$$), meaning replacing the element of string $$$s$$$ at position $$$pos$$$ with character $$$c$$$. After each query you must output the minimal number of characters in the string, which have to be replaced, so that the string doesn't contain string "abc" as a substring. A valid replacement of a character is replacing it with "a", "b" or "c".A string $$$x$$$ is a substring of a string $$$y$$$ if $$$x$$$ can be obtained from $$$y$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. | 256 megabytes | import java.util.*;
public class Codeforces {
// public static void main(String[] args) {
// Scanner scanner = new Scanner(System.in);
// int T = scanner.nextInt();
//
// while (T-- > 0) {
// int N = scanner.nextInt();
// long[] arr = new long[N];
// for (int i = 0; i < N; i++) {
// arr[i] = scanner.nextInt();
// }
//
// int counter = 0;
// int max = 0;
// for(int i = 0; i < N; i++) {
// while (arr[i] % 2 == 0) {
// counter++;
// arr[i] /= 2;
// }
// if(arr[i] > arr[max]) {
// max = i;
// }
// }
//
// String s = "wdasd";
//
//
// for(int i = 0; i < counter; i++) {
// arr[max] *= 2;
// }
//
// System.out.println(Arrays.stream(arr).sum());
//
// }
// }
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int q = scanner.nextInt();
char[] s = scanner.next().toCharArray();
Set<Integer> subs = new HashSet<>();
int counter = 0;
for(int i = 0; i < s.length - 2; i++) {
if(s[i] == 'a' && s[i + 1] == 'b' && s[i + 2] == 'c'){
counter++;
subs.add(i);
}
}
for(int i = 0; i < q; i ++) {
int index = scanner.nextInt() - 1;
char c = scanner.next().charAt(0);
s[index] = c;
//System.out.println("index = " + index + ", c = " + c + ", subs = " + s);
//System.out.println(s);
if(subs.contains(index - 2)) {
if(c == 'c'){
System.out.println(counter);
continue;
} else {
subs.remove(index - 2);
counter--;
}
} else if (subs.contains(index - 1)) {
if(c == 'b') {
System.out.println(counter);
continue;
} else {
subs.remove(index -1);
counter--;
}
} else if(subs.contains(index)){
if(c == 'a'){
System.out.println(counter);
continue;
} else {
subs.remove(index);
counter--;
}
}
if(index < n - 2 && c == 'a' && s[index + 1] == 'b' && s[index + 2] == 'c'){
subs.add(index);
counter++;
}else if(c == 'b' && index < n - 1 && index > 0 && s[index - 1] == 'a' && s[index + 1] == 'c'){
subs.add(index - 1);
counter++;
}else if(c == 'c' && index > 1 && s[index - 2] == 'a' && s[index - 1] == 'b'){
subs.add(index - 2);
counter++;
}
System.out.println(counter);
}
}
}
| Java | ["9 10\nabcabcabc\n1 a\n1 b\n2 c\n3 a\n4 b\n5 c\n8 a\n9 b\n1 c\n4 a"] | 2 seconds | ["3\n2\n2\n2\n1\n2\n1\n1\n1\n0"] | NoteLet's consider the state of the string after each query: $$$s =$$$ "abcabcabc". In this case $$$3$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcaccabb". This string does not contain "abc" as a substring. $$$s =$$$ "bbcabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bccabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bccbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcaabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcabbcabc". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaac". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaac". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "ccabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccaaccaab". In this case the string does not contain "abc" as a substring and no replacements are needed. | Java 11 | standard input | [
"implementation",
"strings"
] | db473ad780a93983667d12b1357c6e2f | The first line contains two integers $$$n$$$ and $$$q$$$ $$$(1 \le n, q \le 10^5)$$$, the length of the string and the number of queries, respectively. The second line contains the string $$$s$$$, consisting of characters "a", "b" and "c". Each of the next $$$q$$$ lines contains an integer $$$i$$$ and character $$$c$$$ $$$(1 \le i \le n)$$$, index and the value of the new item in the string, respectively. It is guaranteed that character's $$$c$$$ value is "a", "b" or "c". | 1,100 | For each query output the minimal number of characters that would have to be replaced so that the string doesn't contain "abc" as a substring. | standard output | |
PASSED | 833c71e92558da7df3c809f596236fb9 | train_110.jsonl | 1638110100 | Before becoming a successful trader William got a university degree. During his education an interesting situation happened, after which William started to listen to homework assignments much more attentively. What follows is the correct formal description of the homework assignment:You are given a string $$$s$$$ of length $$$n$$$ only consisting of characters "a", "b" and "c". There are $$$q$$$ queries of format ($$$pos, c$$$), meaning replacing the element of string $$$s$$$ at position $$$pos$$$ with character $$$c$$$. After each query you must output the minimal number of characters in the string, which have to be replaced, so that the string doesn't contain string "abc" as a substring. A valid replacement of a character is replacing it with "a", "b" or "c".A string $$$x$$$ is a substring of a string $$$y$$$ if $$$x$$$ can be obtained from $$$y$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. | 256 megabytes |
/*
بسم الله الرحمن الرحيم
/$$$$$ /$$$$$$ /$$ /$$ /$$$$$$
|__ $$ /$$__ $$ |$$ |$$ /$$__ $$
| $$| $$ \ $$| $$|$$| $$ \ $$
| $$| $$$$$$$$| $$ / $$/| $$$$$$$$
/ $$ | $$| $$__ $$ \ $$ $$/ | $$__ $$
| $$ | $$| $$ | $$ \ $$$/ | $$ | $$
| $$$$$$/| $$ | $$ \ $/ | $$ | $$
\______/ |__/ |__/ \_/ |__/ |__/
/$$$$$$$ /$$$$$$$ /$$$$$$ /$$$$$$ /$$$$$$$ /$$$$$$ /$$ /$$ /$$ /$$ /$$$$$$$$ /$$$$$$$
| $$__ $$| $$__ $$ /$$__ $$ /$$__ $$| $$__ $$ /$$__ $$| $$$ /$$$| $$$ /$$$| $$_____/| $$__ $$
| $$ \ $$| $$ \ $$| $$ \ $$| $$ \__/| $$ \ $$| $$ \ $$| $$$$ /$$$$| $$$$ /$$$$| $$ | $$ \ $$
| $$$$$$$/| $$$$$$$/| $$ | $$| $$ /$$$$| $$$$$$$/| $$$$$$$$| $$ $$/$$ $$| $$ $$/$$ $$| $$$$$ | $$$$$$$/
| $$____/ | $$__ $$| $$ | $$| $$|_ $$| $$__ $$| $$__ $$| $$ $$$| $$| $$ $$$| $$| $$__/ | $$__ $$
| $$ | $$ \ $$| $$ | $$| $$ \ $$| $$ \ $$| $$ | $$| $$\ $ | $$| $$\ $ | $$| $$ | $$ \ $$
| $$ | $$ | $$| $$$$$$/| $$$$$$/| $$ | $$| $$ | $$| $$ \/ | $$| $$ \/ | $$| $$$$$$$$| $$ | $$
|__/ |__/ |__/ \______/ \______/ |__/ |__/|__/ |__/|__/ |__/|__/ |__/|________/|__/ |__/
*/
import java.util.*;
import java.lang.*;
import java.io.*;
public class Deltix281121B {
public static void main(String[] args) throws java.lang.Exception {
// your code goes here
try {
// Scanner sc=new Scanner(System.in);
FastReader sc = new FastReader();
int t = 1;// sc.nextInt();
while (t-- > 0) {
int n=sc.nextInt();
int q=sc.nextInt();
char[] arr=sc.next().toCharArray();
int cnt=0;
for(int i=0;i<n;i++){
if(arr[i]=='a' && i<n-2){
if(arr[i+1]=='b' && arr[i+2]=='c'){
cnt++;
i+=2;
}
}
}
//System.out.println(cnt);
while(q-->0){
int pos=sc.nextInt()-1;
char ch=sc.next().charAt(0);
boolean cabc=false;
if(arr[pos]=='a' && pos<n-2){
if(arr[pos+1]=='b' && arr[pos+2]=='c'){
cabc=true;
}
}
if(arr[pos]=='b' && pos<n-1 && pos>0){
if(arr[pos-1]=='a' && arr[pos+1]=='c'){
cabc=true;
}
}
if(arr[pos]=='c' && pos>1){
if(arr[pos-1]=='b' && arr[pos-2]=='a'){
cabc=true;
}
}
if(arr[pos]==ch){
System.out.println(cnt);
}
else{
if(cabc){
cnt--;
arr[pos]=ch;
if(arr[pos]=='a' && pos<n-2){
if(arr[pos+1]=='b' && arr[pos+2]=='c'){
cnt++;
}
}
if(arr[pos]=='b' && pos<n-1 && pos>0){
if(arr[pos-1]=='a' && arr[pos+1]=='c'){
cnt++;
}
}
if(arr[pos]=='c' && pos>1){
if(arr[pos-1]=='b' && arr[pos-2]=='a'){
cnt++;
}
}
}
else{
arr[pos]=ch;
if(arr[pos]=='a' && pos<n-2){
if(arr[pos+1]=='b' && arr[pos+2]=='c'){
cnt++;
}
}
if(arr[pos]=='b' && pos<n-1 && pos>0){
if(arr[pos-1]=='a' && arr[pos+1]=='c'){
cnt++;
}
}
if(arr[pos]=='c' && pos>1){
if(arr[pos-1]=='b' && arr[pos-2]=='a'){
cnt++;
}
}
}
System.out.println(cnt);
}
}
/*int[] arr=new int[n];
for(int i=0;i<n;i++){
arr[i]=sc.nextInt();
}*/
/*long[] arr=new long[n];
for(int i=0;i<n;i++){
arr[i]=sc.nextLong();
}*/
}
} catch (Exception e) {
return;
}
}
public static int lowerbound(int[] ar,int k)
{
int s=0;
int e=ar.length;
while (s !=e)
{
int mid = s+e>>1;
if (ar[mid] <k)
{
s=mid+1;
}
else
{
e=mid;
}
}
if(s==ar.length)
{
return -1;
}
return s;
}
public static class pair {
int ff;
int ss;
pair(int ff, int ss) {
this.ff = ff;
this.ss = ss;
}
}
static int BS(int[] arr, int l, int r, int element) {
int low = l;
int high = r;
while (high - low > 1) {
int mid = low + (high - low) / 2;
if (arr[mid] < element) {
low = mid + 1;
} else {
high = mid;
}
}
if (arr[low] == element) {
return low;
} else if (arr[high] == element) {
return high;
}
return -1;
}
static int lower_bound(int[] arr, int l, int r, int element) {
int low = l;
int high = r;
while (high - low > 1) {
int mid = low + (high - low) / 2;
if (arr[mid] < element) {
low = mid + 1;
} else {
high = mid;
}
}
if (arr[low] >= element) {
return low;
} else if (arr[high] >= element) {
return high;
}
return -1;
}
static int upper_bound(int[] arr, int l, int r, int element) {
int low = l;
int high = r;
while (high - low > 1) {
int mid = low + (high - low) / 2;
if (arr[mid] <= element) {
low = mid + 1;
} else {
high = mid;
}
}
if (arr[low] > element) {
return low;
} else if (arr[high] > element) {
return high;
}
return -1;
}
public static int upperbound(long[] arr, int k) {
int s = 0;
int e = arr.length;
while (s != e) {
int mid = s + e >> 1;
if (arr[mid] <= k) {
s = mid + 1;
} else {
e = mid;
}
}
if (s == arr.length) {
return -1;
}
return s;
}
public static long pow(long x,long y,long mod){
if(x==0)return 0l;
if(y==0)return 1l;
//(x^y)%mod
if(y%2l==1l){
return ((x%mod)*(pow(x,y-1l,mod)%mod))%mod;
}
return pow(((x%mod)*(x%mod))%mod,y/2l,mod);
}
public static long gcd_long(long a, long b) {
// a/b,a-> dividant b-> divisor
if (b == 0)
return a;
return gcd_long(b, a % b);
}
public static int gcd_int(int a, int b) {
// a/b,a-> dividant b-> divisor
if (b == 0)
return a;
return gcd_int(b, a % b);
}
public static int lcm(int a, int b) {
int gcd = gcd_int(a, b);
return (a * b) / gcd;
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
double nextDouble(){
return Double.valueOf(Integer.parseInt(next()));
}
String nextLine() {
String s = "";
try {
s = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return s;
}
Long nextLong() {
return Long.parseLong(next());
}
}
public static boolean contains(String main, String Substring) {
boolean flag=false;
if(main==null && main.trim().equals("")) {
return flag;
}
if(Substring==null) {
return flag;
}
char fullstring[]=main.toCharArray();
char sub[]=Substring.toCharArray();
int counter=0;
if(sub.length==0) {
flag=true;
return flag;
}
for(int i=0;i<fullstring.length;i++) {
if(fullstring[i]==sub[counter]) {
counter++;
} else {
counter=0;
}
if(counter==sub.length) {
flag=true;
return flag;
}
}
return flag;
}
}
/*
* public static boolean lie(int n,int m,int k){ if(n==1 && m==1 && k==0){
* return true; } if(n<1 || m<1 || k<0){ return false; } boolean
* tc=lie(n-1,m,k-m); boolean lc=lie(n,m-1,k-n); if(tc || lc){ return true; }
* return false; }
*/
| Java | ["9 10\nabcabcabc\n1 a\n1 b\n2 c\n3 a\n4 b\n5 c\n8 a\n9 b\n1 c\n4 a"] | 2 seconds | ["3\n2\n2\n2\n1\n2\n1\n1\n1\n0"] | NoteLet's consider the state of the string after each query: $$$s =$$$ "abcabcabc". In this case $$$3$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcaccabb". This string does not contain "abc" as a substring. $$$s =$$$ "bbcabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bccabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bccbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcaabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcabbcabc". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaac". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaac". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "ccabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccaaccaab". In this case the string does not contain "abc" as a substring and no replacements are needed. | Java 11 | standard input | [
"implementation",
"strings"
] | db473ad780a93983667d12b1357c6e2f | The first line contains two integers $$$n$$$ and $$$q$$$ $$$(1 \le n, q \le 10^5)$$$, the length of the string and the number of queries, respectively. The second line contains the string $$$s$$$, consisting of characters "a", "b" and "c". Each of the next $$$q$$$ lines contains an integer $$$i$$$ and character $$$c$$$ $$$(1 \le i \le n)$$$, index and the value of the new item in the string, respectively. It is guaranteed that character's $$$c$$$ value is "a", "b" or "c". | 1,100 | For each query output the minimal number of characters that would have to be replaced so that the string doesn't contain "abc" as a substring. | standard output | |
PASSED | 54772a016adddc38f0549a71022a3860 | train_110.jsonl | 1638110100 | Before becoming a successful trader William got a university degree. During his education an interesting situation happened, after which William started to listen to homework assignments much more attentively. What follows is the correct formal description of the homework assignment:You are given a string $$$s$$$ of length $$$n$$$ only consisting of characters "a", "b" and "c". There are $$$q$$$ queries of format ($$$pos, c$$$), meaning replacing the element of string $$$s$$$ at position $$$pos$$$ with character $$$c$$$. After each query you must output the minimal number of characters in the string, which have to be replaced, so that the string doesn't contain string "abc" as a substring. A valid replacement of a character is replacing it with "a", "b" or "c".A string $$$x$$$ is a substring of a string $$$y$$$ if $$$x$$$ can be obtained from $$$y$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. | 256 megabytes | import java.util.*;
import java.io.*;
public class C{
public static void main(String[] args)
{
FastScanner fs = new FastScanner();
PrintWriter out = new PrintWriter(System.out);
int t = 1;
for(int tt=0;tt<t;tt++) {
int n = fs.nextInt(); int q = fs.nextInt();
String str = fs.next();
char[] arr = str.toCharArray();
int ans = 0;
for(int i=0;i<n;i++) {
if(i+2 < n) {
if(str.substring(i, i+3).equals("abc"))ans++;
}
}
// out.println("Ans "+ans);
for(int qq=0;qq<q;qq++) {
int pos = fs.nextInt() - 1; char ch = fs.next().charAt(0);
if(arr[pos] != ch) {
// System.out.println("Inside: "+(qq+1));
String str1 = "";
String str2 = "";
String str3 = "";
if(pos+2 < n) {
for(int start=pos;start<=pos+2;start++)str1 += arr[start];
}
if(pos-2 >= 0) {
for(int start=pos-2;start<=pos;start++)str2 += arr[start];
}
if(pos-1 >= 0 && pos+1 <n) {
for(int start=pos-1;start<=pos+1;start++)str3 += arr[start];
}
if(str1.equals("abc") || str2.equals("abc") || str3.equals("abc")) {
ans -= 1;
}
// System.out.println(qq+" "+str1+" "+str2+" "+str3);
if(ch == 'c') {
if(pos-2 >= 0 && arr[pos-1] == 'b' && arr[pos-2] == 'a' && arr[pos] != 'c') {
ans += 1;
}
arr[pos] = 'c';
}
else if(ch == 'b') {
if(pos+1 < n && pos-1 >= 0 && arr[pos-1] == 'a' && arr[pos+1] == 'c' && arr[pos] != 'b') {
ans += 1;
}
arr[pos] = 'b';
}
else {
if(pos+2 < n && arr[pos+1] == 'b' && arr[pos+2] == 'c' && arr[pos] != 'a') {
ans += 1;
}
arr[pos] = 'a';
}
}
out.println(ans);
}
}
out.close();
}
static class FastScanner {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
}
public static long[] sort(long[] arr)
{
List<Long> temp = new ArrayList();
for(long i:arr)temp.add(i);
Collections.sort(temp);
int start = 0;
for(long i:temp)arr[start++]=i;
return arr;
}
public static String rev(String str)
{
char[] arr = str.toCharArray();
char[] ret = new char[arr.length];
int start = arr.length-1;
for(char i:arr)ret[start--] = i;
String ret1 = "";
for(char ch:ret)ret1+=ch;
return ret1;
}
} | Java | ["9 10\nabcabcabc\n1 a\n1 b\n2 c\n3 a\n4 b\n5 c\n8 a\n9 b\n1 c\n4 a"] | 2 seconds | ["3\n2\n2\n2\n1\n2\n1\n1\n1\n0"] | NoteLet's consider the state of the string after each query: $$$s =$$$ "abcabcabc". In this case $$$3$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcaccabb". This string does not contain "abc" as a substring. $$$s =$$$ "bbcabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bccabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bccbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcaabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcabbcabc". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaac". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaac". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "ccabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccaaccaab". In this case the string does not contain "abc" as a substring and no replacements are needed. | Java 11 | standard input | [
"implementation",
"strings"
] | db473ad780a93983667d12b1357c6e2f | The first line contains two integers $$$n$$$ and $$$q$$$ $$$(1 \le n, q \le 10^5)$$$, the length of the string and the number of queries, respectively. The second line contains the string $$$s$$$, consisting of characters "a", "b" and "c". Each of the next $$$q$$$ lines contains an integer $$$i$$$ and character $$$c$$$ $$$(1 \le i \le n)$$$, index and the value of the new item in the string, respectively. It is guaranteed that character's $$$c$$$ value is "a", "b" or "c". | 1,100 | For each query output the minimal number of characters that would have to be replaced so that the string doesn't contain "abc" as a substring. | standard output | |
PASSED | bec03864a5fcb07bccd4854d2fba6a4e | train_110.jsonl | 1638110100 | Before becoming a successful trader William got a university degree. During his education an interesting situation happened, after which William started to listen to homework assignments much more attentively. What follows is the correct formal description of the homework assignment:You are given a string $$$s$$$ of length $$$n$$$ only consisting of characters "a", "b" and "c". There are $$$q$$$ queries of format ($$$pos, c$$$), meaning replacing the element of string $$$s$$$ at position $$$pos$$$ with character $$$c$$$. After each query you must output the minimal number of characters in the string, which have to be replaced, so that the string doesn't contain string "abc" as a substring. A valid replacement of a character is replacing it with "a", "b" or "c".A string $$$x$$$ is a substring of a string $$$y$$$ if $$$x$$$ can be obtained from $$$y$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) {
FastReader fastReader = new FastReader();
PrintWriter printWriter = new PrintWriter(System.out);
int t = 1;
while (t-- > 0) {
int n = fastReader.nextInt();
int q = fastReader.nextInt();
StringBuilder s = new StringBuilder(fastReader.nextLine());
int ans = 0;
for (int i = 0; i < n-2; i++) {
if (s.substring(i, i+3).equals("abc")) {
ans++;
}
}
for (int i = 0; i < q; i++) {
String[] inp = fastReader.nextLine().split(" ");
int ind = Integer.parseInt(inp[0]) - 1;
char c = inp[1].charAt(0);
ans -= count(s, ind);
s.setCharAt(ind, c);
ans += count(s, ind);
System.out.println(ans);
}
}
printWriter.close();
}
static int count(StringBuilder s, int i) {
int count = 0;
for (int j = i-2; j <= i && j+2 < s.length(); j++) {
if (j < 0) continue;
if (s.substring(j, j+3).equals("abc")) {
count++;
}
}
return count;
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
| Java | ["9 10\nabcabcabc\n1 a\n1 b\n2 c\n3 a\n4 b\n5 c\n8 a\n9 b\n1 c\n4 a"] | 2 seconds | ["3\n2\n2\n2\n1\n2\n1\n1\n1\n0"] | NoteLet's consider the state of the string after each query: $$$s =$$$ "abcabcabc". In this case $$$3$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcaccabb". This string does not contain "abc" as a substring. $$$s =$$$ "bbcabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bccabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bccbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcaabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcabbcabc". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaac". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaac". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "ccabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccaaccaab". In this case the string does not contain "abc" as a substring and no replacements are needed. | Java 11 | standard input | [
"implementation",
"strings"
] | db473ad780a93983667d12b1357c6e2f | The first line contains two integers $$$n$$$ and $$$q$$$ $$$(1 \le n, q \le 10^5)$$$, the length of the string and the number of queries, respectively. The second line contains the string $$$s$$$, consisting of characters "a", "b" and "c". Each of the next $$$q$$$ lines contains an integer $$$i$$$ and character $$$c$$$ $$$(1 \le i \le n)$$$, index and the value of the new item in the string, respectively. It is guaranteed that character's $$$c$$$ value is "a", "b" or "c". | 1,100 | For each query output the minimal number of characters that would have to be replaced so that the string doesn't contain "abc" as a substring. | standard output | |
PASSED | a0ccf1e1718edbb77119a56754ea27d0 | train_110.jsonl | 1638110100 | Before becoming a successful trader William got a university degree. During his education an interesting situation happened, after which William started to listen to homework assignments much more attentively. What follows is the correct formal description of the homework assignment:You are given a string $$$s$$$ of length $$$n$$$ only consisting of characters "a", "b" and "c". There are $$$q$$$ queries of format ($$$pos, c$$$), meaning replacing the element of string $$$s$$$ at position $$$pos$$$ with character $$$c$$$. After each query you must output the minimal number of characters in the string, which have to be replaced, so that the string doesn't contain string "abc" as a substring. A valid replacement of a character is replacing it with "a", "b" or "c".A string $$$x$$$ is a substring of a string $$$y$$$ if $$$x$$$ can be obtained from $$$y$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. | 256 megabytes | import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.InputMismatchException;
/**
* @author ethan55
*/
public class Solution {
public static void main(String[] args) {
TestRunner.run();
}
public static class TestCase {
static final boolean MULTIPLE = false;
public void solve(InputReader in, OutputWriter out) {
int n = in.nextInt();
int q = in.nextInt();
char[] s = in.nextCharArray(n);
boolean[] valid = new boolean[s.length];
int count = 0;
for (int i = 2; i < s.length; i++) {
if (s[i] == 'c' && s[i - 1] == 'b' && s[i - 2] == 'a') {
valid[i] = true;
valid[i - 1] = true;
valid[i - 2] = true;
count++;
}
}
for (int query = 0; query < q; query++) {
int i = in.nextInt() - 1;
char c = in.nextChar();
char prev = s[i];
if (c == prev) {
out.println(count);
continue;
}
s[i] = c;
if (valid[i]) {
valid[i] = false;
if (prev == 'a') {
valid[i + 1] = false;
valid[i + 2] = false;
} else if (prev == 'b') {
valid[i - 1] = false;
valid[i + 1] = false;
} else if (prev == 'c') {
valid[i - 1] = false;
valid[i - 2] = false;
}
count--;
}
if (s[i] == 'a' && i < s.length - 2 && s[i + 1] == 'b' && s[i + 2] == 'c') {
valid[i] = true;
valid[i + 1] = true;
valid[i + 2] = true;
count++;
} else if (s[i] == 'b' && i > 0 && s[i - 1] == 'a' && i < s.length - 1 && s[i + 1] == 'c') {
valid[i] = true;
valid[i - 1] = true;
valid[i + 1] = true;
count++;
} else if (s[i] == 'c' && i > 1 && s[i - 1] == 'b' && s[i - 2] == 'a') {
valid[i] = true;
valid[i - 1] = true;
valid[i - 2] = true;
count++;
}
out.println(count);
}
}
}
public static class TestRunner {
public static void run() {
InputReader in = new InputReader(System.in);
try (OutputWriter out = new OutputWriter(System.out)) {
int testCases = TestCase.MULTIPLE ? in.nextInt() : 1;
for (int i = 1; i <= testCases; i++) {
new TestCase().solve(in, out);
}
}
}
}
public static class OutputWriter implements AutoCloseable {
private static final int BUFFER_SIZE = 1 << 13;
private final byte[] outputBuffer = new byte[BUFFER_SIZE];
private final OutputStream outputStream;
private int outputBufferIndex = 0;
public OutputWriter(OutputStream os) {
this.outputStream = os;
}
public OutputWriter println(int x) {
return writeln(x);
}
public void flush() {
innerFlush();
try {
outputStream.flush();
} catch (IOException e) {
throw new IllegalStateException("Failed to flush");
}
}
private OutputWriter write(byte b) {
outputBuffer[outputBufferIndex++] = b;
if (outputBufferIndex == BUFFER_SIZE) {
innerFlush();
}
return this;
}
private OutputWriter write(String s) {
s.chars()
.forEach(
c -> {
outputBuffer[outputBufferIndex++] = (byte) c;
if (outputBufferIndex == BUFFER_SIZE) {
innerFlush();
}
});
return this;
}
private OutputWriter write(int x) {
if (x == Integer.MIN_VALUE) {
return write((long) x);
}
if (outputBufferIndex + 12 >= BUFFER_SIZE) {
innerFlush();
}
if (x < 0) {
write((byte) '-');
x = -x;
}
int d = countDigits(x);
for (int i = outputBufferIndex + d - 1; i >= outputBufferIndex; i--) {
outputBuffer[i] = (byte) ('0' + x % 10);
x /= 10;
}
outputBufferIndex += d;
return this;
}
private static int countDigits(long l) {
if (l >= 1000000000000000000L) {
return 19;
}
if (l >= 100000000000000000L) {
return 18;
}
if (l >= 10000000000000000L) {
return 17;
}
if (l >= 1000000000000000L) {
return 16;
}
if (l >= 100000000000000L) {
return 15;
}
if (l >= 10000000000000L) {
return 14;
}
if (l >= 1000000000000L) {
return 13;
}
if (l >= 100000000000L) {
return 12;
}
if (l >= 10000000000L) {
return 11;
}
if (l >= 1000000000L) {
return 10;
}
if (l >= 100000000L) {
return 9;
}
if (l >= 10000000L) {
return 8;
}
if (l >= 1000000L) {
return 7;
}
if (l >= 100000L) {
return 6;
}
if (l >= 10000L) {
return 5;
}
if (l >= 1000L) {
return 4;
}
if (l >= 100L) {
return 3;
}
if (l >= 10L) {
return 2;
}
return 1;
}
private OutputWriter write(long x) {
if (x == Long.MIN_VALUE) {
return write("" + x);
}
if (outputBufferIndex + 21 >= BUFFER_SIZE) {
innerFlush();
}
if (x < 0) {
write((byte) '-');
x = -x;
}
int d = countDigits(x);
for (int i = outputBufferIndex + d - 1; i >= outputBufferIndex; i--) {
outputBuffer[i] = (byte) ('0' + x % 10);
x /= 10;
}
outputBufferIndex += d;
return this;
}
private OutputWriter writeln(int x) {
return write(x).writeln();
}
private OutputWriter writeln() {
return write((byte) '\n');
}
private void innerFlush() {
try {
outputStream.write(outputBuffer, 0, outputBufferIndex);
outputBufferIndex = 0;
} catch (IOException e) {
throw new IllegalStateException("Failed to inner flush");
}
}
@Override
public void close() {
flush();
}
}
public static class InputReader {
private final InputStream inputStream;
private final byte[] inputBuffer = new byte[1024];
private int bytesRead = 0;
private int inputBufferIndex = 0;
public InputReader(InputStream inputStream) {
this.inputStream = inputStream;
}
private int readByte() {
if (bytesRead == -1) {
throw new InputMismatchException();
}
if (inputBufferIndex >= bytesRead) {
inputBufferIndex = 0;
try {
bytesRead = inputStream.read(inputBuffer);
} catch (IOException e) {
throw new InputMismatchException();
}
if (bytesRead <= 0) {
return -1;
}
}
return inputBuffer[inputBufferIndex++];
}
private int skip() {
int b;
do {
b = readByte();
} while (b != -1 && Character.isWhitespace(b));
return b;
}
public int nextInt() {
return (int) nextLong();
}
public long nextLong() {
long num = 0;
int b;
boolean minus = false;
do {
b = readByte();
} while (b != -1 && !((b >= '0' && b <= '9') || b == '-'));
if (b == '-') {
minus = true;
b = readByte();
}
while (true) {
if (b >= '0' && b <= '9') {
num = num * 10 + (b - '0');
} else {
return minus ? -num : num;
}
b = readByte();
}
}
public char nextChar() {
return (char) skip();
}
public char[] nextCharArray(int n) {
char[] buf = new char[n];
int b = skip(), p = 0;
while (p < n && !Character.isWhitespace(b)) {
buf[p++] = (char) b;
b = readByte();
}
return n == p ? buf : Arrays.copyOf(buf, p);
}
}
} | Java | ["9 10\nabcabcabc\n1 a\n1 b\n2 c\n3 a\n4 b\n5 c\n8 a\n9 b\n1 c\n4 a"] | 2 seconds | ["3\n2\n2\n2\n1\n2\n1\n1\n1\n0"] | NoteLet's consider the state of the string after each query: $$$s =$$$ "abcabcabc". In this case $$$3$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcaccabb". This string does not contain "abc" as a substring. $$$s =$$$ "bbcabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bccabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bccbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcaabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcabbcabc". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaac". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaac". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "ccabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccaaccaab". In this case the string does not contain "abc" as a substring and no replacements are needed. | Java 11 | standard input | [
"implementation",
"strings"
] | db473ad780a93983667d12b1357c6e2f | The first line contains two integers $$$n$$$ and $$$q$$$ $$$(1 \le n, q \le 10^5)$$$, the length of the string and the number of queries, respectively. The second line contains the string $$$s$$$, consisting of characters "a", "b" and "c". Each of the next $$$q$$$ lines contains an integer $$$i$$$ and character $$$c$$$ $$$(1 \le i \le n)$$$, index and the value of the new item in the string, respectively. It is guaranteed that character's $$$c$$$ value is "a", "b" or "c". | 1,100 | For each query output the minimal number of characters that would have to be replaced so that the string doesn't contain "abc" as a substring. | standard output | |
PASSED | c3f2c0916aff5e34d37972ee8d03075d | train_110.jsonl | 1638110100 | Before becoming a successful trader William got a university degree. During his education an interesting situation happened, after which William started to listen to homework assignments much more attentively. What follows is the correct formal description of the homework assignment:You are given a string $$$s$$$ of length $$$n$$$ only consisting of characters "a", "b" and "c". There are $$$q$$$ queries of format ($$$pos, c$$$), meaning replacing the element of string $$$s$$$ at position $$$pos$$$ with character $$$c$$$. After each query you must output the minimal number of characters in the string, which have to be replaced, so that the string doesn't contain string "abc" as a substring. A valid replacement of a character is replacing it with "a", "b" or "c".A string $$$x$$$ is a substring of a string $$$y$$$ if $$$x$$$ can be obtained from $$$y$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. | 256 megabytes | /*
"Everything in the universe is balanced. Every disappointment
you face in life will be balanced by something good for you!
Keep going, never give up."
*/
import java.util.*;
import java.lang.*;
import java.io.*;
public class Solution {
static int n;
public static void main(String[] args) throws java.lang.Exception {
out = new PrintWriter(new BufferedOutputStream(System.out));
sc = new FastReader();
int test = 1;
for (int t = 0; t < test; t++) {
solve();
}
out.close();
}
private static void solve() {
n = sc.nextInt();
int q = sc.nextInt();
char[] arr = sc.next().toCharArray();
int curr = count(arr);
for (int i = 0; i < q; i++) {
int pos = sc.nextInt() - 1;
char c = sc.next().charAt(0);
int prev = getScore(arr, pos);
arr[pos] = c;
int now = getScore(arr, pos);
curr -= prev;
curr += now;
out.println(curr);
}
}
private static int getScore(char[] arr, int pos) {
int prev = 0;
if (pos + 2 < n) {
if (arr[pos] == 'a' && arr[pos + 1] == 'b' && arr[pos + 2] == 'c') {
prev++;
}
}
if (pos - 1 >= 0 && pos + 1 < n) {
if (arr[pos - 1] == 'a' && arr[pos] == 'b' && arr[pos + 1] == 'c') {
prev++;
}
}
if (pos - 2 >= 0) {
if (arr[pos - 2] == 'a' && arr[pos - 1] == 'b' && arr[pos] == 'c') {
prev++;
}
}
return prev;
}
private static int count(char[] arr) {
int res = 0;
for (int i = 0; i + 2 < arr.length; i++) {
if (arr[i] == 'a' && arr[i + 1] == 'b' && arr[i + 2] == 'c') {
res++;
}
}
return res;
}
public static FastReader sc;
public static PrintWriter out;
static class FastReader
{
BufferedReader br;
StringTokenizer str;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (str == null || !str.hasMoreElements())
{
try
{
str = new StringTokenizer(br.readLine());
}
catch (IOException end)
{
end.printStackTrace();
}
}
return str.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException end)
{
end.printStackTrace();
}
return str;
}
}
} | Java | ["9 10\nabcabcabc\n1 a\n1 b\n2 c\n3 a\n4 b\n5 c\n8 a\n9 b\n1 c\n4 a"] | 2 seconds | ["3\n2\n2\n2\n1\n2\n1\n1\n1\n0"] | NoteLet's consider the state of the string after each query: $$$s =$$$ "abcabcabc". In this case $$$3$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcaccabb". This string does not contain "abc" as a substring. $$$s =$$$ "bbcabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bccabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bccbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcaabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcabbcabc". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaac". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaac". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "ccabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccaaccaab". In this case the string does not contain "abc" as a substring and no replacements are needed. | Java 11 | standard input | [
"implementation",
"strings"
] | db473ad780a93983667d12b1357c6e2f | The first line contains two integers $$$n$$$ and $$$q$$$ $$$(1 \le n, q \le 10^5)$$$, the length of the string and the number of queries, respectively. The second line contains the string $$$s$$$, consisting of characters "a", "b" and "c". Each of the next $$$q$$$ lines contains an integer $$$i$$$ and character $$$c$$$ $$$(1 \le i \le n)$$$, index and the value of the new item in the string, respectively. It is guaranteed that character's $$$c$$$ value is "a", "b" or "c". | 1,100 | For each query output the minimal number of characters that would have to be replaced so that the string doesn't contain "abc" as a substring. | standard output | |
PASSED | 863f20668dd505a4b7d6d75b58c74daa | train_110.jsonl | 1638110100 | Before becoming a successful trader William got a university degree. During his education an interesting situation happened, after which William started to listen to homework assignments much more attentively. What follows is the correct formal description of the homework assignment:You are given a string $$$s$$$ of length $$$n$$$ only consisting of characters "a", "b" and "c". There are $$$q$$$ queries of format ($$$pos, c$$$), meaning replacing the element of string $$$s$$$ at position $$$pos$$$ with character $$$c$$$. After each query you must output the minimal number of characters in the string, which have to be replaced, so that the string doesn't contain string "abc" as a substring. A valid replacement of a character is replacing it with "a", "b" or "c".A string $$$x$$$ is a substring of a string $$$y$$$ if $$$x$$$ can be obtained from $$$y$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main {
public static BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
public static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
public static int mod = (int)1e9+7;
public static void main(String[] args) throws Exception {
// String ss = bf.readLine();
// if(ss.contains(" ")) ss = ss.substring(0,ss.indexOf(" "));
// int t = Integer.parseInt(ss);
String ss[] = bf.readLine().split(" ");
int n = Integer.parseInt(ss[0]);
int q = Integer.parseInt(ss[1]);
String s = bf.readLine();
int a[] = new int[q];
char[] b = new char[q];
int cnt = 0;
int ans = 0;
for(int i = 0;i < n-2;i++){
if(s.charAt(i) == 'a' && s.charAt(i+1) == 'b' && s.charAt(i+2) == 'c'){
cnt++;
}
}
char c[] = s.toCharArray();
for(int i = 0;i < q;i++) {
String s1[] = bf.readLine().split(" ");
a[i] = Integer.parseInt(s1[0]) - 1;
b[i] = s1[1].charAt(0);
int x = Math.max(a[i]-3,0);
int y = Math.min(a[i]+3,n-1);
int c1 = 0,c2 = 0;
for(int j =x;j <= y-2;j++){
if(c[j] == 'a' && c[j+1] == 'b' && c[j+2] == 'c'){
c1++;
}
}
c[a[i]] = b[i];
for(int j =x;j <= y-2;j++){
if(c[j] == 'a' && c[j+1] == 'b' && c[j+2] == 'c'){
c2++;
}
}
ans = cnt + (c2-c1);
cnt = ans;
bw.write(ans+"\n");
}
bw.flush();
}
public static long gcd(long a, long b){
return b == 0 ? a : gcd(b,a%b);
}
public static long lcm(long a, long b){
return a*b / gcd(a,b);
}
} | Java | ["9 10\nabcabcabc\n1 a\n1 b\n2 c\n3 a\n4 b\n5 c\n8 a\n9 b\n1 c\n4 a"] | 2 seconds | ["3\n2\n2\n2\n1\n2\n1\n1\n1\n0"] | NoteLet's consider the state of the string after each query: $$$s =$$$ "abcabcabc". In this case $$$3$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcaccabb". This string does not contain "abc" as a substring. $$$s =$$$ "bbcabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bccabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bccbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcaabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcabbcabc". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaac". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaac". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "ccabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccaaccaab". In this case the string does not contain "abc" as a substring and no replacements are needed. | Java 11 | standard input | [
"implementation",
"strings"
] | db473ad780a93983667d12b1357c6e2f | The first line contains two integers $$$n$$$ and $$$q$$$ $$$(1 \le n, q \le 10^5)$$$, the length of the string and the number of queries, respectively. The second line contains the string $$$s$$$, consisting of characters "a", "b" and "c". Each of the next $$$q$$$ lines contains an integer $$$i$$$ and character $$$c$$$ $$$(1 \le i \le n)$$$, index and the value of the new item in the string, respectively. It is guaranteed that character's $$$c$$$ value is "a", "b" or "c". | 1,100 | For each query output the minimal number of characters that would have to be replaced so that the string doesn't contain "abc" as a substring. | standard output | |
PASSED | 487494d627c29aa3be004a3515e47306 | train_110.jsonl | 1638110100 | Before becoming a successful trader William got a university degree. During his education an interesting situation happened, after which William started to listen to homework assignments much more attentively. What follows is the correct formal description of the homework assignment:You are given a string $$$s$$$ of length $$$n$$$ only consisting of characters "a", "b" and "c". There are $$$q$$$ queries of format ($$$pos, c$$$), meaning replacing the element of string $$$s$$$ at position $$$pos$$$ with character $$$c$$$. After each query you must output the minimal number of characters in the string, which have to be replaced, so that the string doesn't contain string "abc" as a substring. A valid replacement of a character is replacing it with "a", "b" or "c".A string $$$x$$$ is a substring of a string $$$y$$$ if $$$x$$$ can be obtained from $$$y$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. | 256 megabytes | import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.StringTokenizer;
/**
*
* @author eslam
*/
public class WilliamTheVigilant {
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static void main(String[] args) throws IOException {
BufferedWriter log = new BufferedWriter(new OutputStreamWriter(System.out));
FastReader input = new FastReader();
int n = input.nextInt();
int q = input.nextInt();
String w = input.next();
char ch[] = w.toCharArray();
int ans = 0;
for (int i = 0; i < n; i++) {
if (ch[i] == 'a') {
if (i < n - 2) {
if (ch[i + 1] == 'b' && ch[i + 2] == 'c') {
ans++;
}
}
}
}
while (q-- > 0) {
int x = input.nextInt() - 1;
char c = input.next().charAt(0);
if(x-0>=2){
if(ch[x-2]=='a'&&ch[x-1]=='b'&&ch[x]=='c'){
if(c!='c'){
ans--;
}
}else{
if(ch[x-2]=='a'&&ch[x-1]=='b'&&c=='c'){
ans++;
}
}
}
if(x-0>=1&&x<n-1){
if(ch[x-1]=='a'&&ch[x]=='b'&&ch[x+1]=='c'){
if(c!='b'){
ans--;
}
}else{
if(ch[x-1]=='a'&&c=='b'&&ch[x+1]=='c'){
ans++;
}
}
}
if(x<n-2){
if(ch[x]=='a'&&ch[x+1]=='b'&&ch[x+2]=='c'){
if(c!='a'){
ans--;
}
}else{
if(c=='a'&&ch[x+1]=='b'&&ch[x+2]=='c'){
ans++;
}
}
}
ch[x]=c;
log.write(ans+"\n");
}
log.flush();
}
}
| Java | ["9 10\nabcabcabc\n1 a\n1 b\n2 c\n3 a\n4 b\n5 c\n8 a\n9 b\n1 c\n4 a"] | 2 seconds | ["3\n2\n2\n2\n1\n2\n1\n1\n1\n0"] | NoteLet's consider the state of the string after each query: $$$s =$$$ "abcabcabc". In this case $$$3$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcaccabb". This string does not contain "abc" as a substring. $$$s =$$$ "bbcabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bccabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bccbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcaabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcabbcabc". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaac". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaac". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "ccabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccaaccaab". In this case the string does not contain "abc" as a substring and no replacements are needed. | Java 11 | standard input | [
"implementation",
"strings"
] | db473ad780a93983667d12b1357c6e2f | The first line contains two integers $$$n$$$ and $$$q$$$ $$$(1 \le n, q \le 10^5)$$$, the length of the string and the number of queries, respectively. The second line contains the string $$$s$$$, consisting of characters "a", "b" and "c". Each of the next $$$q$$$ lines contains an integer $$$i$$$ and character $$$c$$$ $$$(1 \le i \le n)$$$, index and the value of the new item in the string, respectively. It is guaranteed that character's $$$c$$$ value is "a", "b" or "c". | 1,100 | For each query output the minimal number of characters that would have to be replaced so that the string doesn't contain "abc" as a substring. | standard output | |
PASSED | 91345b61170e3bfb9e9839a9c3a5df52 | train_110.jsonl | 1638110100 | Before becoming a successful trader William got a university degree. During his education an interesting situation happened, after which William started to listen to homework assignments much more attentively. What follows is the correct formal description of the homework assignment:You are given a string $$$s$$$ of length $$$n$$$ only consisting of characters "a", "b" and "c". There are $$$q$$$ queries of format ($$$pos, c$$$), meaning replacing the element of string $$$s$$$ at position $$$pos$$$ with character $$$c$$$. After each query you must output the minimal number of characters in the string, which have to be replaced, so that the string doesn't contain string "abc" as a substring. A valid replacement of a character is replacing it with "a", "b" or "c".A string $$$x$$$ is a substring of a string $$$y$$$ if $$$x$$$ can be obtained from $$$y$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. | 256 megabytes | import java.io.*;
import java.util.*;
public class DeltixB {
static char[] s;
public static void main(String[] args) {
JS scan = new JS();
PrintWriter out =new PrintWriter(System.out);
int n = scan.nextInt();
int q = scan.nextInt();
s = scan.next().toCharArray();
int count = 0;
for(int i = 0;i<n-2;i++){
if(s[i] == 'a' && s[i+1] == 'b' && s[i+2] == 'c'){
count++;
}
}
for(int i =0 ;i<q;i++){
int idx = scan.nextInt()-1;
char in = scan.nextChar();
boolean[] prob = new boolean[6];
boolean problema = false;
int index = 0;
for(int j = Math.max(0,idx-2);j<=idx && j<n-2;j++){
prob[j-idx+2] = checkProblema(j);
if(prob[j-idx+2]){
problema = true;
index = j-idx+2;
}
}
s[idx] = in;
boolean fixed = false;
boolean ruined = false;
// System.out.println(problema+" "+index);
for(int j = Math.max(0,idx-2);j<=idx && j<n-2;j++){
if(!checkProblema(j) && prob[j-idx+2]){
// System.out.println(j-idx+2+" "+checkProblema(j));
fixed = true;
}
if(checkProblema(j) && !prob[j-idx+2]){
ruined = true;
}
}
if(ruined)count++;
if(fixed)count--;
out.println(count);
}
out.flush();
out.close();
}
static boolean checkProblema(int index){
if(s[index] == 'a' && s[index+1] == 'b' && s[index+2] == 'c'){
return true;
}
return false;
}
static class JS {
public int BS = 1 << 16;
public char NC = (char) 0;
byte[] buf = new byte[BS];
int bId = 0, size = 0;
char c = NC;
double num = 1;
BufferedInputStream in;
public JS() {
in = new BufferedInputStream(System.in, BS);
}
public JS(String s) throws FileNotFoundException {
in = new BufferedInputStream(new FileInputStream(new File(s)), BS);
}
public char nextChar() {
while (bId == size) {
try {
size = in.read(buf);
} catch (Exception e) {
return NC;
}
if (size == -1) return NC;
bId = 0;
}
return (char) buf[bId++];
}
public int nextInt() {
return (int) nextLong();
}
public long nextLong() {
num = 1;
boolean neg = false;
if (c == NC) c = nextChar();
for (; (c < '0' || c > '9'); c = nextChar()) {
if (c == '-') neg = true;
}
long res = 0;
for (; c >= '0' && c <= '9'; c = nextChar()) {
res = (res << 3) + (res << 1) + c - '0';
num *= 10;
}
return neg ? -res : res;
}
public double nextDouble() {
double cur = nextLong();
return c != '.' ? cur : cur + nextLong() / num;
}
public String next() {
StringBuilder res = new StringBuilder();
while (c <= 32) c = nextChar();
while (c > 32) {
res.append(c);
c = nextChar();
}
return res.toString();
}
public String nextLine() {
StringBuilder res = new StringBuilder();
while (c <= 32) c = nextChar();
while (c != '\n') {
res.append(c);
c = nextChar();
}
return res.toString();
}
public boolean hasNext() {
if (c > 32) return true;
while (true) {
c = nextChar();
if (c == NC) return false;
else if (c > 32) return true;
}
}
}
}
| Java | ["9 10\nabcabcabc\n1 a\n1 b\n2 c\n3 a\n4 b\n5 c\n8 a\n9 b\n1 c\n4 a"] | 2 seconds | ["3\n2\n2\n2\n1\n2\n1\n1\n1\n0"] | NoteLet's consider the state of the string after each query: $$$s =$$$ "abcabcabc". In this case $$$3$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcaccabb". This string does not contain "abc" as a substring. $$$s =$$$ "bbcabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bccabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bccbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcaabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcabbcabc". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaac". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaac". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "ccabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccaaccaab". In this case the string does not contain "abc" as a substring and no replacements are needed. | Java 11 | standard input | [
"implementation",
"strings"
] | db473ad780a93983667d12b1357c6e2f | The first line contains two integers $$$n$$$ and $$$q$$$ $$$(1 \le n, q \le 10^5)$$$, the length of the string and the number of queries, respectively. The second line contains the string $$$s$$$, consisting of characters "a", "b" and "c". Each of the next $$$q$$$ lines contains an integer $$$i$$$ and character $$$c$$$ $$$(1 \le i \le n)$$$, index and the value of the new item in the string, respectively. It is guaranteed that character's $$$c$$$ value is "a", "b" or "c". | 1,100 | For each query output the minimal number of characters that would have to be replaced so that the string doesn't contain "abc" as a substring. | standard output |
Subsets and Splits