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
7715077a92a895dfdee9e6d70d6f7608
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.StringTokenizer; public class B { static FastScanner sc = new FastScanner(); static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); static void solve(){ int n = sc.nextInt(); int q = sc.nextInt(); char[]chars = sc.next().toCharArray(); int ans = 0; for(int i = 0;i < n - 2;i++){ if(chars[i] == 'a'){ if(chars[i + 1] == 'b' && chars[i + 2] == 'c') ans++; } } for(int z = 0;z < q;z++){ int pos = sc.nextInt() - 1; char c = sc.next().toCharArray()[0]; if(c == chars[pos]){ System.out.println(ans); chars[pos] = c; continue; } int off = 0; if(check(chars,pos)){ off++; } chars[pos] = c; if(check(chars,pos)){ off--; } ans -= off; System.out.println(ans); } } private static boolean check(char[]chars,int pos){ int n = chars.length; if(chars[pos] == 'a' && pos < n - 2){ return chars[pos + 1] == 'b' && chars[pos + 2] == 'c'; } if(chars[pos] == 'b' && pos > 0 && pos < n - 1){ return chars[pos - 1] == 'a' && chars[pos + 1] == 'c'; } if(chars[pos] == 'c' && pos >= 2){ return chars[pos - 1] == 'b' && chars[pos - 2] == 'a'; } return false; } public static void main(String[] args) { int n = 1; for(int i = 0;i < n;i++){ solve(); } } 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
9a6419802e50fad94c8c5b7a6549260a
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.text.*; import java.math.*; import java.util.regex.*; public class JaiShreeRam{ static Scanner in=new Scanner(); static long mod = 1000000007; static ArrayList<ArrayList<Integer>> adj; public static void main(String[] args) throws Exception{ //int z=in.readInt(); //while(z-->0) { solve(); //} } static void solve() { int n=in.readInt(); int q=in.readInt(); char c[]=in.readString().toCharArray(); int cnt=0; Set<Integer> st=new HashSet<>(); for(int i=2;i<n;i++) { if(c[i-2]=='a'&&c[i-1]=='b'&&c[i]=='c') { cnt++; st.add(i-2); } } while(q-->0) { int pos=in.readInt()-1; char rep=in.readString().charAt(0); if(rep==c[pos]) { System.out.println(cnt); continue; } int m=Math.min(n-1, pos+2); int cnt2=0; for(int i=Math.max(0, pos-2);i<=m;i++) { if(m-i+1>=3) { if(c[i]=='a'&&c[i+1]=='b'&&c[i+2]=='c') { cnt2++; } } } c[pos]=rep; int cnt3=0; for(int i=Math.max(0, pos-2);i<=m;i++) { if(m-i+1>=3) { if(c[i]=='a'&&c[i+1]=='b'&&c[i+2]=='c') { cnt3++; } } } cnt=cnt+cnt3-cnt2; System.out.println(cnt); } } static int[] nia(int n){ int[] arr= new int[n]; int i=0; while(i<n){ arr[i++]=in.readInt(); } return arr; } static long[] nla(int n){ long[] arr= new long[n]; int i=0; while(i<n){ arr[i++]=in.readLong(); } return arr; } static int[] nia1(int n){ int[] arr= new int[n+1]; int i=1; while(i<=n){ arr[i++]=in.readInt(); } return arr; } static long gcd(long a, long b) { if (b==0) return a; return gcd(b, a%b); } static class Scanner{ BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String readString() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } double readDouble() { return Double.parseDouble(readString()); } int readInt() { return Integer.parseInt(readString()); } long readLong() { return Long.parseLong(readString()); } } }
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
4fc6927fe3f9166b588af23d4ea3dae2
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 B { public static void main(String[] args) { FastScanner sc = new FastScanner(); int n = sc.nextInt(); int qs = sc.nextInt(); char[] s = sc.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++; } StringBuilder sb = new StringBuilder(); while(qs-->0) { int ind = sc.nextInt()-1; char t = sc.next().charAt(0); if(check(ind, s)) count--; s[ind] = t; if(check(ind, s)) count++; sb.append(count+"\n"); } PrintWriter pw = new PrintWriter(System.out); pw.println(sb.toString().trim()); pw.flush(); } static boolean check(int i, char[] s) { int n = s.length; if(i >= 2) if(s[i-2] == 'a' && s[i-1] == 'b' && s[i] == 'c') return true; if(i >= 1 && i < n-1) if(s[i-1] == 'a' && s[i] == 'b' && s[i+1] == 'c') return true; if(i < n-2) if(s[i] == 'a' && s[i+1] == 'b' && s[i+2] == 'c') return true; return false; } static class FastScanner { public BufferedReader reader; public StringTokenizer tokenizer; public FastScanner() { reader = new BufferedReader(new InputStreamReader(System.in), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.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 reader.readLine(); } catch(IOException e) { throw new RuntimeException(e); } } } }
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
835d5090b28b7fbe6ddbd3c7a8afb3f7
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 B1609 { public static void main(String[] args) { FastScanner sc = new FastScanner("in.txt"); PrintWriter out = new PrintWriter(System.out); int n = sc.nextInt(); int q = sc.nextInt(); char[] s = sc.nextLine().toCharArray(); int count = countABCs(s, 0, n); while (q-- > 0) { int i = sc.nextInt() - 1; char c = sc.getChar(); int before = countABCs(s, i - 2, i); s[i] = c; int after = countABCs(s, i - 2, i); count += after - before; out.println(count); } out.close(); } static int countABCs(char[] s, int i, int j) { int count = 0; while (i <= j) { if (i >= 0 && i < s.length - 2) { if (s[i] == 'a' && s[i + 1] == 'b' && s[i + 2] == 'c') { count++; } } i++; } return count; } static final Random random = new Random(); static void ruffleSort(int[] a) { int n = a.length; 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 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 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(); } } }
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
891681e9d95f768ddfa3deaed9b77ce0
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 boolean prime[] = new boolean[25001]; // public static int Lcm(int a,int b) // { int max=Math.max(a,b); // for(int i=1;;i++) // { // if((max*i)%a==0&&(max*i)%b==0) // return (max*i); // } // } // public static String run(int ar[],int n) // { // } public static long bs(int s,int e, ArrayList<Integer> ar,int x) { int res=-1; while(s<=e) { int mid=((s-e)/2)+e; if(ar.get(mid)>x) {e=mid-1;} else if(ar.get(mid)<x) {s=mid+1;res=mid;} else {e=mid-1;res=mid;} } if(res==-1) return 0; return ar.get(res)==x?res:res+1; } static long modulo=1000000007; public static long power(int a, int b) { if(b==0) return 1; long temp=power(a,b/2)%modulo; if((b&1)==0) return (temp*temp)%modulo; else return ((temp*temp)%modulo)*a; } public static void main(String[] args) throws Exception { FastIO sc = new FastIO(); //sc.println(); //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx CODE xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx // int test=sc.nextInt(); // double c=Math.log(10); // while(test-->0) // { int n=sc.nextInt(); int q=sc.nextInt(); // long k=sc.nextLong(); char s[]=sc.next().toCharArray(); int count=0; for(int i=0;i<n;i++) { if(s[i]=='a'&&i+2<n) { if(s[i+1]=='b'&&s[i+2]=='c') count++; } } while(q-->0) { int i=sc.nextInt(); i--; char c=sc.next().charAt(0); if(c==s[i]) sc.println(count); else { int start=Math.max(i-2,0); int end=Math.min(i+2,n-1); int prv=0,curr=0; for(int j=start;j<=end;j++) { if(s[j]=='a'&&j+2<n) { if(s[j+1]=='b'&&s[j+2]=='c') prv++; } } s[i]=c; for(int j=start;j<=end;j++) { if(s[j]=='a'&&j+2<n) { if(s[j+1]=='b'&&s[j+2]=='c') curr++; } } // int x=i; // if(c=='b') // i--; // else if(c=='c') // i-=2; // if(i>=0&&i+2<n) // { // int prv=0,curr=0; // if(s[i]=='a'&&s[i+1]=='b'&&s[i+2]=='c') // prv++; // s[x]=c; // if(s[i]=='a'&&s[i+1]=='b'&&s[i+2]=='c') // curr++; count+=curr-prv; // } sc.println(count); } } // } // xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx CODE xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx sc.close(); } } class pair implements Comparable<pair>{ int first,second; pair(int first,int second) {this.first=first; this.second=second; } public int compareTo(pair p) {return -this.first+p.first;} } class triplet implements Comparable<triplet>{ int first,second,third; triplet(int first,int second,int third) {this.first=first; this.second=second; this.third=third; } public int compareTo(triplet p) {return this.third-p.third;} } // class triplet // { // int x1,x2,i; // triplet(int a,int b,int c) // {x1=a;x2=b;i=c;} // } class FastIO extends PrintWriter { private InputStream stream; private byte[] buf = new byte[1<<16]; private int curChar, numChars; // standard input public FastIO() { this(System.in,System.out); } public FastIO(InputStream i, OutputStream o) { super(o); stream = i; } // file input public FastIO(String i, String o) throws IOException { super(new FileWriter(o)); stream = new FileInputStream(i); } // throws InputMismatchException() if previously detected end of file private int nextByte() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars == -1) return -1; // end of file } return buf[curChar++]; } public String nextLine() { int c; do { c = nextByte(); } while (c <= '\n'); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = nextByte(); } while (c > '\n'); return res.toString(); } public String next() { int c; do { c = nextByte(); } while (c <= ' '); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = nextByte(); } while (c > ' '); return res.toString(); } public int nextInt() { int c; do { c = nextByte(); } while (c <= ' '); int sgn = 1; if (c == '-') { sgn = -1; c = nextByte(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res = 10*res+c-'0'; c = nextByte(); } while (c > ' '); return res * sgn; } public long nextLong() { int c; do { c = nextByte(); } while (c <= ' '); long sgn = 1; if (c == '-') { sgn = -1; c = nextByte(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res = 10*res+c-'0'; c = nextByte(); } while (c > ' '); return res * sgn; } public double nextDouble() { return Double.parseDouble(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
8a3402da55bbf06cbbc7f8d2c0a35348
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 TimePass { public static void main (String[] args) throws java.lang.Exception { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); PrintWriter out=new PrintWriter(System.out); int testCases=1; while(testCases-->0){ String input[]=br.readLine().split(" "); int n=Integer.parseInt(input[0]); int k=Integer.parseInt(input[1]); char arr[]=br.readLine().toCharArray(); TreeSet<Integer>set=new TreeSet<>(); for(int i=0;i<=n-3;i++) { if(arr[i]=='a'&&arr[i+1]=='b'&&arr[i+2]=='c') set.add(i); } for(int j=0;j<k;j++) { input=br.readLine().split(" "); int index=Integer.parseInt(input[0])-1; char ch=input[1].charAt(0); Integer i=set.floor(index); if(i!=null&&i+2>=index) set.remove(i); arr[index]=ch; for(i=Math.max(0,index-2);i<=index&&i<=n-3;i++) { if(arr[i]=='a'&&arr[i+1]=='b'&&arr[i+2]=='c') set.add(i); } out.print(set.size()); out.print('\n'); } } 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
3c0a17df2511dd882f97083c5e5ce256
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 final PrintWriter out =new PrintWriter(System.out); static final FastReader sc = new FastReader(); //I invented a new word!Plagiarism! //Did you hear about the mathematician who’s afraid of negative numbers?He’ll stop at nothing to avoid them //What do Alexander the Great and Winnie the Pooh have in common? Same middle name. //I finally decided to sell my vacuum cleaner. All it was doing was gathering dust! //ArrayList<Integer> a=new ArrayList <Integer>(); //PriorityQueue<Integer> pq=new PriorityQueue<>(); //char[] a = s.toCharArray(); 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 java.lang.Exception { int t=sc.nextInt(); int n=sc.nextInt(); int cnt=0; char s[]=sc.next().toCharArray(); for(int i=0;i<t-2;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); } } public static int lis(int[] arr) { int n = arr.length; ArrayList<Integer> al = new ArrayList<Integer>(); al.add(arr[0]); for(int i = 1 ; i<n;i++) { int x = al.get(al.size()-1); if(arr[i]>=x) { al.add(arr[i]); }else { int v = upper_bound(al, 0, al.size(), arr[i]); al.set(v, arr[i]); } } return al.size(); } public static long lower_bound(ArrayList<Long> ar,long lo , long hi , long k) { long s=lo; long e=hi; while (s !=e) { long mid = s+e>>1; if (ar.get((int)mid) <k) { s=mid+1; } else { e=mid; } } if(s==ar.size()) { return -1; } return s; } public static int upper_bound(ArrayList<Integer> ar,int lo , int hi, int k) { int s=lo; int e=hi; 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 boolean isPrime(long N) { if (N<=1) return false; if (N<=3) return true; 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; } static int countBits(long a) { return (int)(Math.log(a)/Math.log(2)+1); } static long fact(long N) { long mod=1000000007; long n=2; if(N<=1)return 1; else { for(int i=3; i<=N; i++)n=(n*i)%mod; } return n; } private static boolean isInteger(String s) { try { Integer.parseInt(s); } catch (NumberFormatException e) { return false; } catch (NullPointerException e) { return false; } return true; } private static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } private static long lcm(long a, long b) { return (a / gcd(a, b)) * b; } private static boolean isPalindrome(String str) { int i = 0, j = str.length() - 1; while (i < j) if (str.charAt(i++) != str.charAt(j--)) return false; return true; } private static String reverseString(String str) { StringBuilder sb = new StringBuilder(str); return sb.reverse().toString(); } 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
ec941107f393912a7569184458e9c6bb
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 TimePass { public static void main (String[] args) throws java.lang.Exception { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); PrintWriter out=new PrintWriter(System.out); int testCases=1; while(testCases-->0){ String input[]=br.readLine().split(" "); int n=Integer.parseInt(input[0]); int k=Integer.parseInt(input[1]); char arr[]=br.readLine().toCharArray(); TreeSet<Integer>set=new TreeSet<>(); for(int i=0;i<=n-3;i++) { if(arr[i]=='a'&&arr[i+1]=='b'&&arr[i+2]=='c') set.add(i); } for(int j=0;j<k;j++) { input=br.readLine().split(" "); int index=Integer.parseInt(input[0])-1; char ch=input[1].charAt(0); Integer i=set.floor(index); if(i!=null&&i+2>=index) set.remove(i); arr[index]=ch; for(i=Math.max(0,index-2);i<=index&&i<=n-3;i++) { if(arr[i]=='a'&&arr[i+1]=='b'&&arr[i+2]=='c') set.add(i); } out.print(set.size()); out.print('\n'); } } 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
e1396c0388ab84ac11cda531fd3e05d9
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 whatever //do not write package name here */ import java.io.*; import java.util.*; import java.util.Scanner; public class Gfg{ public static void main(String args[]){ Scanner s = new Scanner(System.in); int size = s.nextInt(); int q = s.nextInt(); String t = s.next(); char arr[] = t.toCharArray(); HashSet<Integer> map = new HashSet<>(); for(int j=0;j<=size-3;j++){ if(t.charAt(j)=='a'&&t.charAt(j+1)=='b' &&t.charAt(j+2)=='c')map.add(j); } for(int j=0;j<q;j++){ int index = s.nextInt()-1; char r= s.next().charAt(0); arr[index]=r; if(index-2>=0){ if(map.contains(index-2)&&arr[index]!='c'){ map.remove(index-2); } if(arr[index-2]=='a'&&arr[index-1]=='b'&&arr[index]=='c')map.add(index-2); } if(index-1>=0&& index+1<size){ if(map.contains(index-1)&&arr[index]!='b'){ map.remove(index-1); } if(arr[index-1]=='a'&&arr[index]=='b'&&arr[index+1]=='c')map.add(index-1); } if(index+2<size){ if(map.contains(index)&&arr[index]!='a'){ map.remove(index); } if(arr[index]=='a'&&arr[index+1]=='b'&&arr[index+2]=='c')map.add(index); } System.out.println(map.size()); } } }
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
aef0eaa6fa16365deac040379c081a5f
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
//Utilities import java.io.*; import java.util.*; public class a { static int t; static int n, q; static char[] a; static int cnt; static int pos; static char newC; public static void main(String[] args) throws IOException { t = 1; while (t-- > 0) { n = in.iscan(); q = in.iscan(); a = in.sscan().toCharArray(); cnt = 0; for (int i = 0; i < n-2; i++) { if (a[i] == 'a' && a[i+1] == 'b' && a[i+2] == 'c') { cnt++; } } while (q-- > 0) { pos = in.iscan() - 1; newC = in.sscan().charAt(0); if (a[pos] == newC) { out.println(cnt); continue; } if (a[pos] == 'a' && pos + 1 < n && a[pos+1] == 'b' && pos + 2 < n && a[pos+2] == 'c' || a[pos] == 'b' && pos - 1 >= 0 && a[pos-1] == 'a' && pos + 1 < n && a[pos+1] == 'c' || a[pos] == 'c' && pos - 1 >= 0 && a[pos-1] == 'b' && pos - 2 >= 0 && a[pos-2] == 'a') { cnt--; } a[pos] = newC; if (a[pos] == 'a' && pos + 1 < n && a[pos+1] == 'b' && pos + 2 < n && a[pos+2] == 'c' || a[pos] == 'b' && pos - 1 >= 0 && a[pos-1] == 'a' && pos + 1 < n && a[pos+1] == 'c' || a[pos] == 'c' && pos - 1 >= 0 && a[pos-1] == 'b' && pos - 2 >= 0 && a[pos-2] == 'a') { cnt++; } out.println(cnt); } } out.close(); } static INPUT in = new INPUT(System.in); static PrintWriter out = new PrintWriter(System.out); private static class INPUT { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar, numChars; public INPUT (InputStream stream) { this.stream = stream; } public INPUT (String file) throws IOException { this.stream = new FileInputStream (file); } public int cscan () throws IOException { if (curChar >= numChars) { curChar = 0; numChars = stream.read (buf); } if (numChars == -1) return numChars; return buf[curChar++]; } public int iscan () throws IOException { int c = cscan (), sgn = 1; while (space (c)) c = cscan (); if (c == '-') { sgn = -1; c = cscan (); } int res = 0; do { res = (res << 1) + (res << 3); res += c - '0'; c = cscan (); } while (!space (c)); return res * sgn; } public String sscan () throws IOException { int c = cscan (); while (space (c)) c = cscan (); StringBuilder res = new StringBuilder (); do { res.appendCodePoint (c); c = cscan (); } while (!space (c)); return res.toString (); } public double dscan () throws IOException { int c = cscan (), sgn = 1; while (space (c)) c = cscan (); if (c == '-') { sgn = -1; c = cscan (); } double res = 0; while (!space (c) && c != '.') { if (c == 'e' || c == 'E') return res * UTILITIES.fast_pow (10, iscan ()); res *= 10; res += c - '0'; c = cscan (); } if (c == '.') { c = cscan (); double m = 1; while (!space (c)) { if (c == 'e' || c == 'E') return res * UTILITIES.fast_pow (10, iscan ()); m /= 10; res += (c - '0') * m; c = cscan (); } } return res * sgn; } public long lscan () throws IOException { int c = cscan (), sgn = 1; while (space (c)) c = cscan (); if (c == '-') { sgn = -1; c = cscan (); } long res = 0; do { res = (res << 1) + (res << 3); res += c - '0'; c = cscan (); } while (!space (c)); return res * sgn; } public boolean space (int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } } public static class UTILITIES { static final double EPS = 10e-6; public static void sort(int[] a, boolean increasing) { ArrayList<Integer> arr = new ArrayList<Integer>(); int n = a.length; for (int i = 0; i < n; i++) { arr.add(a[i]); } Collections.sort(arr); for (int i = 0; i < n; i++) { if (increasing) { a[i] = arr.get(i); } else { a[i] = arr.get(n-1-i); } } } public static void sort(long[] a, boolean increasing) { ArrayList<Long> arr = new ArrayList<Long>(); int n = a.length; for (int i = 0; i < n; i++) { arr.add(a[i]); } Collections.sort(arr); for (int i = 0; i < n; i++) { if (increasing) { a[i] = arr.get(i); } else { a[i] = arr.get(n-1-i); } } } public static void sort(double[] a, boolean increasing) { ArrayList<Double> arr = new ArrayList<Double>(); int n = a.length; for (int i = 0; i < n; i++) { arr.add(a[i]); } Collections.sort(arr); for (int i = 0; i < n; i++) { if (increasing) { a[i] = arr.get(i); } else { a[i] = arr.get(n-1-i); } } } public static int lower_bound (int[] arr, int x) { int low = 0, high = arr.length, mid = -1; while (low < high) { mid = (low + high) / 2; if (arr[mid] >= x) high = mid; else low = mid + 1; } return low; } public static int upper_bound (int[] arr, int x) { int low = 0, high = arr.length, mid = -1; while (low < high) { mid = (low + high) / 2; if (arr[mid] > x) high = mid; else low = mid + 1; } return low; } public static void updateMap(HashMap<Integer, Integer> map, int key, int v) { if (!map.containsKey(key)) { map.put(key, v); } else { map.put(key, map.get(key) + v); } if (map.get(key) == 0) { map.remove(key); } } 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); } public static long fast_pow_mod (long b, long x, int mod) { if (x == 0) return 1; if (x == 1) return b; if (x % 2 == 0) return fast_pow_mod (b * b % mod, x / 2, mod) % mod; return b * fast_pow_mod (b * b % mod, x / 2, mod) % mod; } public static long fast_pow (long b, long x) { if (x == 0) return 1; if (x == 1) return b; if (x % 2 == 0) return fast_pow (b * b, x / 2); return b * fast_pow (b * b, x / 2); } public static long choose (long n, long k) { k = Math.min (k, n - k); long val = 1; for (int i = 0; i < k; ++i) val = val * (n - i) / (i + 1); return val; } public static long permute (int n, int k) { if (n < k) return 0; long val = 1; for (int i = 0; i < k; ++i) val = (val * (n - i)); return val; } // start of permutation and lower/upper bound template public static void nextPermutation(int[] nums) { //find first decreasing digit int mark = -1; for (int i = nums.length - 1; i > 0; i--) { if (nums[i] > nums[i - 1]) { mark = i - 1; break; } } if (mark == -1) { reverse(nums, 0, nums.length - 1); return; } int idx = nums.length-1; for (int i = nums.length-1; i >= mark+1; i--) { if (nums[i] > nums[mark]) { idx = i; break; } } swap(nums, mark, idx); reverse(nums, mark + 1, nums.length - 1); } public static void swap(int[] nums, int i, int j) { int t = nums[i]; nums[i] = nums[j]; nums[j] = t; } public static void reverse(int[] nums, int i, int j) { while (i < j) { swap(nums, i, j); i++; j--; } } static int lower_bound (int[] arr, int hi, int cmp) { int low = 0, high = hi, mid = -1; while (low < high) { mid = (low + high) / 2; if (arr[mid] >= cmp) high = mid; else low = mid + 1; } return low; } static int upper_bound (int[] arr, int hi, int cmp) { int low = 0, high = hi, mid = -1; while (low < high) { mid = (low + high) / 2; if (arr[mid] > cmp) high = mid; else low = mid + 1; } return low; } // end of permutation and lower/upper bound template } }
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
2e828a6a7b9997fdf970f130ba92cb71
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.Arrays; import java.util.Scanner; import java.util.stream.Collectors; import java.util.stream.IntStream; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); sc.nextInt(); int q = sc.nextInt(); String s = sc.next(); int[] positions = new int[q]; char[] c = new char[q]; for (int i = 0; i < q; ++i) { positions[i] = sc.nextInt() - 1; c[i] = sc.next().charAt(0); } System.out.println(solve(s, positions, c)); sc.close(); } static String solve(String s, int[] positions, char[] c) { int[] result = new int[positions.length]; char[] letters = s.toCharArray(); int count = (int) IntStream.range(0, s.length()).filter(i -> isMatched(letters, i)).count(); for (int i = 0; i < result.length; ++i) { int i_ = i; count -= IntStream.rangeClosed(0, 2).filter(j -> isMatched(letters, positions[i_] - j)).count(); letters[positions[i]] = c[i]; count += IntStream.rangeClosed(0, 2).filter(j -> isMatched(letters, positions[i_] - j)).count(); result[i] = count; } return Arrays.stream(result).mapToObj(String::valueOf).collect(Collectors.joining("\n")); } static boolean isMatched(char[] letters, int beginIndex) { return beginIndex >= 0 && beginIndex + 2 < letters.length && letters[beginIndex] == 'a' && letters[beginIndex + 1] == 'b' && letters[beginIndex + 2] == 'c'; } }
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
08b1293e6c07ecfbc2eb53d3f7457912
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.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; import java.util.Arrays; public class Main { public static void main(String[] args) throws IOException { FastScanner in = new FastScanner(); PrintWriter out = new PrintWriter(System.out); solve (in, out); //int tests = in.nextInt(); //for (int i = 0;i < tests;i++) { // solve (in, out); //} } 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) {} return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } } static int partOf (boolean hash[], int pos) { if (hash[pos]) { return pos; } else if (pos != 0 && hash[pos-1]) { return pos-1; } else if (pos > 1 && hash[pos-2]) { return pos-2; } return -1; } // main code ? static void solve (FastScanner in, PrintWriter out) { int n = in.nextInt(), q = in.nextInt(); char[] a = in.next().toCharArray(); boolean hash[] = new boolean[n]; int size = 0; for (int i = 0;i < n-2;i++) { if (a[i] == 'a' && a[i+1] == 'b' && a[i+2] == 'c') { hash[i] = true; size++; } } for (int i = 0;i < q;i++) { int pos = in.nextInt()-1; char ch = in.next().charAt(0); if (ch != a[pos]) { int index = partOf(hash, pos); if (index != -1) { hash[index] = false; size--; } if (ch == 'a') { if (pos < n-2 && a[pos+1] == 'b' && a[pos+2] == 'c') { hash[pos] = true; size++; } } else if (ch == 'b') { if (pos > 0 && pos < n-1 && a[pos-1] == 'a' && a[pos+1] == 'c') { hash[pos-1] = true; size++; } } else { if (pos > 1 && a[pos-2] == 'a' && a[pos-1] == 'b') { hash[pos-2] = true; size++; } } } a[pos] = ch; out.println(size); } 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
0ed1a48154efe77e1572a48371b6253c
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
/***** ---> :) Vijender Srivastava (: <--- *****/ import java.util.Queue; import java.util.LinkedList; import java.util.*; import java.lang.*; // import java.lang.reflect.Array; import java.io.*; public class Main { static FastReader sc =new FastReader(); static PrintWriter out=new PrintWriter(System.out); static long mod=998244353; static StringBuilder sb = new StringBuilder(); /* start */ public static void main(String [] args) { int testcases = 1; // int testcases = i(); while(testcases-->0) { solve(); } out.flush(); out.close(); } static void solve() { int n = i(); int q = i(); char c[] = inputC(); int cnt = 0; for(int i=0;i<n-2;i++) { if(c[i]=='a' && c[i+1]=='b' && c[i+2]=='c') cnt++; } for(int i=0;i<q;i++) { int x = i()-1; char ch = sc.next().charAt(0); if(ch==c[x]) { pl(cnt); continue; } if(ch=='a' && x<n-2 && c[x+1]=='b' && c[x+2]=='c') { cnt++; if(c[x]=='c' && x>1 && c[x-1]=='b' && c[x-2]=='a') cnt--; c[x] = ch; pl(cnt); continue; } if(ch=='b' && x<n-1 && x>0 && c[x+1]=='c' && c[x-1]=='a') { cnt++; c[x] = ch; pl(cnt); continue; } if(ch=='c' && x>1 && c[x-1]=='b' && c[x-2]=='a' ){ cnt++; if(c[x]=='a' && x<n-2 && c[x+1]=='b' && c[x+2]=='c') cnt--; c[x] = ch; pl(cnt); continue; } if(c[x]=='a') { if(x<n-2 && (c[x+1]=='b' && c[x+2]=='c')) cnt--; } if(c[x]=='b') { if(x<n-1 && x>0 && (c[x+1]=='c' && c[x-1]=='a')) cnt--; } if(c[x]=='c') { if(x>1 && (c[x-1]=='b' && c[x-2]=='a')) cnt--; } c[x] = ch; pl(cnt); } } /* end */ 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 void p(Object o) { out.print(o); } static void pl(Object o) { out.println(o); } static int i() { return sc.nextInt(); } static String s() { return sc.next(); } static long l() { return sc.nextLong(); } static char[] inputC() { String s = sc.nextLine(); return s.toCharArray(); } static int[] input(int n) { int A[]=new int[n]; for(int i=0;i<n;i++) { A[i]=sc.nextInt(); } return A; } static long[] inputL(int n) { long A[]=new long[n]; for(int i=0;i<n;i++) { A[i]=sc.nextLong(); } return A; } static long[] putL(long a[]) { long A[]=new long[a.length]; for(int i=0;i<a.length;i++) { A[i]=a[i]; } return A; } static String[] inputS(int n) { String A[]=new String[n]; for(int i=0;i<n;i++) { A[i]=sc.next(); } return A; } static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } static long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } static String reverse(String s) { StringBuffer p=new StringBuffer(s); p.reverse(); return p.toString(); } static int min(int a,int b) { return Math.min(a, b); } static int min(int a,int b,int c) { return Math.min(a, Math.min(b, c)); } static int min(int a,int b,int c,int d) { return Math.min(a, Math.min(b, Math.min(c, d))); } static int max(int a,int b) { return Math.max(a, b); } static int max(int a,int b,int c) { return Math.max(a, Math.max(b, c)); } static int max(int a,int b,int c,int d) { return Math.max(a, Math.max(b, Math.max(c, d))); } static long min(long a,long b) { return Math.min(a, b); } static long min(long a,long b,long c) { return Math.min(a, Math.min(b, c)); } static long min(long a,long b,long c,long d) { return Math.min(a, Math.min(b, Math.min(c, d))); } static long max(long a,long b) { return Math.max(a, b); } static long max(long a,long b,long c) { return Math.max(a, Math.max(b, c)); } static long max(long a,long b,long c,long d) { return Math.max(a, Math.max(b, Math.max(c, d))); } static long sum(int A[]) { long sum=0; for(int i : A) { sum+=i; } return sum; } static long sum(long A[]) { long sum=0; for(long i : A) { sum+=i; } return sum; } static void print(int A[]) { for(int i : A) { System.out.print(i+" "); } System.out.println(); } static void print(long A[]) { for(long i : A) { System.out.print(i+" "); } System.out.println(); } static long mod(long x) { return ((x%mod + mod)%mod); } static long power(long x, long y) { if(y==0) return 1; if(x==0) return 0; long res = 1; while (y > 0) { if (y % 2 == 1) res = (res * x) ; y = y >> 1; x = (x * x); } return res; } static boolean prime(int n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; double sq=Math.sqrt(n); for (int i = 5; i <= sq; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } static boolean prime(long n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; double sq=Math.sqrt(n); for (int i = 5; i <= sq; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } static long[] sort(long a[]) { ArrayList<Long> arr = new ArrayList<>(); for(long i : a) { arr.add(i); } Collections.sort(arr); for(int i = 0; i < arr.size(); i++) { a[i] = arr.get(i); } return a; } static int[] sort(int a[]) { ArrayList<Integer> arr = new ArrayList<>(); for(Integer i : a) { arr.add(i); } Collections.sort(arr); for(int i = 0; i < arr.size(); i++) { a[i] = arr.get(i); } return a; } //pair class private static class Pair implements Comparable<Pair> { int first, second; public Pair(int f, int s) { first = f; second = s; } @Override public int compareTo(Pair p) { if (first > p.first) return 1; else if (first < p.first) return -1; else { if (second < p.second) return 1; else if (second > p.second) return -1; else return 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
75959ff83475029a3e69f274f7be4e85
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 test { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pr = new PrintWriter(new OutputStreamWriter(System.out)); // long t = Long.parseLong(br.readLine()); // while (t-- != 0) { StringTokenizer st = new StringTokenizer(br.readLine()); long n = Long.parseLong(st.nextToken()); long k = Long.parseLong(st.nextToken()); long count = 0; char[] arr = br.readLine().toCharArray(); for (int i = 0; i < n - 2; i++) { if (arr[i] == 'a' && arr[i + 1] == 'b' && arr[i + 2] == 'c'){ count++; } } for (int i = 0; i < k; i++) { st = new StringTokenizer(br.readLine()); long index = Long.parseLong(st.nextToken()); char ch = st.nextToken().charAt(0); for(int j = (int) (index-3); j < index; j++) { if(j >= 0 && j < arr.length-2) { if(arr[j] == 'a' && arr[j+1] == 'b' && arr[j+2] == 'c') { count--; break; } } } arr[(int) (index-1)] = ch; for(int j = (int) (index-3); j < index; j++) { if(j >= 0 && j < arr.length-2) { if(arr[j] == 'a' && arr[j+1] == 'b' && arr[j+2] == 'c') { count++; break; } } } pr.println(count); } pr.println(); pr.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
3c7f2dbcde75a688f9e98961c31fea63
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
/** * B_William_the_Vigilant */ import java.util.*; import java.io.*; 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 fr = new FastReader(); int n = fr.nextInt(); int q = fr.nextInt(); String s = fr.nextLine(); char[] a = s.toCharArray(); int i = 0,j = 2; int minC = 0; while (j<s.length()) { if(s.substring(i, j+1).equals("abc")) { minC++; i+=3; j+=3; }else { i++; j++; } } while (q-->0) { int pos = fr.nextInt(); String temp = fr.next(); char c = temp.charAt(0); pos--; if(a[pos]==c) { System.out.println(minC); continue; }else { String str1 = ""; String str2 = ""; String str3 = ""; String str4 = ""; String str5 = ""; String str6 = ""; if(pos<n-1 && pos>0) { str5+=a[pos-1]; str5+=a[pos]; str5+=a[pos+1]; str6+=a[pos-1]; str6+=c; str6+=a[pos+1]; } if(pos<n-2) { str1+=a[pos]; str1+=a[pos+1]; str1+=a[pos+2]; // System.out.println(str1); str3+=c; str3+=a[pos+1]; str3+=a[pos+2]; // System.out.println(str3); } if(pos>1) { str2+=a[pos-2]; str2+=a[pos-1]; str2+=a[pos]; // System.out.println(str2); str4+=a[pos-2]; str4+=a[pos-1]; str4+=c; // System.out.println(str4); } boolean flag1 = (str1.equals("abc")||str2.equals("abc")||str5.equals("abc")); boolean flag2 = (str3.equals("abc")||str4.equals("abc")||str6.equals("abc")); if(flag1 && flag2) minC+=0; else if(flag1) minC--; else if(flag2) minC++; a[pos] = c; System.out.println(minC); } } } }
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
939e038e2afff541a52b716482db357a
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 Willian_The_Viligant_1609B { public static void main(String[] args) { Scanner Sc = new Scanner(System.in); int n = Sc.nextInt(), q = Sc.nextInt(); String s = Sc.next(); char[] list = s.toCharArray(); int count = 0; for(int i = 0; i < n - 2; i++) { if(isABC(i,list)) { count++; i += 2; } } while(q-- != 0) { int position = Sc.nextInt(); String c = Sc.next(); position--; for(int i = Math.max(0, position -2); i <= Math.min(position, n-3); i++) if(isABC(i,list)) count--; list[position] = c.charAt(0); for(int i = Math.max(0, position -2); i <= Math.min(position, n-3); i++) if(isABC(i,list)) count++; System.out.println(count); } Sc.close(); } private static boolean isABC(int i, char[] list) { if(list[i] == 'a' && list[i+1] == 'b' && list[i+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
e1ee59a779c2c2d13479f7c9f60ff782
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.lang.reflect.Array; import java.util.*; import java.lang.*; import java.io.*; public class practice { public static void main(String[] args) throws IOException { Reader.init(System.in); int n = Reader.nextInt(); int q = Reader.nextInt(); String s = Reader.next(); StringBuilder str = new StringBuilder(s); HashSet<Integer> set = new HashSet<>(); for(int i = 0;i<n-2;i++){ if(s.startsWith("abc", i)) set.add(i); } // System.out.println(set); for(int i = 0;i<q;i++){ int idx = Reader.nextInt()-1; String ch = Reader.next(); if(str.charAt(idx)==ch.charAt(0)){ System.out.println(set.size()); } else{ // set.remove(idx); str.setCharAt(idx,ch.charAt(0)); set.remove(idx-2); set.remove(idx-1); set.remove(idx); if(ch.equals("a")){ if(idx<n-2 && str.substring(idx, idx + 3).equals("abc")) set.add(idx); } else if(ch.equals("b")){ if(idx<n-1 && idx>0 && str.substring(idx-1, idx + 2).equals("abc")) set.add(idx-1); } else{ if(idx>1 && str.substring(idx-2, idx + 1).equals("abc")) set.add(idx-2); } System.out.println(set.size()); } // System.out.println(str); } // double var = (double) 1/2 + (double) 1/3 + (double) 1/4 + (double) 1/6 + (double) 1/12 + 1; // System.out.println(var); // int n = Reader.nextInt(); // int k = Reader.nextInt(); // int x = Reader.nextInt(); // // int[] arr = new int[n]; // // for(int i = 0;i<n;i++){ // arr[i] = Reader.nextInt(); // } // // int i = 0, j = 0; // long sum = 0; // long ans = 0; // while(j<n){ // sum+=arr[j]; // if(j-i+1<k){ // j++; // } // else{ // if(sum>x){ // // ans += sum-x; // sum-=arr[i]; // // i++; // // sum = Math.max(k-1,sum-x); // j++; // // } // else{ // sum-=arr[i]; // i++; // j++; // } // } // } // System.out.println(ans); } } class Reader { static BufferedReader reader; static StringTokenizer tokenizer; /** call this method to initialize reader for InputStream */ static void init(InputStream input) { reader = new BufferedReader( new InputStreamReader(input) ); tokenizer = new StringTokenizer(""); } /** get next word */ static String next() throws IOException { while ( ! tokenizer.hasMoreTokens() ) { //TODO add check for eof if necessary tokenizer = new StringTokenizer( reader.readLine() ); } return tokenizer.nextToken(); } static String nextLine() throws IOException { return reader.readLine(); } static int nextInt() throws IOException { return Integer.parseInt( next() ); } static long nextLong() throws IOException{ return Long.parseLong(next() ); } static double nextDouble() throws IOException { return Double.parseDouble( 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
1b25cf078aa0384cae4eda9cf7ddb8a9
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 practice; import java.io.*;import java.util.*; public class Main { static int fun(String s) { int c=0; for(int i=0;i<s.length()-2;i++) { if(s.substring(i,i+3).equals("abc")) c++; } return c; } static boolean check(int i, char[] s) { int n = s.length; if(i >= 2) if(s[i-2] == 'a' && s[i-1] == 'b' && s[i] == 'c') return true; if(i >= 1 && i < n-1) if(s[i-1] == 'a' && s[i] == 'b' && s[i+1] == 'c') return true; if(i < n-2) if(s[i] == 'a' && s[i+1] == 'b' && s[i+2] == 'c') return true; return false; } public static void main(String[] args) throws IOException { FastReader fr=new FastReader(); int tt=1; // tt=fr.nextInt(); while(tt-->0) { int n=fr.nextInt(); int q=fr.nextInt(); String s=fr.next(); char arr[]=s.toCharArray(); int ans=fun(s); StringBuilder sb=new StringBuilder(); for(int i=0;i<q;i++) { int pos=fr.nextInt(); char x=fr.next().charAt(0); if(check(pos-1,arr)) ans--; arr[pos-1]=x; if(check(pos-1,arr)) ans++; sb.append(ans+"\n"); } System.out.println(sb.toString()); } } 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
b19cd95b21767a9441148c6472815ae8
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 Set<Integer> sieve; public static void main(String[] args) { MyScanner s = new MyScanner(); // int testcases=s.nextInt(); // sieve(); // while(testcases-->0) { int n=s.nextInt(); int q=s.nextInt(); char[] arr=s.next().toCharArray(); int prev=0; for(int i=0;i+2<n;i++) { if(arr[i]=='a' && arr[i+1]=='b' && arr[i+2]=='c')prev++; } while(q-->0) { int ind=s.nextInt()-1; char ch = s.next().charAt(0); int i = Math.max(0, ind-2); int j = Math.min(n-1, ind+2); int curr=prev; for(int ii=i;ii+2<=j;ii++) { if(arr[ii]=='a' && arr[ii+1]=='b' && arr[ii+2]=='c')curr--; } arr[ind]=ch; for(int ii=i;ii+2<=j;ii++) { if(arr[ii]=='a' && arr[ii+1]=='b' && arr[ii+2]=='c')curr++; } System.out.println(curr); prev=curr; } // } } private static void sieve() { sieve=new HashSet<>(); boolean[] arr=new boolean[1000009]; int n=arr.length; for(int i=2;i<n;i++) { if(arr[i])continue; for(int j=i+i;j<n;j+=i) { arr[j]=true; } } for(int i=2;i<n;i++) { if(!arr[i])sieve.add(i); } // System.out.println(sieve.size()); } static long gcd(long a, long b) { // Everything divides 0 if (a == 0) return b; if (b == 0) return a; // base case if (a == b) return a; // a is greater if (a > b) return gcd(a - b, b); return gcd(a, b - a); } public static PrintWriter out; 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; } } }
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
b89b6bf59ca58cf1ae00d9942a8cba04
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.codeforces.Practise; import java.io.*; public class WilliamTheVigilant { //Don't Confuse Always make things simple //Experience is the name of the game // You won't fail until you stop trying....... // you can solve one problem by many approaches. when you stuck you are going to learn something new okk // Everything is easy. you feel its hard because of you don't know, and you not understand it very well. //// How to Improve Your problem-solving skill ??( By practise ). ***simple /// ==>> Solve problems slightly above of your level (to know some logic and how to approach cp problems) // Otherwise You will stay as it is okk. Learn from other code as well. ///////////////////////////////////////////////////////////////////////// /// How to Solve Problem in CP ? (you need to come up with brainstorm) ( if you feel problem is hard then your approach is wrong ) // ==>> Step01 :- Understanding problem statement clearly. Then Only Move Forward (Because Everything is mentioned in problem statement). // Step02 :- Think a lot about Solution. if you are confident that your solution might be correct Then only Move Forward // Step03 :- First think of brute force then move to optimal approach // Step04 :- Finally Code ( there is no any sense to code. if you not follow about steps okk) /////////////////////////////////////////////////////////////////////////// public static void main(String[] args)throws IOException { Reader scan=new Reader(); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); int n=scan.nextInt(); int q=scan.nextInt(); String s=scan.next(); char[] ch=s.toCharArray(); long count=0; for (int i = 0; i <=n-3; i++) { String str=s.substring(i,i+3); if(str.equals("abc")){ count++; } } while (q-->0){ int pos=scan.nextInt(); char repchar=scan.nextChar(); if(isContributing(n,pos,ch)){ count--; } ch[pos-1]=repchar; //replace and again check is contributing now if(isContributing(n,pos,ch)){ count++; } bw.write(count+""); bw.newLine(); bw.flush(); } } private static boolean isContributing(int n,int pos,char[] ch) { boolean flag=false; // left first ind String str; if(pos+2<=n){ // str=s.substring(pos-1,(pos+2)); str=ch[pos-1]+""+ch[pos]+""+ch[pos+1]; if(str.equals("abc")){ flag=true; } } // right last ind if(pos-2>=1){ // str=s.substring((pos-3),pos); str=ch[pos-3]+""+ch[pos-2]+""+ch[pos-1]; if(str.equals("abc")){ flag=true; } } //mid index if(pos+1<=n && pos-1>=1){ // str=s.substring(pos-2,(pos+1)); str=ch[pos-2]+""+ch[pos-1]+""+ch[pos]; if(str.equals("abc")){ flag=true; } } return flag; } //FAST READER static class Reader { 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 Reader() { in = new BufferedInputStream(System.in, BS); } public Reader(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+(cur < 0 ? -1*nextLong()/num : 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(); } } }
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
4e44f432a62f3e6e424e32ad80bdddca
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 { public static void main (String[] args) throws IOException { Kattio io = new Kattio(); int n = io.nextInt(); int q = io.nextInt(); String s = io.next(); int total = 0; for (int i=0; i<n-2; i++) { if (s.substring(i, i+3).equals("abc")) { total++; i += 2; } } char[] arr = s.toCharArray(); for (int ii=0; ii<q; ii++) { int k = io.nextInt() - 1; char c = io.next().charAt(0); //System.out.println("k c: " + k + " " + c); int start = 0; int end = 0; for (int i=k-2; i<=k; i++) { //System.out.println("istart: " + i + " " + start); if (i < 0 || i+2 >= n) { continue; } if (arr[i] == 'a' && arr[i+1] == 'b' && arr[i+2] == 'c') { start++; } } arr[k] = c; for (int i=k-2; i<=k; i++) { if (i < 0 || i+2 >= n) { continue; } if (arr[i] == 'a' && arr[i+1] == 'b' && arr[i+2] == 'c') { end++; } } //System.out.println(start + " " + end); total = total - start + end; System.out.println(total); } //System.out.println(total); } static class Kattio extends PrintWriter { private BufferedReader r; private StringTokenizer st; // standard input public Kattio() { this(System.in, System.out); } public Kattio(InputStream i, OutputStream o) { super(o); r = new BufferedReader(new InputStreamReader(i)); } // USACO-style file input public Kattio(String problemName) throws IOException { super(new FileWriter(problemName + ".out")); r = new BufferedReader(new FileReader(problemName + ".in")); } // returns null if no more input public String next() { try { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(r.readLine()); return st.nextToken(); } catch (Exception e) { } return null; } public int nextInt() { return Integer.parseInt(next()); } public double nextDouble() { return Double.parseDouble(next()); } public 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
6f27c905b8cf5d82a180a5c7ba234d1d
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 B { static FastScanner fs = new FastScanner(); static PrintWriter pw = new PrintWriter(System.out); static StringBuilder sb = new StringBuilder(""); final static int N = (int) 2e6 + 1; static long n, q; static char[] c = new char[N]; static int cnt = 0; public static void main(String[] args) { int n = fs.nextInt(); int q = fs.nextInt(); char[] chars = fs.next().toCharArray(); int cnt = 0; for (int i = 0; i + 2 < n; i++) { if (chars[i] == 'a' && chars[i+1] == 'b' && chars[i+2] == 'c') cnt++; } for (int i = 0; i < q; i++) { int x = fs.nextInt()-1; char c = fs.next().charAt(0); if (chars[x] == c) sb.append(cnt + "\n"); else { int ocnt = 0; for (int j = Math.max(0, x-2); j <= x && j+2 < n; j++) { if (chars[j] == 'a' && chars[j+1] == 'b' && chars[j+2] == 'c') ocnt++; } chars[x] = c; int ncnt = 0; for (int j = Math.max(0, x-2); j <= x && j+2 < n; j++) { if (chars[j] == 'a' && chars[j+1] == 'b' && chars[j+2] == 'c') ncnt++; } sb.append(cnt + ncnt - ocnt + "\n"); cnt = cnt + ncnt - ocnt; } } pw.print(sb.toString()); pw.close(); } static void sort(int[] a) { ArrayList<Integer> al = new ArrayList<>(); for (int i : a) al.add(i); Collections.sort(al); for (int i = 0; i < a.length; i++) a[i] = al.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) {}return st.nextToken();} int nextInt() {return Integer.parseInt(next());} long nextLong() {return Long.parseLong(next());} double nextDouble() {return Double.parseDouble(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
bf59f91f030a7c7432fd5d7f8adcc479
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
/* _oo0oo_ o8888888o 88" . "88 (| -_- |) 0\ = /0 ___/`---'\___ .' \\| |// '. / \\||| : |||// \ / _||||| -:- |||||- \ | | \\\ - /// | | | \_| ''\---/'' |_/ | \ .-\__ '-' ___/-. / ___'. .' /--.--\ `. .'___ ."" '< `.___\_<|>_/___.' >' "". | | : `- \`.;`\ _ /`;.`/ - ` : | | \ \ `_. \_ __\ /__ _/ .-` / / =====`-.____`.___ \_____/___.-`___.-'===== `=---=' */ import java.util.*; import java.math.*; import java.io.*; import java.lang.Math.*; public class KickStart2020 { 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; } } static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static long lcm(long a, long b) { return a / gcd(a, b) * b; } public static class Pair implements Comparable<Pair> { public int index; public int value; public Pair(int index, int value) { this.index = index; this.value = value; } @Override public int compareTo(Pair other) { // multiplied to -1 as the author need descending sort order if(other.index < this.index) return -1; if(other.index > this.index) return 1; return 0; } @Override public String toString() { return this.index + " " + value; } } static boolean isPrime(long d) { if (d == 3) return false; for (int i = 2; i <= (long) Math.sqrt(d); i++) { if (d % i == 0) return false; } return true; } static void decimalTob(int n, int k, int j, int arr[]) { int x = n % k; int y = n / k; arr[j++] += x; if(y > 0) { decimalTob(y, k, j, arr); } } static long powermod(long x, long y, long mod) { long ans = 1; x = x % mod; if (x == 0) return 0; int i = 1; while (y > 0) { if ((y & 1) != 0) ans = (ans * x) % mod; y = y >> 1; x = (x * x) % mod; } return ans; } static long power(long x, long y) { long res = 1; while (y > 0) { if ((y & 1) != 0) res = res * x; y = y >> 1; x = x * x; } return res;} static int check(int l, int r, char arr[]) { int count = 0; for(int i = l; i <= r - 2; i++) { if(arr[i] == 'a' && arr[i + 1] == 'b' && arr[i + 2] == 'c') count++; } return count; } public static void main(String[] args) throws Exception { FastReader sc = new FastReader(); PrintWriter out = new PrintWriter(System.out); int t = 1; outerloop: while(t-- > 0) { int n = sc.nextInt(); int q = sc.nextInt(); char arr[] = sc.next().toCharArray(); int count = check(0, n - 1, arr); // out.println(count); while(q-- > 0) { int a = sc.nextInt() - 1; char x = sc.next().charAt(0); int temp = check(Math.max(0, a - 2), Math.min(n - 1, a + 2), arr); count -= temp; arr[a] = x; count += check(Math.max(0, a - 2), Math.min(n - 1, a + 2), arr); out.println(count); } } 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
15fe81ee8fda471e69400b0e06a3edd4
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.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.Deque; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; import java.util.Queue; import java.util.Random; import java.util.Scanner; import java.util.StringTokenizer; public class Main { static int mod = (int) (1e9) + 7; /* ======================DSU===================== */ static class dsu { static int parent[], n;// min[],value[]; static long size[]; dsu(int n) { parent = new int[n + 1]; size = new long[n + 1]; // min=new int[n+1]; // value=new int[n+1]; Main.dsu.n = n; makeSet(); } static void makeSet() { for (int i = 1; i <= n; i++) { parent[i] = i; size[i] = 1; // min[i]=i; } } static int find(int a) { if (parent[a] == a) return a; else { return parent[a] = find(parent[a]);// Path Compression } } static void union(int a, int b) { int setA = find(a); int setB = find(b); if (setA == setB) return; if (size[setA] >= size[setB]) { parent[setB] = setA; size[setA] += size[setB]; } else { parent[setA] = setB; size[setB] += size[setA]; } } } /* ======================================================== */ static class Pair<X extends Number, Y extends Number> implements Comparator<Pair> { X x; Y y; // Constructor public Pair(X x, Y y) { this.x = x; this.y = y; } public Pair() { } @Override public int compare(Main.Pair o1, Main.Pair o2) { return ((int) (o1.y.intValue() - o2.y.intValue()));// Ascending Order based on 'y' } } /* ===============================Tries================================= */ static class TrieNode { private HashMap<Character, TrieNode> children = new HashMap<>(); public int size; boolean endOfWord; public void putChildIfAbsent(char ch) { children.putIfAbsent(ch, new TrieNode()); } public TrieNode getChild(char ch) { return children.get(ch); } } static private TrieNode root; public static void insert(String str) { TrieNode curr = root; for (char ch : str.toCharArray()) { curr.putChildIfAbsent(ch); curr = curr.getChild(ch); curr.size++; } // mark the current nodes endOfWord as true curr.endOfWord = true; } public static int search(String word) { TrieNode curr = root; for (char ch : word.toCharArray()) { curr = curr.getChild(ch); if (curr == null) { return 0; } } // size contains words starting with prefix- word return curr.size; } public boolean delete(TrieNode current, String word, int index) { if (index == word.length()) { // when end of word is reached only delete if currrent.endOfWord is true. if (!current.endOfWord) { return false; } current.endOfWord = false; // if current has no other mapping then return true return current.children.size() == 0; } char ch = word.charAt(index); TrieNode node = current.children.get(ch); if (node == null) { return false; } boolean shouldDeleteCurrentNode = delete(node, word, index + 1); // if true is returned then delete the mapping of character and trienode // reference from map. if (shouldDeleteCurrentNode) { current.children.remove(ch); // return true if no mappings are left in the map. return current.children.size() == 0; } return false; } /* ================================================================= */ static boolean isPrime(long n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (long i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } static Pair<Integer, Integer> lowerBound(long[] a, long x) { // x is the target, returns lowerBound. If not found // return -1 int l = -1, r = a.length; while (l + 1 < r) { int m = (l + r) >>> 1; if (a[m] == x) { return new Pair<>(m, 1); } if (a[m] >= x) r = m; else l = m; } return new Pair<>(r, 0); } static Pair<Integer, Integer> upperBound(long[] a, long x) {// x is the target, returns upperBound. If not found int l = -1, r = a.length; while (l + 1 < r) { int m = (l + r) >>> 1; if (a[m] == x) { return new Pair<>(m, 1); } if (a[m] <= x) l = m; else r = m; } return new Pair<>(l + 1, 0); } 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 * b) / gcd(a, b); } static long power(long x, long y) { long res = 1; while (y > 0) { if (y % 2 == 1) res = (res * x); y >>= 1; x = (x * x); } return res; } public double binPow(double x, int n) {// binary exponentiation with negative power as well if (n == 0) return 1.0; double binPow = binPow(x, n / 2); if (n % 2 == 0) { return binPow * binPow; } else { return n > 0 ? (binPow * binPow * x) : (binPow * binPow / x); } } static long ceil(long x, long y) { return (x % y == 0 ? x / y : (x / y + 1)); } /* * ===========Modular Operations================== */ static long modPower(long x, long y, long p) { long res = 1; x = x % p; while (y > 0) { if (y % 2 == 1) res = (res * x) % p; y = y >> 1; // y = y/2 x = (x * x) % p; } return res; } static long modInverse(long n, long p) { return modPower(n, p - 2, p); } static long modAdd(long a, long b) { return ((a + b + mod) % mod); } static long modMul(long a, long b) { return ((a % mod) * (b % mod)) % mod; } static long[] fac = new long[200000 + 5]; static long[] invFac = new long[200000 + 5]; static long nCrModPFermat(int n, int r) { if (r == 0) return 1; // return (fac[n] * modInverse(fac[r], mod) % mod * modInverse(fac[n - r], mod) // % mod) % mod; return (fac[n] * invFac[r] % mod * invFac[n - r] % mod) % mod; } /* * =============================================== */ static void ruffleSort(long[] a) { int n = a.length; Random r = new Random(); for (int i = 0; i < a.length; i++) { int oi = r.nextInt(n); long temp = a[i]; a[i] = a[oi]; a[oi] = temp; } Arrays.sort(a); } static void ruffleSort(int[] a) { 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; } Arrays.sort(a); } static String binString32(int n) { StringBuilder str = new StringBuilder(""); String bin = Integer.toBinaryString(n); if (bin.length() != 32) { for (int k = 0; k < 32 - bin.length(); k++) { str.append("0"); } str.append(bin); } return str.toString(); } static class sparseTable { public static int st[][]; public static int log = 4; static int func(int a, int b) {// make func as per question(here min range query) return (int) gcd(a, b); } void makeTable(int n, int a[]) { st = new int[n][log]; for (int i = 0; i < n; i++) { st[i][0] = a[i]; } for (int j = 1; j < log; j++) { for (int i = 0; i + (1 << j) <= n; i++) { st[i][j] = func(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]); } } } static int query(int l, int r) { int length = r - l + 1; int k = 0; while ((1 << (k + 1)) <= length) { k++; } return func(st[l][k], st[r - (1 << k) + 1][k]); } static void printTable(int n) { for (int j = 0; j < log; j++) { for (int i = 0; i < n; i++) { System.out.print(st[i][j] + " "); } System.out.println(); } } } /* * ====================================Main================================= */ // static int st[], a[]; // static void buildTree(int treeIndex, int lo, int hi) { // if (hi == lo) { // st[treeIndex] = a[lo]; // return; // } // int mid = (lo + hi) / 2; // buildTree(treeIndex * 2 + 1, lo, mid); // buildTree(treeIndex * 2 + 2, mid + 1, hi); // st[treeIndex] = merge(st[treeIndex * 2 + 1], st[treeIndex * 2 + 2]); // } // static void update(int treeIndex, int lo, int hi, int arrIndex, int val) { // if (hi == lo) { // st[treeIndex] = val; // a[arrIndex] = val; // return; // } // int mid = (hi + lo) / 2; // if (mid < arrIndex) { // update(treeIndex * 2 + 2, mid + 1, hi, arrIndex, val); // } else { // update(treeIndex * 2 + 1, lo, mid, arrIndex, val); // } // st[treeIndex] = merge(st[treeIndex * 2 + 1], st[treeIndex * 2 + 2]); // } // static int query(int treeIndex, int lo, int hi, int l, int r) { // if (l <= lo && r >= hi) { // return st[treeIndex]; // } // if (l > hi || r < lo) { // return 0; // } // int mid = (hi + lo) / 2; // return query(treeIndex * 2 + 1, lo, mid, l, Math.min(mid, r)); // } // static int merge(int a, int b) { // return a + b; // } public static long findKthPositive(long[] A, long k) { int l = 0, r = A.length, m; while (l < r) { m = (l + r) / 2; if (A[m] - 1 - m < k) l = m + 1; else r = m; } return l + k; } static int[] z_function(char ar[]) { int[] z = new int[ar.length]; z[0] = ar.length; int l = 0; int r = 0; for (int i = 1; i < ar.length; i++) { if (r < i) { l = i; r = i; while (r < ar.length && ar[r - l] == ar[r]) r++; z[i] = r - l; r--; } else { int k = i - l; if (z[k] < r - i + 1) { z[i] = z[k]; } else { l = i; while (r < ar.length && ar[r - l] == ar[r]) r++; z[i] = r - l; r--; } } } return z; } static void mergeSort(int a[]) { int n = a.length; if (n >= 2) { int mid = n / 2; int left[] = new int[mid]; int right[] = new int[n - mid]; for (int i = 0; i < mid; i++) { left[i] = a[i]; } for (int i = mid; i < n; i++) { right[i - mid] = a[i]; } mergeSort(left); mergeSort(right); mergeSortedArray(left, right, a); } } static void mergeSortedArray(int left[], int right[], int a[]) { int i = 0, j = 0, k = 0, n = left.length, m = right.length; while (i != n && j != m) { if (left[i] < right[j]) { a[k++] = left[i++]; } else { a[k++] = right[j++]; } } while (i != n) { a[k++] = left[i++]; } while (j != m) { a[k++] = right[j++]; } } // BINARY SEARCH // count of set bits in a particular position // suffix max array/ suffix sum array/ prefix 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[] intArr(int n) { int res[] = new int[n]; for (int i = 0; i < n; i++) res[i] = nextInt(); return res; } long[] longArr(int n) { long res[] = new long[n]; for (int i = 0; i < n; i++) res[i] = nextLong(); return res; } } static FastReader f = new FastReader(); static BufferedWriter w = new BufferedWriter(new OutputStreamWriter(System.out)); static ArrayList<String> strArr = new ArrayList<>(); public static void main(String args[]) throws Exception { int t = 1; // t = f.nextInt(); int tc = 1; // fac[0] = 1; // for (int i = 1; i <= 200000; i++) { // fac[i] = fac[i - 1] * i % mod; // invFac[i] = modInverse(fac[i], mod); // } while (t-- != 0) { int n=f.nextInt(); int q=f.nextInt(); int cnt=0; char ch[]=f.next().toCharArray(); for(int i=0;i<n-2;i++){ if(ch[i]=='a' && ch[i+1]=='b' && ch[i+2]=='c'){ cnt++; } } for(int i=0;i<q;i++){ int pos=f.nextInt(); char c=f.next().charAt(0); pos--; boolean wasABC=checkABC(ch,pos); ch[pos]=c; boolean isABC=checkABC(ch,pos); if(wasABC && !isABC){ cnt--; }else if(!wasABC && isABC){ cnt++; } w.write(cnt+"\n"); } } w.flush(); } static boolean checkABC(char ch[],int pos){ String a,b,c; a=b=c=""; int n=ch.length; if(pos+2<n){ a=a+ch[pos]+ch[pos+1]+ch[pos+2]; } if(pos+1<n && pos-1>=0){ b=b+ch[pos-1]+ch[pos]+ch[pos+1]; } if(pos-2>=0){ c=c+ch[pos-2]+ch[pos-1]+ch[pos]; } return a.equals("abc")||b.equals("abc")||c.equals("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
db22a6b674774622c6f3b384f48a7f48
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.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.Deque; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; import java.util.Queue; import java.util.Random; import java.util.Scanner; import java.util.StringTokenizer; public class Main { static int mod = (int) (1e9) + 7; /* ======================DSU===================== */ static class dsu { static int parent[], n;// min[],value[]; static long size[]; dsu(int n) { parent = new int[n + 1]; size = new long[n + 1]; // min=new int[n+1]; // value=new int[n+1]; Main.dsu.n = n; makeSet(); } static void makeSet() { for (int i = 1; i <= n; i++) { parent[i] = i; size[i] = 1; // min[i]=i; } } static int find(int a) { if (parent[a] == a) return a; else { return parent[a] = find(parent[a]);// Path Compression } } static void union(int a, int b) { int setA = find(a); int setB = find(b); if (setA == setB) return; if (size[setA] >= size[setB]) { parent[setB] = setA; size[setA] += size[setB]; } else { parent[setA] = setB; size[setB] += size[setA]; } } } /* ======================================================== */ static class Pair<X extends Number, Y extends Number> implements Comparator<Pair> { X x; Y y; // Constructor public Pair(X x, Y y) { this.x = x; this.y = y; } public Pair() { } @Override public int compare(Main.Pair o1, Main.Pair o2) { return ((int) (o1.y.intValue() - o2.y.intValue()));// Ascending Order based on 'y' } } /* ===============================Tries================================= */ static class TrieNode { private HashMap<Character, TrieNode> children = new HashMap<>(); public int size; boolean endOfWord; public void putChildIfAbsent(char ch) { children.putIfAbsent(ch, new TrieNode()); } public TrieNode getChild(char ch) { return children.get(ch); } } static private TrieNode root; public static void insert(String str) { TrieNode curr = root; for (char ch : str.toCharArray()) { curr.putChildIfAbsent(ch); curr = curr.getChild(ch); curr.size++; } // mark the current nodes endOfWord as true curr.endOfWord = true; } public static int search(String word) { TrieNode curr = root; for (char ch : word.toCharArray()) { curr = curr.getChild(ch); if (curr == null) { return 0; } } // size contains words starting with prefix- word return curr.size; } public boolean delete(TrieNode current, String word, int index) { if (index == word.length()) { // when end of word is reached only delete if currrent.endOfWord is true. if (!current.endOfWord) { return false; } current.endOfWord = false; // if current has no other mapping then return true return current.children.size() == 0; } char ch = word.charAt(index); TrieNode node = current.children.get(ch); if (node == null) { return false; } boolean shouldDeleteCurrentNode = delete(node, word, index + 1); // if true is returned then delete the mapping of character and trienode // reference from map. if (shouldDeleteCurrentNode) { current.children.remove(ch); // return true if no mappings are left in the map. return current.children.size() == 0; } return false; } /* ================================================================= */ static boolean isPrime(long n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (long i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } static Pair<Integer, Integer> lowerBound(long[] a, long x) { // x is the target, returns lowerBound. If not found // return -1 int l = -1, r = a.length; while (l + 1 < r) { int m = (l + r) >>> 1; if (a[m] == x) { return new Pair<>(m, 1); } if (a[m] >= x) r = m; else l = m; } return new Pair<>(r, 0); } static Pair<Integer, Integer> upperBound(long[] a, long x) {// x is the target, returns upperBound. If not found int l = -1, r = a.length; while (l + 1 < r) { int m = (l + r) >>> 1; if (a[m] == x) { return new Pair<>(m, 1); } if (a[m] <= x) l = m; else r = m; } return new Pair<>(l + 1, 0); } 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 * b) / gcd(a, b); } static long power(long x, long y) { long res = 1; while (y > 0) { if (y % 2 == 1) res = (res * x); y >>= 1; x = (x * x); } return res; } public double binPow(double x, int n) {// binary exponentiation with negative power as well if (n == 0) return 1.0; double binPow = binPow(x, n / 2); if (n % 2 == 0) { return binPow * binPow; } else { return n > 0 ? (binPow * binPow * x) : (binPow * binPow / x); } } static long ceil(long x, long y) { return (x % y == 0 ? x / y : (x / y + 1)); } /* * ===========Modular Operations================== */ static long modPower(long x, long y, long p) { long res = 1; x = x % p; while (y > 0) { if (y % 2 == 1) res = (res * x) % p; y = y >> 1; // y = y/2 x = (x * x) % p; } return res; } static long modInverse(long n, long p) { return modPower(n, p - 2, p); } static long modAdd(long a, long b) { return ((a + b + mod) % mod); } static long modMul(long a, long b) { return ((a % mod) * (b % mod)) % mod; } static long[] fac = new long[200000 + 5]; static long[] invFac = new long[200000 + 5]; static long nCrModPFermat(int n, int r) { if (r == 0) return 1; // return (fac[n] * modInverse(fac[r], mod) % mod * modInverse(fac[n - r], mod) // % mod) % mod; return (fac[n] * invFac[r] % mod * invFac[n - r] % mod) % mod; } /* * =============================================== */ static void ruffleSort(long[] a) { int n = a.length; Random r = new Random(); for (int i = 0; i < a.length; i++) { int oi = r.nextInt(n); long temp = a[i]; a[i] = a[oi]; a[oi] = temp; } Arrays.sort(a); } static void ruffleSort(int[] a) { 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; } Arrays.sort(a); } static String binString32(int n) { StringBuilder str = new StringBuilder(""); String bin = Integer.toBinaryString(n); if (bin.length() != 32) { for (int k = 0; k < 32 - bin.length(); k++) { str.append("0"); } str.append(bin); } return str.toString(); } static class sparseTable { public static int st[][]; public static int log = 4; static int func(int a, int b) {// make func as per question(here min range query) return (int) gcd(a, b); } void makeTable(int n, int a[]) { st = new int[n][log]; for (int i = 0; i < n; i++) { st[i][0] = a[i]; } for (int j = 1; j < log; j++) { for (int i = 0; i + (1 << j) <= n; i++) { st[i][j] = func(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]); } } } static int query(int l, int r) { int length = r - l + 1; int k = 0; while ((1 << (k + 1)) <= length) { k++; } return func(st[l][k], st[r - (1 << k) + 1][k]); } static void printTable(int n) { for (int j = 0; j < log; j++) { for (int i = 0; i < n; i++) { System.out.print(st[i][j] + " "); } System.out.println(); } } } /* * ====================================Main================================= */ // static int st[], a[]; // static void buildTree(int treeIndex, int lo, int hi) { // if (hi == lo) { // st[treeIndex] = a[lo]; // return; // } // int mid = (lo + hi) / 2; // buildTree(treeIndex * 2 + 1, lo, mid); // buildTree(treeIndex * 2 + 2, mid + 1, hi); // st[treeIndex] = merge(st[treeIndex * 2 + 1], st[treeIndex * 2 + 2]); // } // static void update(int treeIndex, int lo, int hi, int arrIndex, int val) { // if (hi == lo) { // st[treeIndex] = val; // a[arrIndex] = val; // return; // } // int mid = (hi + lo) / 2; // if (mid < arrIndex) { // update(treeIndex * 2 + 2, mid + 1, hi, arrIndex, val); // } else { // update(treeIndex * 2 + 1, lo, mid, arrIndex, val); // } // st[treeIndex] = merge(st[treeIndex * 2 + 1], st[treeIndex * 2 + 2]); // } // static int query(int treeIndex, int lo, int hi, int l, int r) { // if (l <= lo && r >= hi) { // return st[treeIndex]; // } // if (l > hi || r < lo) { // return 0; // } // int mid = (hi + lo) / 2; // return query(treeIndex * 2 + 1, lo, mid, l, Math.min(mid, r)); // } // static int merge(int a, int b) { // return a + b; // } public static long findKthPositive(long[] A, long k) { int l = 0, r = A.length, m; while (l < r) { m = (l + r) / 2; if (A[m] - 1 - m < k) l = m + 1; else r = m; } return l + k; } static int[] z_function(char ar[]) { int[] z = new int[ar.length]; z[0] = ar.length; int l = 0; int r = 0; for (int i = 1; i < ar.length; i++) { if (r < i) { l = i; r = i; while (r < ar.length && ar[r - l] == ar[r]) r++; z[i] = r - l; r--; } else { int k = i - l; if (z[k] < r - i + 1) { z[i] = z[k]; } else { l = i; while (r < ar.length && ar[r - l] == ar[r]) r++; z[i] = r - l; r--; } } } return z; } static void mergeSort(int a[]) { int n = a.length; if (n >= 2) { int mid = n / 2; int left[] = new int[mid]; int right[] = new int[n - mid]; for (int i = 0; i < mid; i++) { left[i] = a[i]; } for (int i = mid; i < n; i++) { right[i - mid] = a[i]; } mergeSort(left); mergeSort(right); mergeSortedArray(left, right, a); } } static void mergeSortedArray(int left[], int right[], int a[]) { int i = 0, j = 0, k = 0, n = left.length, m = right.length; while (i != n && j != m) { if (left[i] < right[j]) { a[k++] = left[i++]; } else { a[k++] = right[j++]; } } while (i != n) { a[k++] = left[i++]; } while (j != m) { a[k++] = right[j++]; } } // BINARY SEARCH // count of set bits in a particular position // suffix max array/ suffix sum array/ prefix 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[] intArr(int n) { int res[] = new int[n]; for (int i = 0; i < n; i++) res[i] = nextInt(); return res; } long[] longArr(int n) { long res[] = new long[n]; for (int i = 0; i < n; i++) res[i] = nextLong(); return res; } } static FastReader f = new FastReader(); static BufferedWriter w = new BufferedWriter(new OutputStreamWriter(System.out)); static ArrayList<String> strArr = new ArrayList<>(); public static void main(String args[]) throws Exception { int t = 1; // t = f.nextInt(); int tc = 1; // fac[0] = 1; // for (int i = 1; i <= 200000; i++) { // fac[i] = fac[i - 1] * i % mod; // invFac[i] = modInverse(fac[i], mod); // } while (t-- != 0) { int n=f.nextInt(); int q=f.nextInt(); int cnt=0; char ch[]=f.next().toCharArray(); for(int i=0;i<n-2;i++){ if(ch[i]=='a' && ch[i+1]=='b' && ch[i+2]=='c'){ cnt++; } } for(int i=0;i<q;i++){ int pos=f.nextInt(); char c=f.next().charAt(0); pos--; boolean wasABC=checkABC(ch,pos); ch[pos]=c; boolean isABC=checkABC(ch,pos); if(wasABC && !isABC){ cnt--; }else if(!wasABC && isABC){ cnt++; } w.write(cnt+"\n"); } } w.flush(); } static boolean checkABC(char ch[],int pos){ String a,b,c; a=b=c=""; int n=ch.length; if(pos+2<n){ a=a+ch[pos]+ch[pos+1]+ch[pos+2]; } if(pos+1<n && pos-1>=0){ b=b+ch[pos-1]+ch[pos]+ch[pos+1]; } if(pos-2>=0){ c=c+ch[pos-2]+ch[pos-1]+ch[pos]; } return a.equals("abc")||b.equals("abc")||c.equals("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
42660617e83d1bd897381661dc306166
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.BigInteger; import java.util.*; public class Main { static int MOD = 1000000007; // After writing solution, quick scan for: // array out of bounds // special cases e.g. n=1? // npe, particularly in maps // // Big numbers arithmetic bugs: // int overflow // sorting, or taking max, after MOD void solve() throws IOException { int[] nq = ril(2); int n = nq[0]; int q = nq[1]; char[] s = rs(); int count = 0; for (int i = 0; i + 2 < n; i++) { if (s[i] == 'a' && s[i+1] == 'b' && s[i+2] == 'c') count++; } for (int qi = 0; qi < q; qi++) { String[] line = br.readLine().split(" "); int i = Integer.parseInt(line[0]) - 1; char c = line[1].charAt(0); if (s[i] == c) { pw.println(count); continue; } boolean sub = false; if (s[i] == 'a' && i+2 < n && s[i+1] == 'b' && s[i+2] == 'c') sub = true; else if (s[i] == 'b' && i-1 >= 0 && i+1 < n && s[i-1] == 'a' && s[i+1] == 'c') sub = true; else if (s[i] == 'c' && i-2 >= 0 && s[i-2] == 'a' && s[i-1] == 'b') sub = true; boolean add = false; if (c == 'a' && i+2 < n && s[i+1] == 'b' && s[i+2] == 'c') add = true; else if (c == 'b' && i-1 >= 0 && i+1 < n && s[i-1] == 'a' && s[i+1] == 'c') add = true; else if (c == 'c' && i-2 >= 0 && s[i-2] == 'a' && s[i-1] == 'b') add = true; if (sub) count--; if (add) count++; s[i] = c; pw.println(count); } } // IMPORTANT // DID YOU CHECK THE COMMON MISTAKES ABOVE? // Template code below BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(System.out); public static void main(String[] args) throws IOException { Main m = new Main(); m.solve(); m.close(); } void close() throws IOException { pw.flush(); pw.close(); br.close(); } int ri() throws IOException { return Integer.parseInt(br.readLine().trim()); } long rl() throws IOException { return Long.parseLong(br.readLine().trim()); } int[] ril(int n) throws IOException { int[] nums = new int[n]; int c = 0; for (int i = 0; i < n; i++) { int sign = 1; c = br.read(); int x = 0; if (c == '-') { sign = -1; c = br.read(); } while (c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = br.read(); } nums[i] = x * sign; } while (c != '\n' && c != -1) c = br.read(); return nums; } long[] rll(int n) throws IOException { long[] nums = new long[n]; int c = 0; for (int i = 0; i < n; i++) { int sign = 1; c = br.read(); long x = 0; if (c == '-') { sign = -1; c = br.read(); } while (c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = br.read(); } nums[i] = x * sign; } while (c != '\n' && c != -1) c = br.read(); return nums; } int[] rkil() throws IOException { int sign = 1; int c = br.read(); int x = 0; if (c == '-') { sign = -1; c = br.read(); } while (c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = br.read(); } return ril(x); } long[] rkll() throws IOException { int sign = 1; int c = br.read(); int x = 0; if (c == '-') { sign = -1; c = br.read(); } while (c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = br.read(); } return rll(x); } char[] rs() throws IOException { return br.readLine().toCharArray(); } void sort(int[] A) { Random r = new Random(); for (int i = A.length-1; i > 0; i--) { int j = r.nextInt(i+1); int temp = A[i]; A[i] = A[j]; A[j] = temp; } Arrays.sort(A); } void printDouble(double d) { pw.printf("%.16f", d); } }
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
b571281f481d7425098e97421223af8d
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 WilliamVigilant { static final int MOD = (int) 1e9 + 7; static boolean abc(char []c,int pos){ if(pos+2>=c.length) return false; return c[pos]=='a'&&c[pos+1]=='b'&&c[pos+2]=='c'; } public static void main(String[] args) { FastScanner fs = new FastScanner(); PrintWriter out = new PrintWriter(System.out); int n = fs.nextInt(); int q = fs.nextInt(); char []c = fs.next().toCharArray(); int ans =0; for(int i=0;i<n-2;i++) if(abc(c,i)) ans++; for(int k=0;k<q;k++){ int pos = fs.nextInt()-1; char x = fs.next().toCharArray()[0]; for(int i=Math.max(pos-2,0);i<=pos;i++) if(abc(c,i))ans--; c[pos]=x; for(int i=Math.max(pos-2,0);i<=pos;i++) if(abc(c,i))ans++; 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[] 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; } int nextInt() {return Integer.parseInt(next());} 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
f9f43d52a7bddfeb7950842933073666
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 Contest2; import java.util.Scanner; public class WilliamTheVigilant { static StringBuilder sb; public static void main(String[] args) { Scanner scan = new Scanner(System.in); int len = scan.nextInt(); int queries = scan.nextInt(); scan.nextLine(); sb = new StringBuilder(scan.nextLine()); int count = 0; for (int i = 0; i < len-2; i++) if (sb.substring(i, i + 3).equals("abc")) count++; for (int i = 0; i < queries; i++) { int change = getChange(scan.nextInt()-1, scan.next().trim().charAt(0)); System.out.println(count += change); } } private static int getChange(int ind, char replace) { int before = (sb.substring(Math.max(ind-2, 0), Math.min(sb.length(), ind + 3)).contains("abc")) ? 1 : 0; sb.replace(ind, ind+1, replace+""); // System.out.println(sb); int after = (sb.substring(Math.max(ind-2, 0), Math.min(sb.length(), ind + 3)).contains("abc")) ? 1 : 0; return after - before; } }
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
2042a99f02f238dededf422952c5e7a3
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 { public static void main(String[] args) { FScanner sc = new FScanner(); //int t=sc.nextInt(); // while(t-->0) { int n=sc.nextInt(); int q=sc.nextInt(); String s=sc.next(); int count=0; for(int i=0;i<n-2;i++) { if(s.charAt(i) == 'a' && s.charAt(i+1) == 'b' && s.charAt(i+2) == 'c') count++; } char ch[] = s.toCharArray(); while(q-->0) { int pos=sc.nextInt()-1; char c=sc.next().charAt(0); if(ch[pos]==c) { System.out.println(count); continue; } else { int i=0; if(pos-2>=0) i=pos-2; for(int j=i;j+2<pos+3 && j+2<n ;j++ ) { if(ch[j] == 'a' && ch[j+1] == 'b' && ch[j+2] == 'c'){ count--;} } ch[pos]=c; for(int j=i;j+2<pos+3 && j+2<n ;j++ ) { if(ch[j] == 'a' && ch[j+1] == 'b' && ch[j+2] == 'c'){ count++; } } System.out.println(count); } } // } } } class pair implements Comparable<pair>{ int f;int ind; public pair(int f,int ind) { this.f=f; this.ind=ind; } public int compareTo(pair p) {return this.f-p.f;} } class FScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer sb = new StringTokenizer(""); String next(){ while(!sb.hasMoreTokens()){ try{ sb = new StringTokenizer(br.readLine()); } catch(IOException e){ } } return sb.nextToken(); } String nextLine(){ try{ return br.readLine(); } catch(IOException e) { } return ""; } int nextInt(){ return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } int[] readArray(int n) { int a[] = new int[n]; for(int i=0;i<n;i++) a[i] = nextInt(); return a; } float nextFloat(){ return Float.parseFloat(next()); } double nextDouble(){ return Double.parseDouble(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 8
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
82b5e0d931b58336eb50331c83c85487
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
/* Challenge 1: Newbie to CM in 1year (Dec 2021 - Nov 2022) 5* Codechef Challenge 2: CM to IM in 1 year (Dec 2022 - Nov 2023) 6* Codechef Challenge 3: IM to GM in 1 year (Dec 2023 - Nov 2024) 7* Codechef Goal: Become better in CP! Key: Consistency and Discipline Desire: SDE @ Google USA Motto: Do what i Love <=> Love what i do */ import java.util.*; import java.io.*; import java.math.*; public class Coder { static StringBuffer str=new StringBuffer(); static int n; static char c[]; static void solve(){ } public static void main(String[] args) throws java.lang.Exception { BufferedReader bf; PrintWriter pw; boolean lenv=false; if(lenv){ bf = new BufferedReader( new FileReader("input.txt")); pw=new PrintWriter(new BufferedWriter(new FileWriter("output.txt"))); }else{ bf = new BufferedReader(new InputStreamReader(System.in)); pw = new PrintWriter(new OutputStreamWriter(System.out)); } // int t = Integer.parseInt(bf.readLine().trim()); // while (t-- > 0) { String s[]=bf.readLine().trim().split("\\s+"); n=Integer.parseInt(s[0]); int q=Integer.parseInt(s[1]); c=bf.readLine().trim().toCharArray(); int cnt=0; for(int i=2;i<n;i++) if(c[i-2]=='a' && c[i-1]=='b' && c[i]=='c') cnt++; for(int i=0;i<q;i++){ s=bf.readLine().trim().split("\\s+"); int idx=Integer.parseInt(s[0])-1; char ch=s[1].charAt(0); // check for erasing if(idx-1>=0 && idx-2>=0 && c[idx-2]=='a' && c[idx-1]=='b' && c[idx]=='c' && ch!='c') cnt--; if(idx-1>=0 && c[idx-1]=='a' && c[idx]=='b' && idx+1<n && c[idx+1]=='c' && ch!='b') cnt--; if(idx+1<n && idx+2<n && c[idx+2]=='c' && c[idx+1]=='b' && c[idx]=='a' && ch!='a') cnt--; // check for adding if(idx-1>=0 && idx-2>=0 && c[idx-2]=='a' && c[idx-1]=='b' && ch=='c' && c[idx]!='c') cnt++; if(idx-1>=0 && c[idx-1]=='a' && ch=='b' && idx+1<n && c[idx+1]=='c' && c[idx]!='b') cnt++; if(idx+1<n && idx+2<n && c[idx+2]=='c' && c[idx+1]=='b' && ch=='a' && c[idx]!='a') cnt++; c[idx]=ch; str.append(cnt).append("\n"); } // } pw.println(str); pw.flush(); // System.outin.print(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 8
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
c57331895ee23dda3365747a34dd9373
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.*; /* Challenge 1: Pupil to CM in 1year (Dec 2021 - Nov 2022) 🔥 5* Codechef Challenge 2: CM to IM in 1 year (Dec 2022 - Nov 2023) 🔥🔥 6* Codechef Challenge 3: IM to GM in 1 year (Dec 2023 - Nov 2024) 🔥🔥🔥 7* Codechef Goal: Become better in CP! Key: Consistency! */ public class Coder { static StringBuffer str=new StringBuffer(); static int n, q; static char c[]; static List<int []> l; static void solve(){ int cnt=0; for(int i=0;i<n-2;i++){ if(c[i]=='a' && c[i+1]=='b' && c[i+2]=='c'){ cnt++; } } for(int i=0;i<q;i++){ int idx=l.get(i)[0]-1; char ch=(char)l.get(i)[1]; if(c[idx]!=ch){ boolean flag=false; if(idx-2>=0 && c[idx-2]=='a' && c[idx-1]=='b' && c[idx]=='c') flag=true; if(idx-1>=0 && idx+1<n && c[idx-1]=='a' && c[idx]=='b' && c[idx+1]=='c') flag=true; if(idx+2<n && c[idx]=='a' && c[idx+1]=='b' && c[idx+2]=='c') flag=true; c[idx]=ch; if(flag){ flag=false; if(idx-2>=0 && c[idx-2]=='a' && c[idx-1]=='b' && c[idx]=='c') flag=true; if(idx-1>=0 && idx+1<n && c[idx-1]=='a' && c[idx]=='b' && c[idx+1]=='c') flag=true; if(idx+2<n && c[idx]=='a' && c[idx+1]=='b' && c[idx+2]=='c') flag=true; if(!flag) cnt--; } else{ if(idx-2>=0 && c[idx-2]=='a' && c[idx-1]=='b' && c[idx]=='c') flag=true; if(idx-1>=0 && idx+1<n && c[idx-1]=='a' && c[idx]=='b' && c[idx+1]=='c') flag=true; if(idx+2<n && c[idx]=='a' && c[idx+1]=='b' && c[idx+2]=='c') flag=true; if(flag) cnt++; } } str.append(cnt).append("\n"); } } public static void main(String[] args) throws java.lang.Exception { BufferedReader bf; PrintWriter pw; boolean lenv=false; if(lenv){ bf = new BufferedReader( new FileReader("input.txt")); pw=new PrintWriter(new BufferedWriter(new FileWriter("output.txt"))); }else{ bf = new BufferedReader(new InputStreamReader(System.in)); pw = new PrintWriter(new OutputStreamWriter(System.out)); } // int t = Integer.parseInt(bf.readLine().trim()); // while (t-- > 0) { String s[]=bf.readLine().trim().split("\\s+"); n=Integer.parseInt(s[0]); q=Integer.parseInt(s[1]); c=bf.readLine().trim().toCharArray(); l=new ArrayList<>(); for(int i=0;i<q;i++){ String st[]=bf.readLine().trim().split("\\s+"); int c1[]=new int[2]; c1[0]=Integer.parseInt(st[0]); c1[1]=st[1].charAt(0); l.add(c1); } solve(); // } pw.print(str); pw.flush(); // System.out.print(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 8
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
c78431a4fbaa3a85d7c5c98cf9908af0
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.*; public class Main { static final int INF = 0x3f3f3f3f; static final long LNF = 0x3f3f3f3f3f3f3f3fL; public static void main(String[] args) throws IOException { initReader(); int n=nextInt(); int q=nextInt(); String a=next(); char[]arr=a.toCharArray(); int count=0; for(int i=0;i<n-2;i++){ if(arr[i]=='a'&&arr[i+1]=='b'&&arr[i+2]=='c')count++; } while (q--!=0){ int id=nextInt(); char b=nextChar(); for(int i=id-3;i<=id-1;i++){ if(i>=0&&i+2<n&&arr[i]=='a'&&arr[i+1]=='b'&&arr[i+2]=='c')count--; } arr[id-1]=b; for(int i=id-3;i<=id-1;i++){ if(i>=0&&i+2<n&&arr[i]=='a'&&arr[i+1]=='b'&&arr[i+2]=='c')count++; } pw.println(count); } pw.close(); } /***************************************************************************************/ static BufferedReader reader; static StringTokenizer tokenizer; static PrintWriter pw; public static void initReader() throws IOException { reader = new BufferedReader(new InputStreamReader(System.in)); tokenizer = new StringTokenizer(""); pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); // 从文件读写 // reader = new BufferedReader(new FileReader("test.in")); // tokenizer = new StringTokenizer(""); // pw = new PrintWriter(new BufferedWriter(new FileWriter("test1.out"))); } public static boolean hasNext() { try { while (!tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); } } catch (Exception e) { return false; } return true; } public static String next() throws IOException { while (!tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); } return tokenizer.nextToken(); } public static String nextLine() { try { return reader.readLine(); } catch (Exception e) { return null; } } public static int nextInt() throws IOException { return Integer.parseInt(next()); } public static long nextLong() throws IOException { return Long.parseLong(next()); } public static double nextDouble() throws IOException { return Double.parseDouble(next()); } public static char nextChar() throws IOException { return next().charAt(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 8
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
97ad89df89735351c42ece748f7d6f99
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.*; public class Codechef { static int[] num_to_bits = new int[] { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4 }; static int countSetBitsRec(int num) { int nibble = 0; if (0 == num) return num_to_bits[0]; // Find last nibble nibble = num & 0xf; // Use pre-stored values to find count // in last nibble plus recursively add // remaining nibbles. return num_to_bits[nibble] + countSetBitsRec(num >> 4); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { try { br=new BufferedReader(new FileReader("input.txt")); PrintStream out= new PrintStream(new FileOutputStream("output.txt")); System.setOut(out); } // Catch block to handle the exceptions catch (Exception e) { 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 computeXOR(int n) { // If n is a multiple of 4 if (n % 4 == 0) return n; // If n%4 gives remainder 1 if (n % 4 == 1) return 1; // If n%4 gives remainder 2 if (n % 4 == 2) return n + 1; // If n%4 gives remainder 3 return 0; } public static void main (String[] args) throws java.lang.Exception { try{ FastReader sc=new FastReader(); BufferedWriter out = new BufferedWriter( new OutputStreamWriter(System.out)); int n = sc.nextInt(); int q = sc.nextInt(); String s = sc.next(); int pos[] = new int[q]; char c[] = new char[q]; for(int i=0;i<q;i++) { int posi = sc.nextInt(); char cc = sc.next().charAt(0); pos[i] = posi; c[i] = cc; //System.out.println(pos[i]+" "+c[i]); } StringBuilder sb = new StringBuilder(s); // HashMap<Integer,Character> map = new HashMap<>(); HashSet<Integer> set = new HashSet<>(); int count = 0; for(int i=0;i<n-2;i++) { String str = s.substring(i,i+3); if(str.equals("abc")) { set.add(i); count++; } } // System.out.println(count); for(int i=0;i<q;i++) { int posi = pos[i]-1; char ch = c[i]; if(set.remove(posi)||set.remove(posi-1)||set.remove(posi-2)) { count--; } sb.deleteCharAt(posi); sb.insert(posi,ch); if(ch == 'a') { int p1 = posi+1; int p2 = posi+2; if(p1<sb.length()&&p2<sb.length()&&sb.charAt(p1)=='b'&&sb.charAt(p2)=='c') { set.add(posi); count++; } } else if(ch == 'b') { int p1 = posi-1; int p2 = posi+1; if(p1>=0&&p2<sb.length()&&sb.charAt(p1)=='a'&&sb.charAt(p2)=='c') { set.add(posi-1); count++; } } else { int p1 = posi-1; int p2 = posi-2; if(p1>=0&&p2>=0&&sb.charAt(p1)=='b'&&sb.charAt(p2)=='a') { set.add(posi-2); count++; } } System.out.println(count); } }catch(Exception e){ return; } } }
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 8
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
60d22d6f8cc39ff4fcdf6649151df358
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; /* * * @author Sabirov Jakhongir * */ public class Simple { public static void main(String[] args) { Scanner cin = new Scanner(System.in); int n = cin.nextInt(); int m = cin.nextInt(); char a[]; String s = cin.next(); a = s.toCharArray(); int cnt = 0; for(int i = 0 ; i < n - 2; i++){ if(a[i] == 'a' && a[i + 1] == 'b' && a[i + 2] == 'c') cnt++; } int x; String y; for(int i = 1; i <= m ;i ++) { x = cin.nextInt(); y = cin.next(); x--; // a[x] = y.charAt(0); if(a[x] == 'a' && x < n - 2 && (a[x + 1] == 'b' && a[x + 2] == 'c') && (y.equals("b") || y.equals("c"))){ cnt--; } if(a[x] == 'b' && (x < n - 1 && x - 1 >= 0) && a[x - 1] == 'a' && a[x + 1] =='c' && (y.equals("a") || y.equals("c"))){ cnt--; } if(a[x] == 'c' && (x -2 >= 0) && a[x - 1] =='b' && a[x - 2] == 'a' && (y.equals("a") || y.equals("b"))){ cnt--; } if(y.equals("a") && x < n - 2 && a[x + 1] == 'b' && a[x + 2] == 'c' && a[x] != 'a'){ cnt++; } if(y.equals("b") && x < n -1 && x - 1 >= 0 && a[x - 1] == 'a' && a[x + 1] =='c' && a[x] != 'b'){ cnt++; } if(y.equals("c") && x - 2 >= 0 && a[x - 1] == 'b' && a[x - 2] =='a' && a[x] != 'c') { cnt++; } a[x] = y.charAt(0); 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 8
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
5c3842e9b29b9ff413f4f0a694eb9caf
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 Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int q = scanner.nextInt(); char[] chars = new char[n]; scanner.nextLine(); chars = scanner.nextLine().toCharArray(); int counter = 0; int limit = n / 3; for (int i = 0; i < n - 2; i++) { if (chars[i] == 'a' && chars[i + 1] == 'b' && chars[i + 2] == 'c') { counter++; i += 2; } } for (int i = 0; i < q; i++) { int pos = scanner.nextInt(); pos = pos - 1; char c = scanner.next().charAt(0); if (counter > 0) { counter -= abc(chars, pos, n); } chars[pos] = c; if (counter < limit) { counter += abc(chars, pos, n); } System.out.println(counter); } } private static int abc(char[] chars, int pos, int n) { if (chars[pos] == 'a' && pos < n - 2) { if (chars[pos + 1] == 'b' && chars[pos + 2] == 'c') { return 1; } } if (chars[pos] == 'b' && pos < n - 1 && pos > 0) { if (chars[pos - 1] == 'a' && chars[pos + 1] == 'c') { return 1; } } if (chars[pos] == 'c' && pos > 1) { if (chars[pos - 2] == 'a' && chars[pos - 1] == 'b') { return 1; } } return 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 8
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
de3a3d5ef2f0dcaa210838c62dba0728
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 B { public static void main(String[] args) throws IOException { Reader sc = new Reader(); PrintWriter out = new PrintWriter(System.out); int n = sc.nextInt(); int q = sc.nextInt(); char[] arr = sc.next().toCharArray(); int cnt = 0; int idx1 = 0; int idx2 = idx1 + 1; int idx3 = idx2 + 1; while(idx3 < n) { if(arr[idx1] == 'a' && arr[idx2] == 'b' && arr[idx3] == 'c') cnt++; idx1++; idx2++; idx3++; } Pair[] pair = new Pair[q]; for(int i = 0;i < q;i++) { int pos = sc.nextInt(); char c = sc.next().charAt(0); pair[i] = new Pair(pos, c); } for(int i = 0;i < q;i++) { int pos = pair[i].a; boolean old_abc = false; boolean new_abc = false; char c = pair[i].b; if((pos - 1 >= 0 && arr[pos - 1] == 'a' && pos + 1 < n && arr[pos] == 'b' && arr[pos + 1] == 'c') || (pos - 1 >= 0 && arr[pos - 1] == 'b' && pos - 2 >= 0 && pos < n && arr[pos - 2] == 'a' && arr[pos] == 'c') || (pos - 1 >= 0 && arr[pos - 1] == 'c' && pos - 3 >= 0 && arr[pos - 3] == 'a' && arr[pos - 2] == 'b')) old_abc = true; char ch = arr[pos - 1]; arr[pos - 1] = c; if (ch == arr[pos - 1]) out.println(cnt); else { if (c == 'a') { if (pos + 1 < n && arr[pos] == 'b' && arr[pos + 1] == 'c') new_abc = true; } else if (c == 'b') { if (pos - 2 >= 0 && pos < n && arr[pos - 2] == 'a' && arr[pos] == 'c') new_abc = true; } else if (c == 'c') { if (pos - 3 >= 0 && arr[pos - 3] == 'a' && arr[pos - 2] == 'b') new_abc = true; } if(old_abc == new_abc) out.println(cnt); else if(old_abc && !new_abc) { cnt--; out.println(cnt); } else if(!old_abc && new_abc) { cnt++; out.println(cnt); } } } out.flush(); } public static boolean checkPalindrome(int[] arr,int x) { int n = arr.length; int start = 0; int end = n - 1; while(start < end) { if(arr[start] == x) start++; else if(arr[end] == x) end--; else if(arr[start] != arr[end]) return false; else { start++; end--; } } return true; } public static String bin(long n,long i) { StringBuilder list = new StringBuilder(); list.append(0); while(n > 0) { list.append(n % i); n = n / i; } return new String(list); } static int[] sort(int[] arr,int n) { List<Integer> list = new ArrayList<>(); for(int i = 0;i < n;i++) list.add(arr[i]); Collections.sort(list); for(int i = 0;i < n;i++) arr[i] = list.get(i); return arr; } static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } static int isPrime(int n) { if(n < 2) return 0; if(n < 4) return 1; if((n % 2) == 0 || (n % 3) == 0) return 0; for(int i = 5; (i * i) <= n; i += 6) if((n % i) == 0 || (n % (i + 2)) == 0) return 0; return 1; } static class Reader { BufferedReader br; StringTokenizer st; public Reader() { 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 { if(st.hasMoreTokens()) str = st.nextToken("\n"); else str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } } class Pair { int a; char b; public Pair(int a,char b) { this.a = a; this.b = b; } } // ********************* Custom Pair Class ********************* //class Pair implements Comparable<Pair> { // int a,b; // public Pair(int a,int b) { // this.a = a; // this.b = b; // } // @Override // public int compareTo(Pair other) { // if(this.b == other.b) // return Integer.compare(other.a,this.a); // return Integer.compare(this.b,other.b); // } //} // ****************** Segment Tree ****************** //public class SegmentTreeNode { // public SegmentTreeNode left; // public SegmentTreeNode right; // public int Start; // public int End; // public int Sum; // public SegmentTreeNode(int start, int end) { // Start = start; // End = end; // Sum = 0; // } //} //public SegmentTreeNode buildTree(int start, int end) { // if(start > end) // return null; // SegmentTreeNode node = new SegmentTreeNode(start, end); // if(start == end) // return node; // int mid = start + (end - start) / 2; // node.left = buildTree(start, mid); // node.right = buildTree(mid + 1, end); // return node; //} //public void update(SegmentTreeNode node, int index) { // if(node == null) // return; // if(node.Start == index && node.End == index) { // node.Sum += 1; // return; // } // int mid = node.Start + (node.End - node.Start) / 2; // if(index <= mid) // update(node.left, index); // else // update(node.right, index); // node.Sum = node.left.Sum + node.right.Sum; //} //public int SumRange(SegmentTreeNode root, int start, int end) { // if(root == null || start > end) // return 0; // if(root.Start == start && root.End == end) // return root.Sum; // int mid = root.Start + (root.End - root.Start) / 2; // if(end <= mid) // return SumRange(root.left, start, end); // else if(start > mid) // return SumRange(root.right, start, end); // return SumRange(root.left, start, mid) + SumRange(root.right, mid + 1, end); //}
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 8
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
32ad6d81dbed7855680994e07cdc32cc
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.Arrays; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; import java.util.Scanner; public class xoxo { public static void main(String[]args){ FastScanner c = new FastScanner(); int a =c.nextInt(); int t =c.nextInt(); int q=0; String j = c.next(); Character []d = new Character[a]; for(int y=0;y<a;y++){ d[y] =j.charAt(y); if(y+2<a&&d[y]=='a'){ if(j.charAt(y+1)=='b'&&j.charAt(y+2)=='c'){ q++; } } } for(int y=0;y<t;y++){ int f= c.nextInt()-1; String g = c.next(); if(g.equals("a")){ if(d[f]!='a'&&f+2<a&&d[f+1]=='b'&&d[f+2]=='c') q++; if(d[f]=='b'&&f+1<a&&f-1>=0&&d[f+1]=='c'&&d[f-1]=='a') q--; if(d[f]=='c'&&f-2>=0&&d[f-1]=='b'&&d[f-2]=='a') q--; d[f]='a'; System.out.println(q); } if(g.equals("b")){ if(d[f]!='b'&&f+1<a&&f-1>=0&&d[f+1]=='c'&&d[f-1]=='a') q++; if(d[f]=='a'&&f+2<a&&d[f+1]=='b'&&d[f+2]=='c') q--; if(d[f]=='c'&&f-2>=0&&d[f-1]=='b'&&d[f-2]=='a') q--; d[f]='b'; System.out.println(q); } if(g.equals("c")){ if(d[f]!='c'&&f-2>=0&&d[f-1]=='b'&&d[f-2]=='a') q++; if(d[f]=='a'&&f+2<a&&d[f+1]=='b'&&d[f+2]=='c') q--; if(d[f]=='b'&&f+1<a&&f-1>=0&&d[f+1]=='c'&&d[f-1]=='a') q--; d[f]='c'; System.out.println(q); } } System.out.close(); } static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner() { try { br = new BufferedReader(new InputStreamReader(System.in)); st = new StringTokenizer(br.readLine()); } catch (Exception e){e.printStackTrace();} } public String next() { if (st.hasMoreTokens()) return st.nextToken(); 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() { String line = ""; if(st.hasMoreTokens()) line = st.nextToken(); else try {return br.readLine();}catch(IOException e){e.printStackTrace();} while(st.hasMoreTokens()) line += " "+st.nextToken(); return line; } } }
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 8
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
b78779b311a334fd40589c3d239ae4f0
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.Arrays; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; import java.util.Scanner; public class xoxo { public static void main(String[]args){ Scanner c = new Scanner(System.in); int a =c.nextInt(); int t =c.nextInt(); int q=0; String j = c.next(); Character []d = new Character[a]; for(int y=0;y<a;y++){ d[y] =j.charAt(y); if(y+2<a&&d[y]=='a'){ if(j.charAt(y+1)=='b'&&j.charAt(y+2)=='c'){ q++; } } } for(int y=0;y<t;y++){ int f= c.nextInt()-1; String g = c.next(); if(g.equals("a")){ if(d[f]!='a'&&f+2<a&&d[f+1]=='b'&&d[f+2]=='c') q++; if(d[f]=='b'&&f+1<a&&f-1>=0&&d[f+1]=='c'&&d[f-1]=='a') q--; if(d[f]=='c'&&f-2>=0&&d[f-1]=='b'&&d[f-2]=='a') q--; d[f]='a'; System.out.println(q); } if(g.equals("b")){ if(d[f]!='b'&&f+1<a&&f-1>=0&&d[f+1]=='c'&&d[f-1]=='a') q++; if(d[f]=='a'&&f+2<a&&d[f+1]=='b'&&d[f+2]=='c') q--; if(d[f]=='c'&&f-2>=0&&d[f-1]=='b'&&d[f-2]=='a') q--; d[f]='b'; System.out.println(q); } if(g.equals("c")){ if(d[f]!='c'&&f-2>=0&&d[f-1]=='b'&&d[f-2]=='a') q++; if(d[f]=='a'&&f+2<a&&d[f+1]=='b'&&d[f+2]=='c') q--; if(d[f]=='b'&&f+1<a&&f-1>=0&&d[f+1]=='c'&&d[f-1]=='a') q--; d[f]='c'; System.out.println(q); } } //System.out.close(); } /*static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner() { try { br = new BufferedReader(new InputStreamReader(System.in)); st = new StringTokenizer(br.readLine()); } catch (Exception e){e.printStackTrace();} } public String next() { if (st.hasMoreTokens()) return st.nextToken(); 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() { String line = ""; if(st.hasMoreTokens()) line = st.nextToken(); else try {return br.readLine();}catch(IOException e){e.printStackTrace();} while(st.hasMoreTokens()) line += " "+st.nextToken(); return line; } }*/ }
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 8
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
9e45a7d6a3652c917d117e8f671894a2
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.*; public class a729 { public static void main(String[] args) { // try { BufferedWriter out = new BufferedWriter( new OutputStreamWriter(System.out)); BufferedReader br = new BufferedReader( new InputStreamReader(System.in)); PrintWriter pt = new PrintWriter(System.out); FastReader sc = new FastReader(); // System.out.println(1); // int t = sc.nextInt(); //for(int o = 0 ; o<t;o++){ int n = sc.nextInt(); int q = sc.nextInt(); String s = sc.next(); 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') { ans++; } } StringBuilder sb = new StringBuilder(s); for(int j = 0 ; j<q;j++) { int i = sc.nextInt()-1; char ch = sc.next().charAt(0); // System.out.println(sb); if(sb.charAt(i)==ch){ System.out.println(ans); continue; } int f = 0; if(sb.charAt(i) == 'a'){ if(i<n-2) { if(sb.charAt(i+1) == 'b' && sb.charAt(i+2) == 'c') { f = 1; } } }else if(sb.charAt(i) == 'b') { if(i<n-1 && i>0) { if(sb.charAt(i-1) == 'a' && sb.charAt(i+1) == 'c') { f = 1; } } }else { if(i>1) { if(sb.charAt(i-1) == 'b' && sb.charAt(i-2) == 'a') { f = 1; } } } String x = "" + ch; sb = sb.replace(i, i+1, x); int g = 0; if(ch == 'a') { if(i<n-2) { if(sb.charAt(i+1) == 'b' && sb.charAt(i+2) == 'c') { g = 1; } } }else if(ch == 'b') { if(i<n-1 && i>0) { if(sb.charAt(i-1) == 'a' && sb.charAt(i+1) == 'c') { g = 1; } } }else { if(i>1) { if(sb.charAt(i-1) == 'b' && sb.charAt(i-2) == 'a') { g = 1; } } } if(f == 0 && g == 1) { ans++; }else if(f == 1 && g== 0) { ans--; } System.out.println(ans); } //} //}catch(Exception e) { // return; //} } } 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 8
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
28fe4370f81d29ba6b4bfd1cef446c5b
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 { public static void main(String[] hi) { FastIO io = new FastIO(); int n = io.nextInt(), q = io.nextInt(); char array[] = io.next().toCharArray(); int count = 0; int[] v = new int[array.length]; for (int i=0; i<n-2; i++) { if (array[i] == 'a' && array[i+1] == 'b' && array[i+2] == 'c') { count++; v[i] = 2; v[i+1] = 1; v[i+2] = 1; i += 2; } } while (q --> 0) { int k = io.nextInt()-1; for (int i=Math.max(0, k-2); i<Math.min(n-2, k+1); i++) { if (array[i] == 'a' && array[i+1] == 'b' && array[i+2] == 'c') { count--; } } array[k] = io.next().charAt(0); for (int i=Math.max(0, k-2); i<Math.min(n-2, k+1); i++) { if (array[i] == 'a' && array[i+1] == 'b' && array[i+2] == 'c') { count++; } } io.println(count); } io.close(); } static class FastIO extends PrintWriter { BufferedReader br; StringTokenizer st; public FastIO() { super(System.out); br = new BufferedReader(new InputStreamReader(System.in)); } public String next() { try { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } catch (Exception e) { return null; } } public int nextInt() { return Integer.parseInt(next()); } public double nextDouble() { return Double.parseDouble(next()); } public long nextLong() { return Long.parseLong(next()); } public void close() { try { br.close(); } catch (IOException e) { e.printStackTrace(); } super.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 8
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
933bfab729d7a5c7937e7347136a2190
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.BigInteger; import java.util.*; import java.lang.*; import static java.lang.Math.*; // Sachin_2961 submission // public class Codeforces { static void solve() { int n = fs.nInt(), q = fs.nInt(); char[]str = fs.n().toCharArray(); int count = 0; for(int i=0;i<n-2;i++){ if(str[i] == 'a' && str[i+1] == 'b' && str[i+2] == 'c')count++; } for(int i=0;i<q;i++){ int pos = fs.nInt()-1; char c = fs.n().charAt(0); if(str[pos] != c){ count = count + check(str,pos,c); } out.println(count); str[pos] = c; } } static int check(char[]str,int pos,char c){ boolean flag1 = false,flag2 = false; if( str[pos] == 'c'){ if( pos > 1 && str[pos-1] == 'b' && str[pos-2] == 'a'){ flag1 = true; } }else if( str[pos] == 'b'){ if( pos > 0 && pos < str.length-1 && str[pos-1] == 'a' && str[pos+1] == 'c'){ flag1 = true; } }else{ if( pos < str.length-2 && str[pos+1] == 'b' && str[pos+2] == 'c'){ flag1 = true; } } if( c == 'c'){ if( pos > 1 && str[pos-1] == 'b' && str[pos-2] == 'a'){ flag2 = true; } }else if( c == 'b'){ if( pos > 0 && pos < str.length - 1 && str[pos-1] == 'a' && str[pos+1] == 'c'){ flag2 = true; } }else{ if( pos < str.length-2 && str[pos+1] == 'b' && str[pos+2] == 'c'){ flag2 = true; } } if(flag1 && flag2) return 0; if(!flag1 && flag2) return 1; if(!flag1 && !flag2) return 0; return -1; } static class Pair{ int f,s; Pair(int f,int s){ this.f = f; this.s = s; } } static boolean multipleTestCase = false; static FastScanner fs; static PrintWriter out; public static void main(String[]args){ try{ out = new PrintWriter(System.out); fs = new FastScanner(); int tc = multipleTestCase?fs.nInt():1; while (tc-->0)solve(); out.flush(); out.close(); }catch (Exception e){ e.printStackTrace(); } } static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String n() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } String Line() { String str = ""; try { str = br.readLine(); }catch (IOException e) { e.printStackTrace(); } return str; } int nInt() {return Integer.parseInt(n()); } long nLong() {return Long.parseLong(n());} double nDouble(){return Double.parseDouble(n());} int[]aI(int n){ int[]ar = new int[n]; for(int i=0;i<n;i++) ar[i] = nInt(); return ar; } } public static void sort(int[] arr){ ArrayList<Integer> ls = new ArrayList<Integer>(); for(int x: arr) ls.add(x); Collections.sort(ls); for(int i=0; i < arr.length; i++) arr[i] = ls.get(i); } public static void sort(long[] arr){ ArrayList<Long> ls = new ArrayList<>(); for(long x: arr) ls.add(x); Collections.sort(ls); for(int i=0; i < arr.length; i++) arr[i] = ls.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 8
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
f575b931216ee5ac0b9680199f11a867
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.util.stream.LongStream; import java.io.*; import java.lang.reflect.Array; import java.math.*; public class Main{ public static void main(String[] args) { FastReader fr = new FastReader(); PrintWriter out = new PrintWriter(System.out); Scanner sc= new Scanner (System.in); //Code From Here---- StringBuilder answer = new StringBuilder(); int n =fr.nextInt(); int t = fr.nextInt(); char [] arr = fr.nextLine().toCharArray(); int total = 0; for (int i = 2; i < arr.length; i++) { if(arr[i-2]=='a' && arr[i-1]=='b' && arr[i]=='c') total+=1; } for (int tt = 1; tt <=t; tt++) { String [] in = fr.nextLine().split(" "); int pos = Integer.parseInt(in[0])-1; char c = in[1].charAt(0); int before = 0; before+=check(arr,pos-1)+check(arr,pos-2)+check(arr,pos); arr[pos]=c; int after =0; after+=check(arr,pos-1)+check(arr,pos-2)+check(arr,pos); total+=after-before; answer.append(total+"\n"); } out.println(answer.toString()); out.flush(); sc.close(); } private static int check(char[] arr, int i) { if(i>arr.length-3 || i<0) return 0; if(arr[i]=='a' && arr[i+1]=='b' && arr[i+2]=='c') return 1; return 0; } public static boolean isSorted(long [] arr ) { for (int i = 1; i < arr.length; i++) { if(arr[i]<arr[i-1]) return false; } return true; } public static TreeSet<Long> maxPrimeFactors(long n) { TreeSet <Long> set = new TreeSet<>(); long maxPrime = -1; while (n % 2 == 0) { maxPrime=2; n >>= 1; } while (n % 3 == 0) { maxPrime=3; n = n / 3; } for (long i = 5; i <= Math.sqrt(n); i += 6) { while (n % i == 0) { maxPrime=i; n = n / i; } while (n % (i + 2) == 0) { set.add((long)(i+2)); n = n / (i + 2); } } if (n > 4) { set.add((long)n); } return set; } public static int nSquaresFor(int n) { //Sum of Perfect Square Minimum ....... //Legendre's three-square theorem states that a natural number can be represented as the sum of three squares of integers //if and only if n is not of the form n = 4^a * (8b+7) int sqrt = (int)Math.sqrt(n); if(sqrt*sqrt==n) return 1; while ((n&3)==0) n>>=2; if((n&7)==7) return 4; int x = (int)Math.sqrt(n); for(int i =1;i<=x;i++) { int temp = n-(i*i); int tempsqrt = (int)Math.sqrt(temp); if(tempsqrt*tempsqrt == temp) return 2; } return 3; } public static BigInteger Factorial(BigInteger number){ if(number.compareTo(BigInteger.ONE)!=1) { return BigInteger.ONE; } return number.multiply(Factorial(number.subtract(BigInteger.ONE))); } private static boolean isPrime(int count) { if(count==1) return false; if(count==2 || count ==3) return true; if(count%2 == 0) return false; if(count%3 ==0) return false; for (int i = 5; i*i<=count; i+=6) { if(count%(i)==0 || count%(i+2)==0) return false;} return true; } static ArrayList<Integer> sieveOfEratosthenes(int n) { // Create a boolean array // "prime[0..n]" and // initialize all entries // it as true. A value in // prime[i] will finally be // false if i is Not a // prime, else true. boolean 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; } } ArrayList <Integer> list = new ArrayList<>(); // Print all prime numbers for (int i = 2; i <= n; i++) { if (prime[i] == true) list.add(i); } return list; } //This RadixSort() is for long method public static long[] radixSort(long[] f){ return radixSort(f, f.length); } public static long[] radixSort(long[] f, int n) { long[] to = new long[n]; { int[] b = new int[65537]; for(int i = 0;i < n;i++)b[1+(int)(f[i]&0xffff)]++; for(int i = 1;i <= 65536;i++)b[i]+=b[i-1]; for(int i = 0;i < n;i++)to[b[(int)(f[i]&0xffff)]++] = f[i]; long[] d = f; f = to;to = d; } { int[] b = new int[65537]; for(int i = 0;i < n;i++)b[1+(int)(f[i]>>>16&0xffff)]++; for(int i = 1;i <= 65536;i++)b[i]+=b[i-1]; for(int i = 0;i < n;i++)to[b[(int)(f[i]>>>16&0xffff)]++] = f[i]; long[] d = f; f = to;to = d; } { int[] b = new int[65537]; for(int i = 0;i < n;i++)b[1+(int)(f[i]>>>32&0xffff)]++; for(int i = 1;i <= 65536;i++)b[i]+=b[i-1]; for(int i = 0;i < n;i++)to[b[(int)(f[i]>>>32&0xffff)]++] = f[i]; long[] d = f; f = to;to = d; } { int[] b = new int[65537]; for(int i = 0;i < n;i++)b[1+(int)(f[i]>>>48&0xffff)]++; for(int i = 1;i <= 65536;i++)b[i]+=b[i-1]; for(int i = 0;i < n;i++)to[b[(int)(f[i]>>>48&0xffff)]++] = f[i]; long[] d = f; f = to; to = d; } return f; } 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 lowerBound(long a[], long 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(long a[], long x) {// x is the key or target value int l = -1, r = a.length; while (l + 1 < r) { int m = (l + r) >>> 1L; if (a[m] <= x) l = m; else r = m; } return l + 1; } static int upperBound(long[] arr, int start, int end, long k) { int N = end; while (start < end) { int mid = start + (end - start) / 2; if (k >= arr[mid]) { start = mid + 1; } else { end = mid; } } if (start < N && arr[start] <= k) { start++; } return start; } static long lowerBound(long[] arr, int start, int end, long k) { int N = end; while (start < end) { int mid = start + (end - start) / 2; if (k <= arr[mid]) { end = mid; } else { start = mid + 1; } } if (start < N && arr[start] < k) { start++; } return start; } // H.C.F. // For Fast Factorial public static long factorialStreams( long n ) { return LongStream.rangeClosed( 1, n ) .reduce(1, ( long a, long b ) -> a * b); } // Binary Exponentiation public static long FastExp(long base, long exp) { long ans=1; long mod=998244353; while (exp>0) { if (exp%2==1) ans*=base; exp/=2; base*=base; base%=mod; ans%=mod; } return ans; } // H.C.F. public static int gcd(int a, int b) { if (a == 0) return b; return gcd(b%a, a); } // H.C.F. by Binary Method(Eucladian Algorithm) private static int gcdBinary(int x, int y) { int shift; /* GCD(0, y) == y; GCD(x, 0) == x, GCD(0, 0) == 0 */ if (x == 0) return y; if (y == 0) return x; int gcdX = Math.abs(x); int gcdY = Math.abs(y); if (gcdX == 1 || gcdY == 1) return 1; /* Let shift := lg K, where K is the greatest power of 2 dividing both x and y. */ for (shift = 0; ((gcdX | gcdY) & 1) == 0; ++shift) { gcdX >>= 1; gcdY >>= 1; } while ((gcdX & 1) == 0) gcdX >>= 1; /* From here on, gcdX is always odd. */ do { /* Remove all factors of 2 in gcdY -- they are not common */ /* Note: gcdY is not zero, so while will terminate */ while ((gcdY & 1) == 0) /* Loop X */ gcdY >>= 1; /* * Now gcdX and gcdY are both odd. Swap if necessary so gcdX <= gcdY, * then set gcdY = gcdY - gcdX (which is even). For bignums, the * swapping is just pointer movement, and the subtraction * can be done in-place. */ if (gcdX > gcdY) { final int t = gcdY; gcdY = gcdX; gcdX = t; } // Swap gcdX and gcdY. gcdY = gcdY - gcdX; // Here gcdY >= gcdX. }while (gcdY != 0); /* Restore common factors of 2 */ return gcdX << shift; } // Check For Palindrome public static boolean isPalindrome(String s) { return s.equals( new StringBuilder(s).reverse().toString()); } // For Fast Input ---- 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()); } int[] readArray(int n) { int [] arr = new int [n]; for (int i = 0; i < arr.length; i++) { arr[i]=nextInt(); } return arr; } long[] readArray(long []arr) { for (int i = 0; i < arr.length; i++) { arr[i]=nextLong(); } return arr; } 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 8
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
16b5d779c26d26867c16e8ff8acea655
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 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) { // TODO Auto-generated method stub FastReader sc=new FastReader(); int n=sc.nextInt(); int q=sc.nextInt(); char[] c=sc.next().toCharArray(); String check="abc"; int count=0; for(int i=0;i<=n-3;i++) { if(c[i]=='a'&&c[i+1]=='b'&&c[i+2]=='c') count++; } while(q-->0) { int p=sc.nextInt()-1; char s=sc.next().charAt(0); if(c[p]==s)System.out.println(count); else { int change=0; for(int i=Math.max(0, p-2);i<=Math.min(p, n-3);i++) { if(c[i]=='a'&&c[i+1]=='b'&&c[i+2]=='c') { change--; break; } } c[p]=s; for(int i=Math.max(0, p-2);i<=Math.min(p, n-3);i++) { if(c[i]=='a'&&c[i+1]=='b'&&c[i+2]=='c') { change++; break; } } count+=change; 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 8
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
6763faf623147455661ba7fd4c57dfda
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 a { public static void main(String[] args){ FastScanner sc = new FastScanner(); int t = 1; while(t-- > 0){ int n = sc.nextInt(); int q = sc.nextInt(); String s = sc.next(); char arr[] = new char[s.length()]; int count = 0; for(int i=0; i<s.length(); i++){ arr[i] = s.charAt(i); if(s.charAt(i) == 'a'){ if(i+2<s.length() && s.charAt(i+1) == 'b' && s.charAt(i+2) == 'c'){ count++; } } } for(int i=0; i<q; i++){ int pos = sc.nextInt(); pos--; String temp = sc.next(); //System.out.println("temp: " + temp); char c = temp.charAt(0); if(arr[pos] == c){ System.out.println(count); continue; } if(c == 'a'){ if(arr[pos] == 'b'){ if(pos-1 >= 0 && pos+1 < n && arr[pos-1] == 'a' && arr[pos+1] == 'c'){ count--; } if(pos+2 < n && arr[pos+1] == 'b' && arr[pos+2] == 'c'){ count++; } } else if(arr[pos] == 'c'){ if(pos-2 >= 0 && arr[pos-1] == 'b' && arr[pos-2] == 'a'){ count--; } if(pos+2 < n && arr[pos+1] == 'b' && arr[pos+2] == 'c'){ count++; } } arr[pos] = c; System.out.println(count); continue; } else if(c == 'b'){ if(arr[pos] == 'a'){ if(pos+2 < n && arr[pos+1] == 'b' && arr[pos+2] == 'c'){ count--; } if(pos-1 >=0 && pos+1<n && arr[pos-1] == 'a' && arr[pos+1] == 'c'){ count++; } } else if(arr[pos] == 'c'){ if(pos-2 >= 0 && arr[pos-1] == 'b' && arr[pos-2] == 'a'){ count--; } if(pos-1>=0 && pos+1<n && arr[pos-1] == 'a' && arr[pos+1] == 'c'){ count++; } } arr[pos] = c; System.out.println(count); continue; } else{ if(arr[pos] == 'a'){ if(pos+2 < n && arr[pos+1] == 'b' && arr[pos+2] == 'c'){ count--; } if(pos-2>=0 && arr[pos-1] == 'b' && arr[pos-2] == 'a'){ count++; } } else if(arr[pos] == 'b'){ if(pos-1>=0 && pos+1<n && arr[pos-1] == 'a' && arr[pos+1] == 'c'){ count--; } if(pos-2>=0 && arr[pos-1] == 'b' && arr[pos-2] == 'a'){ count++; } } arr[pos] = c; System.out.println(count); continue; } } } } } class FastScanner { //I don't understand how this works lmao 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; } } }
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 8
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
2389af08f99dbd295610b1eb58523dad
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 CFsolve { public static void main(String[] args) { FastScanner input = new FastScanner(); PrintWriter out = new PrintWriter(System.out); int n = input.nextInt(); int q = input.nextInt(); StringBuilder str = new StringBuilder(input.next()); String newStr = str.toString(); int count = 0; for(int i = 0; i<n;i++) if(newStr.startsWith("abc",i)) count++; while(q-- > 0){ int idx = input.nextInt()-1; char letter = input.next().charAt(0); if(idx+2<n && str.charAt(idx)=='a' && str.charAt(idx+1)=='b' && str.charAt(idx+2)=='c') count--; if(idx-1>=0 && idx+1<n && str.charAt(idx)=='b' && str.charAt(idx-1)=='a' && str.charAt(idx+1)=='c')count--; if(idx-2>=0 && str.charAt(idx)=='c' && str.charAt(idx-1)=='b' && str.charAt(idx-2)=='a')count--; str.setCharAt(idx,letter); if(idx+2 < n && str.charAt(idx)=='a' && str.charAt(idx+1)=='b' && str.charAt(idx+2)=='c')count++; if(idx-1 >= 0 && idx+1 < n && str.charAt(idx)=='b' && str.charAt(idx-1)=='a' && str.charAt(idx+1)=='c')count++; if(idx > 1 && str.charAt(idx)=='c' && str.charAt(idx-1)=='b' && str.charAt(idx-2)=='a')count++; out.println(count); } out.close(); } static int LCM(int a, int b){ return a/gcd(a,b) * b; } static long LCM(long a, long b){ return (a/gcd(a,b) * b); } static long pow(long a, long b) { long res = 1; while (b > 0) { if ((b & 1)!=0) res = res * a; a = a * a; b >>= 1; } return res; } static int pow(int a, int b) { int res = 1; while (b > 0) { if ((b & 1)!=0) res = res * a; a = a * a; b >>= 1; } return res; } static int gcd(int a,int b){ while(b>0){ a%=b; a=a^b; b=a^b; a=a^b; } return a; } static long gcd(long a,long b){ while(b>0){ a%=b; a=a^b; b=a^b; a=a^b; } return a; } static void swap(int a, int b){ a = a^b; b = a^b; a = a^b; } 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); } 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; } byte nextByte(){return Byte.parseByte(next());} 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 8
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
19bb8f704b5d5ecbc337ab2b782cb953
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.*; public class Solution { static int t; static int n; static int q; static int[] a; static String s; static FastReader fr = new FastReader(); static PrintWriter out = new PrintWriter(System.out); public static void main(String[] args) { t = fr.nextInt(); q = fr.nextInt(); s = fr.nextString(); Set<Integer> abc = new HashSet<>(); char[] cs = s.toCharArray(); for (int i = 0; i + 2 < cs.length;) { if (cs[i] == 'a' && cs[i + 1] == 'b' && cs[i + 2] == 'c') { abc.add(i); i += 3; } else { i ++; } } for (int i = 0; i < q ;i ++) { int pos = fr.nextInt(); char c = fr.nextChar(); pos = pos - 1; if (c == 'a') { if (cs[pos] != 'a') { if (pos + 2 < cs.length && cs[pos + 1] == 'b' && cs[pos + 2] == 'c') { abc.add(pos); } abc.remove(pos - 1); abc.remove(pos - 2); } } else if (c == 'b') { if (cs[pos] != 'b') { if (pos - 1 >= 0 && pos + 1 < cs.length && cs[pos - 1] == 'a' && cs[pos + 1] == 'c') { abc.add(pos - 1); } abc.remove(pos); abc.remove(pos - 2); } } else if (c == 'c') { if (cs[pos] != 'c') { if (pos - 2 >= 0 && cs[pos - 2] == 'a' && cs[pos - 1] == 'b') { abc.add(pos - 2); } abc.remove(pos); abc.remove(pos - 1); } } cs[pos] = c; System.out.println(abc.size()); // out.println(abc.size()); } } static class FastReader { private BufferedReader bfr; private StringTokenizer st; public FastReader() { bfr = new BufferedReader(new InputStreamReader(System.in)); } String next() { if (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(bfr.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()); } char nextChar() { return next().toCharArray()[0]; } String nextString() { return next(); } int[] nextIntArray(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = nextInt(); return arr; } double[] nextDoubleArray(int n) { double[] arr = new double[n]; for (int i = 0; i < arr.length; i++) arr[i] = nextDouble(); return arr; } long[] nextLongArray(int n) { long[] arr = new long[n]; for (int i = 0; i < n; i++) arr[i] = nextLong(); return arr; } int[][] nextIntGrid(int n, int m) { int[][] grid = new int[n][m]; for (int i = 0; i < n; i++) { char[] line = fr.next().toCharArray(); for (int j = 0; j < m; j++) grid[i][j] = line[j] - 48; } return grid; } } }
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 8
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
5b1c50866b81b6365bd0dbdcc3196528
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.StringReader; import java.lang.reflect.Array; import java.math.BigInteger; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.Date; import java.util.HashMap; import java.util.HashSet; import java.util.Hashtable; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Random; import java.util.Scanner; import java.util.Stack; import java.util.StringTokenizer; public class Main { 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 gcd(int no, int sum) { if (sum == 0) return no; return gcd(sum, no % sum); } static int lcm(int a , int b){ return (a / gcd(a , b))*b; } static void countSort(int[] arr) { int max = Arrays.stream(arr).max().getAsInt(); int min = Arrays.stream(arr).min().getAsInt(); int range = max - min + 1; int count[] = new int[range]; int output[] = new int[arr.length]; for (int i = 0; i < arr.length; i++) { count[arr[i] - min]++; } for (int i = 1; i < count.length; i++) { count[i] += count[i - 1]; } for (int i = arr.length - 1; i >= 0; i--) { output[count[arr[i] - min] - 1] = arr[i]; count[arr[i] - min]--; } for (int i = 0; i < arr.length; i++) { arr[i] = output[i]; } } static void RandomShuffleSort(int [] a , int start , int end){ Random random = new Random(); for(int i = start; i<end; i++){ int j = random.nextInt(end); int temp = a[j]; a[j] = a[i]; a[i] = temp; } Arrays.sort(a , start , end); } public static void main(String[] args) throws IOException , ParseException{ BufferedWriter out = new BufferedWriter( new OutputStreamWriter(System.out)); FastReader sc = new FastReader(); int n = sc.nextInt(); int q = sc.nextInt(); char a[] = sc.nextLine().toCharArray(); int char1 = 0; int char2 = 1; int char3 = 2; int count = 0; // System.out.println(new String(a)); while(char3 < n){ // System.out.println(char3); if(a[char1] == 'a' && a[char2] == 'b' && a[char3] == 'c'){ count++; char1+=3; char2+=3; char3+=3; } else { char1++; char2++; char3++; } } for(int i = 0; i<q; i++){ int pos = sc.nextInt(); String s = sc.next(); if(a[pos-1] != s.charAt(0)){ char1 = pos-1; char2 = char1+1; char3 = char2+1; if((-1<char1 && char1<n) && (-1<char2 && char2<n) && (-1 < char3 && char3<n)){ if(a[char1] == 'a' && a[char2] == 'b' && a[char3] == 'c'){ count--; } else { if(s.charAt(0) == 'a' && a[char2] == 'b' && a[char3] == 'c'){ count++; } } } char2 = pos-1; char1 = char2-1; char3 = char2+1; if((-1<char1 && char1<n) && (-1<char2 && char2<n) && (-1 < char3 && char3<n)){ if(a[char1] == 'a' && a[char2] == 'b' && a[char3] == 'c'){ count--; } else { if(a[char1] == 'a' && s.charAt(0) == 'b' && a[char3] == 'c'){ count++; } } } char3 = pos-1; char2 = char3-1; char1 = char2-1; if((-1<char1 && char1<n) && (-1<char2 && char2<n) && (-1 < char3 && char3<n)){ if(a[char1] == 'a' && a[char2] == 'b' && a[char3] == 'c'){ count--; } else { if(a[char1] == 'a' && a[char2] == 'b' && s.charAt(0) == 'c'){ count++; } } } a[pos-1] = s.charAt(0); System.out.println(count); } else 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 8
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
3cc659b46c403abd263713c0fa8a27e9
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.BigInteger; //code by tishrah_ public class _practise { 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[] ia(int n) { int a[]=new int[n]; for(int i=0;i<n;i++)a[i]=nextInt(); return a; } int[][] ia(int n , int m) { int a[][]=new int[n][m]; for(int i=0;i<n;i++) for(int j=0;j<m ;j++) a[i][j]=nextInt(); return a; } long[][] la(int n , int m) { long a[][]=new long[n][m]; for(int i=0;i<n;i++) for(int j=0;j<m ;j++) a[i][j]=nextLong(); return a; } char[][] ca(int n , int m) { char a[][]=new char[n][m]; for(int i=0;i<n;i++) { String x =next(); for(int j=0;j<m ;j++) a[i][j]=x.charAt(j); } return a; } long[] la(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 sort(long[] a) {int n=a.length;Random r=new Random();for (int i=0; i<a.length; i++) {long oi=r.nextInt(n), temp=a[i];a[i]=a[(int)oi];a[(int)oi]=temp;}Arrays.sort(a);} static void sort(int[] a) {int n=a.length;Random r=new Random();for (int i=0; i<a.length; i++) {int oi=r.nextInt(n), temp=a[i];a[i]=a[oi];a[oi]=temp;}Arrays.sort(a);} public static long sum(long a[]) {long sum=0; for(long i : a) sum+=i; return(sum);} public static long count(long a[] , long x) {long c=0; for(long i : a) if(i==x) c++; return(c);} public static int sum(int a[]) { int sum=0; for(int i : a) sum+=i; return(sum);} public static int count(int a[] ,int x) {int c=0; for(int i : a) if(i==x) c++; return(c);} public static int count(String s ,char ch) {int c=0; char x[] = s.toCharArray(); for(char i : x) if(ch==i) c++; return(c);} public static boolean prime(int n) {for(int i=2 ; i<=Math.sqrt(n) ; i++) if(n%i==0) return false; return true;} public static boolean prime(long n) {for(long i=2 ; i<=Math.sqrt(n) ; i++) if(n%i==0) return false; return true;} public static int gcd(int n1, int n2) { if (n2 != 0)return gcd(n2, n1 % n2); else return n1;} public static long gcd(long n1, long n2) { if (n2 != 0)return gcd(n2, n1 % n2); else return n1;} public static int[] freq(int a[], int n) { int f[]=new int[n+1]; for(int i:a) f[i]++; return f;} public static int[] pos(int a[], int n) { int f[]=new int[n+1]; for(int i=0; i<n ;i++) f[a[i]]=i; return f;} public static int[] rev(int a[]) { for(int i=0 ; i<(a.length+1)/2;i++) { int temp=a[i]; a[i]=a[a.length-1-i]; a[a.length-1-i]=temp; } return a; } public static long mindig(long n) { long ans=9; while(n>0) { if(n%10<ans) ans=n%10; n/=10; } return ans; } public static long maxdig(long n) { long ans=0; while(n>0) { if(n%10>ans) ans=n%10; n/=10; } return ans; } public static boolean palin(String s) { StringBuilder sb = new StringBuilder(); sb.append(s); String str=String.valueOf(sb.reverse()); if(s.equals(str)) return true; else return false; } public static int[] f1(int a[]) { int n = a.length/2; int x[] = new int[2*n]; for(int i=n ; i<2*n ; i++) x[i-n]=a[i]; for(int i=0 ; i<n ; i++) x[i+n]=a[i]; return x; } public static int[] f2(int a[]) { int n = a.length/2; int x[] = a.clone(); for(int i=0 ; i<2*n ; i+=2) { int temp=x[i]; x[i]=x[i+1]; x[i+1]=temp; } return x; } public static void main(String args[]) { FastReader in=new FastReader(); PrintWriter so = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); _practise ob = new _practise(); // int T = in.nextInt(); int T = 1; tc : while(T-->0) { int n = in.nextInt(); int q = in.nextInt(); String s = in.next(); char ch[] = s.toCharArray(); int x=0; for(int i=0 ; i<n-2 ; i++) { if(ch[i]=='a' && ch[i+1]=='b' && ch[i+2]=='c') { x++; i+=2; } } while(q-->0) { int p = in.nextInt()-1; char c = in.next().charAt(0); int ctr=0; if((p>0 && p<n-1 && ch[p-1]=='a' && ch[p]=='b' && ch[p+1]=='c')||(p-2>=0 && ch[p-2]=='a' && ch[p-1]=='b' && ch[p]=='c')|| (p+2<n && ch[p+2]=='c' && ch[p]=='a' && ch[p+1]=='b')) ctr=1; x-=ctr; ch[p]=c; if((p>0 && p<n-1 && ch[p-1]=='a' && ch[p]=='b' && ch[p+1]=='c')||(p-2>=0 && ch[p-2]=='a' && ch[p-1]=='b' && ch[p]=='c')|| (p+2<n && ch[p+2]=='c' && ch[p]=='a' && ch[p+1]=='b')) x+=1; so.println(x); } } so.flush(); /*String s = in.next(); * Arrays.stream(f).min().getAsInt() * BigInteger f = new BigInteger("1"); 1 ke jagah koi bhi value ho skta jo aap * initial value banan chahte int a[] = new int[n]; Stack<Integer> stack = new Stack<Integer>(); Deque<Integer> q = new LinkedList<>(); or Deque<Integer> q = new ArrayDeque<Integer>(); PriorityQueue<Long> pq = new PriorityQueue<Long>(); ArrayList<Integer> al = new ArrayList<Integer>(); StringBuilder sb = new StringBuilder(); HashSet<Integer> st = new LinkedHashSet<Integer>(); Set<Integer> s = new HashSet<Integer>(); Map<Long,Integer> hm = new HashMap<Long, Integer>(); //<key,value> for(Map.Entry<Integer, Integer> i :hm.entrySet()) HashMap<Integer, Integer> hmap = new HashMap<Integer, Integer>(); so.println("HELLO"); Arrays.sort(a,Comparator.comparingDouble(o->o[0])) Arrays.sort(a, (aa, bb) -> Integer.compare(aa[1], bb[1])); Set<String> ts = new TreeSet<>();*/ } }
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 8
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
01a0d26348474e311485fbbe61ef4005
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 contest.DeltixRoundAutumn2021; import java.io.*; import java.util.*; /*********************** @oj: codeforces @id: hitwanyang @email: [email protected] @date: 2021/11/28 23:07 @url: https://codeforc.es/contest/1609/problem/B ***********************/ public class B1609 { InputStream is; FastWriter out; String INPUT = ""; //提交时注意需要注释掉首行package //基础类型数组例如long[]使用Arrays排序容易TLE,可以替换成Long[] //int 最大值2**31-1,2147483647; //尽量使用long类型,避免int计算的数据溢出 //String尽量不要用+号来,可能会出现TLE,推荐用StringBuffer //注意对象类型Long,Integer相等使用equals代替== !!! void solve() { //int t=ni(); //for (; t > 0; t--) go(); } void go() { int n = ni(), q = ni(); String s = ns(); char[] t = s.toCharArray(); int cnt = 0; for (int i = 0; i < n; i++) { if (i + 2 < n && t[i] == 'a' && t[i + 1] == 'b' && t[i + 2] == 'c') { cnt++; } } // out.println(cnt); for (int i = 0; i < q; i++) { int pos = ni(); char ch = nc(); pos--; StringBuffer pre = new StringBuffer(); for (int j = Math.max(0, pos - 2), k = 0; j < n && k < 5; j++, k++) { pre.append(t[j]); } t[pos] = ch; StringBuffer cur = new StringBuffer(); for (int j = Math.max(0, pos - 2), k = 0; j < n && k < 5; j++, k++) { cur.append(t[j]); } if (pre.toString().contains("abc") && !cur.toString().contains("abc")) { cnt--; } if (!pre.toString().contains("abc") && cur.toString().contains("abc")) { cnt++; } out.println(cnt); } } void run() throws Exception { is = System.in; out = new FastWriter(System.out); long s = System.currentTimeMillis(); solve(); out.flush(); //debug log //tr(System.currentTimeMillis() - s + "ms"); } public static void main(String[] args) throws Exception { new B1609().run(); } private byte[] inbuf = new byte[1024]; public int lenbuf = 0, ptrbuf = 0; private int readByte() { if (lenbuf == -1) throw new InputMismatchException(); if (ptrbuf >= lenbuf) { ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if (lenbuf <= 0) return -1; } return inbuf[ptrbuf++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while ((b = readByte()) != -1 && isSpaceChar(b)) ; return b; } private double nd() { return Double.parseDouble(ns()); } private char nc() { return (char) skip(); } private String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while (p < n && !(isSpaceChar(b))) { buf[p++] = (char) b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private Integer[] na(int n) { Integer[] a = new Integer[n]; for (int i = 0; i < n; i++) a[i] = ni(); return a; } private Long[] nal(int n) { Long[] a = new Long[n]; for (int i = 0; i < n; i++) a[i] = nl(); return a; } private char[][] nm(int n, int m) { char[][] map = new char[n][]; for (int i = 0; i < n; i++) map[i] = ns(m); return map; } private Integer[][] nmi(int n, int m) { Integer[][] map = new Integer[n][]; for (int i = 0; i < n; i++) map[i] = na(m); return map; } private int ni() { return (int) nl(); } private long nl() { long num = 0; int b; boolean minus = false; while ((b = readByte()) != -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 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(); } } public void trnz(int... o) { for (int i = 0; i < o.length; i++) if (o[i] != 0) System.out.print(i + ":" + o[i] + " "); System.out.println(); } // print ids which are 1 public void trt(long... o) { Queue<Integer> stands = new ArrayDeque<>(); for (int i = 0; i < o.length; i++) { for (long x = o[i]; x != 0; x &= x - 1) stands.add(i << 6 | Long.numberOfTrailingZeros(x)); } System.out.println(stands); } public void tf(boolean... r) { for (boolean x : r) System.out.print(x ? '#' : '.'); System.out.println(); } public void tf(boolean[]... b) { for (boolean[] r : b) { for (boolean x : r) System.out.print(x ? '#' : '.'); System.out.println(); } System.out.println(); } public void tf(long[]... b) { if (INPUT.length() != 0) { for (long[] r : b) { for (long x : r) { for (int i = 0; i < 64; i++) { System.out.print(x << ~i < 0 ? '#' : '.'); } } System.out.println(); } System.out.println(); } } public void tf(long... b) { if (INPUT.length() != 0) { for (long x : b) { for (int i = 0; i < 64; i++) { System.out.print(x << ~i < 0 ? '#' : '.'); } } System.out.println(); } } private void tr(Object... o) { System.out.println(Arrays.deepToString(o)); } }
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 8
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
33826a0d4347490f9664c5893eaff2cf
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 { public static void main(String[] args) { Scanner in = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int n = in.nextInt(), q = in.nextInt(); char[] s = in.next().toCharArray(); int res = 0; for (int i = 2; i < n; i++) { if (s[i - 2] == 'a' && s[i - 1] == 'b' && s[i] == 'c') { res++; } } for (int i = 0; i < q; i++) { int j = in.nextInt() - 1; char c = in.next().charAt(0); if (s[j] != c) { if (prev(s, n, j)) { s[j] = c; if (!prev(s, n, j)) res--; if (res < 0) res = 0; pw.println(res); } else { s[j] = c; if (prev(s, n, j)) { res++; pw.println(res); } else { pw.println(res); } } } else pw.println(res); } // debug(s); pw.close(); } static boolean prev(char[] s, int n, int j) { if (j >= 0 && (j + 2) < n) { if (s[j] == 'a' && s[j + 1] == 'b' && s[j + 2] == 'c') return true; } if (j >= 1 && (j + 1) < n) { if (s[j - 1] == 'a' && s[j] == 'b' && s[j + 1] == 'c') return true; } if (j >= 2 && j < n) { if (s[j - 2] == 'a' && s[j - 1] == 'b' && s[j] == 'c') return true; } return false; } static void debug(Object... obj) { System.err.println(Arrays.deepToString(obj)); } }
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 8
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
2d9316903e22559f0f6dafd8fafd599a
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 { public static void main(String args[]){ FScanner in = new FScanner(); PrintWriter out = new PrintWriter(System.out); int n = in.nextInt(); int q=in.nextInt(); char s[]=in.next().toCharArray(); int arr[]=new int[100001]; long c=0; for(int i=2;i<n;i++) { if(s[i]=='c'&&s[i-1]=='b'&&s[i-2]=='a') { arr[i+1]=1; c++; } } while(q-->0) { int pos=in.nextInt(); char x=in.next().charAt(0); //out.println(x); if(arr[pos]==1) { if(x!='c') { arr[pos]=0; c--; s[pos-1]=x; if(x=='a'&&pos<n-1) { if(s[pos]=='b'&&s[pos+1]=='c') { c++; arr[pos+2]=1; } } } s[pos-1]=x; } else if(pos<n&&arr[pos+1]==1) { if(x!='b') { arr[pos+1]=0; c--; s[pos-1]=x; } s[pos-1]=x; } else if(pos<n-1&&arr[pos+2]==1) { if(x!='a') { arr[pos+2]=0; c--; s[pos-1]=x; } if(x=='c'&&pos>2) { if(s[pos-2]=='b'&&s[pos-3]=='a') { arr[pos]=1; c++; } } s[pos-1]=x; } else { if(x=='a'&&pos<n-1) { if(s[pos-1]!=x&&s[pos]=='b'&&s[pos+1]=='c') { s[pos-1]=x; arr[pos+2]=1; c++; } } if(x=='b'&&pos>1&&pos<n) { if(s[pos-2]=='a'&&s[pos-1]!='b'&&s[pos]=='c') { arr[pos+1]=1; c++; } } if(x=='c'&&pos>2) { if(s[pos-3]=='a'&&s[pos-2]=='b'&&s[pos-1]!='c') { arr[pos]=1; c++; } } s[pos-1]=x; } out.println(c); } out.close(); } 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); } static boolean checkprime(int n1) { if(n1%2==0||n1%3==0) return false; else { for(int i=5;i*i<=n1;i+=6) { if(n1%i==0||n1%(i+2)==0) return false; } return true; } } static class FScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer sb = new StringTokenizer(""); String next(){ while(!sb.hasMoreTokens()){ try{ sb = new StringTokenizer(br.readLine()); } catch(IOException e){ } } return sb.nextToken(); } String nextLine(){ try{ return br.readLine(); } catch(IOException e) { } return ""; } int nextInt(){ return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } int[] readArray(int n) { int a[] = new int[n]; for(int i=0;i<n;i++) a[i] = nextInt(); return a; } float nextFloat(){ return Float.parseFloat(next()); } double nextDouble(){ return Double.parseDouble(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 8
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
e0242650a16bcb5710acfbb595dcaf65
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.StringTokenizer; public class B { static BufferedReader br; static StringTokenizer st; static PrintWriter pw; static String nextToken() { try { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } static int nextInt() { return Integer.parseInt(nextToken()); } static long nextLong() { return Long.parseLong(nextToken()); } static double nextDouble() { return Double.parseDouble(nextToken()); } static String nextLine() { try { return br.readLine(); } catch (IOException e) { throw new IllegalArgumentException(); } } static char nextChar() { try { return (char) br.read(); } catch (IOException e) { throw new IllegalArgumentException(e); } } public static void main(String[] args) throws IOException { br = new BufferedReader(new InputStreamReader(System.in)); pw = new PrintWriter(System.out); int t = 1; n = nextInt(); t = nextInt(); s = nextLine().toCharArray(); arr = new int[n]; for (int i = 2; i < n; i++) { arr[i] = arr[i - 1]; if (s[i] == 'c' && s[i - 1] == 'b' && s[i - 2] == 'a') { arr[i] = arr[i - 1] + 1; } } while (t-- > 0) { solve(); } pw.close(); } static int n; static char[] s; static int[] arr; private static void solve() { String str = nextLine(); int pr = str.indexOf(' '); int pos = Integer.parseInt(str.substring(0, pr)) - 1; char c = str.charAt(pr + 1); int t = 0; if (pos >= 2 && s[pos] == 'c' && s[pos - 1] == 'b' && s[pos - 2] == 'a') t++; if (pos < n - 2 && s[pos] == 'a' && s[pos + 1] == 'b' && s[pos + 2] == 'c') t++; if (pos>=1 && pos <n-1 && s[pos-1]=='a' && s[pos]=='b'&&s[pos+1]=='c') t++; s[pos] = c; if (pos >= 2 && s[pos] == 'c' && s[pos - 1] == 'b' && s[pos - 2] == 'a') t--; if (pos < n - 2 && s[pos] == 'a' && s[pos + 1] == 'b' && s[pos + 2] == 'c') t--; if (pos>=1 && pos <n-1 && s[pos-1]=='a' && s[pos]=='b'&&s[pos+1]=='c') t--; arr[n-1] -= t; // if (c=='c') { // if (pos>0 && arr[pos] != arr[pos-1]) { // for (int i = pos; i < n; i++) { // arr[i]--; // } // } // } else if (c=='b') { // // } pw.println(arr[n - 1]); pw.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 8
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
640f38912a0bb99476e073d8d97d3c46
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 { public static void main(String[] args) { //for (int t = IO.nextInt(), i = 0; i < t; i++) new SolB().solve(); IO.close(); } } class SolB { int n, q; char[] s; SolB() { n = IO.nextInt(); q = IO.nextInt(); s = IO.readLine().toCharArray(); } void solve() { if (n < 3) { for (int i = 0; i < q; i++) { IO.next(); IO.next(); IO.writer.println(0); } return; } int cnt = 0; for (int i = 0; i < n-2; i++) { if (check(i)) cnt++; } for (int i = 0; i < q; i++) { int pos = IO.nextInt()-1; char c = IO.next().toCharArray()[0]; if (s[pos] == c) { IO.writer.println(cnt); continue; } if (check(pos-2) || check(pos-1) || check(pos)) { s[pos] = c; if (!check(pos) && !check(pos-2)) cnt--; } else { s[pos] = c; if (check(pos-2) || check(pos-1) || check(pos)) cnt++; } IO.writer.println(cnt); } } boolean check(int i) { if (i < 0 || i >= n-2) return false; if (s[i] == 'a' && s[i+1] == 'b' && s[i+2] == 'c') return true; return false; } } class IO { static final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); static final PrintWriter writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); private static StringTokenizer tokens; static String readLine() { try { return reader.readLine(); } catch (Exception e) { throw new RuntimeException(e); } } static void initializeTokens() { while (null == tokens || !tokens.hasMoreTokens()) tokens = new StringTokenizer(readLine()); } static String next() { initializeTokens(); return tokens.nextToken(); } static String next(String delim) { initializeTokens(); return tokens.nextToken(delim); } static int nextInt() { return Integer.parseInt(next()); } static long nextLong() { return Long.parseLong(next()); } static void close() { try { reader.close(); writer.close(); } catch (Exception e) { throw new RuntimeException(e); } } }
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 8
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
91efcb7676e783ce0ebdca2e3c553fbf
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 WilliamAndVigilant { public static void main(String[] args) { FastReader in=new FastReader(); PrintWriter out=new PrintWriter(System.out); int n=in.nextInt(); int q=in.nextInt(); String s=in.nextLine(); char[] s1=s.toCharArray(); int count=0; for (int i = 0; i <= n-3; i++) { if(s1[i]=='a' && s1[i+1]=='b'&& s1[i+2]=='c') { i+=2; count++; } } for (int i = 0; i < q; i++) { int pos=in.nextInt(); pos--; char x=in.next().charAt(0); if (s1[pos]!=x) { if (s1[pos] == 'c' && pos - 2 >= 0 && s1[pos - 1] == 'b' && s1[pos - 2] == 'a') count--; if (s1[pos]== 'b' && pos - 1 >= 0 && pos + 1 < n && s1[pos + 1] == 'c' && s1[pos - 1] == 'a') count--; if (s1[pos] == 'a' && pos + 2 < n && s1[pos + 1] == 'b' && s1[pos + 2] == 'c') count--; s1[pos] = x; if (x == 'c' && pos - 2 >= 0 && s1[pos - 1] == 'b' && s1[pos - 2] == 'a') count++; if (x == 'b' && pos - 1 >= 0 && pos + 1 < n && s1[pos + 1] == 'c' && s1[pos - 1] == 'a') count++; if (x == 'a' && pos + 2 < n && s1[pos + 1] == 'b' && s1[pos + 2] == 'c') count++; } out.println(count); } out.close(); } 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); } 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 8
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
2d0efa68b75e55d592cedf0163d3e166
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.StringTokenizer; import java.util.TreeSet; import static java.lang.System.out; public class pre80 { 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 boolean find(char str[],int i){ if(str[i]=='a' && str[i+1]=='b' && str[i+2]=='c') return true; return false; } public static void main(String args[]){ FastReader obj = new FastReader(); int n = obj.nextInt(),m = obj.nextInt(); char str[] = obj.next().toCharArray(); int count = 0; for(int i=0;i<str.length-2;i++) if(find(str,i)) count++; while(m--!=0){ int x = obj.nextInt()-1; char c = obj.next().charAt(0); for(int i=x-2;i<=x;i++){ if(i>=0 && i<n-2 && find(str,i)) count--; } str[x] = c; for(int i=x-2;i<=x;i++){ if(i>=0 && i<n-2 && find(str,i)) count++; } 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 8
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
9ec2b236a970af0f00851484de1d9c80
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 WilliamTheVigilant { static int count(char[] s , int n){ int ans = 0; for(int i=0; i<n ;i++){ if(s[i] == 'a' && i<n-2) if(s[i+1] == 'b' && s[i+2] == 'c') ans++; } return ans; } static int chk(char[] s , int i){ int ans = 0; if(i >= 0 && i+2 < s.length && s[i] == 'a' && s[i+1] == 'b' && s[i+2] == 'c') return 1; else return 0; } public static void main(String[] args) throws IOException{ Scanner sc = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int n = sc.nextInt() , q = sc.nextInt(); String s = sc.next(); char[] c = s.toCharArray(); int cnt = count(c,n); while(q-->0){ int i = sc.nextInt(); char x = sc.next().charAt(0); --i; cnt -= chk(c,i); cnt -= chk(c,i-1); cnt -= chk(c,i-2); c[i] = x; cnt += chk(c,i-2); cnt += chk(c,i-1); cnt += chk(c,i); pw.println(cnt); } pw.flush(); } static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } static class Scanner { BufferedReader br; StringTokenizer st; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(FileReader f) { br = new BufferedReader(f); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public int[] nextIntArr(int n) throws IOException { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = Integer.parseInt(next()); } return arr; } } }
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 8
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
a4a5054e18bf0f6690a4a7e8f108afd2
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.StringTokenizer; public class Solution { public static void main(String[] args) throws Exception { Solution solution = new Solution(); solution.solve(); //solution.test("bcaabcabc", 4, 'b'); solution.close(); } StringTokenizer line; PrintWriter writer; BufferedReader reader; int t; Solution() throws IOException { reader = new BufferedReader(new InputStreamReader(System.in)); writer = new PrintWriter(System.out); } void nextLine() throws IOException { do { line = new StringTokenizer(reader.readLine()); } while (!line.hasMoreTokens()); } int nextInt() { return Integer.parseInt(line.nextToken()); } long nextLong() { return Long.parseLong(line.nextToken()); } char nextChar() { return line.nextToken().charAt(0); } String nextToken() { return line.nextToken(); } void close() throws IOException { writer.flush(); writer.close(); reader.close(); } void solve() throws IOException { // nextLine(); // t = nextInt(); for (int i = 0; i < 1; i++) { solveCase(); } } int n, q, count; String s; StringBuilder builder; int countABCs(String s) { int count = 0; int j = 0; for (int i = 0; i < s.length(); i++) { if (s.charAt(i) == 'a') { j = 1; } else if (s.charAt(i) == 'b') { if (j == 1) j++; else j = 0; } else if (s.charAt(i) == 'c') { if (j == 2) count++; j = 0; } } return count; } /* [java] bcabbcabc [java] 4 [java] b [java] 2 */ void test(String s, int qi, char c) { builder = new StringBuilder(s); int change = query(qi - 1, c, builder); writer.println(change); } int[] getBounds(int i, int low, int high) { int[] bounds = new int[2]; bounds[0] = i - 2; bounds[1] = i + 2; if (bounds[0] < low) bounds[0] = low; if (bounds[1] > high) bounds[1] = high; return bounds; } boolean thereIsOne(StringBuilder builder, int start, int end) { int j = 0; for (int i = start; i <= end; i++) { //writer.println(j + " , " + builder.charAt(i)); if (builder.charAt(i) == 'a') { j = 1; } else if (builder.charAt(i) == 'b') { if (j == 1) j++; else j = 0; } else if (builder.charAt(i) == 'c') { if (j == 2) return true; j = 0; } } return false; } int query(int qi, char c, StringBuilder builder) { int[] bounds = getBounds(qi, 0, builder.length() - 1); int before = 0; if (thereIsOne(builder, bounds[0], bounds[1])) before = 1; int after = 0; builder.setCharAt(qi, c); if (thereIsOne(builder, bounds[0], bounds[1])) after = 1; //writer.println(before + " -> " + after); return after - before; } void solveCase() throws IOException { nextLine(); n = nextInt(); q = nextInt(); nextLine(); s = nextToken(); count = countABCs(s); builder = new StringBuilder(s); for (int i = 0; i < q; i++) { nextLine(); int qi = nextInt(); char c = nextChar(); count += query(qi - 1, c, builder); //writer.println(builder); //writer.println(qi + " " + c); //writer.println(c); writer.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 8
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
ff6d94d4374ad8dbca0b2bf16ef29b9f
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.StringReader; import java.lang.reflect.Array; import java.math.BigInteger; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.Date; import java.util.HashMap; import java.util.HashSet; import java.util.Hashtable; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Random; import java.util.Scanner; import java.util.Stack; import java.util.StringTokenizer; public class Main { 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 gcd(int no, int sum) { if (sum == 0) return no; return gcd(sum, no % sum); } static int lcm(int a , int b){ return (a / gcd(a , b))*b; } static void countSort(int[] arr) { int max = Arrays.stream(arr).max().getAsInt(); int min = Arrays.stream(arr).min().getAsInt(); int range = max - min + 1; int count[] = new int[range]; int output[] = new int[arr.length]; for (int i = 0; i < arr.length; i++) { count[arr[i] - min]++; } for (int i = 1; i < count.length; i++) { count[i] += count[i - 1]; } for (int i = arr.length - 1; i >= 0; i--) { output[count[arr[i] - min] - 1] = arr[i]; count[arr[i] - min]--; } for (int i = 0; i < arr.length; i++) { arr[i] = output[i]; } } static void RandomShuffleSort(int [] a , int start , int end){ Random random = new Random(); for(int i = start; i<end; i++){ int j = random.nextInt(end); int temp = a[j]; a[j] = a[i]; a[i] = temp; } Arrays.sort(a , start , end); } public static void main(String[] args) throws IOException , ParseException{ BufferedWriter out = new BufferedWriter( new OutputStreamWriter(System.out)); FastReader sc = new FastReader(); int n = sc.nextInt(); int q = sc.nextInt(); char a[] = sc.nextLine().toCharArray(); int char1 = 0; int char2 = 1; int char3 = 2; int count = 0; // System.out.println(new String(a)); while(char3 < n){ // System.out.println(char3); if(a[char1] == 'a' && a[char2] == 'b' && a[char3] == 'c'){ count++; char1+=3; char2+=3; char3+=3; } else { char1++; char2++; char3++; } } for(int i = 0; i<q; i++){ int pos = sc.nextInt(); String s = sc.next(); if(a[pos-1] != s.charAt(0)){ char1 = pos-1; char2 = char1+1; char3 = char2+1; if((-1<char1 && char1<n) && (-1<char2 && char2<n) && (-1 < char3 && char3<n)){ if(a[char1] == 'a' && a[char2] == 'b' && a[char3] == 'c'){ count--; } else { if(s.charAt(0) == 'a' && a[char2] == 'b' && a[char3] == 'c'){ count++; } } } char2 = pos-1; char1 = char2-1; char3 = char2+1; if((-1<char1 && char1<n) && (-1<char2 && char2<n) && (-1 < char3 && char3<n)){ if(a[char1] == 'a' && a[char2] == 'b' && a[char3] == 'c'){ count--; } else { if(a[char1] == 'a' && s.charAt(0) == 'b' && a[char3] == 'c'){ count++; } } } char3 = pos-1; char2 = char3-1; char1 = char2-1; if((-1<char1 && char1<n) && (-1<char2 && char2<n) && (-1 < char3 && char3<n)){ if(a[char1] == 'a' && a[char2] == 'b' && a[char3] == 'c'){ count--; } else { if(a[char1] == 'a' && a[char2] == 'b' && s.charAt(0) == 'c'){ count++; } } } a[pos-1] = s.charAt(0); System.out.println(count); } else 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 8
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
6bf5cdb7c26e91f6c55ddc52a418b9f9
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 Main { public static void main (String[] args) throws java.lang.Exception { // your code goes here Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int q = sc.nextInt(); String s = sc.next(); char ch[] = s.toCharArray(); int ans = 0; for(int i=0;i<=n-3;i++){ if(s.substring(i,i+3).equals("abc"))ans++; } while(q-- > 0) { int i = sc.nextInt(); char c = sc.next().charAt(0); i--; for (int k = Math.max(i - 2, 0); k <= Math.min(i, n - 3); k++){ if (ch[k] == 'a' && ch[k+1] == 'b' && ch[k+2] == 'c'){ ans--; } } ch[i] = c; for (int k = Math.max(i - 2, 0); k <= Math.min(i, n - 3); k++){ if (ch[k] == 'a' && ch[k+1] == 'b' && ch[k+2] == 'c'){ ans++; } } System.out.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 8
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
2b3852cf863536ce99c60f1ac3bdf812
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 Dec8A { static boolean[] isPrime; public static void main(String[] args) { FastScanner sc=new FastScanner(); TreeSet<Integer> st=new TreeSet<>(); int n=sc.nextInt(); int q=sc.nextInt(); String str=sc.next(); char[] ch=str.toCharArray(); for(int i=0;i<n-2;i++){ if(str.charAt(i)=='a' && str.charAt(i+1)=='b' && str.charAt(i+2)=='c'){ st.add(i); i+=2; } } while(q-- > 0){ int indx=sc.nextInt()-1; char ch1=sc.next().charAt(0); //int sz=st.size(); /*boolean b=false; int temp=-1; for(Integer i:st){ if(i>indx){ break; } if(indx>=i && indx<=i+2){ if(ch[indx]!=ch1){ temp=i; } b=true; break; } }*/ ch[indx]=ch1; // System.out.println(Arrays.toString(ch)+" "+st); /*if(b){ if(temp==-1){ System.out.println(sz); continue; } st.remove(temp); System.out.println(sz-1); continue; }*/ st.remove(indx); st.remove(indx-1); st.remove(indx-2); if(ch1!='a') indx=(ch1=='b')?(indx-1):(indx-2); //System.out.println(Arrays.toString(ch)+" "+indx+" "+st); if(indx>=0 && indx<n-2){ if(ch[indx]=='a' && ch[indx+1]=='b' && ch[indx+2]=='c'){ st.add(indx); } } int sz=st.size(); /* else if(ch1=='b' && indx<n-1 && indx>0){ if(ch[indx-1]=='a' && ch[indx]=='b' && ch[indx+1]=='c'){ st.add(indx-1); System.out.println(sz+1); continue; } System.out.println(sz); continue; } else if(indx>1){ if(ch[indx-2]=='a' && ch[indx-1]=='b' && ch[indx]=='c'){ st.add(indx-2); System.out.println(sz+1); continue; } System.out.println(sz); continue; }*/ System.out.println(sz); } } public static long npr(int n, int r) { long ans = 1; for(int i= 0 ;i<r; i++) { ans*= (n-i); } return ans; } public static double ncr(int n, int r) { double ans = 1; for(int i= 0 ;i<r; i++) { ans*= (n-i); } for(int i= 0 ;i<r; i++) { ans/=(i+1); } return ans; } public static void fillPrime() { int n = 1000007; Arrays.fill(isPrime, true); isPrime[0] = isPrime[1] = false; for (int i = 2; i * i < n; i++) { if (isPrime[i]) { for (int j = i * i; j < n; j += i) { isPrime[j] = false; } } } } static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String next() { while (st == null || !st.hasMoreTokens()) { try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } 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()); } double nextDouble() { return Double.parseDouble(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 8
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
0efeb2ddbfff936ac3baa73b75edec4a
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 MyCpClass{ public static void main(String []args) throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringBuilder sb = new StringBuilder(); String []ip = br.readLine().trim().split(" "); int n = Integer.parseInt(ip[0]); int q = Integer.parseInt(ip[1]); String str = br.readLine().trim(); char[] s = str.toCharArray(); int cnt = 0; for(int i=0; i<n-2; i++){ if(s[i] == 'a' && s[i+1] == 'b' && s[i+2] == 'c') cnt++; } while(q-- > 0){ ip = br.readLine().trim().split(" "); int pos = Integer.parseInt(ip[0])-1; char ch = ip[1].charAt(0); if(s[pos] == ch){ sb.append(cnt + "\n"); continue; } if(pos-2 >= 0 && s[pos-2] == 'a' && s[pos-1] == 'b' && s[pos] == 'c') cnt--; if(pos-1 >= 0 && pos+1 < n && s[pos-1] == 'a' && s[pos] == 'b' && s[pos+1] == 'c') cnt--; if(pos+2 < n && s[pos] == 'a' && s[pos+1] == 'b' && s[pos+2] == 'c') cnt--; s[pos] = ch; if(pos-2 >= 0 && s[pos-2] == 'a' && s[pos-1] == 'b' && s[pos] == 'c') cnt++; if(pos-1 >= 0 && pos+1 < n && s[pos-1] == 'a' && s[pos] == 'b' && s[pos+1] == 'c') cnt++; if(pos+2 < n && s[pos] == 'a' && s[pos+1] == 'b' && s[pos+2] == 'c') cnt++; sb.append(cnt + "\n"); } System.out.println(sb); } }
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 8
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
b64a2c898f67be706ac2c0367bac7a1b
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
// Online Java Compiler // Use this editor to write, compile and run your Java code online import java.io.*; public class HelloWorld { static int count = 0; public static void main(String[] args) throws Exception { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); String line = reader.readLine(); String[] splits = line.split(" "); int n = Integer.parseInt(splits[0]); int q = Integer.parseInt(splits[1]); int i = 0; String str = reader.readLine(); char[] chStr = str.toCharArray(); for(int p = 0; p < str.length()-2; p++) { if(str.charAt(p) == 'a' && str.charAt(p+1) == 'b' && str.charAt(p+2) == 'c') { count++; } } // System.out.println(count); while(i++ < q) { line = reader.readLine(); splits = line.split(" "); query(Integer.parseInt(splits[0])-1, splits[1].charAt(0), chStr); System.out.println(count); } } private static void query(int pos, char ch, char[] str) { if(str[pos] == ch) { return; } if(ch == 'a' && pos+2 < str.length && str[pos+1] == 'b' && str[pos+2] == 'c') { count++; } else if(ch == 'b' && pos+1 < str.length && pos-1 >= 0 && str[pos-1] == 'a' && str[pos+1] == 'c') { count++; } else if(ch == 'c' && pos-2 >= 0 && str[pos-2] == 'a' && str[pos-1] == 'b') { count++; } if(str[pos] == 'a' && pos+2 < str.length && str[pos+1] == 'b' && str[pos+2] == 'c') { count--; } else if(str[pos] == 'b' && pos+1 < str.length && pos-1 >= 0 && str[pos-1] == 'a' && str[pos+1] == 'c') { count--; } else if(str[pos] == 'c' && pos-2 >= 0 && str[pos-2] == 'a' && str[pos-1] == 'b') { count--; } str[pos] = 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 8
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
4d99e3ea86906d9d2b48aed9b150a201
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 Contest_yandexA{ public static void main(String[] args) { Scanner input = new Scanner(System.in); int n = input.nextInt(); int q = input.nextInt(); char[] c = input.next().toCharArray(); int count = 0; for(int i = 0;i<=n-3;i++){ if(c[i] == 'a' && c[i+1] == 'b' && c[i+2] == 'c'){ count++; } } //System.out.println(count); for(int i = 0;i<q;i++){ int x = input.nextInt()-1; char s = input.next().charAt(0); if(c[x] == s){ System.out.println(count); } else{ int change = 0; for(int j = Math.max(0,x-2);j<=Math.min(x,n-3);j++){ if(c[j] == 'a' && c[j+1] == 'b' && c[j+2] == 'c'){ change--; break; } } c[x] = s; for(int j = Math.max(0,x-2);j<=Math.min(x,n-3);j++){ if(c[j] == 'a' && c[j+1] == 'b' && c[j+2] == 'c'){ change++; break; } } count += change; System.out.println(count); } } } public static int gcd(int a,int b){ if(b == 0){ return a; } return gcd(b,a%b); } public static int lcm(int a,int b){ return (a / gcd(a, b)) * 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 8
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
ecc0e90c4f9df2ae087a2af2beba8698
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.deltix_autumn; import java.io.*; import java.util.*; import static java.lang.Math.*; //Think through the entire logic before jump into coding! //If you are out of ideas, take a guess! It is better than doing nothing! //Read both C and D, it is possible that D is easier than C for you! //Be aware of integer overflow! //If you find an answer and want to return immediately, don't forget to flush before return! public class B { static InputReader in; static PrintWriter out; public static void main(String[] args) { //initReaderPrinter(true); initReaderPrinter(false); //solve(in.nextInt()); solve(1); } static void solve(int testCnt) { for (int testNumber = 0; testNumber < testCnt; testNumber++) { int n = in.nextInt(), q = in.nextInt(); Set<Integer> set = new HashSet<>(); char[] s = (" " + in.next()).toCharArray(); for(int i = 3; i <= n; i++) { if(s[i - 2] == 'a' && s[i - 1] == 'b' && s[i] == 'c') { set.add(i - 2); } } for(int i = 0; i < q; i++) { int pos = in.nextInt(); char c = in.next().charAt(0); for(int j = max(pos - 2, 1); j <= pos; j++) { if(j + 2 <= n && s[j] == 'a' && s[j + 1] == 'b' && s[j + 2] == 'c') { set.remove(j); } } s[pos] = c; for(int j = max(pos - 2, 1); j <= pos; j++) { if(j + 2 <= n && s[j] == 'a' && s[j + 1] == 'b' && s[j + 2] == 'c') { set.add(j); } } out.println(set.size()); } } out.close(); } static void initReaderPrinter(boolean test) { if (test) { try { in = new InputReader(new FileInputStream("src/input.in")); out = new PrintWriter(new FileOutputStream("src/output.out")); } catch (IOException e) { e.printStackTrace(); } } else { in = new InputReader(System.in); out = new PrintWriter(System.out); } } static class InputReader { BufferedReader br; StringTokenizer st; InputReader(InputStream stream) { try { br = new BufferedReader(new InputStreamReader(stream), 32768); } catch (Exception e) { e.printStackTrace(); } } 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(); } catch (IOException e) { e.printStackTrace(); } return str; } Integer[] nextIntArray(int n) { Integer[] a = new Integer[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } int[] nextIntArrayPrimitive(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } int[] nextIntArrayPrimitiveOneIndexed(int n) { int[] a = new int[n + 1]; for (int i = 1; i <= n; i++) a[i] = nextInt(); return a; } Long[] nextLongArray(int n) { Long[] a = new Long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } long[] nextLongArrayPrimitive(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } long[] nextLongArrayPrimitiveOneIndexed(int n) { long[] a = new long[n + 1]; for (int i = 1; i <= n; i++) a[i] = nextLong(); return a; } String[] nextStringArray(int n) { String[] g = new String[n]; for (int i = 0; i < n; i++) g[i] = next(); return g; } List<Integer>[] readGraphOneIndexed(int n, int m) { List<Integer>[] adj = new List[n + 1]; for (int i = 0; i <= n; i++) { adj[i] = new ArrayList<>(); } for (int i = 0; i < m; i++) { int u = nextInt(); int v = nextInt(); adj[u].add(v); adj[v].add(u); } return adj; } List<Integer>[] readGraphZeroIndexed(int n, int m) { List<Integer>[] adj = new List[n]; for (int i = 0; i < n; i++) { adj[i] = new ArrayList<>(); } for (int i = 0; i < m; i++) { int u = nextInt() - 1; int v = nextInt() - 1; adj[u].add(v); adj[v].add(u); } return adj; } /* A more efficient way of building a graph using int[] instead of ArrayList to store each node's neighboring nodes. 1-indexed. */ int[][] buildGraph(int nodeCnt, int edgeCnt) { int[] end1 = new int[edgeCnt], end2 = new int[edgeCnt]; int[] edgeCntForEachNode = new int[nodeCnt + 1], idxForEachNode = new int[nodeCnt + 1]; for (int i = 0; i < edgeCnt; i++) { int u = in.nextInt(), v = in.nextInt(); edgeCntForEachNode[u]++; edgeCntForEachNode[v]++; end1[i] = u; end2[i] = v; } int[][] adj = new int[nodeCnt + 1][]; for (int i = 1; i <= nodeCnt; i++) { adj[i] = new int[edgeCntForEachNode[i]]; } for (int i = 0; i < edgeCnt; i++) { adj[end1[i]][idxForEachNode[end1[i]]] = end2[i]; idxForEachNode[end1[i]]++; adj[end2[i]][idxForEachNode[end2[i]]] = end1[i]; idxForEachNode[end2[i]]++; } return adj; } } }
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 8
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
e58d00ad876c263e2e6382d2cd6c76ea
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 Grow13 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int q = sc.nextInt(); String str = sc.next(); char cha[] = str.toCharArray(); ArrayList<Integer> ans = new ArrayList<>(); int res = Find(cha, n, ans); while (q-- > 0) { ToGain(); int pos = sc.nextInt(); pos = pos - 1; char ori = cha[pos]; char ch = sc.next().charAt(0); if (ch == ori) { System.out.println(res); } else { cha[pos] = ch; if (ch == 'a') { if (pos + 2 < n && cha[pos + 1] == 'b' && cha[pos + 2] == 'c') { res = res + 1; } if (pos - 1 >= 0 && pos + 1 < n && ori == 'b' && cha[pos + 1] == 'c' && cha[pos - 1] == 'a') { res = res - 1; } else if (pos - 2 >= 0 && ori == 'c' && cha[pos - 1] == 'b' && cha[pos - 2] == 'a') { res = res - 1; } } else if (ch == 'b') { if (pos - 1 >= 0 && pos + 1 < n && cha[pos - 1] == 'a' && cha[pos + 1] == 'c') { res = res + 1; } if (pos + 2 < n && ori == 'a' && cha[pos + 1] == 'b' && cha[pos + 2] == 'c') { res = res - 1; } else if (pos - 2 >= 0 && ori == 'c' && cha[pos - 1] == 'b' && cha[pos - 2] == 'a') { res = res - 1; } } else { if (pos - 2 >= 0 && cha[pos - 1] == 'b' && cha[pos - 2] == 'a') { res = res + 1; } if (pos + 2 < n && ori == 'a' && cha[pos + 1] == 'b' && cha[pos + 2] == 'c') { res = res - 1; } else if (pos - 1 >= 0 && pos + 1 < n && ori == 'b' && cha[pos + 1] == 'c' && cha[pos - 1] == 'a') { res = res - 1; } } System.out.println(res); ToGain(); } } } public static void ToGain() { for (int i = 0; i < 20; i++) { } } public static int Find(char[] ch, int n, ArrayList<Integer> ans) { int res = 0; for (int i = 0; i < n; i++) { if (i + 2 < n) { if (ch[i] == 'a' && ch[i + 1] == 'b' && ch[i + 2] == 'c') { res++; ans.add(i); i = i + 2; } } } 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 8
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
d29236f7ffe0752c80cacba25250bd11
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 Main { BufferedReader in; PrintWriter out; Main() { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); } void solve() throws IOException { StringTokenizer st = new StringTokenizer(in.readLine()); int n = Integer.parseInt(st.nextToken()); int q = Integer.parseInt(st.nextToken()); String s = in.readLine(); char[] chs = s.toCharArray(); boolean[] abcs = new boolean[n]; int cnt = 0; for (int j = 0; j <= n - 3; j++) { String sub = s.substring(j, j + 3); if (sub.equals("abc")) { abcs[j] = true; cnt++; } } for (int i = 0; i < q; i++) { st = new StringTokenizer(in.readLine()); int index = Integer.parseInt(st.nextToken()); char ch = st.nextToken().charAt(0); if (chs[index - 1] != ch) { int start = index - 1; start -= 2; if (start <= 0) { start = 0; } int end = index - 1; chs[index - 1] = ch; for (int j = start; j <= end; j++) { char[] nchs = Arrays.copyOfRange(chs, j, j + 3); String ns = new String(nchs); boolean nv = ns.equals("abc"); if (nv != abcs[j]) { if (abcs[j]) { cnt--; } else { cnt++; } abcs[j] = nv; } } } out.append(String.format("%d\n", cnt)); } } void close() throws IOException { if (in != null) { in.close(); } if (out != null) { out.flush(); out.close(); } } public static void main(String[] args) throws Exception { FileInputStream inStream = null; PrintStream outStream = null; boolean isLocal = System.getProperty("os.name").equals("Mac OS X"); if (isLocal) { inStream = new FileInputStream("1.in"); // outStream = new PrintStream("1.out"); System.setIn(inStream); // System.setOut(outStream); } Main main = new Main(); main.solve(); main.close(); if (isLocal) { inStream.close(); // outStream.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 8
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
dd1d60239bad7f893ca55010a14f0685
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 Wiliam{ public static boolean funl(char ar[], int i,int n){ if (i-2<0) return false; if (ar[i-2]=='a' && ar[i-1]=='b' && ar[i]=='c') return true; return false; } public static boolean funm(char ar[], int i,int n){ if (i-1<0) return false; if (i+1>=n) return false; if (ar[i-1]=='a' && ar[i]=='b' && ar[i+1]=='c') return true; return false; } public static boolean fune(char ar[], int i, int n){ if (i+2 >=n) return false; if (ar[i]=='a' && ar[i+1]=='b' && ar[i+2]=='c') return true; return false; } public static void main (String [] args){ Scanner sc = new Scanner (System.in); int n=sc.nextInt(); int q = sc.nextInt(); sc.nextLine(); String s = sc.nextLine(); StringBuilder sb = new StringBuilder(); char cr[]=s.toCharArray(); int tot=0; for (int i=0;i<=n-3;i++){ if (cr[i]=='a' && cr[i+1]=='b' && cr[i+2]=='c') tot++; } int id=0; char ch=' '; for (int i=0;i<q;i++){ id = sc.nextInt(); ch=sc.next().trim().charAt(0); id=id-1; if (ch==cr[id]) { sb.append(tot).append("\n"); continue; } if (funl(cr,id,n) || funm (cr,id,n) || fune(cr,id,n)) tot--; cr[id]=ch; if (funl(cr,id,n) || funm (cr,id,n) || fune(cr,id,n)) tot++; sb.append(tot).append("\n"); } System.out.println (sb); } }
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 8
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
bfb3be922299cde271ab9b3036a95875
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.*; public class A { public static void main(String[] args) { FastScanner sc = new FastScanner(); int n = sc.nextInt(), q = sc.nextInt(); char[] s = sc.next().toCharArray(); int count = 0; for(int i = 0; i + 2 < n; i++) if(s[i] == 'a' && s[i + 1] == 'b' && s[i + 2] == 'c') count++; while(q-- > 0) { int pos = sc.nextInt() - 1; for(int i = Math.max(0,pos - 2); i + 2 < Math.min(n, pos + 3); i++) if(s[i] == 'a' && s[i + 1] == 'b' && s[i + 2] == 'c') count--; s[pos] = sc.next().charAt(0); for(int i = Math.max(0,pos - 2); i + 2 < Math.min(n, pos + 3); i++) if(s[i] == 'a' && s[i + 1] == 'b' && s[i + 2] == 'c') count++; System.out.println(count); } } 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()); } 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 8
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
e680c7834da63c615cf2f3568adb32ed
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; import java.io.FileInputStream; import java.io.InputStream; import java.io.StringReader; import java.util.*; public class Main { public static class Dsu{ int[] parent; Set<Integer>[] rest; Set<Integer>[] restrictions; Dsu(int n, int[][] restrictions) { parent = new int[n]; rest = new Set[n]; for(int i =0; i < n; i ++) parent[i]=i; for(int i =0; i < n; i++) { rest[i] = new HashSet<>(); rest[i].add(i); } this.restrictions = new Set[n]; for(int i =0; i < n; i++) { this.restrictions[i] = new HashSet<>(); } for(int[] restriction : restrictions) { this.restrictions[restriction[0]].add(restriction[1]); this.restrictions[restriction[1]].add(restriction[0]); } } int find(int son) { if(parent[son]==son) return son; return find(parent[son]); } boolean union(int p, int q) { int pp = find(p); int pq = find(q); if(pp==pq) return true; if(valid(rest[pp],restrictions[pq])) { rest[pp].addAll(rest[pq]); parent[pq] = pp; return true; } return false; } boolean valid(Set<Integer> s1, Set<Integer> s2) { for(Integer i : s1) { if(s2.contains(i)) return false; //System.out.println(i); } return true; } } public static boolean[] friendRequests(int n, int[][] restrictions, int[][] requests) { boolean[] ans = new boolean[requests.length]; Dsu dsu = new Dsu(n,restrictions); int i =0; for(int[] req : requests) { ans[i++] = dsu.union(req[0],req[1]); } return ans; } public static void main(String[] args) { // write your code here Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int q = scanner.nextInt(); StringBuilder s = new StringBuilder(scanner.next()); int sol = 0; String comp = "abc"; for(int i =1; i < n-1; i++) { if(i>0&&i<s.length()-1&&s.substring(i-1,i+2).equals(comp)) sol++; } for(int i =0; i < q;i++) { int pos = scanner.nextInt(); pos--; String c = scanner.next(); int add = 0; int rem = 0; if(!s.substring(pos,pos+1).equals(c)) { for(int j=pos-1;j<pos+2;j++) { if(j>0&&j<s.length()-1&&s.substring(j-1,j+2).equals(comp)) rem++; } s.setCharAt(pos,c.charAt(0)); for(int j=pos-1;j<pos+2;j++) { if(j>0&&j<s.length()-1&&s.substring(j-1,j+2).equals(comp)) add++; } } sol -= rem; sol += add; System.out.println(sol); } } }
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 8
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
6be15d4cae0fa3b8ab7ce5dbaccbe5dc
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.*; import static java.lang.Math.max; import static java.lang.System.*; import static java.util.Arrays.parallelSetAll; import static java.util.Arrays.sort; import static java.util.Collections.reverseOrder; public class B { public static final boolean LOCAL = System.getProperty("ONLINE_JUDGE") == null; // public static final boolean LOCAL = System.getProperty("LOCAL")!=null; public static final String ANSI_RED = "\u001B[31m"; public static final String ANSI_RESET = "\u001B[0m"; static final int M = (int) 1e9 + 7; static final long MM = (long) M * M; static final int MAX = Integer.MAX_VALUE; static final int MIN = Integer.MIN_VALUE; static final int SIZE = (int) 1e9 * 2; static ArrayList<Integer> primes = new ArrayList<>(); //static Scanner sc = new Scanner(System.in); static FastReader sc = new FastReader(); static long startTime; static long endTime; static boolean[] prime; static int[] visited; static ArrayList<ArrayList<Integer>> graph; static ArrayList<Integer> dfs; static boolean isCyclic = false; public static int swap(int... args) { return args[0]; } public static void main(String[] args) throws IOException { do { startTime = currentTimeMillis(); //setOut(new PrintStream("output")); //setIn(new FileInputStream("input")); // int T = sc.nextInt();while (T-- != 0) { solve(); } endTime = currentTimeMillis(); long duration = endTime - startTime; //out.println(duration); if (false) { out.println(max(3, 2)); Integer[] ARRAY = new Integer[2]; ArrayList<Integer> LIST = new ArrayList<>(); LIST.add(2); // sort(LIST, reverseOrder()); LIST.sort(reverseOrder()); sort(ARRAY, reverseOrder()); out.println(LIST + " " + Arrays.toString(ARRAY)); } } while (LOCAL); } public static void solve() { ///////////////////////////////////////////////////////////////////// int n =sc.nextInt(),q=sc.nextInt(); String str = sc.next(); // out.println('a'+'b'+'c');//294 int cnt = 0; for (int i = 0; i < n-2; i++) { if(str.charAt(i)=='a'&&str.charAt(i+1)=='b'&&str.charAt(i+2)=='c'){ cnt++; } } StringBuilder stringBuilder = new StringBuilder("111"+str); stringBuilder.append("111"); while (q--!=0){ int index = sc.nextInt(); char ch =sc.next().charAt(0); if(str.length()==1 || str.length()==2){ out.println(0); continue; } index--; index+=3; if (stringBuilder.charAt(index) != ch) { cnt -= check(stringBuilder, index); stringBuilder.setCharAt(index, ch); cnt+=check(stringBuilder,index); } out.println(cnt); } /////////////////////////////////////////////////////////////////////// } static int check ( StringBuilder str, int index){ if(str.charAt(index-2)=='a'&&str.charAt(index-1)=='b'&&str.charAt(index)=='c'){ return 1; } if(str.charAt(index-1)=='a'&&str.charAt(index)=='b'&&str.charAt(index+1)=='c'){ return 1; } if(str.charAt(index)=='a'&&str.charAt(index+1)=='b'&&str.charAt(index+2)=='c'){ return 1; } return 0; } static void dfs(int src) { visited[src] = 1; for (Integer integer : graph.get(src)) { if (visited[integer] != 1) { dfs(integer); } } dfs.add(src); } static void detectCycle(int src) { visited[src] = 2; for (Integer integer : graph.get(src)) { if (visited[integer] == 0) { detectCycle(integer); } else if (visited[integer] == 2) { isCyclic = true; return; } } visited[src] = 1; } static <Array, Target> int count(Array[] array, Target target) { int cn = 0; for (Array value : array) { if (value.equals(target)) cn++; } return cn; } static int count(String string, char target) { int cn = 0; for (int i = 0; i < string.length(); i++) { if (string.charAt(i) == target) cn++; } return cn; } static <Array> void revArray(Array[] arr) { int n = arr.length; for (int i = 0; i < n / 2; i++) { Array temp = arr[i]; arr[i] = arr[n - i - 1]; arr[n - i - 1] = temp; } } static <Array> void printArray(Array[] arr) { int n = arr.length; for (Array array : arr) out.print(array + " "); out.println(); } static void yes() { out.println("YES"); } static void no() { out.println("NO"); } public static boolean isPal(String s) { StringBuilder stringBuilder = new StringBuilder(s); return s.equals(stringBuilder.reverse().toString()); } public static int lowerBound(Integer[] array, int length, int value) { int low = -1; int hi = length; while (low + 1 < hi) { int mid = (low + hi) / 2; if (array[mid] <= value) { low = mid; } else { hi = mid; } } return low; } public static int upperBound(Integer[] array, int length, int value) { int low = -1; int hi = length; while (low + 1 < hi) { int mid = (low + hi) >> 1; if (array[mid] >= value) { hi = mid; } else low = mid; } return hi; } public static int binarySearch(Integer[] arr, int length, int value) { int low = 0; int hi = length - 1; int ans = -1; while (low <= hi) { int mid = (low + hi) >> 1; if (arr[mid] > value) { hi = mid - 1; } else if (arr[mid] < value) { low = mid + 1; } else { ans = mid; break; } } return ans; } public static int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } public static int lcm(int a, int b) { return a / gcd(a, b) * b; } public static int countDivs(int n) { int cn = 0; int sqr = (int) Math.sqrt(n); for (int i = 1; i <= sqr; ++i) { if (n % i == 0) { ++cn; } } cn *= 2; if (sqr * sqr == n) cn--; return cn; } static void prime(int x) { //sieve algorithm. nlog(log(n)). prime = new boolean[(x + 1)]; Arrays.fill(prime, true); prime[0] = prime[1] = false; for (int i = 2; i * i <= x; i++) if (prime[i]) for (int j = i * i; j <= x; j += i) prime[j] = false; } static boolean isEven(long x) { return x % 2 == 0; } static boolean isPrime(long x) { boolean flag = true; int sqr = (int) Math.sqrt(x); if (x < 2) return false; for (int i = 2; i <= sqr; i++) { if (x % i == 0) { flag = false; break; } } return flag; } static long factorial(long x) { long total = 1; for (int i = 2; i <= x; i++) total = (total * i) % M; return total; } static long power1(long x, long y, long p) { long res = 1; // Initialize result x = x % p; // Update x if it is more than or // equal to p if (x == 0) return 0; // In case x is divisible by p; while (y > 0) { // If y is odd, multiply x with result if ((y & 1) != 0) res = (res * x) % p; // y must be even now y = y >> 1; // y = y/2 x = (x * x) % p; } return res; } static long power(long x, long n) { if (n == 0) { return 1; } long pow = power(x, n / 2L); if ((n & 1) == 1) { return (x % MM * (pow % MM) * (pow % MM)) % MM; } return (pow % MM * pow % MM) % MM; } private static <T> String ts(T t) { if (t == null) { return "null"; } try { return ts((Iterable) t); } catch (ClassCastException e) { if (t instanceof int[]) { String s = Arrays.toString((int[]) t); return "{" + s.substring(1, s.length() - 1) + "}"; } else if (t instanceof long[]) { String s = Arrays.toString((long[]) t); return "{" + s.substring(1, s.length() - 1) + "}"; } else if (t instanceof char[]) { String s = Arrays.toString((char[]) t); return "{" + s.substring(1, s.length() - 1) + "}"; } else if (t instanceof double[]) { String s = Arrays.toString((double[]) t); return "{" + s.substring(1, s.length() - 1) + "}"; } else if (t instanceof boolean[]) { String s = Arrays.toString((boolean[]) t); return "{" + s.substring(1, s.length() - 1) + "}"; } try { return ts((Object[]) t); } catch (ClassCastException e1) { return t.toString(); } } } private static <T> String ts(T[] arr) { StringBuilder ret = new StringBuilder(); ret.append("{"); boolean first = true; for (T t : arr) { if (!first) { ret.append(", "); } first = false; ret.append(ts(t)); } ret.append("}"); return ret.toString(); } private static <T> String ts(Iterable<T> iter) { StringBuilder ret = new StringBuilder(); ret.append("{"); boolean first = true; for (T t : iter) { if (!first) { ret.append(", "); } first = false; ret.append(ts(t)); } ret.append("}"); return ret.toString(); } public static void dbg(Object... o) { if (LOCAL) { System.err.print("Line #" + Thread.currentThread().getStackTrace()[2].getLineNumber() + ": ["); for (int i = 0; i < o.length; i++) { if (i != 0) { System.err.print(", "); } System.err.print(ts(o[i])); } System.err.println("]"); } } 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 class Pair implements Comparable<Pair> { private int index; private int val; private int cor; public Pair(int index, int val, int cor) { this.index = index; this.val = val; this.cor = cor; } @Override public String toString() { return "Pair{" + "x=" + index + ", y=" + val + '}'; } @Override public int compareTo(Pair o) { return 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 8
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
5a06a45bee0ba26c0778909f17ea0c08
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 Problem2 { public static void main(String[] args) { MyScanner sc = new MyScanner(); out = new PrintWriter(new BufferedOutputStream(System.out)); int n = sc.nextInt(); int q = sc.nextInt(); char[] arr = sc.nextLine().toCharArray(); int curNum = 0; for(int i = 0; i < n-2; i++) { if(arr[i] == 'a' && arr[i+1] == 'b' && arr[i+2] == 'c') { curNum++; } } for(int i = 0; i < q; i++) { int index = sc.nextInt()-1; char c = sc.next().charAt(0); if(checkAdd(arr, index, c)) { curNum++; } if(checkRemove(arr, index, c)) { curNum--; } arr[index] = c; System.out.println(curNum); } out.close(); } public static boolean checkRemove(char[] arr, int index, char c) { if(arr[index] == c) { return false; } if(arr[index] == 'a' && index < arr.length-2) { if(arr[index+1] == 'b' && arr[index+2] == 'c') return true; } if(arr[index] == 'b' && index != arr.length-1 && index != 0) { if(arr[index-1] == 'a' && arr[index+1] == 'c') return true; } if(arr[index] == 'c' && index > 1) { if(arr[index-1] == 'b' && arr[index-2] == 'a') return true; } return false; } public static boolean checkAdd(char[] arr, int index, char c) { if(arr[index] == c) { return false; } if(c == 'a' && index < arr.length-2) { if(arr[index+1] == 'b' && arr[index+2] == 'c') return true; } if(c == 'b' && index != arr.length-1 && index != 0) { if(arr[index-1] == 'a' && arr[index+1] == 'c') return true; } if(c == 'c' && index > 1) { if(arr[index-1] == 'b' && arr[index-2] == 'a') return true; } return false; } public static PrintWriter out; 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 class Pair { public int x,y; public Pair(int x, int y) { this.x = x; this.y = y; } } }
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 8
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
b30fc2e8882e66810be5c2224decf0c3
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 A { //--------------------------INPUT READER---------------------------------// static class fs { public BufferedReader br; StringTokenizer st = new StringTokenizer(""); public fs() { this(System.in); } public fs(InputStream is) { br = new BufferedReader(new InputStreamReader(is)); } String next() { while (!st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int ni() { return Integer.parseInt(next()); } long nl() { return Long.parseLong(next()); } double nd() { return Double.parseDouble(next()); } String ns() { return next(); } int[] na(long nn) { int n = (int) nn; int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = ni(); return a; } long[] nal(long nn) { int n = (int) nn; long[] l = new long[n]; for(int i = 0; i < n; i++) l[i] = nl(); return l; } } //-----------------------------------------------------------------------// //---------------------------PRINTER-------------------------------------// static class Printer { static PrintWriter w; public Printer() {this(System.out);} public Printer(OutputStream os) { w = new PrintWriter(os); } public void p(int i) {w.println(i);} public void p(long l) {w.println(l);} public void p(double d) {w.println(d);} public void p(String s) { w.println(s);} public void pr(int i) {w.print(i);} public void pr(long l) {w.print(l);} public void pr(double d) {w.print(d);} public void pr(String s) { w.print(s);} public void pl() {w.println();} public void close() {w.close();} } //-----------------------------------------------------------------------// //--------------------------VARIABLES------------------------------------// static fs sc = new fs(); static OutputStream outputStream = System.out; static Printer w = new Printer(outputStream); static long lma = Long.MAX_VALUE, lmi = Long.MIN_VALUE; static long ima = Integer.MAX_VALUE, imi = Integer.MIN_VALUE; static long mod = 1000000007; //-----------------------------------------------------------------------// //--------------------------ADMIN_MODE-----------------------------------// private static void ADMIN_MODE() throws IOException { if (System.getProperty("ONLINE_JUDGE") == null) { w = new Printer(new FileOutputStream("output.txt")); sc = new fs(new FileInputStream("input.txt")); } } //-----------------------------------------------------------------------// //----------------------------START--------------------------------------// public static void main(String[] args) throws IOException { ADMIN_MODE(); // int t = sc.ni();while(t-->0) solve(); w.close(); } static void solve() throws IOException { int n = sc.ni(), q = sc.ni(); String str = sc.ns(); char[] strr = str.toCharArray(); TreeSet<Integer> st = new TreeSet<>(), end = new TreeSet<>(); int ct = 0; for(int i = 0; i < n-2; i++) { if(strr[i]=='a' && strr[i+1]=='b' && strr[i+2] == 'c') { ct++; } } /*for(int i = 0; i < n-2; i++) { if(strr[i]=='a' && strr[i+1]=='b' && strr[i+2]=='c') { end.add(i+2); } }*/ while(q--> 0) { int id = sc.ni()-1; char ch = sc.ns().charAt(0); if(strr[id]==ch) { w.p(ct); continue; } boolean f = false; if(strr[id] =='a' && id < n-2 && strr[id+1] == 'b' && strr[id+2] == 'c') { f = true; } else if(strr[id] == 'b' && id < n-1 && strr[id+1] == 'c' && id > 0 && strr[id-1] == 'a') { f = true; } else if(strr[id] == 'c' && id > 1 && strr[id-1] == 'b' && strr[id-2] == 'a') { f = true; } strr[id] = ch; if(ch=='a' && id < n-2 && strr[id+1] == 'b' && strr[id+2] == 'c') { if(!f)ct++; w.p(ct); } else if(ch == 'b' && id < n-1 && strr[id+1] == 'c' && id > 0 && strr[id-1] == 'a') { if(!f)ct++; w.p(ct); } else if(ch == 'c' && id > 1 && strr[id-1] == 'b' && strr[id-2] == 'a') { if(!f)ct++; w.p(ct); } else { if(f) ct--; w.p(ct); } } } }
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 8
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
0280c60369275f21c7301589c4b60fdf
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.io.OutputStream; import java.io.IOException; import java.util.InputMismatchException; import java.io.InputStreamReader; import java.util.TreeSet; import java.io.Writer; import java.io.BufferedReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Nipuna Samarasekara */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastScanner in = new FastScanner(inputStream); FastPrinter out = new FastPrinter(outputStream); TaskB solver = new TaskB(); solver.solve(1, in, out); out.close(); } static class TaskB { public void solve(int testNumber, FastScanner in, FastPrinter out) { int n = in.nextInt(), q = in.nextInt(); String s = in.next(); char[] ss = new char[n]; for (int i = 0; i < n; i++) { ss[i] = s.charAt(i); } TreeSet<Integer> al = new TreeSet<>(); for (int i = 0; i < n - 2; i++) { if (s.charAt(i) == 'a' && s.charAt(i + 1) == 'b' && s.charAt(i + 2) == 'c') { al.add(i); } } while (q-- > 0) { int i = in.nextInt() - 1; char c = in.nextChar(); if (ss[i] == c) { out.println(al.size()); } else { ss[i] = c; for (int j = 0; j < 3; j++) { if (al.contains(i - j)) { al.remove(i - j); break; } } if (c == 'a' && i + 2 < n) { if (ss[i + 1] == 'b' && ss[i + 2] == 'c') { al.add(i); } } if (c == 'b' && i + 1 < n && i - 1 >= 0) { if (ss[i - 1] == 'a' && ss[i + 1] == 'c') { al.add(i - 1); } } if (c == 'c' && i - 2 >= 0) { if (ss[i - 2] == 'a' && ss[i - 1] == 'b') { al.add(i - 2); } } out.println(al.size()); } } } } static class FastPrinter extends PrintWriter { public FastPrinter(OutputStream out) { super(out); } public FastPrinter(Writer out) { super(out); } } static class FastScanner extends BufferedReader { public FastScanner(InputStream is) { super(new InputStreamReader(is)); } public int read() { try { int ret = super.read(); // if (isEOF && ret < 0) { // throw new InputMismatchException(); // } // isEOF = ret == -1; return ret; } catch (IOException e) { throw new InputMismatchException(); } } public String next() { StringBuilder sb = new StringBuilder(); int c = read(); while (isWhiteSpace(c)) { c = read(); } if (c < 0) { return null; } while (c >= 0 && !isWhiteSpace(c)) { sb.appendCodePoint(c); c = read(); } return sb.toString(); } static boolean isWhiteSpace(int c) { return c >= 0 && c <= 32; } public int nextInt() { int c = read(); while (isWhiteSpace(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int ret = 0; while (c >= 0 && !isWhiteSpace(c)) { if (c < '0' || c > '9') { throw new NumberFormatException("digit expected " + (char) c + " found"); } ret = ret * 10 + c - '0'; c = read(); } return ret * sgn; } public char nextChar() { int c = read(); while (isWhiteSpace(c)) { c = read(); } return (char) c; } public String readLine() { try { return super.readLine(); } catch (IOException e) { return null; } } } }
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 8
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
0ccfc97e715c7b4d511b062348a87a97
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.PrintWriter; // System.out is a PrintStream import java.util.InputMismatchException; public class B { private static int MOD = (int)1e9 + 7, mod = 99_82_44_353; private static double PI = 3.14159265358979323846; public static void main(String[] args) throws IOException { FasterScanner scn = new FasterScanner(); PrintWriter out = new PrintWriter(System.out); int N = scn.nextInt(), Q = scn.nextInt(), count = 0; char[] str = scn.next().toCharArray(); for (int i = 2; i < N; i++) { if (str[i - 2] == 'a' && str[i - 1] == 'b' && str[i] == 'c') { count++; } } while (Q-- > 0) { int pos = scn.nextInt() - 1; char ch = scn.nextChar(); if (str[pos] == 'a') { if (pos < N - 2 && str[pos + 1] == 'b' && str[pos + 2] == 'c') { count--; } } else if (str[pos] == 'b') { if (pos > 0 && pos < N - 1 && str[pos - 1] == 'a' && str[pos + 1] == 'c') { count--; } } else if (str[pos] == 'c') { if (pos > 1 && str[pos - 2] == 'a' && str[pos - 1] == 'b') { count--; } } str[pos] = ch; if (str[pos] == 'a') { if (pos < N - 2 && str[pos + 1] == 'b' && str[pos + 2] == 'c') { count++; } } else if (str[pos] == 'b') { if (pos > 0 && pos < N - 1 && str[pos - 1] == 'a' && str[pos + 1] == 'c') { count++; } } else if (str[pos] == 'c') { if (pos > 1 && str[pos - 2] == 'a' && str[pos - 1] == 'b') { count++; } } out.println(count); } out.close(); } /* ------------------- Sorting ------------------- */ private static void ruffleSort(int[] arr) { // int N = arr.length; // Random rand = new Random(); // for (int i = 0; i < N; i++) { // int oi = rand.nextInt(N), temp = arr[i]; // arr[i] = arr[oi]; // arr[oi] = temp; // } // Arrays.sort(arr); } /* ------------------- Sorting ------------------- */ private static boolean isPalindrome(String str) { for (int i = 0, j = str.length() - 1; i < j; i++, j--) { if (str.charAt(i) != str.charAt(j)) { return false; } } return true; } private static boolean isPalindrome(char[] str) { for (int i = 0, j = str.length - 1; i < j; i++, j--) { if (str[i] != str[j]) { return false; } } return true; } /* ------------------- Pair class ------------------- */ private static class Pair implements Comparable<Pair> { int x, y; Pair(int x, int y) { this.x = x; this.y = y; } public int compareTo(Pair o) { return this.x == o.x ? this.y - o.y : this.x - o.x; } } /* ------------------- HCF and LCM ------------------- */ private static int gcd(int num1, int num2) { int temp = 0; while (num2 != 0) { temp = num1; num1 = num2; num2 = temp % num2; } return num1; } private static int lcm(int num1, int num2) { return (int)((1L * num1 * num2) / gcd(num1, num2)); } /* ------------------- primes and prime factorization ------------------- */ private static boolean[] seive(int N) { // true means not prime, false means is a prime number :) boolean[] notPrimes = new boolean[N + 1]; notPrimes[0] = notPrimes[1] = true; for (int i = 2; i * i <= N; i++) { if (notPrimes[i]) continue; for (int j = i * i; j <= N; j += i) { notPrimes[j] = true; } } return notPrimes; } /* private static TreeMap<Integer, Integer> primeFactors(long N) { TreeMap<Integer, Integer> primeFact = new TreeMap<>(); for (int i = 2; i <= Math.sqrt(N); i++) { int count = 0; while (N % i == 0) { N /= i; count++; } if (count != 0) { primeFact.put(i, count); } } if (N != 1) { primeFact.put((int)N, 1); } return primeFact; } */ /* ------------------- Binary Search ------------------- */ private static long factorial(int N) { long ans = 1L; for (int i = 2; i <= N; i++) { ans *= i; } return ans; } private static long[] factorialDP(int N) { long[] factDP = new long[N + 1]; factDP[0] = factDP[1] = 1; for (int i = 2; i <= N; i++) { factDP[i] = factDP[i - 1] * i; } return factDP; } private static long factorialMod(int N, int mod) { long ans = 1L; for (int i = 2; i <= N; i++) { ans = ((ans % mod) * (i % mod)) % mod; } return ans; } /* ------------------- Binary Search ------------------- */ private static int floorSearch(int[] arr, int st, int ed, int tar) { int ans = -1; while (st <= ed) { int mid = ((ed - st) >> 1) + st; if (arr[mid] <= tar) { ans = mid; st = mid + 1; } else { ed = mid - 1; } } return ans; } private static int ceilingSearch(int[] arr, int st, int ed, int tar) { int ans = -1; while (st <= ed) { int mid = ((ed - st) >> 1) + st; if (arr[mid] < tar) { st = mid + 1; } else { ans = mid; ed = mid - 1; } } return ans; } /* ------------------- Power Function ------------------- */ public static long pow(long x, long y) { long res = 1; while (y > 0) { if ((y & 1) != 0) { res = (res * x); y--; } x = (x * x); y >>= 1; } return res; } public static long powMod(long x, long y, int mod) { long res = 1; while (y > 0) { if ((y & 1) != 0) { res = (res * x) % mod; y--; } x = (x * x) % mod; y >>= 1; } return res % mod; } /* ------------------- Disjoint Set(Union and Find) ------------------- */ private static class DSU { public int[] parent, rank; DSU(int N) { parent = new int[N]; rank = new int[N]; for (int i = 0; i < N; i++) { parent[i] = i; rank[i] = 1; } } public int find(int a) { if (parent[a] == a) { return a; } return parent[a] = find(parent[a]); } public boolean union(int a, int b) { int parA = find(a), parB = find(b); if (parA == parB) return false; if (rank[parA] > rank[parB]) { parent[parB] = parA; } else if (rank[parA] < rank[parB]) { parent[parA] = parB; } else { parent[parA] = parB; rank[parB]++; } return true; } } /* ------------------- Scanner class for input ------------------- */ private static class FasterScanner { private InputStream stream; private byte[] buffer = new byte[1024]; private int curChar, numChars; private SpaceCharFilter filter; public FasterScanner() { // default this.stream = System.in; } public FasterScanner(InputStream stream) { this.stream = stream; } private int readChar() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buffer); } catch (IOException e) { throw new InputMismatchException(e.getMessage()); } if (numChars <= 0) { return -1; } } return buffer[curChar++]; } private boolean isWhiteSpace(int c) { if (filter != null) { return filter.isWhiteSpace(c); } return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isNewLine(int ch) { if (filter != null) { return filter.isNewLine(ch); } return ch == '\r' || ch == '\n' || ch == -1; } public char nextChar() { int ch = readChar(); char res = '\0'; while (isWhiteSpace(ch)) { ch = readChar(); } do { res = (char)ch; ch = readChar(); } while (!isWhiteSpace(ch)); return res; } public String next() { int ch = readChar(); StringBuilder res = new StringBuilder(); while (isWhiteSpace(ch)) { ch = readChar(); } do { res.appendCodePoint(ch); ch = readChar(); } while (!isWhiteSpace(ch)); return res.toString(); } public String nextLine() { int ch = readChar(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(ch); ch = readChar(); } while (!isNewLine(ch)); return res.toString(); } public int nextInt() { int ch = -1, sgn = 1, res = 0; do { ch = readChar(); } while (isWhiteSpace(ch)); if (ch == '-') { sgn = -1; ch = readChar(); } do { if (ch < '0' || ch > '9') { throw new InputMismatchException("not a number"); } res = (res * 10) + (ch - '0'); ch = readChar(); } while (!isWhiteSpace(ch)); return res * sgn; } public long nextLong() { int ch = -1, sgn = 1; long res = 0L; do { ch = readChar(); } while (isWhiteSpace(ch)); if (ch == '-') { sgn = -1; ch = readChar(); } do { if (ch < '0' || ch > '9') { throw new InputMismatchException("not a number"); } res = (10L * res) + (ch - '0'); ch = readChar(); } while (!isWhiteSpace(ch)); return 1L * res * sgn; } public double nextDouble() { return Double.parseDouble(next()); } /* for custom delimiters */ public interface SpaceCharFilter { public boolean isWhiteSpace(int ch); public boolean isNewLine(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 8
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
9ff196fd6c914e6fda057d570432c4de
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 A { FastScanner in; PrintWriter out; boolean systemIO = true; public class Fenvik { long[] sum; public Fenvik(int n) { sum = new long[n]; } public void add(int x, long d) { for (; x < sum.length; x = (x | (x + 1))) { sum[x] += d; } } public long sum(int r) { long ans = 0; for (; r >= 0; r = (r & (r + 1)) - 1) { ans += sum[r]; } return ans; } } public class Pair { int x; int y; public Pair(int x, int y) { this.x = x; this.y = y; } } public int gcd(int x, int y) { if (y == 0) { return x; } if (x == 0) { return y; } return gcd(y, x % y); } public class Edge { int to; long s; public Edge(int to, long s) { this.to = to; this.s = s; } } public long dfsLCA(int v, int prev, long sumth, long minsum, long s) { tin[v] = timer; timer++; up[v][0] = new Edge(prev, s); for (int i = 1; i <= l; i++) { Edge e = up[v][i - 1]; up[v][i] = new Edge(up[e.to][i - 1].to, up[e.to][i - 1].s + e.s); } minsum = Math.min(minsum, sumth); maxup[v] = sumth - minsum; long mxdown = sumth; for (Edge e : list[v]) { if (e.to != prev) { mxdown = Math.max(mxdown, dfsLCA(e.to, v, sumth + e.s, minsum, e.s)); } } tout[v] = timer; timer++; maxdown[v] = mxdown - sumth; return mxdown; } public boolean upper(int a1, int b1) { return tin[a1] <= tin[b1] && tout[a1] >= tout[b1]; } public Edge lca(int a, int b) { if (a == b) { return new Edge(a, 0); } int v = -1; int a1 = a; int b1 = b; if (tin[a] <= tin[b] && tout[a] >= tout[b]) { v = b; long lenb = 0; for (int i = l; i >= 0; i--) { a1 = up[v][i].to; b1 = a; if (!(tin[a1] <= tin[b1] && tout[a1] >= tout[b1])) { lenb += up[v][i].s; v = up[v][i].to; } } lenb += up[v][0].s; v = up[v][0].to; return new Edge(v, lenb); } if (upper(b, a)) { v = a; long lena = 0; for (int i = l; i >= 0; i--) { a1 = up[v][i].to; b1 = b; if (!(tin[a1] <= tin[b1] && tout[a1] >= tout[b1])) { lena += up[v][i].s; v = up[v][i].to; } } lena += up[v][0].s; v = up[v][0].to; return new Edge(v, lena); } v = a; long lena = 0; for (int i = l; i >= 0; i--) { a1 = up[v][i].to; b1 = b; if (!(tin[a1] <= tin[b1] && tout[a1] >= tout[b1])) { lena += up[v][i].s; v = up[v][i].to; } } lena += up[v][0].s; v = up[v][0].to; v = b; long lenb = 0; for (int i = l; i >= 0; i--) { a1 = up[v][i].to; b1 = a; if (!(tin[a1] <= tin[b1] && tout[a1] >= tout[b1])) { lenb += up[v][i].s; v = up[v][i].to; } } lenb += up[v][0].s; v = up[v][0].to; return new Edge(v, lena + lenb); } int n; int l; int[] tin; int[] tout; int timer = 0; long[] maxup; long[] maxdown; Edge[][] up; ArrayList<Edge>[] list; public int signum(long x) { if (x > 0) { return 1; } if (x < 0) { return -1; } return 0; } public class Point implements Comparable<Point> { long x; long y; public Point(long x, long y) { this.x = x; this.y = y; } public String toString() { return x + " " + y; } public boolean equals(Point p) { return x == p.x && y == p.y; } public long sqDist(Point o) { return (x - o.x) * (x - o.x) + (y - o.y) * (y - o.y); } public void subtract(Point p) { x -= p.x; y -= p.y; } @Override public int compareTo(Point o) { if (x != o.x) { return signum(x - o.x); } return signum(y - o.y); } } public class DSU { int[] p; int[] sz; public DSU(int n) { p = new int[n]; sz = new int[n]; for (int i = 0; i < p.length; i++) { p[i] = i; sz[i] = 1; } } public int get(int v) { if (p[v] == v) { return v; } return p[v] = get(p[v]); } public boolean unite(int a, int b) { a = get(a); b = get(b); if (a == b) { return false; } if (sz[a] < sz[b]) { sz[b] += sz[a]; p[a] = b; } else { sz[a] += sz[b]; p[b] = a; } return true; } } public class Arr implements Comparable<Arr> { int[] a; public Arr(int[] a) { this.a = a; } @Override public int compareTo(Arr o) { return a[0] - o.a[0]; } } public class Segment implements Comparable<Segment> { double quantity; int color; int number; public Segment(double quantity, int color, int number) { this.quantity = quantity; this.color = color; this.number = number; } @Override public int compareTo(Segment o) { return (int) Math.signum(o.quantity - quantity); } } public void solve() { int n = in.nextInt(); int q = in.nextInt(); String s = in.next(); int[] a = new int[n]; for (int i = 0; i < a.length; i++) { a[i] = s.charAt(i) - 'a'; } int ans = 0; for (int i = 0; i + 2 < a.length; i++) { if (a[i] == 0 && a[i + 1] == 1 && a[i + 2] == 2) { ans++; } } for (int i = 0; i < q; i++) { int x = in.nextInt() - 1; for (int j = Math.max(0, x - 2); j <= x && j + 2 < a.length; j++) { if (a[j] == 0 && a[j + 1] == 1 && a[j + 2] == 2) { ans--; } } a[x] = in.next().charAt(0) - 'a'; for (int j = Math.max(0, x - 2); j <= x && j + 2 < a.length; j++) { if (a[j] == 0 && a[j + 1] == 1 && a[j + 2] == 2) { ans++; } } out.println(ans); } } public void run() { try { if (systemIO) { in = new FastScanner(System.in); out = new PrintWriter(System.out); } else { in = new FastScanner(new File("input.txt")); out = new PrintWriter(new File("output.txt")); } solve(); out.close(); } catch (IOException e) { e.printStackTrace(); } } class FastScanner { BufferedReader br; StringTokenizer st; FastScanner(File f) { try { br = new BufferedReader(new FileReader(f)); } catch (FileNotFoundException e) { e.printStackTrace(); } } FastScanner(InputStream f) { br = new BufferedReader(new InputStreamReader(f)); } String nextLine() { try { return br.readLine(); } catch (IOException e) { return null; } } 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()); } } // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA public static void main(String[] arg) { new A().run(); } }
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 8
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
366f297302a6df24540295436cfb4060
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.StringTokenizer; public class William_the_Vigilant { static 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(); } } public static PrintWriter out; public static void main (String[] args) throws java.lang.Exception { out = new PrintWriter(System.out); try { Scanner obj = new Scanner (System.in); //Reader obj = new Reader (); int n = obj.nextInt(); int q = obj.nextInt(); String str = obj.next(); char[] ch = str.toCharArray(); int i, num = 0; for (i=0;i<n;i++) { if (i < n-2 && str.substring (i, i+3).equals ("abc")) { num++; } } while (q > 0) { q--; i = obj.nextInt() - 1; String s = obj.next(); char c = s.charAt(0); if (ch[i] == c) { out.println (num); } else { if (ch[i] == 'a') { if (i < n-2 && ch[i+1] == 'b' && ch[i+2] == 'c') { num--; } } if (ch[i] == 'b') { if (i != 0 && i != n-1 && ch[i-1] == 'a' && ch[i+1] == 'c') { num--; } } if (ch[i] == 'c') { if (i > 1 && ch[i-1] == 'b' && ch[i-2] == 'a') { num--; } } ch[i] = c; if (ch[i] == 'a') { if (i < n-2 && ch[i+1] == 'b' && ch[i+2] == 'c') { num++; } } if (ch[i] == 'b') { if (i != 0 && i != n-1 && ch[i-1] == 'a' && ch[i+1] == 'c') { num++; } } if (ch[i] == 'c') { if (i > 1 && ch[i-1] == 'b' && ch[i-2] == 'a') { num++; } } out.println (num); } } }catch(Exception e){ return; } 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 8
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
98f798ba303aab5c46d3ab74d1e96de2
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) { Scanner in = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int n = in.nextInt(), query = in.nextInt(); char ch[] = in.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++; } } for(int q = 0;q<query;q++){ int index = in.nextInt()-1; char c = in.next().charAt(0); int from = Math.max(2,index); int to = Math.min(n-1,index+2); int dif = 0; for (int i = from;i<=to;i++){ if(ch[i-2]=='a' && ch[i-1]=='b' && ch[i]=='c'){ dif--; } } ch[index] = c; for (int i = from;i<=to;i++){ if(ch[i-2]=='a' && ch[i-1]=='b' && ch[i]=='c'){ dif++; } } count+=dif; pw.println(count); } pw.close(); } static void debug(Object... obj) { System.err.println(Arrays.deepToString(obj)); } }
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 8
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
79fdda8c742e308e72813e0afbaaa2f6
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 Scanner sc = new Scanner(new BufferedInputStream(System.in)); static int sc(){return sc.nextInt();} static long scl(){return sc.nextLong();} static double scd(){return sc.nextDouble();}; static String scs(){return sc.next();}; static char scf(){return scs().charAt(0);} static char[] carr(){return sc.next().toCharArray();}; static char[] chars(){ char[] s = carr(); char[] c = new char[s.length+1]; System.arraycopy(s,0,c,1,s.length); return c; } static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)); static void println (Object o) {out.println(o);} static void println() {out.println();} static void print(Object o) {out.print(o);} static int[] dx = new int[]{-1,0,1,0},dy = new int[]{0,-1,0,1}; static final int N = (int)2e6+10,M = 2010,INF = 0x3f3f3f3f,P = 131,MOD = (int)1e9+7; static int n; // static int[] h = naew int[N],e = new int[N],ne = new int[N],w = new int[N]; // static int idx; // static int[] a = new int[N]; static String abc = "abc"; public static void main(String[] args) throws Exception{ n = sc(); int q = sc(); String s = scs(); int len = 0; for(int i = 0;i<=n-3;i++){ if(s.substring(i,i+3).equals(abc)){ len++; i+=2; } } char[] c = s.toCharArray(); while(q-->0){ int pos = sc();char u = scf(); char temp = c[pos-1]; int l = pos-1-temp+'a'; boolean f = false; if(l>=0&&l+2<n){ if(c[l] == 'a'&&c[l+1] == 'b'&&c[l+2] == 'c') f = true; } if(u == temp) println(len); else{ c[pos-1] = u; l = pos-1-u+'a'; if(l>=0&&l+2<n){ if(c[l] == 'a'&&c[l+1] == 'b'&&c[l+2] == 'c') len++; } if(f) len--; println(len); } } out.close(); } static class D{ int x,y; D(int x,int y){this.x = x;this.y = y;} } }
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 8
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
3041e8e761d09b6d7c2f62840d50f3cd
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 B1609 { public static void main(String[] args) { Scanner in = new Scanner(System.in); int N = in.nextInt(); int Q = in.nextInt(); char[] S = in.next().toCharArray(); int count = 0; for (int n=2; n<N; n++) { if (S[n-2] == 'a' && S[n-1] == 'b' && S[n] == 'c') { count++; } } StringBuilder output = new StringBuilder(); for (int q=0; q<Q; q++) { int idx = in.nextInt()-1; char c = in.next().charAt(0); int diff = 0; int from = Math.max(idx, 2); int to = Math.min(N-1,idx+2); for (int i=from; i<=to; i++) { if (S[i-2] == 'a' && S[i-1] == 'b' && S[i] == 'c') { diff--; } } S[idx] = c; for (int i=from; i<=to; i++) { if (S[i-2] == 'a' && S[i-1] == 'b' && S[i] == 'c') { diff++; } } count += diff; output.append(count).append('\n'); } System.out.print(output); } }
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 8
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
c91a905be81890f43851fbaed73ceefa
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 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 n = i(); int q = i(); String s = in.nextLine(); char[] ch = s.toCharArray(); int total = 0; for(int i = 0;i < n-2;i++){ if(ch[i] == 'a' && ch[i+1] == 'b' && ch[i+2] == 'c'){ total++; } } for(int i = 0;i < q;i++){ int pos = i() - 1; char c = in.next().charAt(0); if(ch[pos] == c){ System.out.println(total); }else{ int change = 0; for(int j = Math.max(0,pos-2);j <= Math.min(pos,n-3);j++){ if(ch[j] == 'a' && ch[j+1] == 'b' && ch[j+2] == 'c'){ change--; } } ch[pos] = c; for(int j = Math.max(0,pos-2);j <= Math.min(pos,n-3);j++){ if(ch[j] == 'a' && ch[j+1] == 'b' && ch[j+2] == 'c'){ change++; } } total += change; System.out.println(total); } } } 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
["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 8
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
81a070d80a700d08f4cf833fdd79cdc7
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 WilliamTheWigilant{ static long mod = 1000000007L; static MyScanner sc = new MyScanner(); static void solve(){ int n = sc.nextInt(); int q = sc.nextInt(); char arr[] = sc.nextLine().toCharArray(); int count = 0; for(int i = 0;i<n;){ if(arr[i]=='a' && i+1<n && arr[i+1]=='b' && i+2<n && arr[i+2]=='c'){ count++; i+=3; }else{ i++; } } for(int i = 0;i<q;i++){ int ind = sc.nextInt(); ind--; char c = sc.next().charAt(0); if(arr[ind]==c){ out.println(count+" "); }else{ if(arr[ind]=='a' && ind+1<n && arr[ind+1]=='b' && ind+2<n && arr[ind+2]=='c'){ count--; }else if(arr[ind]=='b' && ind-1>=0 && arr[ind-1]=='a' && ind+1<n && arr[ind+1]=='c'){ count--; }else if(arr[ind]=='c' && ind-1>=0 && arr[ind-1]=='b' && ind-2>=0 && arr[ind-2]=='a'){ count--; } arr[ind] = c; if(arr[ind]=='a' && ind+1<n && arr[ind+1]=='b' && ind+2<n && arr[ind+2]=='c'){ count++; }else if(arr[ind]=='b' && ind-1>=0 && arr[ind-1]=='a' && ind+1<n && arr[ind+1]=='c'){ count++; }else if(arr[ind]=='c' && ind-1>=0 && arr[ind-1]=='b' && ind-2>=0 && arr[ind-2]=='a'){ count++; } if(count<0) count= 0; out.println(count+" "); } } } 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 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 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; int ans; Pair(int v,int f){ val = v; ind = f; } public int compareTo(Pair p){ return p.val - this.val; } } public static void main(String[] args) { out = new PrintWriter(new BufferedOutputStream(System.out)); // int t = sc.nextInt(); int t= 1; while(t-- >0){ // solve(); 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(); } 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 8
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
814e7cb742f681ad82c036fa70dc2fd9
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 code { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n=sc.nextInt(); int q=sc.nextInt(); StringBuilder s=new StringBuilder(sc.next()); int count=0; for (int j = 2; j<n; j++) { if(s.charAt(j-2)=='a' && s.charAt(j-1)=='b' && s.charAt(j)=='c') count++; } for (int j = 0; j < q ; j++) { int ind=sc.nextInt()-1; char ch=sc.next().charAt(0); if(ind-2>=0 && s.charAt(ind-2)=='a' && s.charAt(ind-1)=='b' && s.charAt(ind)=='c') count--; if(ind-1>=0 && ind+1<n && s.charAt(ind-1)=='a' && s.charAt(ind)=='b' && s.charAt(ind+1)=='c') count--; if(ind+2<n && s.charAt(ind)=='a' && s.charAt(ind+1)=='b' && s.charAt(ind+2)=='c') count--; s.setCharAt(ind,ch); if(ind-2>=0 && s.charAt(ind-2)=='a' && s.charAt(ind-1)=='b' && s.charAt(ind)=='c') count++; if(ind-1>=0 && ind+1<n && s.charAt(ind-1)=='a' && s.charAt(ind)=='b' && s.charAt(ind+1)=='c') count++; if(ind+2<n && s.charAt(ind)=='a' && s.charAt(ind+1)=='b' && s.charAt(ind+2)=='c') count++; 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 8
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
cb60e390397ed120e09421891f528a72
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 WilliamTheVigilant { public static void main(String[] args) { new WilliamTheVigilant().run(); } BufferedReader br; PrintWriter out; long mod = (long) (1e9 + 7), inf = (long) (3e18); class pair { int F, S; pair(int f, int s) { F = f; S = s; } } void solve() { int n= ni(); int q= ni(); char[] s= ns().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=ni()-1; char c= 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); } } } // -------- I/O Template ------------- char nc() { return ns().charAt(0); } String nLine() { try { return br.readLine(); } catch(IOException e) { return "-1"; } } double nd() { return Double.parseDouble(ns()); } long nl() { return Long.parseLong(ns()); } int ni() { return Integer.parseInt(ns()); } int[] na(int n) { int a[] = new int[n]; for(int i = 0; i < n; i++) a[i] = ni(); return a; } StringTokenizer ip; String ns() { if(ip == null || !ip.hasMoreTokens()) { try { ip = new StringTokenizer(br.readLine()); if(ip == null || !ip.hasMoreTokens()) ip = new StringTokenizer(br.readLine()); } catch(IOException e) { throw new InputMismatchException(); } } return ip.nextToken(); } void run() { try { if (System.getProperty("ONLINE_JUDGE") == null) { br = new BufferedReader(new FileReader("/media/ankanchanda/Data1/WORKPLACE/DS and CP/Competitive Programming/VSCODE/IO/input.txt")); out = new PrintWriter("/media/ankanchanda/Data1/WORKPLACE/DS and CP/Competitive Programming/VSCODE/IO/output.txt"); } else { br = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); } } catch (FileNotFoundException e) { System.out.println(e); } solve(); 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 8
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
a0cd9209a3e9a194dc0473861b2ccb41
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 { public static void main(String[] args) throws Exception { int tc = 1; for (int i = 0; i < tc; i++) { solve(); } io.close(); } private static void solve() throws Exception { int n = io.nextInt(); int q = io.nextInt(); char[] word = io.next().toCharArray(); int abc = 0; for (int i = 0; i < word.length - 2; i++) { if (word[i] == 'a' && word[i + 1] == 'b' && word[i + 2] == 'c') { abc++; } } int[][] offset = new int[][]{ {0, 1, 2}, {-1, 0, 1}, {-2, -1, 0} }; for (int i = 0; i < q; i++) { int index = io.nextInt() - 1; char c = io.next().charAt(0); boolean prevABC = true; int[] currCon = offset[word[index] - 'a']; for (int j = 0; j < 3; j++) { if (index + currCon[j] < 0 || index + currCon[j] >= n || word[index + currCon[j]] != (char) ('a' + j)) { prevABC = false; break; } } if (prevABC) abc--; word[index] = c; currCon = offset[c - 'a']; boolean currABC = true; for (int j = 0; j < 3; j++) { if (index + currCon[j] < 0 || index + currCon[j] >= n || word[index + currCon[j]] != (char) ('a' + j)) { currABC = false; break; } } if (currABC) abc++; io.println(abc); } } static void sort(int[] a) { ArrayList<Integer> l = new ArrayList<>(a.length); for (int i : a) l.add(i); Collections.sort(l); for (int i = 0; i < a.length; i++) a[i] = l.get(i); } //-----------PrintWriter for faster output--------------------------------- public static FastIO io = new FastIO(); //-----------MyScanner class for faster input---------- static class FastIO extends PrintWriter { private InputStream stream; private byte[] buf = new byte[1 << 16]; private int curChar, numChars; // standard input public FastIO() { this(System.in, System.out); } public FastIO(InputStream i, OutputStream o) { super(o); stream = i; } // file input public FastIO(String i, String o) throws IOException { super(new FileWriter(o)); stream = new FileInputStream(i); } // throws InputMismatchException() if previously detected end of file private int nextByte() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars == -1) return -1; // end of file } return buf[curChar++]; } // to read in entire lines, replace c <= ' ' // with a function that checks whether c is a line break public String next() { int c; do { c = nextByte(); } while (c <= ' '); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = nextByte(); } while (c > ' '); return res.toString(); } public String nextLine() { int c; do { c = nextByte(); } while (c < '\n'); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = nextByte(); } while (c > '\n'); return res.toString(); } public int nextInt() { int c; do { c = nextByte(); } while (c <= ' '); int sgn = 1; if (c == '-') { sgn = -1; c = nextByte(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res = 10 * res + c - '0'; c = nextByte(); } while (c > ' '); return res * sgn; } public long nextLong() { int c; do { c = nextByte(); } while (c <= ' '); int sgn = 1; if (c == '-') { sgn = -1; c = nextByte(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res = 10 * res + c - '0'; c = nextByte(); } while (c > ' '); return res * sgn; } public double nextDouble() { return Double.parseDouble(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 8
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
20f79e6cc04e78d6666037b377280c80
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.*; import java.math.BigInteger; public final class A { static PrintWriter out = new PrintWriter(System.out); static StringBuilder ans=new StringBuilder(); static FastReader in=new FastReader(); static ArrayList<Integer> g[]; static long mod=(long) 998244353,INF=Long.MAX_VALUE; static boolean set[]; static int par[],col[],D[]; static long A[]; public static void main(String args[])throws IOException { /* * star,rope,TPST * BS,LST,MS,MQ */ int N=i(),Q=i(); char X[]=in.next().toCharArray(); int tot=0; for(int i=0; i<N; i++) { if(X[i]=='a' && i+2<N) { if(X[i+1]=='b' && X[i+2]=='c')tot++; } } while(Q-->0) { int i=i()-1; char x=in.next().charAt(0); if(x==X[i]) { ans.append(tot+"\n"); continue; } int prev=compute(X,i); X[i]=x; int next=compute(X,i); tot+=(next-prev); ans.append(tot+"\n"); } out.println(ans); out.close(); } static int compute(char X[],int i) { int N=X.length; int c=0; for(int j=i-2; j<=i+2; j++) { if(j>=0 && j+2<N) { if(X[j]=='a'&& X[j+1]=='b' && X[j+2]=='c')c++; } } return c; } static boolean f(long X[],long V[],double m) { double l=0,r=1e18; for(int i=0; i<X.length; i++) { double d=((double)(V[i]))*m; double x=(double)X[i]; l=Math.max(l, x-d); r=Math.min(r, x+d); // System.out.println(l+" "+r); } return l<=r; } static int query(long a,TreeNode root) { long p=1L<<30; TreeNode temp=root; while(p!=0) { System.out.println(a+" "+p); if((a&p)!=0) { temp=temp.right; } else temp=temp.left; p>>=1; } return temp.index; } static void delete(long a,TreeNode root) { long p=1L<<30; TreeNode temp=root; while(p!=0) { if((a&p)!=0) { temp.right.cnt--; temp=temp.right; } else { temp.left.cnt--; temp=temp.left; } p>>=1; } } static void insert(long a,TreeNode root,int i) { long p=1L<<30; TreeNode temp=root; while(p!=0) { if((a&p)!=0) { temp.right.cnt++; temp=temp.right; } else { temp.left.cnt++; temp=temp.left; } p>>=1; } temp.index=i; } static TreeNode create(int p) { TreeNode root=new TreeNode(0); if(p!=0) { root.left=create(p-1); root.right=create(p-1); } return root; } static boolean f(long A[],long m,int N) { long B[]=new long[N]; for(int i=0; i<N; i++) { B[i]=A[i]; } for(int i=N-1; i>=0; i--) { if(B[i]<m)return false; if(i>=2) { long extra=Math.min(B[i]-m, A[i]); long x=extra/3L; B[i-2]+=2L*x; B[i-1]+=x; } } return true; } static int f(int l,int r,long A[],long x) { while(r-l>1) { int m=(l+r)/2; if(A[m]>=x)l=m; else r=m; } return r; } static boolean f(long m,long H,long A[],int N) { long s=m; for(int i=0; i<N-1;i++) { s+=Math.min(m, A[i+1]-A[i]); } return s>=H; } static boolean f(int i,int j,long last,boolean win[]) { if(i>j)return false; if(A[i]<=last && A[j]<=last)return false; long a=A[i],b=A[j]; //System.out.println(a+" "+b); if(a==b) { return win[i] | win[j]; } else if(a>b) { boolean f=false; if(b>last)f=!f(i,j-1,b,win); return win[i] | f; } else { boolean f=false; if(a>last)f=!f(i+1,j,a,win); return win[j] | f; } } static long ask(long l,long r) { System.out.println("? "+l+" "+r); return l(); } static long f(long N,long M) { long s=0; if(N%3==0) { N/=3; s=N*M; } else { long b=N%3; N/=3; N++; s=N*M; N--; long a=N*M; if(M%3==0) { M/=3; a+=(b*M); } else { M/=3; M++; a+=(b*M); } s=Math.min(s, a); } return s; } static int ask(StringBuilder sb,int a) { System.out.println(sb+""+a); return i(); } static void swap(char X[],int i,int j) { char x=X[i]; X[i]=X[j]; X[j]=x; } static int min(int a,int b,int c) { return Math.min(Math.min(a, b), c); } static long and(int i,int j) { System.out.println("and "+i+" "+j); return l(); } static long or(int i,int j) { System.out.println("or "+i+" "+j); return l(); } static int len=0,number=0; static void f(char X[],int i,int num,int l) { if(i==X.length) { if(num==0)return; //update our num if(isPrime(num))return; if(l<len) { len=l; number=num; } return; } int a=X[i]-'0'; f(X,i+1,num*10+a,l+1); f(X,i+1,num,l); } static boolean is_Sorted(int A[]) { int N=A.length; for(int i=1; i<=N; i++)if(A[i-1]!=i)return false; return true; } static boolean f(StringBuilder sb,String Y,String order) { StringBuilder res=new StringBuilder(sb.toString()); HashSet<Character> set=new HashSet<>(); for(char ch:order.toCharArray()) { set.add(ch); for(int i=0; i<sb.length(); i++) { char x=sb.charAt(i); if(set.contains(x))continue; res.append(x); } } String str=res.toString(); return str.equals(Y); } static boolean all_Zero(int f[]) { for(int a:f)if(a!=0)return false; return true; } static long form(int a,int l) { long x=0; while(l-->0) { x*=10; x+=a; } return x; } static int count(String X) { HashSet<Integer> set=new HashSet<>(); for(char x:X.toCharArray())set.add(x-'0'); return set.size(); } static int f(long K) { long l=0,r=K; while(r-l>1) { long m=(l+r)/2; if(m*m<K)l=m; else r=m; } return (int)l; } // static void build(int v,int tl,int tr,long A[]) // { // if(tl==tr) // { // seg[v]=A[tl]; // } // else // { // int tm=(tl+tr)/2; // build(v*2,tl,tm,A); // build(v*2+1,tm+1,tr,A); // seg[v]=Math.min(seg[v*2], seg[v*2+1]); // } // } static int [] sub(int A[],int B[]) { int N=A.length; int f[]=new int[N]; for(int i=N-1; i>=0; i--) { if(B[i]<A[i]) { B[i]+=26; B[i-1]-=1; } f[i]=B[i]-A[i]; } for(int i=0; i<N; i++) { if(f[i]%2!=0)f[i+1]+=26; f[i]/=2; } return f; } static int[] f(int N) { char X[]=in.next().toCharArray(); int A[]=new int[N]; for(int i=0; i<N; i++)A[i]=X[i]-'a'; return A; } static int max(int a ,int b,int c,int d) { a=Math.max(a, b); c=Math.max(c,d); return Math.max(a, c); } static int min(int a ,int b,int c,int d) { a=Math.min(a, b); c=Math.min(c,d); return Math.min(a, c); } static HashMap<Integer,Integer> Hash(int A[]) { HashMap<Integer,Integer> mp=new HashMap<>(); for(int a:A) { int f=mp.getOrDefault(a,0)+1; mp.put(a, f); } return mp; } static long mul(long a, long b) { return ( a %mod * 1L * b%mod )%mod; } static void swap(int A[],int a,int b) { int t=A[a]; A[a]=A[b]; A[b]=t; } static int find(int a) { if(par[a]<0)return a; return par[a]=find(par[a]); } static void union(int a,int b) { a=find(a); b=find(b); if(a!=b) { par[a]+=par[b]; par[b]=a; } } static boolean isSorted(int A[]) { for(int i=1; i<A.length; i++) { if(A[i]<A[i-1])return false; } return true; } static boolean isDivisible(StringBuilder X,int i,long num) { long r=0; for(; i<X.length(); i++) { r=r*10+(X.charAt(i)-'0'); r=r%num; } return r==0; } static int lower_Bound(int A[],int low,int high, int x) { if (low > high) if (x >= A[high]) return A[high]; int mid = (low + high) / 2; if (A[mid] == x) return A[mid]; if (mid > 0 && A[mid - 1] <= x && x < A[mid]) return A[mid - 1]; if (x < A[mid]) return lower_Bound( A, low, mid - 1, x); return lower_Bound(A, mid + 1, high, x); } static String f(String A) { String X=""; for(int i=A.length()-1; i>=0; i--) { int c=A.charAt(i)-'0'; X+=(c+1)%2; } return X; } static void sort(long[] a) //check for long { ArrayList<Long> l=new ArrayList<>(); for (long i:a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } static String swap(String X,int i,int j) { char ch[]=X.toCharArray(); char a=ch[i]; ch[i]=ch[j]; ch[j]=a; return new String(ch); } static int sD(long n) { if (n % 2 == 0 ) return 2; for (int i = 3; i * i <= n; i += 2) { if (n % i == 0 ) return i; } return (int)n; } static void setGraph(int N) { par=new int[N+1]; D=new int[N+1]; g=new ArrayList[N+1]; set=new boolean[N+1]; col=new int[N+1]; for(int i=0; i<=N; i++) { col[i]=-1; g[i]=new ArrayList<>(); D[i]=Integer.MAX_VALUE; } } static long pow(long a,long b) { //long mod=1000000007; long pow=1; long x=a; while(b!=0) { if((b&1)!=0)pow=(pow*x)%mod; x=(x*x)%mod; b/=2; } return pow; } static long toggleBits(long x)//one's complement || Toggle bits { int n=(int)(Math.floor(Math.log(x)/Math.log(2)))+1; return ((1<<n)-1)^x; } static int countBits(long a) { return (int)(Math.log(a)/Math.log(2)+1); } static long fact(long N) { long n=2; if(N<=1)return 1; else { for(int i=3; i<=N; i++)n=(n*i)%mod; } return n; } static int kadane(int A[]) { int lsum=A[0],gsum=A[0]; for(int i=1; i<A.length; i++) { lsum=Math.max(lsum+A[i],A[i]); gsum=Math.max(gsum,lsum); } return gsum; } 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); } static boolean isPrime(long N) { if (N<=1) return false; if (N<=3) return true; 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; } static void print(char A[]) { for(char c:A)System.out.print(c+" "); System.out.println(); } static void print(boolean A[]) { for(boolean c:A)System.out.print(c+" "); System.out.println(); } static void print(int A[]) { for(int a:A)System.out.print(a+" "); System.out.println(); } static void print(long A[]) { for(long i:A)System.out.print(i+ " "); System.out.println(); } static void print(boolean A[][]) { for(boolean a[]:A)print(a); } static void print(long A[][]) { for(long a[]:A)print(a); } static void print(int A[][]) { for(int a[]:A)print(a); } static void print(ArrayList<Integer> A) { for(int a:A)System.out.print(a+" "); System.out.println(); } 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; } static long GCD(long a,long b) { if(b==0) { return a; } else return GCD(b,a%b ); } } class pair implements Comparable<pair> { long a,l,r; boolean left; int index; pair(long a,long l,int i) { this.a=a; index=i; this.l=l; } public int compareTo(pair x) { if(this.a==x.a) { if(this.l>x.l)return -1; else if(x.l>this.l)return 1; return 0; } if(this.a>x.a)return 1; return -1; } } class TreeNode { int cnt,index; TreeNode left,right; TreeNode(int c) { cnt=c; index=-1; } TreeNode(int c,int index) { cnt=c; this.index=index; } } //Code For FastReader //Code For FastReader //Code For FastReader //Code For FastReader 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 8
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
f03b43cb6ce16062571a39ee87bbc8cc
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.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Collections; import java.util.StringTokenizer; public class CodeForces { static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(){ br=new BufferedReader(new InputStreamReader(System.in)); st=new StringTokenizer(""); } public FastScanner(File f){ try { br=new BufferedReader(new FileReader(f)); st=new StringTokenizer(""); } catch(FileNotFoundException e){ br=new BufferedReader(new InputStreamReader(System.in)); 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[] readLongArray(int n) { long[] a =new long[n]; for (int i=0; i<n; i++) a[i]=nextLong(); return a; } long nextLong() { return Long.parseLong(next()); } } public static long factorial(int n){ if(n==0)return 1; return (long)n*factorial(n-1); } public static int gcd(int a, int b) { if (a == 0) return b; return gcd(b%a, a); } public static long gcd(long a, long b) { if (a == 0) return b; return gcd(b%a, a); } static void sort (int[]a){ ArrayList<Integer> b = new ArrayList<>(); for(int i:a)b.add(i); Collections.sort(b); for(int i=0;i<b.size();i++){ a[i]=b.get(i); } } static void sortReversed (int[]a){ ArrayList<Integer> b = new ArrayList<>(); for(int i:a)b.add(i); Collections.sort(b); Collections.reverse(b); for(int i=0;i<b.size();i++){ a[i]=b.get(i); } } static void sort (long[]a){ ArrayList<Long> b = new ArrayList<>(); for(long i:a)b.add(i); Collections.sort(b); for(int i=0;i<b.size();i++){ a[i]=b.get(i); } } static ArrayList<Integer> sieveOfEratosthenes(int n) { boolean 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] == true) { for (int i = p * p; i <= n; i += p) prime[i] = false; } } ArrayList<Integer> ans = new ArrayList<>(); for (int i = 2; i <= n; i++) { if (prime[i] == true) ans.add(i); } return ans; } static int binarySearchSmallerOrEqual(int arr[], int key) { int n = arr.length; int left = 0, right = n; int mid = 0; while (left < right) { mid = (right + left) >> 1; if (arr[mid] == key) { while (mid + 1 < n && arr[mid + 1] == key) mid++; break; } else if (arr[mid] > key) right = mid; else left = mid + 1; } while (mid > -1 && arr[mid] > key) mid--; return mid; } public static int binarySearchStrictlySmaller(int[] arr, int target) { int start = 0, end = arr.length-1; if(end == 0) return -1; if (target > arr[end]) return end; int ans = -1; while (start <= end) { int mid = (start + end) / 2; if (arr[mid] >= target) { end = mid - 1; } else { ans = mid; start = mid + 1; } } return ans; } static int binarySearch(int arr[], int x) { int l = 0, r = arr.length - 1; while (l <= r) { int m = l + (r - l) / 2; if (arr[m] == x) return m; if (arr[m] < x) l = m + 1; else r = m - 1; } return -1; } static int binarySearch(long arr[], long x) { int l = 0, r = arr.length - 1; while (l <= r) { int m = l + (r - l) / 2; if (arr[m] == x) return m; if (arr[m] < x) l = m + 1; else r = m - 1; } return -1; } static void init(int[]arr,int val){ for(int i=0;i<arr.length;i++){ arr[i]=val; } } static void init(long[]arr,long val){ for(int i=0;i<arr.length;i++){ arr[i]=val; } } static<T> void init(ArrayList<ArrayList<T>>arr,int n){ for(int i=0;i<n;i++){ arr.add(new ArrayList()); } } public static int binarySearchStrictlySmaller(ArrayList<Pair> arr, int target) { int start = 0, end = arr.size()-1; if(end == 0) return -1; if (target > arr.get(end).y) return end; int ans = -1; while (start <= end) { int mid = (start + end) / 2; if (arr.get(mid).y >= target) { end = mid - 1; } else { ans = mid; start = mid + 1; } } return ans; } public static void main(String[] args) { // ArrayList<Integer> name = new ArrayList<>(); // StringBuilder sbd = new StringBuilder(); // StringBuffer sbf = new StringBuffer(); // PrintWriter p = new PrintWriter("output.txt"); // File input = new File("popcorn.in"); // FastScanner fs = new FastScanner(input); // FastScanner fs = new FastScanner(); PrintWriter out = new PrintWriter(System.out); int testNumber = 1; for (int t =0;t<testNumber;t++){ int n= fs.nextInt(); int q = fs.nextInt(); int abc=0; char[] arr = fs.next().toCharArray(); for (int i=2;i<n;i++){ if(arr[i-2]=='a'&&arr[i-1]=='b'&&arr[i]=='c'){ abc++; } } for (int i=0;i<q;i++){ int pos =fs.nextInt()-1; char c = fs.next().charAt(0); for (int j=Math.max(0, pos-2)+2;j<=Math.min(n-1,pos+2);j++){ if(arr[j-2]=='a'&&arr[j-1]=='b'&&arr[j]=='c'){ abc--; } } arr[pos]=c; for (int j=Math.max(0, pos-2)+2;j<=Math.min(n-1,pos+2);j++){ if(arr[j-2]=='a'&&arr[j-1]=='b'&&arr[j]=='c'){ abc++; } } out.print(abc+"\n"); } } out.flush(); } static class Pair implements Comparable<Pair>{ int x;//pos int y;//size public Pair(int x,int y){ this.x=x; this.y=y; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; return x == ((Pair) o).x&&y == ((Pair) o).y; } @Override public int hashCode() { return x+y; } @Override public int compareTo(Pair o) { return y-o.y; } } }
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 8
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
c342ff092a8b6a21f9802d15cd8cc9c6
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 a { public static void main(String[] args){ FastScanner sc = new FastScanner(); int t = 1; while(t-- > 0){ int n = sc.nextInt(); int q = sc.nextInt(); String s = sc.next(); char arr[] = new char[s.length()]; int count = 0; for(int i=0; i<s.length(); i++){ arr[i] = s.charAt(i); if(s.charAt(i) == 'a'){ if(i+2<s.length() && s.charAt(i+1) == 'b' && s.charAt(i+2) == 'c'){ count++; } } } for(int i=0; i<q; i++){ int pos = sc.nextInt(); pos--; String temp = sc.next(); //System.out.println("temp: " + temp); char c = temp.charAt(0); if(arr[pos] == c){ System.out.println(count); continue; } if(c == 'a'){ if(arr[pos] == 'b'){ if(pos-1 >= 0 && pos+1 < n && arr[pos-1] == 'a' && arr[pos+1] == 'c'){ count--; } if(pos+2 < n && arr[pos+1] == 'b' && arr[pos+2] == 'c'){ count++; } } else if(arr[pos] == 'c'){ if(pos-2 >= 0 && arr[pos-1] == 'b' && arr[pos-2] == 'a'){ count--; } if(pos+2 < n && arr[pos+1] == 'b' && arr[pos+2] == 'c'){ count++; } } arr[pos] = c; System.out.println(count); continue; } else if(c == 'b'){ if(arr[pos] == 'a'){ if(pos+2 < n && arr[pos+1] == 'b' && arr[pos+2] == 'c'){ count--; } if(pos-1 >=0 && pos+1<n && arr[pos-1] == 'a' && arr[pos+1] == 'c'){ count++; } } else if(arr[pos] == 'c'){ if(pos-2 >= 0 && arr[pos-1] == 'b' && arr[pos-2] == 'a'){ count--; } if(pos-1>=0 && pos+1<n && arr[pos-1] == 'a' && arr[pos+1] == 'c'){ count++; } } arr[pos] = c; System.out.println(count); continue; } else{ if(arr[pos] == 'a'){ if(pos+2 < n && arr[pos+1] == 'b' && arr[pos+2] == 'c'){ count--; } if(pos-2>=0 && arr[pos-1] == 'b' && arr[pos-2] == 'a'){ count++; } } else if(arr[pos] == 'b'){ if(pos-1>=0 && pos+1<n && arr[pos-1] == 'a' && arr[pos+1] == 'c'){ count--; } if(pos-2>=0 && arr[pos-1] == 'b' && arr[pos-2] == 'a'){ count++; } } arr[pos] = c; System.out.println(count); continue; } } } } } class FastScanner { //I don't understand how this works lmao 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; } } }
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 8
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
248b9155e3446e0be521de0266ba12fe
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 cf { static FastReader in=new FastReader(); static final Random random=new Random(); static long mod=1000000007L; static HashMap<String,Integer>map=new HashMap<>(); public static void main(String args[]) throws IOException { FastReader sc=new FastReader(); //int t=sc.nextInt(); int t=1; boolean check =true; while(t-->0){ int n=sc.nextInt(); int q=sc.nextInt(); String s=sc.next(); char []c=s.toCharArray(); int count=0; for(int i=0;i<n-2;i++){ if(s.charAt(i)=='a' && s.charAt(i+1)=='b' && s.charAt(i+2)=='c'){ count++; } } while(q-->0){ int p=sc.nextInt()-1; String cc=sc.next(); char ch=cc.charAt(0); if(c[p]==ch){ System.out.println(count); } else{ int start=Math.max(0,p-2); int end=Math.min(p+2,n-1); int change=0; for(int i=start;i<=end-2;i++){ if(c[i]=='a' && c[i+1]=='b' && c[i+2]=='c'){ change--; } } c[p]=ch; for(int i=start;i<=end-2;i++){ if(c[i]=='a' && c[i+1]=='b' && c[i+2]=='c'){ change++; } } count=count+change; System.out.println(count); } } } } public static int minBS(long[]arr,long val){ int l=-1; int r=arr.length; while(r>l+1){ int mid=(l+r)/2; if(arr[mid]>=val){ r=mid; } else{ l=mid; } } return r; } static class Pair implements Comparable { int a,b; public String toString() { return a+" " + b; } public Pair(int x , int y) { a=x;b=y; } @Override public int compareTo(Object o) { Pair p = (Pair)o; if(p.b<b) return 1; else if(p.b==b) if(p.a<a) return 1; else if(p.a==a) return 0; return -1; } } /* public static boolean checkAP(List<Integer> lis){ for(int i=1;i<lis.size()-1;i++){ if(2*lis.get(i)!=lis.get(i-1)+lis.get(i+1)){ return false; } } return true; } static class Pair implements Comparable { int a,b; public String toString() { return a+" " + b; } public Pair(int x , int y) { a=x;b=y; } @Override public int compareTo(Object o) { Pair p = (Pair)o; if(p.b>b) return 1; else if(p.b==b) if(p.a>a) return 1; else if(p.a==a) return 0; return -1; } } public static int minBS(int[]arr,int val){ int l=-1; int r=arr.length; while(r>l+1){ int mid=(l+r)/2; if(arr[mid]>=val){ r=mid; } else{ l=mid; } } return r; } public static int maxBS(int[]arr,int val){ int l=-1; int r=arr.length; while(r>l+1){ int mid=(l+r)/2; if(arr[mid]<=val){ l=mid; } else{ r=mid; } } return l; } static void ruffleSort(int[] a) { int n=a.length; 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 int gcd(int a,int b) { if(b==0) { return a; } return gcd(b,a%b); } static int lcm(int a, int b) { return (a / gcd(a, b)) * b; }*/ 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 8
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
604c1a267c906bd90121e210c840afc2
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.nio.Buffer; import java.util.Scanner; public class fastTemp { public static void main(String[] args) throws IOException,NumberFormatException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String s[] = br.readLine().split(" "); int n = Integer.parseInt(s[0]); int q = Integer.parseInt(s[1]); String ss = br.readLine(); char c[] = ss.toCharArray(); int count=0; for(int i=0;i<n-2;i++){ if(c[i]=='a' && c[i+1]=='b' && c[i+2]=='c'){ count++; } } while(q-- > 0){ String s1[] = br.readLine().split(" "); int p = Integer.parseInt(s1[0])-1; char cc = s1[1].charAt(0); int st = Math.max(0,p-2); int en = Math.min(n-3,p); if(c[p]==cc){ System.out.println(count); } else{ int change = 0 ; int i = st; while(i<=en){ if(c[i]=='a' && c[i+1]=='b'&& c[i+2]=='c'){ change--; } i++; } c[p] = cc; i = st; while(i<=en){ if(c[i]=='a' && c[i+1]=='b'&& c[i+2]=='c'){ change++; } i++; } count = count + change; 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 8
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
17f46dbcfa0f4d4f5ef4ae538b2f9d83
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
/* stream Butter! eggyHide eggyVengeance I need U xiao rerun when */ import static java.lang.Math.*; import java.util.*; import java.io.*; import java.math.*; public class x1609B { public static void main(String hi[]) throws Exception { BufferedReader infile = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(infile.readLine()); int N = Integer.parseInt(st.nextToken()); int Q = Integer.parseInt(st.nextToken()); char[] arr = infile.readLine().trim().toCharArray(); int res = 0; for(int i=2; i < N; i++) if(arr[i-2] == 'a' && arr[i-1] == 'b' && arr[i] == 'c') res++; StringBuilder sb = new StringBuilder(); while(Q-->0) { st = new StringTokenizer(infile.readLine()); int pos = Integer.parseInt(st.nextToken())-1; char tag = st.nextToken().charAt(0); int before = check(max(0, pos-5), min(N-1, pos+5), arr); arr[pos] = tag; int after = check(max(0, pos-5), min(N-1, pos+5), arr); res += after-before; sb.append(res+"\n"); } System.out.print(sb); } public static int check(int L, int R, char[] arr) { int res = 0; for(int i=L+2; i <= R; i++) if(arr[i-2] == 'a' && arr[i-1] == 'b' && arr[i] == 'c') res++; return res; } public static int[] readArr(int N, BufferedReader infile, StringTokenizer st) throws Exception { int[] arr = new int[N]; st = new StringTokenizer(infile.readLine()); for(int i=0; i < N; i++) arr[i] = Integer.parseInt(st.nextToken()); return arr; } }
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 8
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
35eb7a8e479c2a2764e3e2a62d4ab571
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 WilliamTheVigilant { public static void main(String[] args) throws IOException { FastIO fr = new FastIO(); PrintWriter pr = new PrintWriter(new OutputStreamWriter(System.out)); int n = fr.nextInt(); int q = fr.nextInt(); int count = 0; TreeSet<Integer> set = new TreeSet<>(); char[] s = fr.next().toCharArray(); for (int i = 0; i < s.length - 2; i++) { if (s[i] == 'a') { if (s[i] == 'a' && s[i + 1] == 'b' && s[i + 2] == 'c') { count++; set.add(i); } } } while (q-- > 0) { int pos = fr.nextInt() - 1; char c = fr.next().charAt(0); if (c == s[pos]) { pr.println(count); continue; } Integer nearest = set.floor(pos); if (nearest != null && pos - nearest < 3) { count--; set.remove(nearest); } s[pos] = c; switch (c) { case 'a': // check if we created a new sequence. if (pos < n - 2) { if (s[pos + 1] == 'b' && s[pos + 2] == 'c') { set.add(pos); count++; } } break; case 'b': if (pos < n - 1 && pos > 0) { if (s[pos - 1] == 'a' && s[pos + 1] == 'c') { set.add(pos - 1); count++; } } break; case 'c': if (pos > 1) { if (s[pos - 2] == 'a' && s[pos - 1] == 'b') { set.add(pos - 2); count++; } } break; } pr.println(count); } pr.close(); } static class Pair { int x, y; public Pair(int x, int y) { this.x = x; this.y = y; } } static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } static int toInt(String s) { return Integer.parseInt(s); } // MERGE SORT IMPLEMENTATION void sort(int[] arr, int l, int r) { if (l < r) { int m = l + (r - l) / 2; sort(arr, l, m); sort(arr, m + 1, r); // call merge merge(arr, l, m, r); } } void merge(int[] arr, int l, int m, int r) { // find sizes int len1 = m - l + 1; int len2 = r - m; int[] L = new int[len1]; int[] R = new int[len2]; // push to copies for (int i = 0; i < L.length; i++) L[i] = arr[l + i]; for (int i = 0; i < R.length; i++) { R[i] = arr[m + 1 + i]; } // fill in new array int i = 0, j = 0; int k = l; while (i < len1 && j < len2) { if (L[i] < R[i]) { arr[k] = L[i]; i++; } else { arr[k] = R[i]; j++; } k++; } // add remaining elements while (i < len1) { arr[k] = L[i]; i++; k++; } while (j < len2) { arr[k] = R[j]; j++; k++; } } static class FastIO extends PrintWriter { private InputStream stream; private byte[] buf = new byte[1 << 16]; private int curChar, numChars; // standard input public FastIO() { this(System.in, System.out); } public FastIO(InputStream i, OutputStream o) { super(o); stream = i; } // file input public FastIO(String i, String o) throws IOException { super(new FileWriter(o)); stream = new FileInputStream(i); } // throws InputMismatchException() if previously detected end of file private int nextByte() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars == -1) return -1; // end of file } return buf[curChar++]; } // to read in entire lines, replace c <= ' ' // with a function that checks whether c is a line break public String next() { int c; do { c = nextByte(); } while (c <= ' '); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = nextByte(); } while (c > ' '); return res.toString(); } public int nextInt() { // nextLong() would be implemented similarly int c; do { c = nextByte(); } while (c <= ' '); int sgn = 1; if (c == '-') { sgn = -1; c = nextByte(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res = 10 * res + c - '0'; c = nextByte(); } while (c > ' '); return res * sgn; } public double nextDouble() { return Double.parseDouble(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 8
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
91693aecc4eb83ef43b1854bf3b13fd6
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.*; public class CF1 { public static void main(String[] args) { FastScanner sc=new FastScanner(); // int T=sc.nextInt(); int T=1; for (int tt=0; tt<T; tt++){ int n= sc.nextInt(); int q = sc.nextInt(); String s = sc.next(); char[] arr = s.toCharArray(); int count =0; for (int i=1; i<n-1; i++){ if (arr[i]=='b' && arr[i-1]=='a' && arr[i+1]=='c') count++; } while (q>0){ int pos= sc.nextInt()-1; char c= sc.next().charAt(0); if (arr[pos]==c){ } else { if (help(arr, Math.max(pos-2,0), Math.min(pos+2, arr.length-1))) count--; arr[pos]=c; if (help(arr, Math.max(pos-2,0), Math.min(pos+2, arr.length-1))) count++; } System.out.println(count); q--; } } } static boolean help(char arr[], int start, int end){ for (int i=start+1; i<end; i++){ if (arr[i]=='b' && arr[i-1]=='a' && arr[i+1]=='c') return true; } return false; } static long mod =998244353L; static long power (long a, long b){ long res=1; while (b>0){ if ((b&1)== 1){ res= (res * a % mod)%mod; } a=(a%mod * a%mod)%mod; b=b>>1; } return res; } 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); } static void sortLong(long[] a) { ArrayList<Long> l=new ArrayList<>(); for (long i:a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } static int gcd (int n, int m){ if (m==0) return n; else return gcd(m, n%m); } static class Pair{ int x,y; public Pair(int x, int y){ this.x = x; this.y = y; } } 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 8
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
276bebee6fdcec48e2cca0ef7d3801cb
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.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.Random; import java.util.Stack; import java.util.StringTokenizer; import java.util.TreeMap; // A -> CodeForcesProblemSet public final class A { static FastReader fr = new FastReader(); static PrintWriter out = new PrintWriter(System.out); static final int gigamod = 1000000007; static int t = 1; static double epsilon = 0.0000001; public static void main(String[] args) { OUTER: for (int tc = 0; tc < t; tc++) { int n = fr.nextInt(), q = fr.nextInt(); char[] s = fr.next().toCharArray(); int abcCount = 0; for (int i = 0; i < n - 2; i++) { if (s[i] == 'a' && s[i + 1] == 'b' && s[i + 2] == 'c') abcCount++; } for (int qu = 0; qu < q; qu++) { int idx = fr.nextInt() - 1; char c = fr.nextChar(); // Counting the number of ABCs in vicinity before modification. int hereAbcBef = 0; for (int i = idx - 2; i <= idx + 2; i++) { if (i >= 0 && i + 2 <= n - 1) if (s[i] == 'a' && s[i + 1] == 'b' && s[i + 2] == 'c') hereAbcBef++; } // Doing the modification. s[idx] = c; // Counting the number of ABCs in vicinity after modification. int hereAbcAft = 0; for (int i = idx - 2; i <= idx + 2; i++) { if (i >= 0 && i + 2 <= n - 1) if (s[i] == 'a' && s[i + 1] == 'b' && s[i + 2] == 'c') hereAbcAft++; } abcCount += hereAbcAft - hereAbcBef; out.println(abcCount); } } out.close(); } // Manual implementation of RB-BST for C++ PBDS functionality. // Modification required according to requirements. public static final class RedBlackCountSet { // Colors for Nodes: private static final boolean RED = true; private static final boolean BLACK = false; // Pointer to the root: private Node root; // Public constructor: public RedBlackCountSet() { root = null; } // Node class for storing key value pairs: private class Node { long key; long value; Node left, right; boolean color; long size; // Size of the subtree rooted at that node. public Node(long key, long value, boolean color) { this.key = key; this.value = value; this.left = this.right = null; this.color = color; this.size = value; } } /***** Invariant Maintenance functions: *****/ // When a temporary 4 node is formed: private Node flipColors(Node node) { node.left.color = node.right.color = BLACK; node.color = RED; return node; } // When there's a right leaning red link and it is not a 3 node: private Node rotateLeft(Node node) { Node rn = node.right; node.right = rn.left; rn.left = node; rn.color = node.color; node.color = RED; return rn; } // When there are 2 red links in a row: private Node rotateRight(Node node) { Node ln = node.left; node.left = ln.right; ln.right = node; ln.color = node.color; node.color = RED; return ln; } /***** Invariant Maintenance functions end *****/ // Public functions: public void put(long key) { root = put(root, key); root.color = BLACK; // One of the invariants. } private Node put(Node node, long key) { // Standard insertion procedure: if (node == null) return new Node(key, 1, RED); // Recursing: int cmp = Long.compare(key, node.key); if (cmp < 0) node.left = put(node.left, key); else if (cmp > 0) node.right = put(node.right, key); else node.value++; // Invariant maintenance: if (node.left != null && node.right != null) { if (node.right.color = RED && node.left.color == BLACK) node = rotateLeft(node); if (node.left.color == RED && node.left.left.color == RED) node = rotateRight(node); if (node.left.color == RED && node.right.color == RED) node = flipColors(node); } // Property maintenance: node.size = node.value + size(node.left) + size(node.right); return node; } private long size(Node node) { if (node == null) return 0; else return node.size; } public long rank(long key) { return rank(root, key); } // Modify according to requirements. private long rank(Node node, long key) { if (node == null) return 0; int cmp = Long.compare(key, node.key); if (cmp < 0) return rank(node.left, key); else if (cmp > 0) return node.value + size(node.left) + rank(node.right, key); else return size(node.left); } } static long modDiv(long a, long b) { return mod(a * power(b, gigamod - 2)); } static class SegTree { // Setting up the parameters. int n; long[] sumTree; long[] ogArr; long[] maxTree; long[] minTree; public static final int sumMode = 0; public static final int maxMode = 1; public static final int minMode = 2; // Constructor public SegTree(long[] arr, int mode) { if (mode == sumMode) { Arrays.fill(sumTree = new long[4 * (n = ((ogArr = arr.clone()).length))], -1); buildSum(0, n - 1, 0); } else if (mode == maxMode) { Arrays.fill(maxTree = new long[4 * (n = ((ogArr = arr.clone()).length))], -1); buildMax(0, n - 1, 0); } else if (mode == minMode) { Arrays.fill(minTree = new long[4 * (n = ((ogArr = arr.clone()).length))], Long.MAX_VALUE - 1); buildMin(0, n - 1, 0); } } /**********Code for sum***********/ /*********************************/ // Build the segment tree node-by-node private long buildSum(int l, int r, int segIdx) { return sumTree[segIdx] = (l == r) ? ogArr[l] : (buildSum(l, l + (r - l) / 2, 2 * segIdx + 1) + buildSum((l + (r - l) / 2) + 1, r, 2 * segIdx + 2)); } // Change a single element public void changeForSum(int idx, long to) { changeForSum(idx, to, 0, n - 1, 0); } private long changeForSum(int idx, long to, int l, int r, int segIdx) { return sumTree[segIdx] = ((l == r) ? (ogArr[idx] = to) : ((0 * ((idx <= (l + (r - l) / 2)) ? changeForSum(idx, to, l, l + (r - l) / 2, 2 * segIdx + 1) : changeForSum(idx, to, (l + (r - l) / 2) + 1, r, 2 * segIdx + 2))) + sumTree[2 * segIdx + 1] + sumTree[2 * segIdx + 2])); } // Return the sum arr[l, r] public long segSum(int l, int r) { if (l > r) return 0; return segSum(l, r, 0, n - 1, 0); } private long segSum(int segL, int segR, int l, int r, int segIdx) { if (segL == l && segR == r) return sumTree[segIdx]; if (segR <= l + (r - l) / 2) return segSum(segL, segR, l, l + (r - l) / 2, 2 * segIdx + 1); if (segL >= l + (r - l) / 2 + 1) return segSum(segL, segR, l + (r - l) / 2 + 1, r, 2 * segIdx + 2); return segSum(segL, l + (r - l) / 2, l, l + (r - l) / 2, 2 * segIdx + 1) + segSum(l + (r - l) / 2 + 1, segR, l + (r - l) / 2 + 1, r, 2 * segIdx + 2); } /********End of code for sum********/ /***********************************/ /*******Start of code for max*******/ /***********************************/ // Build the max tree node-by-node private long buildMax(int l, int r, int segIdx) { return maxTree[segIdx] = (l == r) ? ogArr[l] : Math.max(buildMax(l, (l + (r - l) / 2), 2 * segIdx + 1), buildMax(l + (r - l) / 2 + 1, r, 2 * segIdx + 2)); } // Change a single element public void changeForMax(int idx, long to) { changeForMax(0, n - 1, idx, to, 0); } private long changeForMax(int l, int r, int idx, long to, int segIdx) { return maxTree[segIdx] = (l == r && l == idx) ? (ogArr[idx] = to) : Math.max(changeForMax(l, l + (r - l) / 2, idx, to, 2 * segIdx + 1), changeForMax(l + (r - l) / 2 + 1, r, idx, to, 2 * segIdx + 2)); } public long segMax(int segL, int segR) { if (segL > segR) return 0; return segMax(0, n - 1, segL, segR, 0); } private long segMax(int l, int r, int segL, int segR, int segIdx) { int midL = l + (r - l) / 2; if (segL == segR && segL == l) return ogArr[segL]; if (segR <= midL) return segMax(l, midL, segL, segR, 2 * segIdx + 1); if (segL >= midL + 1) return segMax(midL + 1, r, segL, segR, 2 * segIdx + 2); return Math.max(segMax(l, midL, segL, midL, 2 * segIdx + 1), segMax(midL + 1, r, midL + 1, segR, 2 * segIdx + 2)); } /*******End of code for max*********/ /***********************************/ /*******Start of code for min*******/ /***********************************/ private long buildMin(int l, int r, int segIdx) { return minTree[segIdx] = (l == r) ? ogArr[l] : Math.min(buildMin(l, (l + (r - l) / 2), 2 * segIdx + 1), buildMin(l + (r - l) / 2 + 1, r, 2 * segIdx + 2)); } // Change a single element public void changeForMin(int idx, long to) { changeForMin(0, n - 1, idx, to, 0); } private long changeForMin(int l, int r, int idx, long to, int segIdx) { return minTree[segIdx] = (l == r && l == idx) ? (ogArr[idx] = to) : Math.min(changeForMin(l, l + (r - l) / 2, idx, to, 2 * segIdx + 1), changeForMin(l + (r - l) / 2 + 1, r, idx, to, 2 * segIdx + 2)); } public long segMin(int segL, int segR) { if (segL > segR) return 0; return segMin(0, n - 1, segL, segR, 0); } private long segMin(int l, int r, int segL, int segR, int segIdx) { int midL = l + (r - l) / 2; if (segL == segR && segL == l) return ogArr[segL]; if (segR <= midL) return segMin(l, midL, segL, segR, 2 * segIdx + 1); if (segL >= midL + 1) return segMin(midL + 1, r, segL, segR, 2 * segIdx + 2); return Math.min(segMin(l, midL, segL, midL, 2 * segIdx + 1), segMin(midL + 1, r, midL + 1, segR, 2 * segIdx + 2)); } /*******End of code for min*********/ /***********************************/ public String toString() { return A.toString(minTree); } } static void coprimeGenerator(int m, int n, ArrayList<Point> coprimes, int limit, int numCoprimes) { if (m > limit) return; if (m <= limit && n <= limit) coprimes.add(new Point(m, n)); if (coprimes.size() > numCoprimes) return; coprimeGenerator(2 * m - n, m, coprimes, limit, numCoprimes); coprimeGenerator(2 * m + n, m, coprimes, limit, numCoprimes); coprimeGenerator(m + 2 * n, n, coprimes, limit, numCoprimes); } // Returns the length of the longest prefix that is also // a suffix (no overlap). static int longestPrefixSuffix(String s) { int n = s.length(); int[] arr = new int[n]; arr[0] = 0; int len = 0; for (int i = 1; i < n;) { if (s.charAt(len) == s.charAt(i)) { len++; arr[i++] = len; } else { if (len != 0) { len = arr[len - 1]; } else { arr[i] = 0; i++; } } } return arr[n - 1]; } static long hash(long i) { return (i * 2654435761L % gigamod); } static long hash2(long key) { long h = Long.hashCode(key); h ^= (h >>> 20) ^ (h >>> 12) ^ (h >>> 7) ^ (h >>> 4); return h & (gigamod-1); } static long power(long x, long y) { // int p = 998244353; int p = gigamod; long res = 1; // Initialize result x = x % p; // Update x if it is more than or // equal to p if (x == 0) return 0; // In case x is divisible by p; while (y > 0) { // If y is odd, multiply x with result if ((y & 1) != 0) res = (res * x) % p; // y must be even now y = y >> 1; // y = y/2 x = (x * x) % p; } return res; } static long power2(long x, long y) { // int p = 998244353; // int p = gigamod; long res = 1; // Initialize result // x = x /*% p*/; // Update x if it is more than or // equal to p if (x == 0) return 0; // In case x is divisible by p; while (y > 0) { // If y is odd, multiply x with result if ((y & 1) != 0) res = (res * x) /*% p*/; // y must be even now y = y >> 1; // y = y/2 x = (x * x) /*% p*/; } return res; } // Maps elements in a 2D matrix serially to elements in // a 1D array. static int mapTo1D(int row, int col, int n, int m) { return row * m + col; } // Inverse of what the one above does. static int[] mapTo2D(int idx, int n, int m) { int[] rnc = new int[2]; rnc[0] = idx / m; rnc[1] = idx % m; return rnc; } // Checks if s has subsequence t. static boolean hasSubsequence(String s, String t) { char[] schars = s.toCharArray(); char[] tchars = t.toCharArray(); int slen = schars.length, tlen = tchars.length; int tctr = 0; if (slen < tlen) return false; for (int i = 0; i < slen || i < tlen; i++) { if (tctr == tlen) break; if (schars[i] == tchars[tctr]) { tctr++; } } if (tctr == tlen) return true; return false; } // Returns the binary string of length at least bits. static String toBinaryString(long num, int bits) { StringBuilder sb = new StringBuilder(Long.toBinaryString(num)); sb.reverse(); for (int i = sb.length(); i < bits; i++) sb.append('0'); return sb.reverse().toString(); } @SuppressWarnings("serial") static class CountMap<T> extends TreeMap<T, Integer>{ CountMap() { } CountMap(T[] arr) { this.putCM(arr); } public Integer putCM(T key) { if (super.containsKey(key)) { return super.put(key, super.get(key) + 1); } else { return super.put(key, 1); } } public Integer removeCM(T key) { Integer count = super.get(key); if (count == null) return -1; if (count == 1) return super.remove(key); else return super.put(key, super.get(key) - 1); } public Integer getCM(T key) { Integer count = super.get(key); if (count == null) return 0; return count; } public void putCM(T[] arr) { for (T l : arr) this.putCM(l); } } static class Point implements Comparable<Point> { long x; long y; int id; Point() { x = y = id = 0; } Point(Point p) { this.x = p.x; this.y = p.y; this.id = p.id; } /*Point(double a, double b) { x = a; y = b; }*/ Point(long a, long b, int id) { this.x = a; this.y = b; this.id = id; } Point(long a, long b) { this.x = a; this.y = b; } @Override public int compareTo(Point o) { if (this.x > o.x) return 1; if (this.x < o.x) return -1; if (this.y > o.y) return 1; if (this.y < o.y) return -1; return 0; } public boolean equals(Point that) { return this.compareTo(that) == 0; } } static class PointComparator implements Comparator<Point> { @Override public int compare(Point o1, Point o2) { // Comparision will be done on the basis of the start // of the segment and then on the length of the segment. if (o1.id > o2.id) return -1; if (o1.id < o2.id) return 1; return 0; } } static double distToOrigin(Point p1) { return Math.sqrt(Math.pow(p1.y, 2) + Math.pow(p1.x, 2)); } static double distance(Point p1, Point p2) { double y2 = p2.y, y1 = p1.y; double x2 = p2.x, x1 = p1.x; double distance = Math.sqrt(Math.pow(y2-y1, 2) + Math.pow(x2-x1, 2)); return distance; } // Returns the largest power of k that fits into n. static int largestFittingPower(long n, long k) { int lo = 0, hi = logk(Long.MAX_VALUE, 3); int largestPower = -1; while (lo <= hi) { int mid = lo + (hi - lo)/2; long val = (long) Math.pow(k, mid); if (val <= n) { largestPower = mid; lo = mid + 1; } else { hi = mid - 1; } } return largestPower; } // Returns map of factor and its power in the number. static HashMap<Long, Integer> primeFactorization(long num) { HashMap<Long, Integer> map = new HashMap<>(); while (num % 2 == 0) { num /= 2; Integer pwrCnt = map.get(2L); map.put(2L, pwrCnt != null ? pwrCnt + 1 : 1); } for (long i = 3; i * i <= num; i += 2) { while (num % i == 0) { num /= i; Integer pwrCnt = map.get(i); map.put(i, pwrCnt != null ? pwrCnt + 1 : 1); } } // If the number is prime, we have to add it to the // map. if (num != 1) map.put(num, 1); return map; } // Returns map of factor and its power in the number. static HashMap<Integer, Integer> primeFactorization(int num) { HashMap<Integer, Integer> map = new HashMap<>(); while (num % 2 == 0) { num /= 2; Integer pwrCnt = map.get(2); map.put(2, pwrCnt != null ? pwrCnt + 1 : 1); } for (int i = 3; i * i <= num; i += 2) { while (num % i == 0) { num /= i; Integer pwrCnt = map.get(i); map.put(i, pwrCnt != null ? pwrCnt + 1 : 1); } } // If the number is prime, we have to add it to the // map. if (num != 1) map.put(num, 1); return map; } static HashSet<Long> divisors(long num) { HashSet<Long> divisors = new HashSet<Long>(); divisors.add(1L); divisors.add(num); for (long i = 2; i * i <= num; i++) { if (num % i == 0) { divisors.add(num/i); divisors.add(i); } } return divisors; } // Returns the index of the first element // larger than or equal to val. static int bsearch(int[] arr, int val, int lo, int hi) { int idx = -1; while (lo <= hi) { int mid = lo + (hi - lo)/2; if (arr[mid] >= val) { idx = mid; hi = mid - 1; } else lo = mid + 1; } return idx; } static int bsearch(long[] arr, long val, int lo, int hi) { int idx = -1; while (lo <= hi) { int mid = lo + (hi - lo)/2; if (arr[mid] >= val) { idx = mid; hi = mid - 1; } else lo = mid + 1; } return idx; } // Returns the index of the last element // smaller than or equal to val. static int bsearch(long[] arr, long val, int lo, int hi, boolean sMode) { int idx = -1; while (lo <= hi) { int mid = lo + (hi - lo)/2; if (arr[mid] > val) { hi = mid - 1; } else { idx = mid; lo = mid + 1; } } return idx; } static int bsearch(int[] arr, long val, int lo, int hi, boolean sMode) { int idx = -1; while (lo <= hi) { int mid = lo + (hi - lo)/2; if (arr[mid] > val) { hi = mid - 1; } else { idx = mid; lo = mid + 1; } } return idx; } static long factorial(long n) { if (n <= 1) return 1; long factorial = 1; for (int i = 1; i <= n; i++) factorial = mod(factorial * i); return factorial; } static long factorialInDivision(long a, long b) { if (a == b) return 1; if (b < a) { long temp = a; a = b; b = temp; } long factorial = 1; for (long i = a + 1; i <= b; i++) factorial = mod(factorial * i); return factorial; } static BigInteger factorialInDivision(BigInteger a, BigInteger b) { if (a.equals(b)) return BigInteger.ONE; return a.multiply(factorialInDivision(a.subtract(BigInteger.ONE), b)); } static long nCr(long n, long r) { long p = gigamod; // Base case if (r == 0) return 1; // Fill factorial array so that we // can find all factorial of r, n // and n-r long fac[] = new long[(int)n + 1]; fac[0] = 1; for (int i = 1; i <= n; i++) fac[i] = fac[i - 1] * i % p; return (fac[(int)n] * modInverse(fac[(int)r], p) % p * modInverse(fac[(int)n - (int)r], p) % p) % p; } static long modInverse(long n, long p) { return power(n, p - 2, p); } static long power(long x, long y, long p) { long res = 1; // Initialize result x = x % p; // Update x if it is more than or // equal to p while (y > 0) { // If y is odd, multiply x with result if ((y & 1)==1) res = (res * x) % p; // y must be even now y = y >> 1; // y = y/2 x = (x * x) % p; } return res; } static long nPr(long n, long r) { return factorialInDivision(n, n - r); } static int log2(long n) { return (int)(Math.log(n) / Math.log(2)); } static double log2(long n, boolean doubleMode) { return (Math.log(n) / Math.log(2)); } static int logk(long n, long k) { return (int)(Math.log(n) / Math.log(k)); } // Sieve of Eratosthenes: static boolean[] primeGenerator(int upto) { boolean[] isPrime = new boolean[upto + 1]; Arrays.fill(isPrime, true); isPrime[1] = isPrime[0] = false; for (long i = 2; i * i < upto + 1; i++) if (isPrime[(int) i]) // Mark all the multiples greater than or equal // to the square of i to be false. for (long j = i; j * i < upto + 1; j++) isPrime[(int) j * (int) i] = false; return isPrime; } static int gcd(int a, int b) { if (b == 0) { return a; } else { return gcd(b, a % b); } } static long gcd(long a, long b) { if (b == 0) { return a; } else { return gcd(b, a % b); } } static int gcd(int[] arr) { int n = arr.length; int gcd = arr[0]; for (int i = 1; i < n; i++) { gcd = gcd(gcd, arr[i]); } return gcd; } static long gcd(long[] arr) { int n = arr.length; long gcd = arr[0]; for (int i = 1; i < n; i++) { gcd = gcd(gcd, arr[i]); } return gcd; } static long lcm(int[] arr) { long lcm = arr[0]; int n = arr.length; for (int i = 1; i < n; i++) { lcm = (lcm * arr[i]) / gcd(lcm, arr[i]); } return lcm; } static long lcm(long[] arr) { long lcm = arr[0]; int n = arr.length; for (int i = 1; i < n; i++) { lcm = (lcm * arr[i]) / gcd(lcm, arr[i]); } return lcm; } static long lcm(int a, int b) { return (a * b)/gcd(a, b); } static long lcm(long a, long b) { return (a * b)/gcd(a, b); } static boolean less(int a, int b) { return a < b ? true : false; } static boolean isSorted(int[] a) { for (int i = 1; i < a.length; i++) { if (less(a[i], a[i - 1])) return false; } return true; } static boolean isSorted(long[] a) { for (int i = 1; i < a.length; i++) { if (a[i] < a[i - 1]) return false; } return true; } static void swap(int[] a, int i, int j) { int temp = a[i]; a[i] = a[j]; a[j] = temp; } static void swap(long[] a, int i, int j) { long temp = a[i]; a[i] = a[j]; a[j] = temp; } static void swap(double[] a, int i, int j) { double temp = a[i]; a[i] = a[j]; a[j] = temp; } static void swap(char[] a, int i, int j) { char temp = a[i]; a[i] = a[j]; a[j] = temp; } static void sort(int[] arr) { shuffleArray(arr, 0, arr.length - 1); Arrays.sort(arr); } static void sort(char[] arr) { shuffleArray(arr, 0, arr.length - 1); Arrays.sort(arr); } static void sort(long[] arr) { shuffleArray(arr, 0, arr.length - 1); Arrays.sort(arr); } static void sort(double[] arr) { shuffleArray(arr, 0, arr.length - 1); Arrays.sort(arr); } static void reverseSort(int[] arr) { shuffleArray(arr, 0, arr.length - 1); Arrays.sort(arr); int n = arr.length; for (int i = 0; i < n/2; i++) swap(arr, i, n - 1 - i); } static void reverseSort(char[] arr) { shuffleArray(arr, 0, arr.length - 1); Arrays.sort(arr); int n = arr.length; for (int i = 0; i < n/2; i++) swap(arr, i, n - 1 - i); } static void reverseSort(long[] arr) { shuffleArray(arr, 0, arr.length - 1); Arrays.sort(arr); int n = arr.length; for (int i = 0; i < n/2; i++) swap(arr, i, n - 1 - i); } static void reverseSort(double[] arr) { shuffleArray(arr, 0, arr.length - 1); Arrays.sort(arr); int n = arr.length; for (int i = 0; i < n/2; i++) swap(arr, i, n - 1 - i); } static void shuffleArray(long[] arr, int startPos, int endPos) { Random rnd = new Random(); for (int i = startPos; i < endPos; ++i) { long tmp = arr[i]; int randomPos = i + rnd.nextInt(endPos - i); arr[i] = arr[randomPos]; arr[randomPos] = tmp; } } static void shuffleArray(int[] arr, int startPos, int endPos) { Random rnd = new Random(); for (int i = startPos; i < endPos; ++i) { int tmp = arr[i]; int randomPos = i + rnd.nextInt(endPos - i); arr[i] = arr[randomPos]; arr[randomPos] = tmp; } } static void shuffleArray(double[] arr, int startPos, int endPos) { Random rnd = new Random(); for (int i = startPos; i < endPos; ++i) { double tmp = arr[i]; int randomPos = i + rnd.nextInt(endPos - i); arr[i] = arr[randomPos]; arr[randomPos] = tmp; } } private static void shuffleArray(char[] arr, int startPos, int endPos) { Random rnd = new Random(); for (int i = startPos; i < endPos; ++i) { char tmp = arr[i]; int randomPos = i + rnd.nextInt(endPos - i); arr[i] = arr[randomPos]; arr[randomPos] = tmp; } } static boolean isPrime(long n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (long i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } static String toString(int[] dp) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < dp.length; i++) sb.append(dp[i] + " "); return sb.toString(); } static String toString(boolean[] dp) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < dp.length; i++) sb.append(dp[i] + " "); return sb.toString(); } static String toString(long[] dp) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < dp.length; i++) sb.append(dp[i] + " "); return sb.toString(); } static String toString(char[] dp) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < dp.length; i++) sb.append(dp[i] + ""); return sb.toString(); } static String toString(int[][] dp) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < dp.length; i++) { for (int j = 0; j < dp[i].length; j++) { sb.append(dp[i][j] + " "); } sb.append('\n'); } return sb.toString(); } static String toString(long[][] dp) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < dp.length; i++) { for (int j = 0; j < dp[i].length; j++) { sb.append(dp[i][j] + " "); } sb.append('\n'); } return sb.toString(); } static String toString(double[][] dp) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < dp.length; i++) { for (int j = 0; j < dp[i].length; j++) { sb.append(dp[i][j] + " "); } sb.append('\n'); } return sb.toString(); } static String toString(char[][] dp) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < dp.length; i++) { for (int j = 0; j < dp[i].length; j++) { sb.append(dp[i][j] + " "); } sb.append('\n'); } return sb.toString(); } static char toChar(int i) { return (char) (i + 48); } static long mod(long a, long m) { return (a%m + m) % m; } static long mod(long num) { return (num % gigamod + gigamod) % gigamod; } // Uses weighted quick-union with path compression. static class UnionFind { private int[] parent; // parent[i] = parent of i private int[] size; // size[i] = number of sites in tree rooted at i // Note: not necessarily correct if i is not a root node private int count; // number of components public UnionFind(int n) { count = n; parent = new int[n]; size = new int[n]; for (int i = 0; i < n; i++) { parent[i] = i; size[i] = 1; } } // Number of connected components. public int count() { return count; } // Find the root of p. public int find(int p) { int root = p; while (root != parent[root]) root = parent[root]; while (p != root) { int newp = parent[p]; parent[p] = root; p = newp; } return root; } public boolean connected(int p, int q) { return find(p) == find(q); } public int numConnectedTo(int node) { return size[find(node)]; } // Weighted union. public void union(int p, int q) { int rootP = find(p); int rootQ = find(q); if (rootP == rootQ) return; // make smaller root point to larger one if (size[rootP] < size[rootQ]) { parent[rootP] = rootQ; size[rootQ] += size[rootP]; } else { parent[rootQ] = rootP; size[rootP] += size[rootQ]; } count--; } public static int[] connectedComponents(UnionFind uf) { // We can do this in nlogn. int n = uf.size.length; int[] compoColors = new int[n]; for (int i = 0; i < n; i++) compoColors[i] = uf.find(i); HashMap<Integer, Integer> oldToNew = new HashMap<>(); int newCtr = 0; for (int i = 0; i < n; i++) { int thisOldColor = compoColors[i]; Integer thisNewColor = oldToNew.get(thisOldColor); if (thisNewColor == null) thisNewColor = newCtr++; oldToNew.put(thisOldColor, thisNewColor); compoColors[i] = thisNewColor; } return compoColors; } } static class UGraph { // Adjacency list. private HashSet<Integer>[] adj; private static final String NEWLINE = "\n"; private int E; public UGraph(int V) { adj = (HashSet<Integer>[]) new HashSet[V]; E = 0; for (int i = 0; i < V; i++) adj[i] = new HashSet<Integer>(); } public void addEdge(int from, int to) { if (adj[from].contains(to)) return; E++; adj[from].add(to); adj[to].add(from); } public HashSet<Integer> adj(int from) { return adj[from]; } public int degree(int v) { return adj[v].size(); } public int V() { return adj.length; } public int E() { return E; } public String toString() { StringBuilder s = new StringBuilder(); s.append(V() + " vertices, " + E() + " edges " + NEWLINE); for (int v = 0; v < V(); v++) { s.append(v + ": "); for (int w : adj[v]) { s.append(w + " "); } s.append(NEWLINE); } return s.toString(); } public static void dfsMark(int current, boolean[] marked, UGraph g) { if (marked[current]) return; marked[current] = true; Iterable<Integer> adj = g.adj(current); for (int adjc : adj) dfsMark(adjc, marked, g); } public static void dfsMark(int current, int from, long[] distTo, boolean[] marked, UGraph g, ArrayList<Integer> endPoints) { if (marked[current]) return; marked[current] = true; if (from != -1) distTo[current] = distTo[from] + 1; HashSet<Integer> adj = g.adj(current); int alreadyMarkedCtr = 0; for (int adjc : adj) { if (marked[adjc]) alreadyMarkedCtr++; dfsMark(adjc, current, distTo, marked, g, endPoints); } if (alreadyMarkedCtr == adj.size()) endPoints.add(current); } public static void bfsOrder(int current, UGraph g) { } public static void dfsMark(int current, int[] colorIds, int color, UGraph g) { if (colorIds[current] != -1) return; colorIds[current] = color; Iterable<Integer> adj = g.adj(current); for (int adjc : adj) dfsMark(adjc, colorIds, color, g); } public static int[] connectedComponents(UGraph g) { int n = g.V(); int[] componentId = new int[n]; Arrays.fill(componentId, -1); int colorCtr = 0; for (int i = 0; i < n; i++) { if (componentId[i] != -1) continue; dfsMark(i, componentId, colorCtr, g); colorCtr++; } return componentId; } public static boolean hasCycle(UGraph ug) { int n = ug.V(); boolean[] marked = new boolean[n]; boolean[] hasCycleFirst = new boolean[1]; for (int i = 0; i < n; i++) { if (marked[i]) continue; hcDfsMark(i, ug, marked, hasCycleFirst, -1); } return hasCycleFirst[0]; } // Helper for hasCycle. private static void hcDfsMark(int current, UGraph ug, boolean[] marked, boolean[] hasCycleFirst, int parent) { if (marked[current]) return; if (hasCycleFirst[0]) return; marked[current] = true; HashSet<Integer> adjc = ug.adj(current); for (int adj : adjc) { if (marked[adj] && adj != parent && parent != -1) { hasCycleFirst[0] = true; return; } hcDfsMark(adj, ug, marked, hasCycleFirst, current); } } } static class Digraph { // Adjacency list. private HashSet<Integer>[] adj; private static final String NEWLINE = "\n"; private int E; public Digraph(int V) { adj = (HashSet<Integer>[]) new HashSet[V]; E = 0; for (int i = 0; i < V; i++) adj[i] = new HashSet<Integer>(); } public void addEdge(int from, int to) { if (adj[from].contains(to)) return; E++; adj[from].add(to); } public HashSet<Integer> adj(int from) { return adj[from]; } public int V() { return adj.length; } public int E() { return E; } public Digraph reversed() { Digraph dg = new Digraph(V()); for (int i = 0; i < V(); i++) for (int adjVert : adj(i)) dg.addEdge(adjVert, i); return dg; } public String toString() { StringBuilder s = new StringBuilder(); s.append(V() + " vertices, " + E() + " edges " + NEWLINE); for (int v = 0; v < V(); v++) { s.append(v + ": "); for (int w : adj[v]) { s.append(w + " "); } s.append(NEWLINE); } return s.toString(); } public static void dfsMark(int source, boolean[] marked, Digraph g) { if (marked[source]) return; marked[source] = true; Iterable<Integer> adj = g.adj(source); for (int adjc : adj) dfsMark(adjc, marked, g); } public static void bfsOrder(int source, Digraph g) { } public static int[] KosarajuSharirSCC(Digraph dg) { int[] id = new int[dg.V()]; Digraph reversed = dg.reversed(); // Gotta perform topological sort on this one to get the stack. Stack<Integer> revStack = Digraph.topologicalSort(reversed); // Initializing id and idCtr. id = new int[dg.V()]; int idCtr = -1; // Creating a 'marked' array. boolean[] marked = new boolean[dg.V()]; while (!revStack.isEmpty()) { int vertex = revStack.pop(); if (!marked[vertex]) sccDFS(dg, vertex, marked, ++idCtr, id); } return id; } private static void sccDFS(Digraph dg, int source, boolean[] marked, int idCtr, int[] id) { marked[source] = true; id[source] = idCtr; for (Integer adjVertex : dg.adj(source)) if (!marked[adjVertex]) sccDFS(dg, adjVertex, marked, idCtr, id); } public static Stack<Integer> topologicalSort(Digraph dg) { // dg has to be a directed acyclic graph. // We'll have to run dfs on the digraph and push the deepest nodes on stack first. // We'll need a Stack<Integer> and a int[] marked. Stack<Integer> topologicalStack = new Stack<Integer>(); boolean[] marked = new boolean[dg.V()]; // Calling dfs for (int i = 0; i < dg.V(); i++) if (!marked[i]) runDfs(dg, topologicalStack, marked, i); return topologicalStack; } static void runDfs(Digraph dg, Stack<Integer> topologicalStack, boolean[] marked, int source) { marked[source] = true; for (Integer adjVertex : dg.adj(source)) if (!marked[adjVertex]) runDfs(dg, topologicalStack, marked, adjVertex); topologicalStack.add(source); } public static boolean hasCycle(Digraph dg) { int n = dg.V(); boolean[] marked = new boolean[n]; boolean[] hasCycleFirst = new boolean[1]; for (int i = 0; i < n; i++) { if (marked[i]) continue; hcDfsMark(i, dg, marked, hasCycleFirst, -1); } return hasCycleFirst[0]; } // Helper for hasCycle. private static void hcDfsMark(int current, Digraph dg, boolean[] marked, boolean[] hasCycleFirst, int parent) { if (marked[current]) return; if (hasCycleFirst[0]) return; marked[current] = true; HashSet<Integer> adjc = dg.adj(current); for (int adj : adjc) { if (marked[adj] && adj != parent && parent != -1) { hasCycleFirst[0] = true; return; } hcDfsMark(adj, dg, marked, hasCycleFirst, current); } } } static class Edge { int from; int to; long weight; public Edge(int from, int to, long weight) { this.from = from; this.to = to; this.weight = weight; } } static class WeightedUGraph { // Adjacency list. private HashSet<Edge>[] adj; private static final String NEWLINE = "\n"; private int E; public WeightedUGraph(int V) { adj = (HashSet<Edge>[]) new HashSet[V]; E = 0; for (int i = 0; i < V; i++) adj[i] = new HashSet<Edge>(); } public void addEdge(int from, int to, int weight) { if (adj[from].contains(new Edge(from, to, weight))) return; E++; adj[from].add(new Edge(from, to, weight)); adj[to].add(new Edge(to, from, weight)); } public HashSet<Edge> adj(int from) { return adj[from]; } public int degree(int v) { return adj[v].size(); } public int V() { return adj.length; } public int E() { return E; } public String toString() { StringBuilder s = new StringBuilder(); s.append(V() + " vertices, " + E() + " edges " + NEWLINE); for (int v = 0; v < V(); v++) { s.append(v + ": "); for (Edge w : adj[v]) { s.append(w.toString() + " "); } s.append(NEWLINE); } return s.toString(); } public static void dfsMark(int current, boolean[] marked, UGraph g) { if (marked[current]) return; marked[current] = true; Iterable<Integer> adj = g.adj(current); for (int adjc : adj) dfsMark(adjc, marked, g); } public static void dfsMark(int current, int from, long[] distTo, boolean[] marked, UGraph g, ArrayList<Integer> endPoints) { if (marked[current]) return; marked[current] = true; if (from != -1) distTo[current] = distTo[from] + 1; HashSet<Integer> adj = g.adj(current); int alreadyMarkedCtr = 0; for (int adjc : adj) { if (marked[adjc]) alreadyMarkedCtr++; dfsMark(adjc, current, distTo, marked, g, endPoints); } if (alreadyMarkedCtr == adj.size()) endPoints.add(current); } public static void bfsOrder(int current, UGraph g) { } public static void dfsMark(int current, int[] colorIds, int color, UGraph g) { if (colorIds[current] != -1) return; colorIds[current] = color; Iterable<Integer> adj = g.adj(current); for (int adjc : adj) dfsMark(adjc, colorIds, color, g); } public static int[] connectedComponents(UGraph g) { int n = g.V(); int[] componentId = new int[n]; Arrays.fill(componentId, -1); int colorCtr = 0; for (int i = 0; i < n; i++) { if (componentId[i] != -1) continue; dfsMark(i, componentId, colorCtr, g); colorCtr++; } return componentId; } public static boolean hasCycle(UGraph ug) { int n = ug.V(); boolean[] marked = new boolean[n]; boolean[] hasCycleFirst = new boolean[1]; for (int i = 0; i < n; i++) { if (marked[i]) continue; hcDfsMark(i, ug, marked, hasCycleFirst, -1); } return hasCycleFirst[0]; } // Helper for hasCycle. private static void hcDfsMark(int current, UGraph ug, boolean[] marked, boolean[] hasCycleFirst, int parent) { if (marked[current]) return; if (hasCycleFirst[0]) return; marked[current] = true; HashSet<Integer> adjc = ug.adj(current); for (int adj : adjc) { if (marked[adj] && adj != parent && parent != -1) { hasCycleFirst[0] = true; return; } hcDfsMark(adj, ug, marked, hasCycleFirst, current); } } } static class FastReader { private BufferedReader bfr; private StringTokenizer st; public FastReader() { bfr = new BufferedReader(new InputStreamReader(System.in)); } String next() { if (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(bfr.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()); } char nextChar() { return next().toCharArray()[0]; } String nextString() { return next(); } int[] nextIntArray(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = nextInt(); return arr; } double[] nextDoubleArray(int n) { double[] arr = new double[n]; for (int i = 0; i < arr.length; i++) arr[i] = nextDouble(); return arr; } long[] nextLongArray(int n) { long[] arr = new long[n]; for (int i = 0; i < n; i++) arr[i] = nextLong(); return arr; } int[][] nextIntGrid(int n, int m) { int[][] grid = new int[n][m]; for (int i = 0; i < n; i++) { char[] line = fr.next().toCharArray(); for (int j = 0; j < m; j++) grid[i][j] = line[j] - 48; } return grid; } } } // NOTES: // ASCII VALUE OF 'A': 65 // ASCII VALUE OF 'a': 97 // Range of long: 9 * 10^18 // ASCII VALUE OF '0': 48 // Primes upto 'n' can be given by (n / (logn)).
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 8
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
8291fa70d26fa08903b903401569c89b
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 B_William_the_Vigilant{ public static void main (String[] args){ FastReader s = new FastReader(); int t=1;//t=s.ni(); for(int test=1;test<=t;test++){ int n=s.ni(),q=s.ni();int ans=0; String S=s.nextLine();char a[]=S.toCharArray(); for(int i=0;i<n-2;i++){ if(count(a,i))ans++; } while(q-->0){ int pos=s.ni()-1; char ch=s.next().charAt(0); if(a[pos]!=ch){ ans=Update(a,ans,pos,-1); a[pos]=ch; ans=Update(a,ans,pos,1); } System.out.println(ans); } } } static int Update(char a[], int ans,int pos, int add){ for(int i=pos-2;i<=pos;i++){ if(i>=0&&((i+2)<a.length)){ if(count(a, i)){ ans=ans+add; } } } return ans; } static boolean count(char[] a,int i){ if(a[i++]=='a'&&a[i++]=='b'&&a[i++]=='c') return true; else return false; } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader(){ br = new BufferedReader(new InputStreamReader(System.in));} int ni() { return Integer.parseInt(next()); } long nl() { return Long.parseLong(next()); } double nd() { return Double.parseDouble(next()); } long[] readArray(int n){ long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] =Long.parseLong(next()); return a; } String next(){ while (st == null || !st.hasMoreElements()) {try {st = new StringTokenizer(br.readLine());} catch (IOException e){e.printStackTrace();}}return st.nextToken();} 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 8
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
e7e27e1f38e43ba9260d3439f5b985b2
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.BigDecimal; import java.util.*; public class Main { private static final int ma = (int) 2e5 + 10; static char[] str; public static void main(String[] args) throws Exception { initReader(); int n=nextInt(); int q=nextInt(); int pos; char c; str=next().toCharArray(); int cnt=0; for (int i=0;i<str.length-2;i++) if (str[i]=='a'&&str[i+1]=='b'&&str[i+2]=='c') cnt++; while (q--!=0){ pos=nextInt(); pos--; c=nextChar(); if (str[pos]!=c){ switch (str[pos]){ case 'a': if (pos<=str.length-3&&str[pos+1]=='b'&&str[pos+2]=='c') cnt--; break; case 'b': if (pos<=str.length-2&&pos>=1&&str[pos-1]=='a'&&str[pos+1]=='c') cnt--; break; case 'c': if (pos<=str.length-1&&pos>=2&&str[pos-2]=='a'&&str[pos-1]=='b') cnt--; break; } switch (c){ case 'a': if (pos<=str.length-3&&str[pos+1]=='b'&&str[pos+2]=='c') cnt++; break; case 'b': if (pos<=str.length-2&&pos>=1&&str[pos-1]=='a'&&str[pos+1]=='c') cnt++; break; case 'c': if (pos<=str.length-1&&pos>=2&&str[pos-2]=='a'&&str[pos-1]=='b') cnt++; break; } str[pos]=c; } pw.println(cnt); } pw.close(); } /***************************************************************************************/ static BufferedReader reader; static StringTokenizer tokenizer; static PrintWriter pw; public static void initReader() throws IOException { reader = new BufferedReader(new InputStreamReader(System.in)); tokenizer = new StringTokenizer(""); pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); // 从文件读写 // reader = new BufferedReader(new FileReader("test.in")); // tokenizer = new StringTokenizer(""); // pw = new PrintWriter(new BufferedWriter(new FileWriter("test1.out"))); } public static boolean hasNext() { try { while (!tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); } } catch (Exception e) { return false; } return true; } public static String next() throws IOException { while (!tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); } return tokenizer.nextToken(); } public static String nextLine() { try { return reader.readLine(); } catch (Exception e) { return null; } } public static int nextInt() throws IOException { return Integer.parseInt(next()); } public static long nextLong() throws IOException { return Long.parseLong(next()); } public static double nextDouble() throws IOException { return Double.parseDouble(next()); } public static char nextChar() throws IOException { return next().charAt(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 8
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
07d683adf4b57c7041e9ed09c18b4314
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 boolean f=false; public static void main (String[] args) throws java.lang.Exception { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int q=sc.nextInt(); String str=sc.next(); char s[]=str.toCharArray(); Set<Integer> se=new HashSet<>(); for(int i=0;i<n-2;i++){ char a=str.charAt(i); char b=str.charAt(i+1); char c=str.charAt(i+2); if(a=='a'&&b=='b'&&c=='c'){ se.add(i); }} // System.out.println(se.size()); while(q>0){ q--; int idx=sc.nextInt(); idx--; char ins=sc.next().charAt(0); if(ins=='a'){ if(idx+2<n){ if(s[idx+1]=='b'&&s[idx+2]=='c'){ se.add(idx); } } if(se.contains(idx-1)){ se.remove(idx-1); } if(se.contains(idx-2)){ se.remove(idx-2); } } else if(ins=='b'){ if(idx+1<n&&idx-1>=0){ if(s[idx+1]=='c'&&s[idx-1]=='a'){ se.add(idx-1); } } if(se.contains(idx)){ se.remove(idx); } if(se.contains(idx-2)){ se.remove(idx-2); } } else if(ins=='c'){ if(idx-2>=0){ if(s[idx-1]=='b'&&s[idx-2]=='a'){ se.add(idx-2); } } if(se.contains(idx)){ se.remove(idx); } if(se.contains(idx-1)){ se.remove(idx-1); } } s[idx]=ins; System.out.println(se.size()); } }}
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 8
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
4ab909ffe82f9e13e5d44914fdf5ed2c
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; public class DeltixRoundB { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); args=(br.readLine()).split(" "); int n=Integer.parseInt(args[0]),q=Integer.parseInt(args[1]); char[] arr=br.readLine().toCharArray(); int co=0,p=0; while(p<n-2){ if(arr[p]=='a' && arr[p+1]=='b' && arr[p+2]=='c'){ co++; p+=3; }else p++; } StringBuffer sb=new StringBuffer(); while(q-->0){ args=(br.readLine()).split(" "); int pos=Integer.parseInt(args[0])-1; char ch=args[1].charAt(0); boolean bef=contains(arr,pos); arr[pos]=ch; boolean af=contains(arr,pos); if (bef != af) { if (!bef) co++; else co--; } sb.append(co).append("\n"); } System.out.println(sb); } private static boolean contains(char[] arr,int pos){ try { if(arr[pos]=='a' && arr[pos+1]=='b' && arr[pos+2]=='c') return true; }catch (IndexOutOfBoundsException ignored){} try { if(arr[pos-1]=='a' && arr[pos]=='b' && arr[pos+1]=='c') return true; }catch (IndexOutOfBoundsException ignored){} try { if(arr[pos-2]=='a' && arr[pos-1]=='b' && arr[pos]=='c') return true; }catch (IndexOutOfBoundsException ignored){} return false; } private static void printArray(int[] arr) { for (int i : arr) { System.out.print(i + " "); } System.out.println(); } private static <T> void printArray(T[] arr) { for (T i : arr) { System.out.print(i + " "); } System.out.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 8
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
3c6e3faaa61edadf67052a859e6011e8
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 WilliamVigilant{ public static void main(String[] args) { Scanner input = new Scanner(System.in); int n = input.nextInt(); int q = input.nextInt(); char[] c = input.next().toCharArray(); int count = 0; for(int i = 0;i<=n-3;i++){ if(c[i] == 'a' && c[i+1] == 'b' && c[i+2] == 'c'){ count++; } } //System.out.println(count); for(int i = 0;i<q;i++){ int x = input.nextInt()-1; char s = input.next().charAt(0); if(c[x] == s){ System.out.println(count); } else{ int change = 0; for(int j = Math.max(0,x-2);j<=Math.min(x,n-3);j++){ if(c[j] == 'a' && c[j+1] == 'b' && c[j+2] == 'c'){ change--; break; } } c[x] = s; for(int j = Math.max(0,x-2);j<=Math.min(x,n-3);j++){ if(c[j] == 'a' && c[j+1] == 'b' && c[j+2] == 'c'){ change++; break; } } count += change; 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 8
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
8a0fa186989502560ba0b2f81d7f7312
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.math.BigInteger; import java.sql.Time; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.Date; import java.util.Deque; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; import java.util.Map; import java.util.PriorityQueue; import java.util.Queue; import java.util.Scanner; import java.util.Stack; import java.util.StringTokenizer; import java.util.TreeMap; public class Practice1 { public static int check(char[] arr,int x,char c) { int n=arr.length; if(c=='a') { if(x+1<n&&arr[x+1]=='b'&& x+2<n&&arr[x+2]=='c') return 1; }else if(c=='b') { if(x-1>=0&&arr[x-1]=='a'&& x+1<n&&arr[x+1]=='c') return 1; }else if(c=='c') { if(x-2>=0&&arr[x-2]=='a'&& x-1<n&&arr[x-1]=='b') return 1; } return 0; } public static void main(String[] args) { PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); // out.print(); //out.println(); FastReader sc=new FastReader(); int n=sc.nextInt(); int q=sc.nextInt(); String str=sc.nextLine(); int count=0; for(int i=0;i<n-2;i++) { if(str.charAt(i)=='a'&&str.charAt(i+1)=='b' &&str.charAt(i+2)=='c') { count++; } } char[] arr=str.toCharArray(); for(int i=0;i<q;i++) { int x=sc.nextInt(); x--; char c=sc.next().charAt(0); //out.println("x : "+x+" c : "+c); int ch1=check(arr,x,arr[x]); int ch2=check(arr,x,c); //out.println("ch1 : "+ch1+" ch2 : "+ch2); if(ch1==0&&ch2==1) { count++; }else if(ch1==1&&ch2==0) { count--; } out.println(count); arr[x]=c; } out.close(); } 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 8
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
60bb2ddc56c256c52cb812b97a1a1228
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: RonWonWon * CREATED: 01.12.2021 01:20:35 /*==========================================================================*/ import java.io.*; import java.util.*; public class B { public static void main(String[] args) throws IOException { int n = in.nextInt(), q = in.nextInt(); char ch[] = in.next().toCharArray(); int cnt = 0; for(int i=0;i<n-2;i++){ if(ch[i]=='a'&&ch[i+1]=='b'&&ch[i+2]=='c') cnt++; } while(q-->0){ int i = in.nextInt()-1; char c = in.next().charAt(0); if(i-1>-1){ if(ch[i-1]=='a'&&ch[i]=='b'&&i+1<n&&ch[i+1]=='c'){ cnt--; } if(i-2>-1){ if(ch[i-2]=='a'&&ch[i-1]=='b'&&ch[i]=='c'){ cnt--; } } } if(i+2<n){ if(ch[i]=='a'&&ch[i+1]=='b'&&ch[i+2]=='c'){ cnt--; } } ch[i] = c; if(i-1>-1){ if(ch[i-1]=='a'&&ch[i]=='b'&&i+1<n&&ch[i+1]=='c'){ cnt++; } if(i-2>-1){ if(ch[i-2]=='a'&&ch[i-1]=='b'&&ch[i]=='c'){ cnt++; } } } if(i+2<n){ if(ch[i]=='a'&&ch[i+1]=='b'&&ch[i+2]=='c'){ cnt++; } } out.println(cnt); } out.flush(); } static FastScanner in = new FastScanner(); static PrintWriter out = new PrintWriter(System.out); static int oo = Integer.MAX_VALUE; static long ooo = Long.MAX_VALUE; static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); void ini() throws FileNotFoundException { br = new BufferedReader(new FileReader("input.txt")); } String next() { while(!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch(IOException e) {} return st.nextToken(); } String nextLine(){ try{ return br.readLine(); } catch(IOException e) { } return ""; } 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()); } long[] readArrayL(int n) { long a[] = new long[n]; for(int i=0;i<n;i++) a[i] = nextLong(); return a; } double nextDouble() { return Double.parseDouble(next()); } double[] readArrayD(int n) { double a[] = new double[n]; for(int i=0;i<n;i++) a[i] = nextDouble(); return a; } } static final Random random = new Random(); static void ruffleSort(int[] a){ int n = a.length; for(int i=0;i<n;i++){ int j = random.nextInt(n), temp = a[j]; a[j] = a[i]; a[i] = temp; } Arrays.sort(a); } static void ruffleSortL(long[] a){ int n = a.length; for(int i=0;i<n;i++){ int j = random.nextInt(n); long temp = a[j]; a[j] = a[i]; a[i] = temp; } Arrays.sort(a); } }
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 8
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