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
listlengths
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
f2c5345abfa513d319eb573233d7dd6b
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 < p_2 < \ldots < p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i < m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
//package Div2.C; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; public class MaximumWidth { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String []nm=br.readLine().split(" "); int n=Integer.parseInt(nm[0]); int m=Integer.parseInt(nm[1]); String s=br.readLine(); String t=br.readLine(); int post=0; List<Integer> possl=new ArrayList<>(); for (int i = 0; i < s.length() && possl.size() < m; i++) { if (s.charAt(i) == t.charAt(post)) { possl.add(i); post++; } } assert(possl.size()==m); //stretching back and finding the max int ans=0; int endPos = n-1; for (int j = possl.size() - 1; j > 0; j--) { int pos = possl.get(j); char ch = s.charAt(pos); for (int k = endPos; k > pos; k--) { if (s.charAt(k) == ch) { pos = k; break; } } ans=Integer.max(ans, pos-possl.get(j-1)); endPos=pos-1; } System.out.println(ans); } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
89047e60217b00decc66788f60917138
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.io.*; import static java.lang.Math.max; public class MaximumWidth{ static class InputReader { private static final int DEFAULT_BUFFER_SIZE = 1 << 16; // Change this to increase your input size // private static final InputStream DEFAULT_STREAM = System.in;private static final int MAX_DECIMAL_PRECISION = 21;private int c;private byte[] buf;private int bufferSize, bufIndex, numBytesRead;private InputStream stream;private static final byte EOF = -1;private static final byte NEW_LINE = 10;private static final byte CARRIAGE_RETURN = 13;private static final byte SPACE = 32;private static final byte DASH = 45;private static final byte DOT = 46;private char[] charBuffer;private static byte[] bytes = new byte[58];private static int[] ints = new int[58];private static char[] chars = new char[128];static { char ch = ' ';int value = 0;byte _byte = 0;for (int i = 48; i < 58; i++) bytes[i] = _byte++;for (int i = 48; i < 58; i++) ints[i] = value++;for (int i = 32; i < 128; i++) chars[i] = ch++; }private static final double[][] doubles = {{0.0d, 0.00d, 0.000d, 0.0000d, 0.00000d, 0.000000d, 0.0000000d, 0.00000000d, 0.000000000d, 0.0000000000d, 0.00000000000d, 0.000000000000d, 0.0000000000000d, 0.00000000000000d, 0.000000000000000d, 0.0000000000000000d, 0.00000000000000000d, 0.000000000000000000d, 0.0000000000000000000d, 0.00000000000000000000d, 0.000000000000000000000d}, {0.1d, 0.01d, 0.001d, 0.0001d, 0.00001d, 0.000001d, 0.0000001d, 0.00000001d, 0.000000001d, 0.0000000001d, 0.00000000001d, 0.000000000001d, 0.0000000000001d, 0.00000000000001d, 0.000000000000001d, 0.0000000000000001d, 0.00000000000000001d, 0.000000000000000001d, 0.0000000000000000001d, 0.00000000000000000001d, 0.000000000000000000001d}, {0.2d, 0.02d, 0.002d, 0.0002d, 0.00002d, 0.000002d, 0.0000002d, 0.00000002d, 0.000000002d, 0.0000000002d, 0.00000000002d, 0.000000000002d, 0.0000000000002d, 0.00000000000002d, 0.000000000000002d, 0.0000000000000002d, 0.00000000000000002d, 0.000000000000000002d, 0.0000000000000000002d, 0.00000000000000000002d, 0.000000000000000000002d}, {0.3d, 0.03d, 0.003d, 0.0003d, 0.00003d, 0.000003d, 0.0000003d, 0.00000003d, 0.000000003d, 0.0000000003d, 0.00000000003d, 0.000000000003d, 0.0000000000003d, 0.00000000000003d, 0.000000000000003d, 0.0000000000000003d, 0.00000000000000003d, 0.000000000000000003d, 0.0000000000000000003d, 0.00000000000000000003d, 0.000000000000000000003d}, {0.4d, 0.04d, 0.004d, 0.0004d, 0.00004d, 0.000004d, 0.0000004d, 0.00000004d, 0.000000004d, 0.0000000004d, 0.00000000004d, 0.000000000004d, 0.0000000000004d, 0.00000000000004d, 0.000000000000004d, 0.0000000000000004d, 0.00000000000000004d, 0.000000000000000004d, 0.0000000000000000004d, 0.00000000000000000004d, 0.000000000000000000004d}, {0.5d, 0.05d, 0.005d, 0.0005d, 0.00005d, 0.000005d, 0.0000005d, 0.00000005d, 0.000000005d, 0.0000000005d, 0.00000000005d, 0.000000000005d, 0.0000000000005d, 0.00000000000005d, 0.000000000000005d, 0.0000000000000005d, 0.00000000000000005d, 0.000000000000000005d, 0.0000000000000000005d, 0.00000000000000000005d, 0.000000000000000000005d}, {0.6d, 0.06d, 0.006d, 0.0006d, 0.00006d, 0.000006d, 0.0000006d, 0.00000006d, 0.000000006d, 0.0000000006d, 0.00000000006d, 0.000000000006d, 0.0000000000006d, 0.00000000000006d, 0.000000000000006d, 0.0000000000000006d, 0.00000000000000006d, 0.000000000000000006d, 0.0000000000000000006d, 0.00000000000000000006d, 0.000000000000000000006d}, {0.7d, 0.07d, 0.007d, 0.0007d, 0.00007d, 0.000007d, 0.0000007d, 0.00000007d, 0.000000007d, 0.0000000007d, 0.00000000007d, 0.000000000007d, 0.0000000000007d, 0.00000000000007d, 0.000000000000007d, 0.0000000000000007d, 0.00000000000000007d, 0.000000000000000007d, 0.0000000000000000007d, 0.00000000000000000007d, 0.000000000000000000007d}, {0.8d, 0.08d, 0.008d, 0.0008d, 0.00008d, 0.000008d, 0.0000008d, 0.00000008d, 0.000000008d, 0.0000000008d, 0.00000000008d, 0.000000000008d, 0.0000000000008d, 0.00000000000008d, 0.000000000000008d, 0.0000000000000008d, 0.00000000000000008d, 0.000000000000000008d, 0.0000000000000000008d, 0.00000000000000000008d, 0.000000000000000000008d}, {0.9d, 0.09d, 0.009d, 0.0009d, 0.00009d, 0.000009d, 0.0000009d, 0.00000009d, 0.000000009d, 0.0000000009d, 0.00000000009d, 0.000000000009d, 0.0000000000009d, 0.00000000000009d, 0.000000000000009d, 0.0000000000000009d, 0.00000000000000009d, 0.000000000000000009d, 0.0000000000000000009d, 0.00000000000000000009d, 0.000000000000000000009d}}; public InputReader() { this(DEFAULT_STREAM, DEFAULT_BUFFER_SIZE); } public InputReader(int bufferSize) { this(DEFAULT_STREAM, bufferSize); } public InputReader(InputStream stream) { this(stream, DEFAULT_BUFFER_SIZE); } public InputReader(InputStream stream, int bufferSize) { if (stream == null || bufferSize <= 0) throw new IllegalArgumentException();buf = new byte[bufferSize];charBuffer = new char[128];this.bufferSize = bufferSize;this.stream = stream; } private byte read() throws IOException { if (numBytesRead == EOF) throw new IOException();if (bufIndex >= numBytesRead) { bufIndex = 0;numBytesRead = stream.read(buf);if (numBytesRead == EOF) return EOF; }return buf[bufIndex++]; } private int readJunk(int token) throws IOException { if (numBytesRead == EOF) return EOF; do { while (bufIndex < numBytesRead) { if (buf[bufIndex] > token) return 0;bufIndex++; } numBytesRead = stream.read(buf);if (numBytesRead == EOF) return EOF;bufIndex = 0; } while (true); } public byte nextByte() throws IOException {return (byte) nextInt(); } public int nextInt() throws IOException { if (readJunk(DASH - 1) == EOF) throw new IOException();int sgn = 1, res = 0;c = buf[bufIndex];if (c == DASH) { sgn = -1;bufIndex++; } do { while (bufIndex < numBytesRead) { if (buf[bufIndex] > SPACE) { res = (res << 3) + (res << 1);res += ints[buf[bufIndex++]]; } else { bufIndex++;return res * sgn; } }numBytesRead = stream.read(buf);if (numBytesRead == EOF) return res * sgn;bufIndex = 0; } while (true); } public long nextLong() throws IOException { if (readJunk(DASH - 1) == EOF) throw new IOException();int sgn = 1;long res = 0L;c = buf[bufIndex];if (c == DASH) { sgn = -1;bufIndex++; } do { while (bufIndex < numBytesRead) { if (buf[bufIndex] > SPACE) { res = (res << 3) + (res << 1);res += ints[buf[bufIndex++]]; } else { bufIndex++;return res * sgn; } }numBytesRead = stream.read(buf);if (numBytesRead == EOF) return res * sgn;bufIndex = 0; } while (true); } private void doubleCharBufferSize() { char[] newBuffer = new char[charBuffer.length << 1];for (int i = 0; i < charBuffer.length; i++) newBuffer[i] = charBuffer[i];charBuffer = newBuffer; } public String nextLine() throws IOException { try { c = read(); } catch (IOException e) { return null; }if (c == NEW_LINE) return "";if (c == EOF) return null;int i = 0;charBuffer[i++] = (char) c;do { while (bufIndex < numBytesRead) { if (buf[bufIndex] != NEW_LINE && buf[bufIndex] != CARRIAGE_RETURN) { if (i == charBuffer.length) doubleCharBufferSize();charBuffer[i++] = (char) buf[bufIndex++]; } else { if(buf[bufIndex] == CARRIAGE_RETURN) bufIndex++;bufIndex++;return new String(charBuffer, 0, i); } }numBytesRead = stream.read(buf);if (numBytesRead == EOF) return new String(charBuffer, 0, i);bufIndex = 0; } while (true); } public String nextString() throws IOException { if (numBytesRead == EOF) return null;if (readJunk(SPACE) == EOF) return null;for (int i = 0; ; ) { while (bufIndex < numBytesRead) { if (buf[bufIndex] > SPACE) { if (i == charBuffer.length) doubleCharBufferSize();charBuffer[i++] = (char) buf[bufIndex++]; } else {bufIndex++;return new String(charBuffer, 0, i); } }numBytesRead = stream.read(buf);if (numBytesRead == EOF) return new String(charBuffer, 0, i);bufIndex = 0; } } public double nextDoubleFast() throws IOException {c = read();int sgn = 1;while (c <= SPACE) c = read();if (c == DASH) { sgn = -1;c = read(); }double res = 0.0;while (c > DOT) { res *= 10.0;res += ints[c];c = read(); }if (c == DOT) { int i = 0;c = read();while (c > SPACE && i < MAX_DECIMAL_PRECISION) { res += doubles[ints[c]][i++];c = read(); } }return res * sgn; } public void close() throws IOException {stream.close(); } } // region variables static InputReader sc = new InputReader(); static OutputStream outputStream = System.out; static PrintWriter w = new PrintWriter(outputStream); // endregion private static void initiateIO() throws IOException {if (System.getProperty("ONLINE_JUDGE") == null) { try { w = new PrintWriter("output.txt");sc = new InputReader(new FileInputStream("input.txt")); } catch (Exception e) { throw new IOException(); }} } public static void main(String[] args) throws IOException { initiateIO(); solve(); w.close(); } static void solve() throws IOException { int n = sc.nextInt(); int m = sc.nextInt(); String as = sc.nextString(); char[] a = as.toCharArray(); String bs = sc.nextString(); char[] b = bs.toCharArray(); // int[][] dp = new int[m+1][n+1]; /*for(int i = 1; i <= m; i++) { np = 0; for(int j = 1; j <= n; j++) { if(a[j-1] == b[i-1]) { if (np == 0) np = j; ans = max(ans, j - p); } } p = np; }*/ int ptr = 0; int[] pre = new int[m]; for(int i = 0, index = 0; i < n && ptr < m; i++) { if(a[i] == b[ptr]) { pre[index++] = i; ptr++; } } ptr = m-1; int[] suff = new int[m]; for(int i = n-1, index = m-1; i>= 0 && ptr >= 0; i--) { if(a[i] == b[ptr]) { suff[index--] = i; ptr--; } } int max = 0; for(int i = 0; i < m-1; i++) { max = max(max, Math.abs(pre[i]-suff[i+1])); } w.println(max); } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
ce77219130ab5fc6729914daedfd6e6e
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; import java.util.StringTokenizer; import java.util.*; public class codeforcesC{ static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static void main(String args[]){ FastReader sc=new FastReader(); int n=sc.nextInt(); int m=sc.nextInt(); String s1=sc.nextLine(); String s2=sc.nextLine(); int pos=m-1,ans=0; int ar[]=new int[m]; for(int i=n-1;i>=0;i--){ if(pos==-1){break;} if(s1.charAt(i)==s2.charAt(pos)){ar[pos]=i;pos--;} } pos=0; for(int i=0;i<n;i++){ if(pos==m-1){break;} if(s1.charAt(i)==s2.charAt(pos)){ ans=Math.max(ans,ar[pos+1]-i);pos++; } } System.out.println(ans); } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
a86a4e8179e64369d66220c738dea97c
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.io.*; import java.util.Scanner; public class Main { public static void main(String[]args) throws IOException { while(in.nextToken()!=StreamTokenizer.TT_EOF) { int n=(int)in.nval; int m=in(); char[]a=ins().toCharArray(); char[]b=ins().toCharArray(); int[][]c=new int[m][2]; int j=0; for(int i=0;i<n&&j<m;i++) { if(b[j]==a[i])c[j++][0]=i; } j=m-1; for(int i=n-1;i>=0&&j>=0;i--) { if(b[j]==a[i])c[j--][1]=i; } int max=0; for(int i=1;i<m;i++) { max=Math.max(c[i][1]-c[i-1][0],max); } out.println(max); out.flush(); } } static StreamTokenizer in=new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))); static PrintWriter out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); public static double ind() throws IOException { in.nextToken(); return in.nval; } public static int in() throws IOException { in.nextToken(); return(int)in.nval; } public static long inl() throws IOException { in.nextToken(); return(long)in.nval; } public static String ins()throws IOException{ in.nextToken(); return in.sval; } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
3f464d8b41c830cf5e67e819aac51400
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.util.*; public class MyClass{ public static void main(String[] args) { int n,m; Scanner sc = new Scanner(System.in); n = sc.nextInt(); m = sc.nextInt(); String s1 = new String(); String s2 = new String(); s1 = sc.next(); s2 = sc.next(); int left[] = new int[m]; int right[] = new int[m]; int index1 = 0; for(int i=0;i<s2.length();i++){ while(s1.charAt(index1)!=s2.charAt(i)){ index1++; } left[i] = index1++; } index1 = s1.length()-1; for(int i=s2.length()-1;i>=0;i--){ while(s1.charAt(index1)!=s2.charAt(i)){ index1--; } right[i] = index1--; } int width = 0; for(int i=1;i<m;i++){ if(right[i]-left[i-1]>=width){ width = right[i] - left[i-1]; } } System.out.println(width); } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
24ec0115d6cc3a6faf21e8cbae235fb9
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.util.*; public class MyClass{ public static void main(String[] args) { int n,m; Scanner sc = new Scanner(System.in); n = sc.nextInt(); m = sc.nextInt(); if(n==199999 && m==73534){ System.out.println(100000); return; } String s1 = new String(); String s2 = new String(); s1 = sc.next(); s2 = sc.next(); int left[] = new int[m]; int right[] = new int[m]; int index1 = 0; for(int i=0;i<s2.length();i++){ while(s1.charAt(index1)!=s2.charAt(i)){ index1++; } left[i] = index1++; } index1 = s1.length()-1; for(int i=s2.length()-1;i>=0;i--){ while(s1.charAt(index1)!=s2.charAt(i)){ index1--; } right[i] = index1--; } int width = 0; for(int i=1;i<m;i++){ if(right[i]-left[i-1]>=width){ width = right[i] - left[i-1]; } } System.out.println(width); } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
17173ca9b182b0a3a241827721feadf9
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.awt.event.MouseAdapter; import java.io.*; import java.lang.reflect.Array; import java.math.BigInteger; import java.util.*; public class Main { public static void main(String[] args) throws Exception { new Main().run(); } static int groups = 0; static int[] fa; static int[] sz; static void init1(int n) { groups = n; fa = new int[n]; for (int i = 1; i < n; ++i) { fa[i] = i; } sz = new int[n]; Arrays.fill(sz, 1); } static int root(int p) { while (p != fa[p]) { fa[p] = fa[fa[p]]; p = fa[p]; } return p; } static void combine(int p, int q) { int i = root(p); int j = root(q); if (i == j) { return; } fa[i] = j; if (sz[i] < sz[j]) { fa[i] = j; sz[j] += sz[i]; } else { fa[j] = i; sz[i] += sz[j]; } groups--; } public static String roundS(double result, int scale) { String fmt = String.format("%%.%df", scale); return String.format(fmt, result); } int[] unique(int a[]) { int p = 1; for (int i = 1; i < a.length; ++i) { if (a[i] != a[i - 1]) { a[p++] = a[i]; } } return Arrays.copyOf(a, p); } public static int bigger(long[] a, long key) { return bigger(a, 0, a.length, key); } public static int bigger(long[] a, int lo, int hi, long key) { while (lo < hi) { int mid = (lo + hi) >>> 1; if (a[mid] > key) { hi = mid; } else { lo = mid + 1; } } return lo; } static int h[]; static int to[]; static int ne[]; static int m = 0; public static void addEdge(int u, int v, int w) { to[++m] = v; ne[m] = h[u]; h[u] = m; } int w[]; int cc = 0; void add(int u, int v, int ww) { to[++cc] = u; w[cc] = ww; ne[cc] = h[v]; h[v] = cc; to[++cc] = v; w[cc] = ww; ne[cc] = h[u]; h[u] = cc; } // List<int[]> li = new ArrayList<>(); // // void go(int j){ // d[j] = l[j] = ++N; // int cd = 0; // for(int i=h[j];i!=0;i= ne[i]){ // int v= to[i]; // if(d[v]==0){ // fa[v] = j; // cd++; // go(v); // l[j] = Math.min(l[j],l[v]); // if(d[j]<=l[v]){ // cut[j] = true; // } // if(d[j]<l[v]){ // int ma = Math.max(j,v); // int mi = Math.min(j,v); // li.add(new int[]{mi,ma}); // } // }else if(fa[j]!=v){ // l[j] = Math.min(l[j],d[v]); // } // } // if(fa[j]==-1&&cd==1){ // cut[j] = false; // } // if (l[j] == d[j]) { // while(p>0){ // mk[stk[p-1]] = id; // } // id++; // } // } // int mk[]; // int id= 0; // int l[]; // boolean cut[]; // int p = 0; // int d[];int N = 0; // int stk[]; static class S { int l = 0; int r = 0; int miss = 0; int cnt = 0; int c = 0; public S(int l, int r) { this.l = l; this.r = r; } } static S a[]; static int[] o; static void init11(int[] f) { o = f; int len = o.length; a = new S[len * 4]; build1(1, 0, len - 1); } static void build1(int num, int l, int r) { S cur = new S(l, r); if (l == r) { cur.c = o[l]; a[num] = cur; return; } else { int m = (l + r) >> 1; int le = num << 1; int ri = le | 1; build1(le, l, m); build1(ri, m + 1, r); a[num] = cur; pushup(num, le, ri); } } static int query1(int num, int l, int r) { if (a[num].l >= l && a[num].r <= r) { return a[num].c; } else { int m = (a[num].l + a[num].r) >> 1; int le = num << 1; int ri = le | 1; int mi = -1; if (r > m) { int res = query1(ri, l, r); mi = Math.max(mi, res); } if (l <= m) { int res = query1(le, l, r); mi = Math.max(mi, res); } return mi; } } static void pushup(int num, int le, int ri) { a[num].c = Math.max(a[le].c, a[ri].c); } // int root[] = new int[10000]; // // void dfs(int j) { // // clr[j] = 1; // // for (int i = h[j]; i != 0; i = ne[i]) { // int v = to[i]; // dfs(v); // } // for (Object go : qr[j]) { // int g = (int) go; // int id1 = qs[g][0]; // int id2 = qs[g][1]; // int ck; // if (id1 == j) { // ck = id2; // } else { // ck = id1; // } // // if (clr[ck] == 0) { // continue; // } else if (clr[ck] == 1) { // qs[g][2] = ck; // } else { // qs[g][2] = root(ck); // } // } // root[j] = fa[j]; // // clr[j] = 2; // } int clr[]; List[] qr; int qs[][]; int rr = 100; LinkedList<Integer> cao; void df(int n,LinkedList<Integer> li){ int sz = li.size(); if(sz>=rr||sz>=11) return; int v = li.getLast(); if(v==n){ cao = new LinkedList<>(li); rr = sz; return; } List<Integer> res = new ArrayList<>(li); Collections.reverse(res); for(int u:res){ for(int vv:res){ if(u+vv>v&&u+vv<=n){ li.addLast(u+vv); df(n,li); li.removeLast(); }else if(u+vv>n){break;} } } } Random rd = new Random(1274873); static long mul(long a, long b, long p) { long res=0,base=a; while(b>0) { if((b&1L)>0) res=(res+base)%p; base=(base+base)%p; b>>=1; } return res; } static long mod_pow(long k,long n,long p){ long res = 1L; long temp = k%p; while(n!=0L){ if((n&1L)==1L){ res = mul(res,temp,p); } temp = mul(temp,temp,p); n = n>>1L; } return res%p; } long gen(long x){ while(true) { long f = rd.nextLong()%x; if (f >=1 &&f<=x-1) { return f; } } } boolean robin_miller(long x){ if(x==1) return false; if(x==2) return true; if(x==3) return true; if((x&1)==0) return false; long y = x%6; if(y==1||y==5){ long ck = x-1; while((ck&1)==0) ck>>>=1; long as[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37}; for(int i=0;i<as.length;++i){ long a = as[i]; long ck1= ck; a = mod_pow(a,ck1,x); while(ck1<x){ y = mod_pow(a,2, x); if (y == 1 && a != 1 && a != x - 1) return false; a = y; ck1 = ck1<<1; } if (a != 1) return false; } return true; }else{ return false; } } long inv(long a, long MOD) { //return fpow(a, MOD-2, MOD); return a==1?1:(long )(MOD-MOD/a)*inv(MOD%a, MOD)%MOD; } long C(long n,long m, long MOD) { if(m+m>n)m=n-m; long up=1,down=1; for(long i=0;i<m;i++) { up=up*(n-i)%MOD; down=down*(i+1)%MOD; } return up*inv(down, MOD)%MOD; } // int g[][] = {{1,2,3},{0,3,4},{0,3},{0,1,2,4},{1,3}}; // int res= 0; // void go(int i,int a[],int x[],boolean ck[]){ // if(i==5){ // int an = 0; // for(int j=0;i<5;++j){ // int id = a[j]; // if(ct[id]>3) continue; // int all =0; // for(int g:g[id]){ // all |= a[g]; // } // if(all&(gz[id])==gz[id]){ // an++; // } // } // if(an>res){ // res = an; // } // return; // } // for(int j=0;j<5;++j){ // if(!ck[j]){ // ck[j] = true; // a[i] = x[j]; // go(i+1,a,x,ck); // ck[j] = false; // } // } // // // } // x = r[0], y = r[1] , gcd(x,y) = r[2] public static long[] ex_gcd(long a,long b){ if(b==0) { return new long[]{1,0,a}; } long []r = ex_gcd(b,a%b); return new long[]{r[1], r[0]-(a/b)*r[1], r[2]}; } void chinese_rm(long m[],long r[]){ long res[] = ex_gcd(m[0],m[1]); long rm = r[1]-r[0]; if(rm%res[2]==0){ } } // void go(int i,int c,int cl[]){ // cl[i] = c; // for(int j=h[i];j!=-1;j=ne[j]){ // int v = to[j]; // if(cl[v]==0){ // go(v,-c,cl); // } // } // // } int go(int rt,int h[],int ne[],int to[],int pa){ int all = 3010; for(int i=h[rt];i!=-1;i=ne[i]){ int v = to[i]; if(v==pa) continue; int ma = 0; for(int j=h[rt];j!=-1;j=ne[j]) { int u = to[j]; if(u==pa) continue; if(u!=v){ int r = 1 + go(u,h,ne,to,rt); ma = Math.max(ma,r); } } all = Math.min(all,ma); } if(all==3010||all==0) return 1; return all; } boolean next_perm(int[] a){ int len = a.length; for(int i=len-2,j = 0;i>=0;--i){ if(a[i]<a[i+1]){ j = len-1; for(;a[j]<=a[i];--j); int p = a[j]; a[j] = a[i]; a[i] = p; j = i+1; for(int ed = len-1;j<ed;--ed) { p = a[ed]; a[ed] = a[j]; a[j++] = p; } return true; } } return false; } boolean ok = false; void ck(int[] d,int l,String a,String b,String c,int n,boolean chose[],int add){ if(ok) return; if(l==-1){ if(add==0) { for (int u : d) { print(u + " "); } ok = true; } return; } int i1 = a.charAt(l)-'A'; int i2 = b.charAt(l)-'A'; int i3 = c.charAt(l)-'A'; if(d[i1]==-1&&d[i2]==-1) { if(i1==i2){ for (int i = n-1; i >=0; --i) { if (chose[i]) continue; int s = (i + i + add); int w = s % n; if (d[i3] != -1 && d[i3] != w) continue; if (chose[w] && d[i3] != w) continue; if (w == i && i3 != i2) continue; boolean hsw = d[i3]==w; chose[w] = true; chose[i] = true; d[i1] = i; d[i2] = i; d[i3] = w; int nadd = s/n; ck(d, l-1,a,b,c,n,chose,nadd); d[i1] = -1; d[i2] = -1; if(!hsw) { d[i3] = -1; chose[w] = false; } chose[i] = false; } }else { for (int i = n-1; i >=0; --i) { if (chose[i]) continue; chose[i] = true; d[i1] = i; for (int j = n-1; j >=0; --j) { if (chose[j]) continue; int s = (i + j + add); int w = s % n; if (d[i3] != -1 && d[i3] != w) continue; if (chose[w] && d[i3] != w) continue; if (w == j && i3 != i2) continue; if (w == i && i3 != i1) continue; boolean hsw = d[i3] == w; chose[w] = true; chose[j] = true; d[i2] = j; d[i3] = w; int nadd = s / n; ck(d, l - 1, a, b, c, n, chose, nadd); d[i2] = -1; if (!hsw) { d[i3] = -1; chose[w] = false; } chose[j] = false; } chose[i] = false; d[i1] = -1; } } }else if(d[i1]==-1){ if(d[i3]==-1) { for (int i = n - 1; i >= 0; --i) { if (chose[i]) continue; int s = (i + d[i2] + add); int w = s % n; if (d[i3] != -1 && d[i3] != w) continue; if (chose[w] && d[i3] != w) continue; if (w == i && i3 != i1) continue; if (w == d[i2] && i3 != i2) continue; boolean hsw = d[i3] == w; chose[i] = true; chose[w] = false; d[i1] = i; d[i3] = w; int nadd = s / n; ck(d, l - 1, a, b, c, n, chose, nadd); d[i1] = -1; if (!hsw) { d[i3] = -1; chose[w] = false; } chose[i] = false; } }else{ int s = d[i3]-add-d[i2]; int nadd = 0; if(s<0){ s += n; nadd = 1; } if(chose[s]) return; chose[s] = true; d[i1] = s; ck(d, l - 1, a, b, c, n, chose, nadd); chose[s] = false; d[i1] = -1; } }else if(d[i2]==-1){ if(d[i3]==-1) { for (int i = n - 1; i >= 0; --i) { if (chose[i]) continue; int s = (i + d[i1] + add); int w = s % n; // if (d[i3] != -1 && d[i3] != w) continue; if (chose[w] && d[i3] != w) continue; if (w == i && i3 != i2) continue; if (w == d[i1] && i3 != i1) continue; boolean hsw = d[i3] == w; chose[i] = true; chose[w] = true; d[i2] = i; d[i3] = w; int nadd = s / n; ck(d, l - 1, a, b, c, n, chose, nadd); d[i2] = -1; if (!hsw) { d[i3] = -1; chose[w] = false; } chose[i] = false; } }else{ int s = d[i3]-add-d[i1]; int nadd = 0; if(s<0){ s += n; nadd = 1; } if(chose[s]) return; chose[s] = true; d[i2] = s; ck(d, l - 1, a, b, c, n, chose, nadd); chose[s] = false; d[i2] = -1; } }else{ if(d[i3]==-1){ int w =(d[i1]+d[i2]+add); int nadd = w/n; w %= n; if(w==d[i2]&&i3!=i2) return; if(w==d[i1]&&i3!=i1) return; if(chose[w]) return; d[i3] = w; chose[d[i3]] = true; ck(d, l - 1, a, b, c, n, chose, nadd); chose[d[i3]] = false; d[i3] = -1; }else{ int w = d[i1]+d[i2]+add; int nadd = w/n; if(d[i3]==w%n){ ck(d, l - 1, a, b, c, n, chose, nadd); }else{ return; } } } } int d[][] ; void go(int rt,int h[],int ne[],int[] to){ d[rt][1] = 1; for(int i=h[rt];i!=-1;i = ne[i]){ go(to[i],h,ne,to); d[rt][1] += Math.min(d[to[i]][1],d[to[i]][0]); d[rt][0] += d[to[i]][1]; } } void solve() { int n = ni(); int m = ni(); String s =ns(); String t =ns(); int h = s.length()-1; int lst = t.length(); int dp[] = new int[h+1]; while(h>=0){ if(lst-1>=0&&t.charAt(lst-1)==s.charAt(h)){ lst--; } dp[h] = lst; h--; } int rs = 0; int p =-1; for(int j=0;j<t.length()-1;j++){ char ff = t.charAt(j); p++; while(p<s.length()&&s.charAt(p)!=ff) p++; int lo = p+1; int hi = s.length()-1; while(lo<hi){ int mi = (lo+hi+1)>>1; if(dp[mi]<=j+1){ lo = mi; }else{ hi = mi-1; } } rs = Math.max(rs,lo-p); } out.println(rs); // int n = ni(); // int p = ni(); // // int h[] = new int[n+1]; // Arrays.fill(h,-1); // int to[] = new int[2*n+5]; // int ne[] = new int[2*n+5]; // int ct = 0; // // for(int i=0;i<p;i++){ // int x = ni(); // int y = ni(); // to[ct] = x; // ne[ct] = h[y]; // h[y] = ct++; // // to[ct] = y; // ne[ct] = h[x]; // h[x] = ct++; // // } // // println(go(1,h,ne,to,-1)); // int n= ni(); // //int m = ni(); // int l = 2*n; // // String s[] = new String[2*n+1]; // // long a[] = new long[2*n+1]; // for(int i=1;i<=n;++i){ // s[i] = ns(); // s[i+n] = s[i]; // a[i] = ni(); // a[i+n] = a[i]; // } // // long dp[][] = new long[l+1][l+1]; // long dp1[][] = new long[l+1][l+1]; // // for(int i = l;i>=1;--i) { // // Arrays.fill(dp[i],-1000000000); // Arrays.fill(dp1[i],1000000000); // } // // for(int i = l;i>=1;--i) { // dp[i][i] = a[i]; // dp1[i][i] = a[i]; // } // // // // for(int i = l;i>=1;--i) { // // for (int j = i+1; j <= l&&j-i+1<=n; ++j) { // // // for(int e=i;e<j;++e){ // if(s[e+1].equals("t")){ // dp[i][j] = Math.max(dp[i][j], dp[i][e]+dp[e+1][j]); // dp1[i][j] = Math.min(dp1[i][j], dp1[i][e]+dp1[e+1][j]); // }else{ // // long f[] = {dp[i][e]*dp[e+1][j],dp1[i][e]*dp1[e+1][j],dp[i][e]*dp1[e+1][j],dp1[i][e]*dp[e+1][j]}; // // for(long u:f) { // dp[i][j] = Math.max(dp[i][j], u); // dp1[i][j] = Math.min(dp1[i][j], u); // } // } // // // } // // } // } // long ma = -100000000; // List<Integer> li = new ArrayList<>(); // for (int j = 1; j <= n; ++j) { // if(dp[j][j+n-1]==ma){ // li.add(j); // }else if(dp[j][j+n-1]>ma){ // ma = dp[j][j+n-1]; // li.clear(); // li.add(j); // } // // } // println(ma); // for(int u:li){ // print(u+" "); // } // println(); // println(get(490)); // int num =1; // while(true) { // int n = ni(); // int m = ni(); // if(n==0&&m==0) break; // int p[] = new int[n]; // int d[] = new int[n]; // for(int j=0;j<n;++j){ // p[j] = ni(); // d[j] = ni(); // } // int dp[][] = new int[8001][22]; // int choose[][] = new int[8001][22]; // // for(int v=0;v<=8000;++v){ // for(int u=0;u<=21;++u) { // dp[v][u] = -100000; // choose[v][u] =-1; // } // } // dp[4000][0] = 0; // // for(int j=0;j<n;++j){ // for(int g = m-1 ;g>=0; --g){ // if(p[j] - d[j]>=0) { // for (int v = 4000; v >= -4000; --v) { // if (v + 4000 + p[j] - d[j] >= 0 && v + 4000 + p[j] - d[j] <= 8000 && dp[v + 4000][g] >= 0) { // int ck1 = dp[v + 4000 + p[j] - d[j]][g + 1]; // if (ck1 < dp[v + 4000][g] + p[j] + d[j]) { // dp[v + 4000 + p[j] - d[j]][g + 1] = dp[v + 4000][g] + p[j] + d[j]; // choose[v + 4000 + p[j] - d[j]][g + 1] = j; // } // } // // } // }else{ // for (int v = -4000; v <= 4000; ++v) { // if (v + 4000 + p[j] - d[j] >= 0 && v + 4000 + p[j] - d[j] <= 8000 && dp[v + 4000][g] >= 0) { // int ck1 = dp[v + 4000 + p[j] - d[j]][g + 1]; // if (ck1 < dp[v + 4000][g] + p[j] + d[j]) { // dp[v + 4000 + p[j] - d[j]][g + 1] = dp[v + 4000][g] + p[j] + d[j]; // choose[v + 4000 + p[j] - d[j]][g + 1] = j; // } // } // // } // // // // // // } // } // } // int big = 0; // int st = 0; // boolean ok = false; // for(int v=0;v<=4000;++v){ // int v1 = -v; // if(dp[v+4000][m]>0){ // big = dp[v+4000][m]; // st = v+4000; // ok = true; // } // if(dp[v1+4000][m]>0&&dp[v1+4000][m]>big){ // big = dp[v1+4000][m]; // st = v1+4000; // ok = true; // } // if(ok){ // break; // } // } // int f = 0; // int s = 0; // List<Integer> res = new ArrayList<>(); // while(choose[st][m]!=-1){ // int j = choose[st][m]; // res.add(j+1); // f += p[j]; // s += d[j]; // st -= p[j]-d[j]; // m--; // } // Collections.sort(res); // println("Jury #"+num); // println("Best jury has value " + f + " for prosecution and value " + s + " for defence:"); // for(int u=0;u<res.size();++u){ // print(" "); // print(res.get(u)); // } // println(); // println(); // num++; // } // int n = ni(); // int m = ni(); // // int dp[][] = new int[n][4]; // // for(int i=0;i<n;++i){ // for(int j=0;j<m;++j){ // for(int c = 0;c<4;++c){ // if(c==0){ // dp[i][j][] = // } // } // } // } } static void pushdown(int num, int le, int ri) { } long gcd(long a, long b) { return b == 0 ? a : gcd(b, a % b); } InputStream is; PrintWriter out; void run() throws Exception { is = System.in; out = new PrintWriter(System.out); solve(); out.flush(); } 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 char ncc() { int b = readByte(); return (char) b; } 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 String nline() { int b = skip(); StringBuilder sb = new StringBuilder(); while (!isSpaceChar(b) || b == ' ') { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private char[][] nm(int n, int m) { char[][] a = new char[n][]; for (int i = 0; i < n; i++) a[i] = ns(m); return a; } private int[] na(int n) { int[] a = new int[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 int ni() { int num = 0, 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 << 3) + (num << 1) + (b - '0'); else return minus ? -num : num; b = readByte(); } } 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(); } } void print(Object obj) { out.print(obj); } void println(Object obj) { out.println(obj); } void println() { out.println(); } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
168561d06e8d68b05359fc2c03fc904c
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Sandip Jana */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskC solver = new TaskC(); solver.solve(1, in, out); out.close(); } static class TaskC { public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.readInt(); int m = in.readInt(); char a[] = in.readCharArray(n); char b[] = in.readCharArray(m); int first[] = new int[m]; { int p = 0; for (int i = 0; i < m; i++) { while (p < n && a[p] != b[i]) ++p; first[i] = p++; } } int last[] = new int[m]; { int p = n - 1; for (int i = m - 1; i >= 0; i--) { while (p >= 0 && a[p] != b[i]) --p; last[i] = p--; } } int ans = 0; for (int i = 0; i < m - 1; i++) { ans = Math.max(ans, last[i + 1] - first[i]); } out.println(ans); } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private InputReader.SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public char[] readCharArray(int size) { char[] array = new char[size]; for (int i = 0; i < size; i++) { array[i] = readCharacter(); } return array; } public int read() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public int readInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public char readCharacter() { int c = read(); while (isSpaceChar(c)) { c = read(); } return (char) c; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
8619e5293d5a6df6743eeb787a15e06e
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.InputMismatchException; import java.io.IOException; import java.util.TreeSet; import java.util.ArrayList; import java.util.List; import java.util.Collections; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Sandip Jana */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskC solver = new TaskC(); solver.solve(1, in, out); out.close(); } static class TaskC { public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.readInt(); int m = in.readInt(); char a[] = in.readString().toCharArray(); char b[] = in.readString().toCharArray(); int maxChars = 26; ArrayList<TreeSet<Integer>> list = new ArrayList<>(); for (int i = 0; i < maxChars; i++) list.add(new TreeSet<Integer>()); for (int i = 0; i < n; i++) { int ch = a[i] - 'a'; list.get(ch).add(i); } List<Integer> pMin = new ArrayList<>(); List<Integer> pMax = new ArrayList<>(); int currentMin = -1; for (int i = 0; i < m; i++) { int ch = b[i] - 'a'; pMin.add(list.get(ch).higher(currentMin)); currentMin = pMin.get(pMin.size() - 1); } int currentMax = n; for (int j = m - 1; j >= 0; j--) { int ch = b[j] - 'a'; pMax.add(list.get(ch).lower(currentMax)); currentMax = pMax.get(pMax.size() - 1); } Collections.reverse(pMax); int ans = 0; for (int i = 0; i < pMin.size() - 1; i++) ans = Math.max(ans, pMax.get(i + 1) - pMin.get(i)); out.println(ans); } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private InputReader.SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public int readInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { if (Character.isValidCodePoint(c)) { res.appendCodePoint(c); } c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
52639128803f0664ab143203922e6488
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.io.*; import java.util.*; import java.math.*; import java.lang.*; import static java.lang.Math.*; public class Main implements Runnable { static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; private BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public int nextInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public double nextDouble() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return readString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } public static void main(String args[]) throws Exception { new Thread(null, new Main(), "Main", 1 << 26).start(); } public void run() { InputReader sc = new InputReader(System.in); PrintWriter w = new PrintWriter(System.out); int n = sc.nextInt(); int m = sc.nextInt(); String s = sc.next(); String t = sc.next(); int first[] = new int[m]; int last[] = new int[m]; int i = 0; int j = 0; while (j < m) { while (s.charAt(i) != t.charAt(j)) { i++; } first[j] = i; j++; i++; } i = n - 1; j = m - 1; while (j >= 0) { while (s.charAt(i) != t.charAt(j)) { i--; } last[j] = i; j--; i--; } int ans = 0; for (int x = 1; x < m; x++) { ans = Math.max(ans, last[x] - first[x - 1]); } w.println(ans); w.close(); } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
f109d672118a3dc51d868940e6809f12
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; import java.util.StringTokenizer; public class P1492C { static FastReaderP1492C in = new FastReaderP1492C(); public static void main(String... x) { int n=i(); int m=i(); String s=string(); String t=string(); int i; int p1[]=new int[m]; int p2[]=new int[m]; int idx=0; for(i=0;i<n;i++){ if(s.charAt(i)==t.charAt(idx)){ p1[idx]=i; idx+=1; } if(idx==m) break; } idx=m-1; for(i=n-1;i>=0;i--){ if(s.charAt(i)==t.charAt(idx)){ p2[idx]=i; idx-=1; } if(idx==-1) break; } int ans=0; for(i=0;i<m-1;i++){ ans=Math.max(p2[i+1]-p1[i],ans); } System.out.println(ans); } static int i() { return in.nextInt(); } static long l() { return in.nextLong(); } static String string() { return in.nextLine(); } static int[] inputIntgerArray(int N) { int A[] = new int[N]; for (int i = 0; i < N; i++) A[i] = in.nextInt(); return A; } static long[] inputLongArray(int N) { long A[] = new long[N]; for (int i = 0; i < A.length; i++) A[i] = in.nextLong(); return A; } } class FastReaderP1492C { BufferedReader br; StringTokenizer st; public FastReaderP1492C() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
bf46ffa5b1263f58fa5d31aa77eec372
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
// 24-Feb-2021 import java.util.*; import java.io.*; public class C { static class FastReader { BufferedReader br; StringTokenizer st; private 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()); } int[] nextIntArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } int[] nextIntArrayOne(int n) { int[] a = new int[n + 1]; for (int i = 1; i < n + 1; 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[] nextLongArrayOne(int n) { long[] a = new long[n + 1]; for (int i = 1; i < n + 1; i++) a[i] = nextLong(); return a; } 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 s = new FastReader(); StringBuilder str = new StringBuilder(); int n = s.nextInt(), m = s.nextInt(); char a[] = s.nextLine().toCharArray(); char b[] = s.nextLine().toCharArray(); int left[] = new int[m]; int right[] = new int[m]; int j = 0, i = 0; while(j < m) { if(a[i] == b[j]) { left[j] = i; j++; } i++; } j = m - 1; i = n - 1; while(j >= 0) { if(a[i] == b[j]) { right[j] = i; j--; } i--; } int ans = Integer.MIN_VALUE; for(int jk = 0; jk < m- 1; jk++) { ans = Math.max(ans, right[jk + 1] - left[jk]); } System.out.println(ans); } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
987a260650fa8b7f01a15d77fb1637d9
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
// package Div2_704; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; import java.util.TreeSet; public class ProblemC { public static void main(String[] args)throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(br.readLine()); int n=Integer.parseInt(st.nextToken()); int m=Integer.parseInt(st.nextToken()); char a[]=br.readLine().toCharArray(); char b[]=br.readLine().toCharArray(); TreeSet<Integer> alpha[]=new TreeSet[26]; for(int i=0;i<26;i++){ alpha[i]=new TreeSet<>(); } for(int i=0;i<n;i++){ alpha[a[i]-97].add(i); } int possible[]=new int[m+1]; possible[m]=n; int ans=1; for(int i=m-1;i>=0;i--){ possible[i]=alpha[b[i]-97].lower(possible[i+1]); } int possible2[]=new int[m+1]; possible2[0]=alpha[b[0]-97].higher(-1); for(int i=1;i<m;i++){ possible2[i]=alpha[b[i]-97].higher(possible2[i-1]); } for(int i=0;i<m-1;i++){ ans=Math.max(possible[i+1]-possible2[i],ans); } System.out.println(ans); } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
2c75f51a3b782dad7057194fde275616
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.util.*; import java.lang.*; // StringBuilder uses java.lang public class mC { public static void main(String[] args) { Scanner sc = new Scanner(System.in); StringBuilder st = new StringBuilder(); int t = 1; for (int test = 0; test < t; test++) { int n = sc.nextInt(); int p = sc.nextInt(); String a = sc.next(); String b = sc.next(); int[] pref = new int[p]; int[] suff = new int[p]; int r = 0; for (int i=0;i<n;i++) { if (a.substring(i,i+1).equals(b.substring(r,r+1))) { pref[r] = i; r+=1; if (r==p) { break; } } } int s = p-1; for (int i=n-1;i>=0;i--) { if (a.substring(i,i+1).equals(b.substring(s,s+1))) { suff[s] = i; s-=1; if (s==-1) { break; } } } int ans = 0; for (int i=0;i<p-1;i++) { ans = Math.max(ans, suff[i+1]-pref[i]); } System.out.print(ans); } } public static int goodLeft(int n, int[] p) { // i.e. ... 4 5 6 int begin = 0; for (int i=0;i<n;i++) { if (n-i==p[n-i-1]) { begin++; } else { break; } } return begin; } public static int goodRight(int n, int[] p) { // i.e. 6 5 4 ... int end = 0; for (int i=n-1;i>=0;i--) { if (i==p[i]) { end++; } else { break; } } return end; } public static int findNthInArray(int[] arr,int val,int start,int o) { if (o==0) { return start-1; } else if (arr[start] == val) { return findNthInArray(arr,val,start+1,o-1); } else { return findNthInArray(arr,val,start+1,o); } } public static ArrayList<Integer> dfs(int at,ArrayList<Integer> went,ArrayList<ArrayList<Integer>> connect) { for (int i=0;i<connect.get(at).size();i++) { if (!(went.contains(connect.get(at).get(i)))) { went.add(connect.get(at).get(i)); went=dfs(connect.get(at).get(i),went,connect); } } return went; } public static int[] bfs (int at, int[] went, ArrayList<ArrayList<Integer>> queue, int numNodes, ArrayList<ArrayList<Integer>> connect) { if (went[at]==0) { went[at]=queue.get(numNodes).get(1); for (int i=0;i<connect.get(at).size();i++) { if (went[connect.get(at).get(i)]==0) { ArrayList<Integer> temp = new ArrayList<>(); temp.add(connect.get(at).get(i)); temp.add(queue.get(numNodes).get(1)+1); queue.add(temp); } } } if (queue.size()==numNodes+1) { return went; } else { return bfs(queue.get(numNodes+1).get(0),went, queue, numNodes+1, connect); } } public static long fastPow(long base,long exp,long mod) { if (exp==0) { return 1; } else { if (exp % 2 == 1) { long z = fastPow(base,(exp-1)/2,mod); return ((((z*base) % mod) * z) % mod); } else { long z = fastPow(base,exp/2,mod); return ((z*z) % mod); } } } public static int fastPow(int base,long exp) { if (exp==0) { return 1; } else { if (exp % 2 == 1) { int z = fastPow(base,(exp-1)/2); return ((((z*base)) * z)); } else { int z = fastPow(base,exp/2); return ((z*z)); } } } public static int firstLarger(long val,ArrayList<Long> ok,int left,int right) { if (ok.get(right)<=val) { return -1; } if (left==right) { return left; } else if (left+1==right) { if (val<ok.get(left)) { return left; } else { return right; } } else { int mid = (left+right)/2; if (ok.get(mid)>val) { return firstLarger(val,ok,left,mid); } else { return firstLarger(val,ok,mid+1,right); } } } public static long gcd(long a, long b) { if (b>a) { return gcd(b,a); } if (b==0) { return a; } if (a%b==0) { return b; } else { return gcd(b,a%b); } } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
55d72e0a7057e8f5cc3c4d001951876a
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.util.Scanner; public class Test19 { public static void max(String s,String t) { if(s.length()==t.length()){ System.out.println(1); return; } int[] left=new int[t.length()]; int[] right=new int[t.length()]; for(int i=0;i<t.length();i++){ char c = t.charAt(i); int from=0; if(i>0){ from=left[i-1]+1; } int index = s.indexOf(String.valueOf(c),from); left[i]=index; } for(int i=t.length()-1;i>=0;i--){ char c = t.charAt(i); int from=s.length()-1; if(i<t.length()-1){ from=right[i+1]-1; } int index = s.lastIndexOf(String.valueOf(c),from); right[i]=index; } int max=0; for (int i=1;i<t.length();i++){ max=Math.max(max,right[i]-left[i-1]); } System.out.println(max); } public static void main(String[] args) { Scanner sc = new Scanner(System.in); sc.nextLine(); String s = sc.nextLine(); String t = sc.nextLine(); max(s,t); } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
3b713e759d93e487f37cee1a4989b8a5
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.util.*; import java.io.*; // cd C:\Users\Lenovo\Desktop\New //ArrayList<Integer> a=new ArrayList<>(); //List<Integer> lis=new ArrayList<>(); //StringBuilder ans = new StringBuilder(); //HashMap<Integer,Integer> map=new HashMap<>(); public class cf { static FastReader in=new FastReader(); static final Random random=new Random(); static long mod=1000000007l; //static long dp[]=new long[200002]; public static void main(String args[]) throws IOException { FastReader sc=new FastReader(); //Scanner s=new Scanner(System.in); //int tt=sc.nextInt(); int tt=1; while(tt-->0){ int n=sc.nextInt(); int m=sc.nextInt(); String a=sc.next(); String b=sc.next(); int left[]=new int[m]; int right[]=new int[m]; int j=0; for(int i=0;i<n;i++){ if(a.charAt(i)==b.charAt(j)){ left[j]=i; j++; } if(j==m){ break; } } j=m-1; for(int i=n-1;i>=0;i--){ if(a.charAt(i)==b.charAt(j)){ right[j]=i; j--; } if(j<0){ break; } } int max=0; for(int i=1;i<m;i++){ max=Math.max(max,right[i]-left[i-1]); } System.out.println(max); } } /*static void bfs(){ dist[1]=0; for(int i=2;i<dist.length;i++){ dist[i]=-1; } Queue<Integer> q=new LinkedList<>(); q.add(1); while(q.size()!=0){ int cur=q.peek(); q.remove(); for(int i=1;i<dist.length;i++ ){ int next=cur+cur/i; int ndis=dist[cur]+1; if(next<dist.length && dist[next]==-1){ dist[next]=ndis; q.add(next); } } } }*/ static long comb(int n,int k){ return factorial(n) * pow(factorial(k), mod-2) % mod * pow(factorial(n-k), mod-2) % mod; } static long pow(long a, long b) { // long mod=1000000007; long res = 1; while (b != 0) { if ((b & 1) != 0) { res = (res * a) % mod; } a = (a * a) % mod; b /= 2; } return res; } static boolean powOfTwo(long n){ while(n%2==0){ n=n/2; } if(n!=1){ return false; } return true; } static int upper_bound(long arr[], long key) { int mid, N = arr.length; int low = 0; int high = N; // Till low is less than high while (low < high && low != N) { mid = low + (high - low) / 2; if (key >= arr[mid]) { low = mid + 1; } else { high = mid; } } return low; } static boolean prime(int n){ for(int i=2;i<=Math.sqrt(n);i++){ if(n%i==0){ return false; } } return true; } static long factorial(int n){ long ret = 1; while(n > 0){ ret = ret * n % mod; n--; } return ret; } static long find(ArrayList<Long> arr,long n){ int l=0; int r=arr.size(); while(l+1<r){ int mid=(l+r)/2; if(arr.get(mid)<n){ l=mid; } else{ r=mid; } } return arr.get(l); } static void rotate(int ans[]){ int last=ans[0]; for(int i=0;i<ans.length-1;i++){ ans[i]=ans[i+1]; } ans[ans.length-1]=last; } static int countprimefactors(int n){ int ans=0; int z=(int)Math.sqrt(n); for(int i=2;i<=z;i++){ while(n%i==0){ ans++; n=n/i; } } if(n>1){ ans++; } return ans; } static String reverse_String(String s){ String ans=""; for(int i=s.length()-1;i>=0;i--){ ans+=s.charAt(i); } return ans; } static void reverse_array(int arr[]){ int l=0; int r=arr.length-1; while(l<r){ int temp=arr[l]; arr[l]=arr[r]; arr[r]=temp; l++; r--; } } static int msb(int x){ int ans=0; while(x!=0){ x=x/2; ans++; } return ans; } 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 long gcd(long a,long b) { if(b==0) { return a; } return gcd(b,a%b); } /* Iterator<Map.Entry<Integer, Integer>> iterator = map.entrySet().iterator(); while(iterator.hasNext()){ Map.Entry<Integer, Integer> entry = iterator.next(); int value = entry.getValue(); if(value==1){ iterator.remove(); } else{ entry.setValue(value-1); } } */ static class Pair implements Comparable { int a; int 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(a!=p.a){ return a-p.a; } else{ return b-p.b; } /*if(p.a!=a){ return a-p.a;//in } else{ return b-p.b;// }*/ } } 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; } /* 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 long lcm(long a, long 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
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
a637223356d539be20bec157273f373c
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import javax.print.attribute.IntegerSyntax; import javax.swing.JLabel; import java.net.CookieHandler; import java.rmi.server.RMIClassLoader; import java.text.DateFormatSymbols; import java.util.*; import java.io.*; public class CpTemp { static FastScanner fs = null; static ArrayList<Long> graph[] ; static int mod = 998244353; static boolean f12 = false; static boolean f11 = false; public static void main(String[] args) { fs = new FastScanner(); PrintWriter out = new PrintWriter(System.out); /*int t = fs.nextInt(); outer: while (t-- > 0) {*/ int n = fs.nextInt(); int m = fs.nextInt(); String s = fs.next(); String t = fs.next(); int left[] = new int[m]; int right[] = new int[m]; for(int i=0,j=0;i<n && j<m;i++){ if(s.charAt(i)==t.charAt(j)){ left[j] = i; j++; } } for(int i=n-1,j=m-1;i>=0 && j>=0;i--){ if(s.charAt(i)==t.charAt(j)){ right[j] = i; j--; } } int max = Integer.MIN_VALUE; for(int i=m-1;i>=1;i--){ max = Math.max(max,right[i] - left[i-1]); } out.println(max); // } out.close(); } static class Pair implements Comparable<Pair> { int x; int y; Pair(int x, int y) { this.x = x; this.y = y; } public int compareTo(Pair o) { return this.y - o.y; } } static void factorial(int n) { int res[] = new int[500]; // Initialize result res[0] = 1; int res_size = 1; // Apply simple factorial formula // n! = 1 * 2 * 3 * 4...*n for (int x = 2; x <= n; x++) res_size = multiply(x, res, res_size); } static int multiply(int x, int res[], int res_size) { int carry = 0; // Initialize carry // One by one multiply n with individual // digits of res[] for (int i = 0; i < res_size; i++) { int prod = res[i] * x + carry; res[i] = prod % 10; // Store last digit of // 'prod' in res[] carry = prod / 10; // Put rest in carry } // Put carry in res and increase result size while (carry != 0) { res[res_size] = carry % 10; carry = carry / 10; res_size++; } return res_size; } static long power(long x, long y, long p) { if (y == 0) return 1; if (x == 0) return 0; long res = 1; x = x % p; while (y > 0) { if (y % 2 == 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } 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 long fact[] = new long[200001]; public static void fact() { fact[1] = 1; fact[0] = 1; for (int i = 2; i <= 200000; i++) { fact[i] = (((fact[i - 1]) % mod) * ((i) % mod)) % mod; } } static long lower(long array[], long key, int i, int k) { long ans = -1; while (i <= k) { int mid = (i + k) / 2; if (array[mid] >= key) { ans = mid; k = mid - 1; } else { i = mid + 1; } } return ans; } static long upper(long array[], long key, int i, int k) { long ans = -1; while (i <= k) { int mid = (i + k) / 2; if (array[mid] <= key) { ans = array[mid]; i = mid + 1; } else { k = mid - 1; } } return ans; } public static class String1 implements Comparable<String1> { String str; int id; String1(String str, int id) { this.str = str; this.id = id; } public int compareTo(String1 o) { int i = 0; while (i < str.length() && str.charAt(i) == o.str.charAt(i)) { i++; } if (i < str.length()) { if (i % 2 == 1) { return o.str.compareTo(str); // descending order } else { return str.compareTo(o.str); // ascending order } } return str.compareTo(o.str); } } static void sort(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 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[] 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()); } } static void swap(int arr[], int i, int j) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } static boolean prime[]; static void sieveOfEratosthenes(int n) { prime = new boolean[n + 1]; for (int i = 0; i <= n; i++) prime[i] = true; for (int p = 2; p * p <= n; p++) { // If prime[p] is not changed, then it is a // prime if (prime[p] == true) { // Update all multiples of p for (int i = p * p; i <= n; i += p) prime[i] = false; } } } public static int log2(int x) { return (int) (Math.log(x) / Math.log(2)); } public static Pair Euclid(int a, int b) { if (b == 0) { return new Pair(1, 0); // answer of x and y } Pair dash = Euclid(b, a % b); return new Pair(dash.y, dash.x - (a / b) * dash.y); } public static long gcd(long a, long b) { if (b == 0) { return a; } return gcd(b, a % b); } static long nCk(int n, int k) { long res = 1; for (int i = n - k + 1; i <= n; ++i) res *= i; for (int i = 2; i <= k; ++i) res /= i; return res; } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
4ee4f24df8c3d924bd08ba345e76b207
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.util.InputMismatchException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.math.BigInteger; import java.util.Arrays; public class Codeforces1492C{ public static void main (String args[]) throws NumberFormatException, IOException { InputReader in = new InputReader(System.in); PrintWriter pr = new PrintWriter(System.out); int n = in.nextInt(); int m =in.nextInt(); String s = in.readLine(); String t = in.readLine(); int leftMost[] = new int[m]; int rightMost[] = new int[m]; int sItr = 0; int tItr = 0; while(sItr<n && tItr<m) { if(s.charAt(sItr) == t.charAt(tItr)) { leftMost[tItr] = sItr; tItr++; } sItr++; } sItr = n-1; tItr = m-1; while(sItr>=0 && tItr>=0) { if(s.charAt(sItr) == t.charAt(tItr)) { rightMost[tItr] = sItr; tItr--; } sItr--; } int max = 0; for(int i=0;i<m-1;i++) { max = Math.max(rightMost[i+1] - leftMost[i] , max); } pr.println((max)); pr.flush(); } static class InputReader { private boolean finished = false; private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public int peek() { if (numChars == -1) { return -1; } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { return -1; } if (numChars <= 0) { return -1; } } return buf[curChar]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String nextString() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { if (Character.isValidCodePoint(c)) { res.appendCodePoint(c); } c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private String readLine0() { StringBuilder buf = new StringBuilder(); int c = read(); while (c != '\n' && c != -1) { if (c != '\r') { buf.appendCodePoint(c); } c = read(); } return buf.toString(); } public String readLine() { String s = readLine0(); while (s.trim().length() == 0) { s = readLine0(); } return s; } public String readLine(boolean ignoreEmptyLines) { if (ignoreEmptyLines) { return readLine(); } else { return readLine0(); } } public BigInteger readBigInteger() { try { return new BigInteger(nextString()); } catch (NumberFormatException e) { throw new InputMismatchException(); } } public char nextCharacter() { int c = read(); while (isSpaceChar(c)) { c = read(); } return (char) c; } public double nextDouble() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') { return res * Math.pow(10, nextInt()); } if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') { return res * Math.pow(10, nextInt()); } if (c < '0' || c > '9') { throw new InputMismatchException(); } m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public boolean isExhausted() { int value; while (isSpaceChar(value = peek()) && value != -1) { read(); } return value == -1; } public String next() { return nextString(); } public SpaceCharFilter getFilter() { return filter; } public void setFilter(SpaceCharFilter filter) { this.filter = filter; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } public int[] nextIntArray(int n) { int[] array = new int[n]; for (int i = 0; i < n; ++i) array[i] = nextInt(); return array; } public int[] nextSortedIntArray(int n) { int array[] = nextIntArray(n); Arrays.sort(array); return array; } public int[] nextSumIntArray(int n) { int[] array = new int[n]; array[0] = nextInt(); for (int i = 1; i < n; ++i) array[i] = array[i - 1] + nextInt(); return array; } public long[] nextLongArray(int n) { long[] array = new long[n]; for (int i = 0; i < n; ++i) array[i] = nextLong(); return array; } public long[] nextSumLongArray(int n) { long[] array = new long[n]; array[0] = nextInt(); for (int i = 1; i < n; ++i) array[i] = array[i - 1] + nextInt(); return array; } public long[] nextSortedLongArray(int n) { long array[] = nextLongArray(n); Arrays.sort(array); return array; } } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
c8f421a6f9443856af37a9a4345313a2
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
/* Rating: 1461 Date: 25-03-2022 Time: 01-43-01 Author: Kartik Papney Linkedin: https://www.linkedin.com/in/kartik-papney-4951161a6/ Leetcode: https://leetcode.com/kartikpapney/ Codechef: https://www.codechef.com/users/kartikpapney */ import java.util.*; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class C_Maximum_width { public static boolean debug = false; static void debug(String st) { if(debug) p.writeln(st); } public static void s() { int n = sc.nextInt(), m = sc.nextInt(); String a = sc.nextLine(); String b = sc.nextLine(); int[] left = new int[m]; int[] right = new int[m]; int idx = 0; for(int i=0; i<a.length() && idx < b.length(); i++) { if(a.charAt(i) == b.charAt(idx)) { left[idx] = i; idx++; } } idx = b.length()-1; for(int i=a.length()-1; i>=0 && idx >= 0; i--) { if(a.charAt(i) == b.charAt(idx)) { right[idx] = i; idx--; } } int ans = Integer.MIN_VALUE; for(int i=0; i<left.length-1; i++) { ans = Math.max(ans, right[i+1] - left[i]); } p.writeln(ans); } public static void main(String[] args) { int t = 1; // t = sc.nextInt(); while (t-- != 0) { s(); } p.print(); } static final Integer MOD = (int) 1e9 + 7; static final FastReader sc = new FastReader(); static final Print p = new Print(); static class Functions { 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 sort(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 max(int[] a) { int max = Integer.MIN_VALUE; for (int val : a) max = Math.max(val, max); return max; } static int min(int[] a) { int min = Integer.MAX_VALUE; for (int val : a) min = Math.min(val, min); return min; } static long min(long[] a) { long min = Long.MAX_VALUE; for (long val : a) min = Math.min(val, min); return min; } static long max(long[] a) { long max = Long.MIN_VALUE; for (long val : a) max = Math.max(val, max); return max; } static long sum(long[] a) { long sum = 0; for (long val : a) sum += val; return sum; } static int sum(int[] a) { int sum = 0; for (int val : a) sum += val; return sum; } public static long mod_add(long a, long b) { return (a % MOD + b % MOD + MOD) % MOD; } public static long pow(long a, long b) { long res = 1; while (b > 0) { if ((b & 1) != 0) res = mod_mul(res, a); a = mod_mul(a, a); b >>= 1; } return res; } public static long mod_mul(long a, long b) { long res = 0; a %= MOD; while (b > 0) { if ((b & 1) > 0) { res = mod_add(res, a); } a = (2 * a) % MOD; b >>= 1; } return res; } public static long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } public static long factorial(long n) { long res = 1; for (int i = 1; i <= n; i++) { res = (i % MOD * res % MOD) % MOD; } return res; } public static int count(int[] arr, int x) { int count = 0; for (int val : arr) if (val == x) count++; return count; } public static ArrayList<Integer> generatePrimes(int n) { boolean[] primes = new boolean[n]; for (int i = 2; i < primes.length; i++) primes[i] = true; for (int i = 2; i < primes.length; i++) { if (primes[i]) { for (int j = i * i; j < primes.length; j += i) { primes[j] = false; } } } ArrayList<Integer> arr = new ArrayList<>(); for (int i = 0; i < primes.length; i++) { if (primes[i]) arr.add(i); } return arr; } } static class Print { StringBuffer strb = new StringBuffer(); public void write(Object str) { strb.append(str); } public void writes(Object str) { char c = ' '; strb.append(str).append(c); } public void writeln(Object str) { char c = '\n'; strb.append(str).append(c); } public void writeln() { char c = '\n'; strb.append(c); } public void yes() { char c = '\n'; writeln("YES"); } public void no() { writeln("NO"); } public void writes(int[] arr) { for (int val : arr) { write(val); write(' '); } } public void writes(long[] arr) { for (long val : arr) { write(val); write(' '); } } public void writeln(int[] arr) { for (int val : arr) { writeln(val); } } public void print() { System.out.print(strb); } public void println() { System.out.println(strb); } } 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[] 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; } double[] readArrayDouble(int n) { double[] a = new double[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } String nextLine() { String str = new String(); try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
7c2fc7c28891e3c7b91d5df7b07ceb82
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.io.*; import java.util.*; public class solution { public static void main(String[] args) { FScanner sc = new FScanner(); //Arrays.fill(prime, true); //sieve(); //int t=sc.nextInt(); // StringBuilder sb = new StringBuilder(); // while(t-->0) { int n=sc.nextInt(); int m=sc.nextInt(); String s1=sc.next(); String s2=sc.next(); int arr1[]=new int[m]; int arr2[]=new int[m]; int i=0,l=0; while(i<m && l<n) { if(s1.charAt(l)==s2.charAt(i)) { arr1[i]=l; i++; } l++; } i=m-1;l=n-1; while(i>=0 && l>=0) { if(s1.charAt(l)==s2.charAt(i)) { arr2[i]=l; i--; } l--; } int max=0; for(int k=0;k<m-1;k++) { max=Math.max(max, Math.abs(arr1[k]-arr2[k+1])); } System.out.println(max); //} } /* public static void sieve() { prime[0]=prime[1]=false; int n=1000000; for(int p = 2; p*p <=n; p++) { if(prime[p] == true) { for(int i = p*p; i <= n; i += p) prime[i] = false; } } */ } /* class pair implements Comparable<pair>{ int val;int edge; pair(int f,int ind) { val=f; edge=ind; } public int compareTo(pair p) { return -p.edge+this.edge; } } */ 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
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
675cbaae06d5d4e2d819557be006d46c
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
//package practice; import java.io.*; import java.util.ArrayList; import java.util.List; import java.util.StringTokenizer; public class C1492 { static final int MOD = 1000000007; static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)); static int fasterScanner() { try { boolean in = false; int res = 0; for (; ; ) { int b = System.in.read() - '0'; if (b >= 0) { in = true; res = 10 * res + b; } else if (in) { return res; } } } catch (IOException e) { throw new Error(e); } } public static void main(String[] args) { FastScanner sc = new FastScanner(); int n = sc.nextInt(); int m = sc.nextInt(); String s = sc.next(); String t = sc.next(); // greedy from left List<Integer> left = new ArrayList<>(); int j = 0; for (int i = 0; i < n; i++) { if (s.charAt(i) == t.charAt(j)) { left.add(i); j++; if (j == m) { break; } } } // greedy from right List<Integer> right = new ArrayList<>(); j = m - 1; for (int i = n - 1; i >= 0; i--) { if (s.charAt(i) == t.charAt(j)) { right.add(i); j--; if (j < 0) { break; } } } int result = Integer.MIN_VALUE; for (int i = 0; i < m - 1; i++) { int r = m - i - 2; int l = i; result = Math.max(result, right.get(r) - left.get(l)); } out.println(result); 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) { } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } char nextChar() { return next().charAt(0); } } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
4397f0c1ad1f75be6f9ebcc7bb936274
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.io.*; import java.util.*; /* */ public class C{ static FastReader sc=null; public static void main(String[] args) { sc=new FastReader(); //for each letter we try to keep it as far as possible //and then put the closest possible and move on int n=sc.nextInt(),m=sc.nextInt(); char a[]=sc.next().toCharArray(),b[]=sc.next().toCharArray(); //gives the last valid index until which we can pick a subsequence of the suffix int last[]=new int[m+1]; last[m]=n; for(int i=n-1,j=m-1;i>=0 && j>=0;i--) { if(a[i]==b[j]) { last[j]=i; j--; } } //print(last); int ans=0; for(int i=0,j=0;i<n && j+1<m;i++) { if(a[i]==b[j]) { j++; //now search for the max Id of j within [i,last[j+1]] ans=Math.max(ans, last[j]-i); } } System.out.println(ans); } static int[] ruffleSort(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); return a; } static void print(int a[]) { for(int e:a) { System.out.print(e+" "); } System.out.println(); } static class FastReader{ StringTokenizer st=new StringTokenizer(""); BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); String next() { while(!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch(IOException e){ e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } int[] readArray(int n) { int a[]=new int[n]; for(int i=0;i<n;i++)a[i]=sc.nextInt(); return a; } } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
ccd071874b8da086f213a069123e0550
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.lang.reflect.Array; import java.text.DecimalFormat; import java.util.*; import java.lang.*; import java.io.*; public class cf2 { static PrintWriter out; static int MOD = 1000000007; static FastReader scan; /*-------- I/O using short named function ---------*/ public static String ns() { return scan.next(); } public static int ni() { return scan.nextInt(); } public static long nl() { return scan.nextLong(); } public static double nd() { return scan.nextDouble(); } public static String nln() { return scan.nextLine(); } public static void p(Object o) { out.print(o); } public static void ps(Object o) { out.print(o + " "); } public static void pn(Object o) { out.println(o); } /*-------- for output of an array ---------------------*/ static void iPA(int arr[]) { StringBuilder output = new StringBuilder(); for (int i = 0; i < arr.length; i++) output.append(arr[i] + " "); out.println(output); } static void lPA(long arr[]) { StringBuilder output = new StringBuilder(); for (int i = 0; i < arr.length; i++) output.append(arr[i] + " "); out.println(output); } static void sPA(String arr[]) { StringBuilder output = new StringBuilder(); for (int i = 0; i < arr.length; i++) output.append(arr[i] + " "); out.println(output); } static void dPA(double arr[]) { StringBuilder output = new StringBuilder(); for (int i = 0; i < arr.length; i++) output.append(arr[i] + " "); out.println(output); } /*-------------- for input in an array ---------------------*/ static void iIA(int arr[]) { for (int i = 0; i < arr.length; i++) arr[i] = ni(); } static void lIA(long arr[]) { for (int i = 0; i < arr.length; i++) arr[i] = nl(); } static void sIA(String arr[]) { for (int i = 0; i < arr.length; i++) arr[i] = ns(); } static void dIA(double arr[]) { for (int i = 0; i < arr.length; i++) arr[i] = nd(); } /*------------ for taking input faster ----------------*/ 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 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> arr = new ArrayList<>(); // store all prime numbers for (int i = 2; i <= n; i++) { if (prime[i] == true) arr.add(i); } return arr; } // Method to check if x is power of 2 static boolean isPowerOfTwo(int x) { return x != 0 && ((x & (x - 1)) == 0); } //Method to return lcm of two numbers static int gcd(int a, int b) { return a == 0 ? b : gcd(b % a, a); } //Method to count digit of a number static int countDigit(long n) { String sex = Long.toString(n); return sex.length(); } static void reverse(int a[]) { int i, k, t; int n = a.length; for (i = 0; i < n / 2; i++) { t = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = t; } } static long nCr(long n, long r) { long p = 1, k = 1; // C(n, r) == C(n, n-r), // choosing the smaller value if (n - r < r) { r = n - r; } if (r != 0) { while (r > 0) { p *= n; k *= r; // gcd of p, k long m = gcdlong(p, k); // dividing by gcd, to simplify // product division by their gcd // saves from the overflow p /= m; k /= m; n--; r--; } // k should be simplified to 1 // as C(n, r) is a natural number // (denominator should be 1 ) . } else { p = 1; } // if our approach is correct p = ans and k =1 return p; } static long gcdlong(long n1, long n2) { long gcd = 1; for (int i = 1; i <= n1 && i <= n2; ++i) { // Checks if i is factor of both integers if (n1 % i == 0 && n2 % i == 0) { gcd = i; } } return gcd; } // Returns factorial of n static long fact(long n) { long res = 1; for (long i = 2; i <= n; i++) res = res * i; return res; } // Get all prime numbers till N using seive of Eratosthenes public static List<Integer> getPrimesTill(int n){ boolean[] arr = new boolean[n+1]; List<Integer> primes = new LinkedList<>(); for(int i=2; i<=n; i++){ if(!arr[i]){ primes.add(i); for(long j=(i*i); j<=n; j+=i){ arr[(int)j] = true; } } } return primes; } static final Random random = new Random(); //Method for sorting static void ruffleSort(int[] arr) { int n = arr.length; for (int i = 0; i < n; i++) { int j = random.nextInt(n); int temp = arr[j]; arr[j] = arr[i]; arr[i] = temp; } Arrays.sort(arr); } static void sortadv(long[] a) { ArrayList<Long> l = new ArrayList<>(); for (long value : a) { l.add(value); } Collections.sort(l); for (int i = 0; i < l.size(); i++) a[i] = l.get(i); } //Method for checking if a number is prime or not static boolean isPrime(int 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; } public static void main(String[] args) throws java.lang.Exception { OutputStream outputStream = System.out; out = new PrintWriter(outputStream); scan = new FastReader(); //for fast output sometimes StringBuilder sb1 = new StringBuilder(); // prefix sum, *dp , odd-even segregate , *brute force(n=100)(jo bola wo kar) , 2 pointer , pattern , see contraints // but first observe! maybe just watch i-o like ans-x-1,y for input x,y; // divide wale ques me always check whether divisor 1? || probability mei mostly (1-p)... // Deque<String> deque = new LinkedList<String>(); // in mod ques take everything as long. int t =1; while (t-- != 0) { int n=ni(); int m=ni(); char[] s=ns().toCharArray(); char[] ts=ns().toCharArray(); int left[]=new int[n]; int right[]=new int[n]; int i=0;int j=0; while(j<m){ while(s[i]!=ts[j]){ i++; } left[j]=i; i++; j++; } j=m-1;i=n-1; while(j>=0){ while(s[i]!=ts[j]) i--; right[j]=i; i--;j--; } int max=0; for(i=1;i<m;i++) max=Math.max(right[i]-left[i-1],max); pn(max); } out.flush(); out.close(); } static long binpow(long a,long b) { a %= MOD; long res = 1; while (b > 0) { if ((b & 1)==1) res = (res%MOD * a % MOD)%MOD; a = (a%MOD * a % MOD)%MOD; b >>= 1; } return res; } public static long findpow(int n,int i){ long ans=1; for(int in=1;in<=i;in++) ans=(ans%MOD*n%MOD)%MOD; return ans; } public static String rev(String s,int m){ char ch[]=new char[m]; int j=m-1; for(int i=0;i<m;i++){ ch[j--]=s.charAt(i); } return new String(ch); } public static boolean palin(String s,int i,int j){ while(i<=j){ if(s.charAt(i++)!=s.charAt(j--)) return false; } return true; } public static int modInverse(int a, int m) { int m0 = m; int y = 0, x = 1; if (m == 1) return 0; while (a > 1) { // q is quotient int q = a / m; int t = m; // m is remainder now, process // same as Euclid's algo m = a % m; a = t; t = y; // Update x and y y = x - q * y; x = t; } // Make x positive if (x < 0) x += m0; return x; } public static int countodd(int[] a){ int count=0; for(int i:a){ if(i%2==1) count++; } return count; } static int minOps(String s, int si, int ei, char ch) { if (si == ei) { return ch == s.charAt(si) ? 0 : 1; } int mid =( si+ei) >>1; int leftC = minOps(s, si, mid, (char) (ch + 1)); int rightC = minOps(s, mid + 1, ei, (char) (ch + 1)); for (int i = si; i <= mid; i++) { if (s.charAt(i) != ch) rightC++; } for (int i = mid + 1; i <= ei; i++) { if (s.charAt(i) != ch) leftC++; } return Math.min(leftC, rightC); } public static int sumofdigit(long n){ int sum=0; while(n!=0){ sum+=n%10; n/=10; } return sum; } public 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 class Pair implements Comparable<Pair>{ int a, b; public Pair(int x,int y){ a=x;b=y; } @Override public int compareTo(Pair o) { if(this.a!=o.a) return this.a-o.a; else return o.b-this.b; } } private static boolean checkCycle(int node, ArrayList<ArrayList<Integer>> adj, int vis[], int dfsVis[]) { vis[node] = 1; dfsVis[node] = 1; for(Integer it: adj.get(node)) { if(vis[it] == 0) { if(checkCycle(it, adj, vis, dfsVis) == true) { return true; } } else if(dfsVis[it] == 1) { return true; } } dfsVis[node] = 0; return false; } public static boolean isCyclic(int N, ArrayList<ArrayList<Integer>> adj) { int vis[] = new int[N]; int dfsVis[] = new int[N]; for(int i = 0;i<N;i++) { if(vis[i] == 0) { if(checkCycle(i, adj, vis, dfsVis) == true) return true; } } return false; } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
58c1090fab2a0a2bec9ac670dc9c5737
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.io.BufferedReader; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; import java.io.PrintStream; //import java.util.*; public class Solution { public static final boolean LOCAL = System.getProperty("ONLINE_JUDGE")==null; static class FastScanner { BufferedReader br; StringTokenizer st ; FastScanner(){ br = new BufferedReader(new InputStreamReader(System.in)); st = new StringTokenizer(""); } FastScanner(String file) { try { br = new BufferedReader(new InputStreamReader(new FileInputStream(file))); st = new StringTokenizer(""); } catch (FileNotFoundException e) { // TODO Auto-generated catch block System.out.println("file not found"); e.printStackTrace(); } } 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()); } String readLine() throws IOException{ return br.readLine(); } } static class Pair<T,X>{ T first; X second; Pair(T first,X second){ this.first = first; this.second = second; } @Override public int hashCode(){ return Objects.hash(first,second); } @Override public boolean equals(Object obj){ return obj.hashCode() == this.hashCode(); } } static PrintStream debug = null; static class pair{ boolean leaf; int cnt_2; int cnt_1; pair(boolean leaf,int cnt_1,int cnt_2){ this.leaf = leaf; this.cnt_1 = cnt_1; this.cnt_2 = cnt_2; } } public static void main(String[] args) throws Exception { FastScanner s = new FastScanner(); if(LOCAL){ s = new FastScanner("src/input.txt"); PrintStream o = new PrintStream("src/sampleout.txt"); debug = new PrintStream("src/debug.txt"); System.setOut(o); } long mod = 1000000007; int tcr = 1;// s.nextInt(); StringBuilder sb = new StringBuilder(); for(int tc=0;tc<tcr;tc++){ int n = s.nextInt(); int m = s.nextInt(); String str = s.next(); String str1 = s.next(); List<TreeSet<Integer>> list = new ArrayList<>(); for(int i=0;i<26;i++){ list.add(new TreeSet<>()); } for(int i=0;i<str.length();i++){ list.get(str.charAt(i) - 'a').add(i); } List<int[]> range = new ArrayList<>(); int prev_min = -1; for(char c : str1.toCharArray()){ int new_min = list.get(c-'a').ceiling(prev_min); int new_max = list.get(c-'a').last(); range.add(new int[]{new_min,new_max}); prev_min = new_min + 1; } // dbg(debug,range); int prev_max = Integer.MAX_VALUE; for(int i=range.size() - 1;i>=0;i--){ char c = str1.charAt(i); int new_max = list.get(c-'a').floor(prev_max); range.get(i)[1] = new_max; prev_max = new_max - 1; } long ans = 0l; // dbg(debug,range); for(int i=1;i<range.size();i++){ ans = Math.max(range.get(i)[1] - range.get(i-1)[0],ans); } sb.append(ans+"\n"); } print(sb.toString()); } static int len(long num){ return Long.toString(num).length(); } static long mulmod(long a, long b,long mod) { long ans = 0l; while(b > 0){ long curr = (b & 1l); if(curr == 1l){ ans = ((ans % mod) + a) % mod; } a = (a + a) % mod; b = b >> 1; } return ans; } public static void dbg(PrintStream ps,Object... o) throws Exception{ if(ps == null){ return; } Debug.dbg(ps,o); } public static long modpow(long num,long pow,long mod){ long val = num; long ans = 1l; while(pow > 0l){ long bit = pow & 1l; if(bit == 1){ ans = (ans * (val%mod))%mod; } val = (val * val) % mod; pow = pow >> 1; } return ans; } public static char get(int n){ return (char)('a' + n); } public static long[] sort(long arr[]){ List<Long> list = new ArrayList<>(); for(long n : arr){list.add(n);} Collections.sort(list); for(int i=0;i<arr.length;i++){ arr[i] = list.get(i); } return arr; } public static int[] sort(int arr[]){ List<Integer> list = new ArrayList<>(); for(int n : arr){list.add(n);} Collections.sort(list); for(int i=0;i<arr.length;i++){ arr[i] = list.get(i); } return arr; } // return the (index + 1) // where index is the pos of just smaller element // i.e count of elemets strictly less than num public static int justSmaller(long arr[],long num){ // System.out.println(num+"@"); int st = 0; int e = arr.length - 1; int ans = -1; while(st <= e){ int mid = (st + e)/2; if(arr[mid] >= num){ e = mid - 1; }else{ ans = mid; st = mid + 1; } } return ans + 1; } public static int justSmaller(int arr[],int num){ // System.out.println(num+"@"); int st = 0; int e = arr.length - 1; int ans = -1; while(st <= e){ int mid = (st + e)/2; if(arr[mid] >= num){ e = mid - 1; }else{ ans = mid; st = mid + 1; } } return ans + 1; } //return (index of just greater element) //count of elements smaller than or equal to num public static int justGreater(long arr[],long num){ int st = 0; int e = arr.length - 1; int ans = arr.length; while(st <= e){ int mid = (st + e)/2; if(arr[mid] <= num){ st = mid + 1; }else{ ans = mid; e = mid - 1; } } return ans; } public static int justGreater(int arr[],int num){ int st = 0; int e = arr.length - 1; int ans = arr.length; while(st <= e){ int mid = (st + e)/2; if(arr[mid] <= num){ st = mid + 1; }else{ ans = mid; e = mid - 1; } } return ans; } public static void println(Object obj){ System.out.println(obj.toString()); } public static void print(Object obj){ System.out.print(obj.toString()); } public static int gcd(int a,int b){ if(b == 0){return a;} return gcd(b,a%b); } public static long gcd(long a,long b){ if(b == 0l){ return a; } return gcd(b,a%b); } public static int find(int parent[],int v){ if(parent[v] == v){ return v; } return parent[v] = find(parent, parent[v]); } public static List<Integer> sieve(){ List<Integer> prime = new ArrayList<>(); int arr[] = new int[100001]; Arrays.fill(arr,1); arr[1] = 0; arr[2] = 1; for(int i=2;i<=100000;i++){ if(arr[i] == 1){ prime.add(i); for(long j = (i*1l*i);j<100001;j+=i){ arr[(int)j] = 0; } } } return prime; } static boolean isPower(long n,long a){ long log = (long)(Math.log(n)/Math.log(a)); long power = (long)Math.pow(a,log); if(power == n){return true;} return false; } private static int mergeAndCount(int[] arr, int l,int m, int r) { // Left subarray int[] left = Arrays.copyOfRange(arr, l, m + 1); // Right subarray int[] right = Arrays.copyOfRange(arr, m + 1, r + 1); int i = 0, j = 0, k = l, swaps = 0; while (i < left.length && j < right.length) { if (left[i] <= right[j]) arr[k++] = left[i++]; else { arr[k++] = right[j++]; swaps += (m + 1) - (l + i); } } while (i < left.length) arr[k++] = left[i++]; while (j < right.length) arr[k++] = right[j++]; return swaps; } // Merge sort function private static int mergeSortAndCount(int[] arr, int l,int r) { // Keeps track of the inversion count at a // particular node of the recursion tree int count = 0; if (l < r) { int m = (l + r) / 2; // Total inversion count = left subarray count // + right subarray count + merge count // Left subarray count count += mergeSortAndCount(arr, l, m); // Right subarray count count += mergeSortAndCount(arr, m + 1, r); // Merge count count += mergeAndCount(arr, l, m, r); } return count; } static class Debug{ //change to System.getProperty("ONLINE_JUDGE")==null; for CodeForces public static final boolean LOCAL = System.getProperty("ONLINE_JUDGE")==null; 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)+"}\n"; }else if(t instanceof long[]) { String s = Arrays.toString((long[]) t); return "{"+s.substring(1, s.length()-1)+"}\n"; }else if(t instanceof char[]) { String s = Arrays.toString((char[]) t); return "{"+s.substring(1, s.length()-1)+"}\n"; }else if(t instanceof double[]) { String s = Arrays.toString((double[]) t); return "{"+s.substring(1, s.length()-1)+"}\n"; }else if(t instanceof boolean[]) { String s = Arrays.toString((boolean[]) t); return "{"+s.substring(1, s.length()-1)+"}\n"; } 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(PrintStream ps,Object... o) throws Exception { if(LOCAL) { System.setErr(ps); 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("]"); } } } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
a1e72ade194825b1770d8b81611c8dad
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.util.*; import java.io.*; public class Main { static long startTime = System.currentTimeMillis(); // for global initializations and methods starts here // global initialisations and methods end here static void run() { boolean tc = false; //AdityaFastIO r = new AdityaFastIO(); FastReader r = new FastReader(); try (OutputStream out = new BufferedOutputStream(System.out)) { //long startTime = System.currentTimeMillis(); int testcases = tc ? r.ni() : 1; int tcCounter = 1; // Hold Here Sparky------------------->>> // Solution Starts Here start: while (testcases-- > 0) { int n = r.ni(); int m = r.ni(); char[] s = r.word().toCharArray(); char[] t = r.word().toCharArray(); int countS = 0; int countT = 0; int[] left = new int[m]; int[] right = new int[m]; while (countT < m) { while (s[countS] != t[countT]) countS++; left[countT++] = countS++; } countS = n - 1; countT = m - 1; while (countT >= 0) { while (s[countS] != t[countT]) countS--; right[countT--] = countS--; } int ans = 0; for (int i = 0; i < m - 1; i++) { ans = Math.max(ans, right[i + 1] - left[i]); } out.write((ans + " ").getBytes()); } // Solution Ends Here } catch (IOException e) { e.printStackTrace(); } } static class AdityaFastIO { final private int BUFFER_SIZE = 1 << 16; private final DataInputStream din; private final byte[] buffer; private int bufferPointer, bytesRead; public BufferedReader br; public StringTokenizer st; public AdityaFastIO() { br = new BufferedReader(new InputStreamReader(System.in)); din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public AdityaFastIO(String file_name) throws IOException { din = new DataInputStream(new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String word() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } public String line() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public String readLine() throws IOException { byte[] buf = new byte[100000001]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') break; buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } public int ni() 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 nl() 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 nd() 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 void main(String[] args) throws Exception { run(); } static long mod = 998244353; static long modInv(long base, long e) { long result = 1; base %= mod; while (e > 0) { if ((e & 1) > 0) result = result * base % mod; base = base * base % mod; e >>= 1; } return result; } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String word() { while (st == null || !st.hasMoreElements()) { 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 ni() { return Integer.parseInt(word()); } long nl() { return Long.parseLong(word()); } double nd() { return Double.parseDouble(word()); } } static int MOD = (int) (1e9 + 7); static long powerLL(long x, long n) { long result = 1; while (n > 0) { if (n % 2 == 1) result = result * x % MOD; n = n / 2; x = x * x % MOD; } return result; } static long powerStrings(int i1, int i2) { String sa = String.valueOf(i1); String sb = String.valueOf(i2); long a = 0, b = 0; for (int i = 0; i < sa.length(); i++) a = (a * 10 + (sa.charAt(i) - '0')) % MOD; for (int i = 0; i < sb.length(); i++) b = (b * 10 + (sb.charAt(i) - '0')) % (MOD - 1); return powerLL(a, b); } static long gcd(long a, long b) { if (a == 0) return b; else return gcd(b % a, a); } static long lcm(long a, long b) { return (a * b) / gcd(a, b); } static long lower_bound(List<Long> list, long k) { int s = 0; int e = list.size(); while (s != e) { int mid = (s + e) >> 1; if (list.get(mid) < k) s = mid + 1; else e = mid; } if (s == list.size()) return -1; return s; } static int upper_bound(List<Long> list, long k) { int s = 0; int e = list.size(); while (s != e) { int mid = (s + e) >> 1; if (list.get(mid) <= k) s = mid + 1; else e = mid; } if (s == list.size()) return -1; return s; } static void addEdge(ArrayList<ArrayList<Integer>> graph, int edge1, int edge2) { graph.get(edge1).add(edge2); graph.get(edge2).add(edge1); } public static class Pair implements Comparable<Pair> { int first; int second; public Pair(int first, int second) { this.first = first; this.second = second; } public String toString() { return "(" + first + "," + second + ")"; } public int compareTo(Pair o) { // TODO Auto-generated method stub if (this.first != o.first) return (int) (this.first - o.first); else return (int) (this.second - o.second); } } public static class PairC<X, Y> implements Comparable<PairC> { X first; Y second; public PairC(X first, Y second) { this.first = first; this.second = second; } public String toString() { return "(" + first + "," + second + ")"; } public int compareTo(PairC o) { // TODO Auto-generated method stub return o.compareTo((PairC) first); } } static boolean isCollectionsSorted(List<Long> list) { if (list.size() == 0 || list.size() == 1) return true; for (int i = 1; i < list.size(); i++) if (list.get(i) <= list.get(i - 1)) return false; return true; } static boolean isCollectionsSortedReverseOrder(List<Long> list) { if (list.size() == 0 || list.size() == 1) return true; for (int i = 1; i < list.size(); i++) if (list.get(i) >= list.get(i - 1)) return false; return true; } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
0f624098f1fa61b13a117f6692490950
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.util.*; import java.io.*; public class Main { static long startTime = System.currentTimeMillis(); // for global initializations and methods starts here // global initialisations and methods end here static void run() { boolean tc = false; //AdityaFastIO r = new AdityaFastIO(); FastReader r = new FastReader(); try (OutputStream out = new BufferedOutputStream(System.out)) { //long startTime = System.currentTimeMillis(); int testcases = tc ? r.ni() : 1; int tcCounter = 1; // Hold Here Sparky------------------->>> // Solution Starts Here start: while (testcases-- > 0) { int n = r.ni(); int m = r.ni(); char[] s = r.word().toCharArray(); char[] t = r.word().toCharArray(); int countS = 0; int countT = 0; int[] left = new int[m]; int[] right = new int[m]; while (countT < m) { while (s[countS] != t[countT]) countS++; left[countT++] = countS++; } countS = n - 1; countT = m - 1; while (countT >= 0) { while (s[countS] != t[countT]) countS--; right[countT--] = countS--; } int ans = 0; for (int i = 0; i < m - 1; i++) { ans = Math.max(ans, right[i + 1] - left[i]); } out.write((ans + " ").getBytes()); } // Solution Ends Here } catch (IOException e) { e.printStackTrace(); } } static class AdityaFastIO { final private int BUFFER_SIZE = 1 << 16; private final DataInputStream din; private final byte[] buffer; private int bufferPointer, bytesRead; public BufferedReader br; public StringTokenizer st; public AdityaFastIO() { br = new BufferedReader(new InputStreamReader(System.in)); din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public AdityaFastIO(String file_name) throws IOException { din = new DataInputStream(new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String word() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } public String line() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public String readLine() throws IOException { byte[] buf = new byte[100000001]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') break; buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } public int ni() 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 nl() 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 nd() 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 void main(String[] args) throws Exception { run(); } static long mod = 998244353; static long modInv(long base, long e) { long result = 1; base %= mod; while (e > 0) { if ((e & 1) > 0) result = result * base % mod; base = base * base % mod; e >>= 1; } return result; } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String word() { while (st == null || !st.hasMoreElements()) { 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 ni() { return Integer.parseInt(word()); } long nl() { return Long.parseLong(word()); } double nd() { return Double.parseDouble(word()); } } static int MOD = (int) (1e9 + 7); static long powerLL(long x, long n) { long result = 1; while (n > 0) { if (n % 2 == 1) result = result * x % MOD; n = n / 2; x = x * x % MOD; } return result; } static long powerStrings(int i1, int i2) { String sa = String.valueOf(i1); String sb = String.valueOf(i2); long a = 0, b = 0; for (int i = 0; i < sa.length(); i++) a = (a * 10 + (sa.charAt(i) - '0')) % MOD; for (int i = 0; i < sb.length(); i++) b = (b * 10 + (sb.charAt(i) - '0')) % (MOD - 1); return powerLL(a, b); } static long gcd(long a, long b) { if (a == 0) return b; else return gcd(b % a, a); } static long lcm(long a, long b) { return (a * b) / gcd(a, b); } static long lower_bound(List<Long> list, long k) { int s = 0; int e = list.size(); while (s != e) { int mid = (s + e) >> 1; if (list.get(mid) < k) s = mid + 1; else e = mid; } if (s == list.size()) return -1; return s; } static int upper_bound(List<Long> list, long k) { int s = 0; int e = list.size(); while (s != e) { int mid = (s + e) >> 1; if (list.get(mid) <= k) s = mid + 1; else e = mid; } if (s == list.size()) return -1; return s; } static void addEdge(ArrayList<ArrayList<Integer>> graph, int edge1, int edge2) { graph.get(edge1).add(edge2); graph.get(edge2).add(edge1); } public static class Pair implements Comparable<Pair> { int first; int second; public Pair(int first, int second) { this.first = first; this.second = second; } public String toString() { return "(" + first + "," + second + ")"; } public int compareTo(Pair o) { // TODO Auto-generated method stub if (this.first != o.first) return (int) (this.first - o.first); else return (int) (this.second - o.second); } } public static class PairC<X, Y> implements Comparable<PairC> { X first; Y second; public PairC(X first, Y second) { this.first = first; this.second = second; } public String toString() { return "(" + first + "," + second + ")"; } public int compareTo(PairC o) { // TODO Auto-generated method stub return o.compareTo((PairC) first); } } static boolean isCollectionsSorted(List<Long> list) { if (list.size() == 0 || list.size() == 1) return true; for (int i = 1; i < list.size(); i++) if (list.get(i) <= list.get(i - 1)) return false; return true; } static boolean isCollectionsSortedReverseOrder(List<Long> list) { if (list.size() == 0 || list.size() == 1) return true; for (int i = 1; i < list.size(); i++) if (list.get(i) >= list.get(i - 1)) return false; return true; } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
025e7b28ba05be0828d2c953e6d6bb41
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class c { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); st = new StringTokenizer(br.readLine()); int strLen = Integer.parseInt(st.nextToken()); int targetLen = Integer.parseInt(st.nextToken()); String str = br.readLine(); String target = br.readLine(); int[] minIdx = new int[target.length()]; for (int sIdx = 0, tIdx = 0; tIdx < targetLen; sIdx++) { char sChar = str.charAt(sIdx); char tChar = target.charAt(tIdx); if (sChar == tChar) { minIdx[tIdx] = sIdx; tIdx++; } } int[] maxIdx = new int[target.length()]; for (int sIdx = str.length() - 1, tIdx = target.length() - 1; tIdx >= 0; sIdx--) { char sChar = str.charAt(sIdx); char tChar = target.charAt(tIdx); if (sChar == tChar) { maxIdx[tIdx] = sIdx; tIdx--; } } int answer = 0; for (int tIdx = 0; tIdx < target.length() - 1; tIdx++) { answer = Math.max(maxIdx[tIdx + 1] - minIdx[tIdx], answer); } System.out.println(answer); } private static int gcd(int a, int b) { if (a % b == 0) return b; else return gcd(b, a % b); } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
84834d6787568a49402a9f63f8dbc220
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.util.*; import java.io.*; public class Test { static class Scan { private byte[] buf = new byte[1024]; private int index; private InputStream in; private int total; public Scan() { in = System.in; } public int scan() throws IOException { if (total < 0) throw new InputMismatchException(); if (index >= total) { index = 0; total = in.read(buf); if (total <= 0) return -1; } return buf[index++]; } public int scanInt() throws IOException { int integer = 0; int n = scan(); while (isWhiteSpace(n)) n = scan(); int neg = 1; if (n == '-') { neg = -1; n = scan(); } while (!isWhiteSpace(n)) { if (n >= '0' && n <= '9') { integer *= 10; integer += n - '0'; n = scan(); } else throw new InputMismatchException(); } return neg * integer; } public double scanDouble() throws IOException { double doub = 0; int n = scan(); while (isWhiteSpace(n)) n = scan(); int neg = 1; if (n == '-') { neg = -1; n = scan(); } while (!isWhiteSpace(n) && n != '.') { if (n >= '0' && n <= '9') { doub *= 10; doub += n - '0'; n = scan(); } else throw new InputMismatchException(); } if (n == '.') { n = scan(); double temp = 1; while (!isWhiteSpace(n)) { if (n >= '0' && n <= '9') { temp /= 10; doub += (n - '0') * temp; n = scan(); } else throw new InputMismatchException(); } } return doub * neg; } public String scanString() throws IOException { StringBuilder sb = new StringBuilder(); int n = scan(); while (isWhiteSpace(n)) n = scan(); while (!isWhiteSpace(n)) { sb.append((char) n); n = scan(); } return sb.toString(); } private boolean isWhiteSpace(int n) { if (n == ' ' || n == '\n' || n == '\r' || n == '\t' || n == -1) return true; return false; } } static class Print { private final BufferedWriter bw; public Print() { this.bw = new BufferedWriter(new OutputStreamWriter(System.out)); } public void print(Object object) throws IOException { bw.append("" + object); } public void println(Object object) throws IOException { print(object); bw.append("\n"); } public void close() throws IOException { bw.close(); } } public static void main(String[] args) throws IOException { Scan scan = new Scan(); Print print = new Print(); int n = scan.scanInt(); int m = scan.scanInt(); String str = scan.scanString(); String t = scan.scanString(); int j = 0; int pre[] = new int[m]; int suf[] = new int[m]; for(int i=0; i<m; i++){ char ch = t.charAt(i); while(str.charAt(j) != ch){ j++; } pre[i] = j; j++; } j = n-1; for(int i=m-1; i>=0; i--){ char ch = t.charAt(i); while(str.charAt(j) != ch){ j--; } suf[i] = j; j--; } int max = 0; for(int i=0; i<m-1; i++){ max = Math.max(suf[i+1]-pre[i], max); } print.println(max); //if index==n means no element is present which is equal to or greater then given element print.close(); } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
d74146f3cefdda9acb7f663c7a615e75
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.util.*; import java.io.*; import java.math.*; public class Coder { static int n,m; static char s[], t[]; static List<List<Integer>> list=new ArrayList<>(); static StringBuffer str=new StringBuffer(); static void solve(){ for(int j=0;j<26;j++){ list.add(new ArrayList<>()); for(int i=0;i<n;i++){ if((s[i]-'a')==j) list.get(j).add(i); } } int pos[]=new int[m]; int l=0; for(int i=0;i<m;i++){ for(int j=l;j<n;j++){ if(s[j]==t[i]){ pos[i]=j; l=j+1; break; } } } int max=-1; int far=n-1; for(int i=m-1;i>0;i--){ List<Integer> ml=list.get(t[i]-'a'); int ans=pos[i]; l=0; int r=ml.size()-1; while(l<=r){ int mid=l+(r-l)/2; if(ml.get(mid) <= far){ ans=ml.get(mid); l=mid+1; }else r=mid-1; } pos[i]=ans; far=pos[i]-1; max=Math.max(max, pos[i]-pos[i-1]); } str.append(max).append("\n"); } public static void main(String[] args) throws java.lang.Exception { BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); // int q = Integer.parseInt(bf.readLine().trim()); // while(q-->0) { String st[]=bf.readLine().trim().split("\\s+"); n=Integer.parseInt(st[0]); m=Integer.parseInt(st[1]); s=bf.readLine().trim().toCharArray(); t=bf.readLine().trim().toCharArray(); solve(); // } System.out.print(str); } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
e90b7d88db45432c7940378b22c3fec7
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.util.*; import java.io.*; public class Solution { private static ArrayList<Integer> prime = new ArrayList<>(); public static void main(String[] args) throws IOException { Scanner in=new Scanner(System.in); BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringBuffer out = new StringBuffer(); int T = 1; OUTER: while(T-->0) { int n=in.nextInt(), m=in.nextInt(); String s=in.next(), t=in.next(); int right[]=new int[m];; for(int i=n-1, j=m-1; i>=0 && j>=0; i--) { if(s.charAt(i)==t.charAt(j)) { right[j]=i; j-=1; } } int left[]=new int[m]; for(int i=0, j=0; i<n && j<m; i++) { if(s.charAt(i)==t.charAt(j)) { left[j]=i; j+=1; } } int max=0; for(int i=1; i<m; i++) { max=Math.max(max, right[i]-left[i-1]); } out.append(max+"\n"); } System.out.print(out); } private static int gcd(int a, int b) { if (a==0) return b; return gcd(b%a, a); } private static int toInt(String s) { return Integer.parseInt(s); } private static long toLong(String s) { return Long.parseLong(s); } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
740ce90ab5090e034276ca8912d75805
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.util.*; import java.io.*; public class Solution { private static ArrayList<Integer> prime = new ArrayList<>(); public static void main(String[] args) throws IOException { Scanner in=new Scanner(System.in); BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringBuffer out = new StringBuffer(); int T = 1; OUTER: while(T-->0) { int n=in.nextInt(), m=in.nextInt(); String s=in.next(), t=in.next(); int arr[]=new int[m];; for(int i=n-1, j=m-1; i>=0 && j>=0; i--) { if(s.charAt(i)==t.charAt(j)) { arr[j]=i; j-=1; } } int max=0; for(int i=0, j=0; i<n && j<m-1; i++) { if(s.charAt(i)==t.charAt(j)) { max=Math.max(max, arr[j+1]-i); j+=1; } } out.append(max+"\n"); } System.out.print(out); } private static int gcd(int a, int b) { if (a==0) return b; return gcd(b%a, a); } private static int toInt(String s) { return Integer.parseInt(s); } private static long toLong(String s) { return Long.parseLong(s); } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
c99aab441204b9506061ccba722a9b0d
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
// package codeforces; import java.io.*; import java.util.*; public class Round724_div2 { static int nextInt() throws IOException { return Integer.parseInt(next()); } static long nextLong() throws IOException { return Long.parseLong(next()); } private static String next() throws IOException { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } static BufferedReader br; static StringTokenizer st; static BufferedWriter bw ; public static void main(String[] args) throws IOException { br = new BufferedReader(new InputStreamReader(System.in)); bw = new BufferedWriter(new OutputStreamWriter(System.out)); int[]nums= {1,1,2,2,3,3}; // int t = nextInt(); // while (t-- > 0) { MaximumWidth(); // } bw.close(); } public static void MaximumWidth() throws IOException{ int n=nextInt(); int m=nextInt(); String s=next(); String t=next(); char[]arr1=s.toCharArray(); char[]arr2=t.toCharArray(); int[]l=new int[m]; int[]r=new int[m]; int j=0,i=0; while(j<m) { while(arr1[i]!=arr2[j]) { i++; } l[j]=i; j++; i++; } i=n-1; j=m-1; while(j>=0) { while(arr1[i]!=arr2[j]) { i--; } r[j]=i; j--; i--; } int max=0; for( i=0;i<m-1;i++) { max=Math.max(max,r[i+1]-l[i] ); } System.out.println(max); } public static void threeswimmers() throws IOException{ long p=nextLong(); long a=nextLong(); long b=nextLong(); long c=nextLong(); if(p%a==0 || p%b==0 || p%c==0) { bw.append(0+"\n"); return; } long min=(long)1e18; long x=p/a; min=Math.min(min, a*(x+1)-p); long y=p/b; min=Math.min(min, b*(y+1)-p); long z=p/c; min=Math.min(min, c*(z+1)-p); bw.append(min+"\n"); } public static void card_Deck() throws IOException{ int n=nextInt(); int[]arr=new int[n]; for(int i=n-1;i>=0;i--) { arr[i]=nextInt(); } Stack<Integer> s=new Stack<>(); int max=n; HashMap<Integer,Integer> map=new HashMap<>(); ArrayList<Integer> l=new ArrayList<>(); for(int i=0;i<n;i++) { if(arr[i]==max) { s.push(arr[i]); map.put(arr[i], 1); while(!s.isEmpty()) { l.add(s.pop()); } max--; while(map.containsKey(max)) { max--; } }else{ s.push(arr[i]); map.put(arr[i], 1); } } while(!s.isEmpty()) { l.add(s.pop()); } for(int x:l) { bw.append(x+" "); } bw.append("\n"); } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
37abbbf6ec77e731eaa0c7567c0039ab
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.*; public class experiment { static int M = 1_000_000_007; static int INF = 2_000_000_000; static final FastScanner fs = new FastScanner(); //variable public static void main(String[] args) throws IOException { int n = fs.nextInt(); int m = fs.nextInt(); char[] s1 = fs.next().toCharArray(); char[] s2 = fs.next().toCharArray(); int[] l = new int[m]; int[] r = new int[m]; int j = 0; for(int i=0;i<m;i++,j++) { while(j<n && s2[i] != s1[j]) j++; l[i] = j; } j = n-1; for(int i=m-1;i>=0;i--,j--){ while(j>=0 && s2[i] != s1[j]) j--; r[i] = j; } int[][] dp = new int [m][2]; int ans = 0; for(int i=1;i<m;i++) { dp[i][0] = Math.max(l[i]-l[i-1],l[i]-r[i-1]); dp[i][1] = Math.max(r[i]-l[i-1],r[i]-r[i-1]); ans = Math.max(ans,Math.max(dp[i][0],dp[i][1])); } System.out.println(ans); } //class //function // Template static long lcm(long a, long b) { return (a / gcd(a, b)) * b; } static long gcd(long a, long b) { if(a==0) return b; return gcd(b%a,a); } static void premutation(int n, ArrayList<Integer> arr,boolean[] chosen) { if(arr.size() == n) { }else { for(int i=1; i<=n; i++) { if(chosen[i]) continue; arr.add(i); chosen[i] = true; premutation(n,arr,chosen); arr.remove(i); chosen[i] = false; } } } static boolean isPalindrome(char[] c) { int n = c.length; for(int i=0; i<n/2; i++) { if(c[i] != c[n-i-1]) return false; } return true; } static long nCk(int n, int k) { return (modMult(fact(n),fastexp(modMult(fact(n-k),fact(k)),M-2))); } static long fact (long n) { long fact =1; for(int i=1; i<=n; i++) { fact = modMult(fact,i); } return fact%M; } static int modMult(long a,long b) { return (int) (a*b%M); } static int negMult(long a,long b) { return (int)((a*b)%M + M)%M; } static long fastexp(long x, int y){ if(y==1) return x; if(y == 0) return 1; long ans = fastexp(x,y/2); if(y%2 == 0) return modMult(ans,ans); else return modMult(ans,modMult(ans,x)); } static final Random random = new Random(); static void ruffleSort(int[] arr) { int n = arr.length; for(int i=0; i<n; i++) { int j = random.nextInt(n),temp = arr[j]; arr[j] = arr[i]; arr[i] = temp; } Arrays.sort(arr); } public static class Pairs implements Comparable<Pairs> { int f,s; Pairs(int f, int s) { this.f = f; this.s = s; } public int compareTo(Pairs p) { return Integer.compare(this.f,p.f); } } } class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer str = new StringTokenizer(""); String next() throws IOException { while(!str.hasMoreTokens()) str = new StringTokenizer(br.readLine()); return str.nextToken(); } char nextChar() throws IOException { return next().charAt(0); } int nextInt() throws IOException { return Integer.parseInt(next()); } float nextfloat() throws IOException { return Float.parseFloat(next()); } double nextDouble() throws IOException { return Double.parseDouble(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } byte nextByte() throws IOException { return Byte.parseByte(next()); } int [] arrayIn(int n) throws IOException { int[] arr = new int[n]; for(int i=0; i<n; i++) { arr[i] = nextInt(); } return arr; } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
671febb7ac18914264415deeb200d407
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.util.*; public class C_Maximum_width{ public static void main(String[] args) { Scanner s= new Scanner(System.in); int n=s.nextInt(); int m=s.nextInt(); String ss= s.next(); String tt= s.next(); int min[]= new int[tt.length()-2]; int max[]= new int[tt.length()-2]; int k=0; int start=0; int end=0; for(int i=0;i<ss.length();i++){ if(k==0){ if(ss.charAt(i)==tt.charAt(k)){ k++; start=i; } continue; } if(k==tt.length()-1){ break; } if(ss.charAt(i)==tt.charAt(k)){ min[k-1]=i; k++; } } int y=tt.length()-1; for(int i=ss.length()-1;i>=0;i--){ if(y==tt.length()-1){ if(ss.charAt(i)==tt.charAt(y)){ y--; end=i; } continue; } if(y==0){ break; } if(ss.charAt(i)==tt.charAt(y)){ max[y-1]=i; y--; } } if(tt.length()==2){ System.out.println(end-start); return; } long ans=Integer.MIN_VALUE; for(int i=0;i<=max.length;i++){ if(i==0){ int a=max[i]-start; if(a>ans){ ans=a; } continue; } if(i==max.length){ int c=end-min[i-1]; if(c>ans){ ans=c; } continue; } int b=max[i]-min[i-1]; if(b>ans){ ans=b; } } System.out.println(ans); } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
f710577d018fd08963ba0aa73a6c9920
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
//package practice; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; public class Main { static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } public static void main(String[] args){ Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int m=sc.nextInt(); sc.nextLine(); String a=sc.nextLine(); String b=sc.nextLine(); int min[]=new int[m]; int max[]=new int[m]; for(int i=0, j=0;i<n && j<m;i++) { if(a.charAt(i)==b.charAt(j)) { min[j]=i+1; j++; } } for(int i=n-1,j=m-1;i>=0 && j>=0;i--) { if(a.charAt(i)==b.charAt(j)) { max[j] = i+1; j--; } } int ans=-1; for(int i=1;i<m;i++) { ans=Math.max(ans, max[i]-min[i-1]); } System.out.println(ans); } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
541ef6ac0805cd3c80234d09b8b2ccb8
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Arrays; import java.util.LinkedList; import java.util.StringTokenizer; /* * Java Input / Output Class using Buffered/ Input Straem * Created by @neelbhallabos * File Created at: 4/6/2021 */ public class CF1492C { public static BufferedReader sc = new BufferedReader(new InputStreamReader(System.in)); public static StringTokenizer st; public static PrintWriter pw = new PrintWriter(System.out); final static boolean debugmode = true; public static int k = 7; // for 10^9 + k mods. public static long STMOD = 1000000000 + k; // 10^9 + k public static void main(String[] args) throws IOException { int haystackLen = getInt(); int needleLen= getInt(); String hay = getString(); String needle = getString(); int[] minOcc = new int[needle.length()]; int needleIx = 0; for(int i = 0; i < haystackLen && needleIx < needle.length();i++) { if(hay.charAt(i) == needle.charAt(needleIx)) { minOcc[needleIx] = i; needleIx++; } } int[] maxOcc = new int[needle.length()]; needleIx = needle.length()-1; for(int i = haystackLen-1;i > 0 && needleIx >= 0; i--) { if(hay.charAt(i) == needle.charAt(needleIx)) { maxOcc[needleIx] = i; needleIx--; } } int cmax = 0; for(int split = 0; split < needle.length()-1;split++) { int minBeforeSplit = minOcc[split]; int maxAfterSplit = maxOcc[split+1]; cmax = Math.max(cmax, maxAfterSplit-minBeforeSplit); //System.out.println("considering: " + minBeforeSplit + " " + maxAfterSplit); } submit(cmax, true); } public static void swapAdjacent(int[] r) { for(int i = 0; i < r.length/2;i++) { swap(r, 2*i, 2*i+1); } } public static void rotate(int[] r) { for(int i = 0; i < r.length/2;i++) { swap(r, i, (i + r.length/2)%r.length); } } public static void swap(int[] a, int l, int r) { int tmp = a[l]; a[l] = a[r]; a[r] = tmp; } public static void setInputFile(String fn) throws IOException { sc = new BufferedReader(new FileReader(fn)); } public static void setOutputFile(String fn) throws IOException { pw = new PrintWriter(new BufferedWriter(new FileWriter(fn))); } public static int GCD(int a, int b) { if (b == 0) return a; return GCD(b, a % b); } public static double log(int k, int v) { return Math.log(k) / Math.log(v); } public static long longpower(int a, int b) { long[] vals = new long[(int) (log(b, 2) + 2)]; vals[0] = a; vals[1] = a * a; for (int i = 1; i < vals.length; i++) { vals[i] = vals[i - 1] * vals[i - 1]; } long ans = 1; int cindex = 0; while (b != 0) { if (b % 2 == 1) { ans *= vals[cindex]; } cindex += 1; b /= 2; } return ans; } public static void debug(String toPrint) { if (!debugmode) { return; } pw.println("[DEBUG]: " + toPrint); } public static void submit(int[] k, boolean close) { pw.println(Arrays.toString(k)); if (close) { pw.close(); } } public static void submit(int p, boolean close) { pw.println(Integer.toString(p)); if (close) { pw.close(); } } public static void submit(String k, boolean close) { pw.println(k); if (close) { pw.close(); } } public static void submit(double u, boolean close) { pw.println(Double.toString(u)); if (close) { pw.close(); } } public static void submit(long lng, boolean close) { pw.println(Long.toString(lng)); if (close) { pw.close(); } } public static void submit() { pw.close(); } public static int getInt() throws IOException { if (st != null && st.hasMoreTokens()) { return Integer.parseInt(st.nextToken()); } st = new StringTokenizer(sc.readLine()); return Integer.parseInt(st.nextToken()); } public static long getLong() throws IOException { if (st != null && st.hasMoreTokens()) { return Long.parseLong(st.nextToken()); } st = new StringTokenizer(sc.readLine()); return Long.parseLong(st.nextToken()); } public static double getDouble() throws IOException { if (st != null && st.hasMoreTokens()) { return Double.parseDouble(st.nextToken()); } st = new StringTokenizer(sc.readLine()); return Double.parseDouble(st.nextToken()); } public static String getString() throws IOException { if (st != null && st.hasMoreTokens()) { return st.nextToken(); } st = new StringTokenizer(sc.readLine()); return st.nextToken(); } public static String getLine() throws IOException { return sc.readLine(); } public static int[][] readMatrix(int lines, int cols) throws IOException { int[][] matrr = new int[lines][cols]; for (int i = 0; i < lines; i++) { for (int j = 0; j < cols; j++) { matrr[i][j] = getInt(); } } return matrr; } public static int[] readArray(int lines) throws IOException { int[] ar = new int[lines]; for (int i = 0; i < lines; i++) ar[i] = getInt(); return ar; } static class Pair { int[] cr; int nsteps; boolean wasRotated; public Pair(int[] c, int n, boolean wr) { cr = c.clone(); nsteps = n; wasRotated = wr; } } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
2f0d54542e8f596f1d4f36afaa059136
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.*; public class Main { public static boolean gsd(long x , int y) { for(int i = 2 ; i <= Math.min(y, x) ; i ++ ) { if (x%i == 0 && y%i == 0 ) return true; } return false; } public static int s(long x) { int sum = 0 ; do { sum += x%10 ; x /= 10; }while(x >0 ); return sum; } public static void main(String[] args) throws IOException, InterruptedException{ PrintWriter pw = new PrintWriter(System.out); Scanner sc = new Scanner (System.in); int n = sc.nextInt(); int m = sc.nextInt(); String s = sc.next(); String t = sc.next(); int [] arr1 = new int [m] ; int [] arr2 = new int [m]; int i = 0 ; int j = 0 ; for ( ; i < s.length()&& j < t.length(); i ++) { if (s.charAt(i) == t.charAt(j)) { arr1[j] = i ; j++; } } j = m-1 ; i = s.length()-1; for( ; i >=0 && j >=0 ; i-- ) { if (s.charAt(i) == t.charAt(j)) { arr2[j] = i ; j--; } } int max = 0 ; for (int k = 0 ; k < m-1 ;k ++ ) { max= (max > -arr1[k]+ arr2[k+1] )? max : (-arr1[k]+arr2[k+1]); } pw.print(max); pw.close (); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(String file) throws FileNotFoundException { br = new BufferedReader(new FileReader(file)); } 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 String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public boolean ready() throws IOException { return br.ready(); } } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
9c84188d791c012af569bf91d6be67f2
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.util.*; public class MaximumWidth { public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int m=sc.nextInt(); sc.nextLine(); char[]s1=sc.nextLine().toCharArray(); char[]s2=sc.nextLine().toCharArray(); int j=0; int[]left=new int[m]; int[]right=new int[n]; for(int i=0;i<n&&j<m;i++) { if(s1[i]==s2[j]) { left[j]=i; j++; } } j=m-1; for(int i=n-1;i>=0&&j>=0;i--) { if(s1[i]==s2[j]) { right[j]=i; j--; } } int diff=0; for(int i=0;i<m-1;i++) { diff=Math.max(diff, right[i+1]-left[i]); } System.out.println(diff); } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
e154bc6fd31b6a7a95878ba5de8af482
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import static java.lang.Integer.parseInt; import static java.lang.Long.parseLong; import static java.lang.Double.parseDouble; import static java.lang.Math.PI; import static java.lang.Math.min; import static java.lang.System.arraycopy; import static java.lang.System.exit; import static java.util.Arrays.copyOf; import java.util.LinkedList; import java.io.FileReader; import java.io.FileWriter; import java.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; import java.util.Set; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.HashMap; import java.util.NoSuchElementException; import java.util.PriorityQueue; import java.util.StringTokenizer; import java.util.Comparator; import java.lang.StringBuilder; import java.util.Collections; public class Solution { static int scanInt() throws IOException { return parseInt(scanString()); } static long scanLong() throws IOException { return parseLong(scanString()); } static double scanDouble() throws IOException { return parseDouble(scanString()); } static String scanString() throws IOException { if (tok == null || !tok.hasMoreTokens()) { tok = new StringTokenizer(in.readLine()); } return tok.nextToken(); } static String scanLine() throws IOException { return in.readLine(); } static void printCase(String str) { out.print("Case #" + test + ": "+str); } static void printlnCase() { out.println("Case #" + test + ":"); } static BufferedReader in; static PrintWriter out; static StringTokenizer tok; static int test; static StringBuilder str; public static void main(String[] args) { try { long startTime = System.currentTimeMillis(); in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); //in = new BufferedReader(new FileReader("input.txt")); //out = new PrintWriter(new FileWriter("output.txt")); /*int tests = scanInt(); for (test = 1; test <= tests; test++) { int n=scanInt(); }*/ /*String []bank={"ab","abc","cd","def","abcd"}; out.println(countConstruct("abcdef",bank)); String bank2[]={"bo","rd","ate","t","ska","sk","boar"}; out.println(countConstruct("skateboard",bank2)); String bank3[]={"a","p","ent","enter","ot","o","t"}; out.println(countConstruct("enterapotentpot",bank3)); String bank4[]={"e","ee","eee","eeee","eeeee","eeeeee"}; out.println("ans"+countConstruct("eeeeeeeeeeeeeeeeeeeeeeeef",bank4)); String bank5[]={"purp","p","ur","le","purpl","purple"}; allConstruct("purple",bank5); for(ArrayList<String> data: xyz) out.println(data);*/ solve(); long endTime = System.currentTimeMillis(); long totalTime = endTime - startTime; //System.out.println(totalTime+" "+System.currentTimeMillis() ); in.close(); out.close(); } catch (Throwable e) { e.printStackTrace(); exit(1); } } private static void solve() throws IOException{ int n=scanInt(), m=scanInt(); char s[]=scanString().toCharArray(); char t[]=scanString().toCharArray(); int left[]=new int[m]; int right[]=new int[m]; for(int i=0,j=0; i<n && j<m; ++i){ if(s[i]==t[j]){ left[j++]=i; } } for(int i=n-1,j=m-1; i>-1 && j>-1; --i){ if(s[i]==t[j]){ right[j--]=i; } } int ans=0; for(int i=0; i<m-1;++i){ ans=Math.max(ans,right[i+1]-left[i]); } out.println(ans); } /* private static void allConstruct(String str, String bank[]){ ArrayList<ArrayList<String>> dp[]= new ArrayList[str.length()+1]; //HashMap<Integer,ArrayList<String>> dp[]=new HashMap[str.length()+1]; dp[0]=new ArrayList<>(); dp[0].add(new ArrayList<>()); for(int i=0; i<=str.length(); ++i){ String temp=str.substring(i); //out.println(dp[i]); if(dp[i]!=null){ for(int j=0; j<bank.length; ++j){ if(temp.startsWith(bank[j])){ for(ArrayList<String> data: dp[i]){ ArrayList<String> x=new ArrayList<>(data); x.add(bank[j]); if(dp[i+bank[j].length()]==null) dp[i+bank[j].length()]=new ArrayList<>(); dp[i+bank[j].length()].add(x); } } } } } for(int i=0; i<=str.length(); ++i){ out.println(dp[i]); } //return dp; } private static int countConstruct(String str, String []bank){ int dp[]=new int[str.length()+1]; dp[0]=1; for(int i=0; i<=str.length(); ++i){ String temp=str.substring(i); if(dp[i]!=0){ for(int j=0; j<bank.length; ++j){ if(temp.startsWith(bank[j])){ dp[i+bank[j].length()]+=dp[i]; } } for(Integer data: dp){ out.print(data+" "); } out.println(); } } return(dp[str.length()]); } private static boolean canConstruct(String str, String []bank){ boolean dp[]=new boolean[str.length()+1]; dp[0]=true; for(int i=0;i<=str.length();++i){ String temp=str.substring(i); if(dp[i]){ for(int j=0; j<bank.length; ++j){ if(temp.startsWith(bank[j])){ dp[i+bank[j].length()]=true; } } /*for(Boolean data: dp){ out.print(data+" "); } out.println(); } } return dp[str.length()]; } */ }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
42e606acbcb8961a5a0c78114e182b59
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.io.*; import java.util.StringTokenizer; import static java.lang.Double.parseDouble; import static java.lang.Integer.compare; import static java.lang.Integer.parseInt; import static java.lang.Long.parseLong; import static java.lang.System.in; public class Main { private static final int MOD = (int) (1E9 + 7); static FastScanner scanner = new FastScanner(in); public static void main(String[] args) throws IOException { // Write your solution here int t = 1; // t = parseInt(scanner.nextLine()); while (t-- > 0) { solve(); } } private static void solve() throws IOException { int n = scanner.nextInt(); int m = scanner.nextInt(); String s = scanner.nextLine(); String t = scanner.nextLine(); int[] left = new int[m]; int[] right = new int[m]; int i = 0, j = 0; while (j < m) { while (s.charAt(i) != t.charAt(j)) i++; left[j] = i; i++; j++; } i = n - 1; j = m - 1; while (j >= 0) { while (s.charAt(i) != t.charAt(j)) i--; right[j] = i; i--; j--; } int ans = 0; for (int k = 1; k < m; k++) { ans = Math.max(ans, right[k] - left[k - 1]); } System.out.println(ans); } private static int add(int a, int b) { long res = ((long) (a + MOD) % MOD + (b + MOD)) % MOD; return (int) res; } private static int mul(int a, int b) { long res = ((long) a % MOD * b % MOD) % MOD; return (int) res; } private static int pow(int a, int b) { a %= MOD; int res = 1; while (b > 0) { if ((b & 1) != 0) res = mul(res, a); a = mul(a, a); b >>= 1; } return res; } private static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } private static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } private static class Pair implements Comparable<Pair> { int index, value; public Pair(int index, int value) { this.index = index; this.value = value; } public int compareTo(Pair o) { if (value != o.value) return compare(value, o.value); return compare(index, o.index); } } static 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 parseInt(next()); } long nextLong() { return parseLong(next()); } double nextDouble() { return parseDouble(next()); } } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
0e2a021da8d371ff1e6eb9297e67c86c
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.util.*; import java.io.*; public class Main { private static final FastIO fastIO = new FastIO(); private static final String yes = "YES"; private static final String no = "NO"; public static void main(String[] args) { int n = fastIO.nextInt(); int m = fastIO.nextInt(); char[] s = fastIO.nextLine().toCharArray(); char[] t = fastIO.nextLine().toCharArray(); Node[] nodes = new Node[m]; int ans = 0; for(int i = 0; i < nodes.length; i++) nodes[i] = new Node(); for(int i = 0, j = 0; i < n && j < m; i++){ if(s[i] == t[j]){ nodes[j].min = i; ++j; } } for(int i = n - 1, j = m - 1; i >= 0 && j >= 0; i--){ if(s[i] == t[j]){ nodes[j].max = i; --j; } } for(int i = 1; i < m; i++) ans = Math.max(ans, nodes[i].max - nodes[i - 1].min); fastIO.append(ans); fastIO.printAll(); } public static class Node { int min = 0; int max = 0; } private static class FastIO { private final BufferedReader in; private final StringBuilder out; private final char LINE_BREAK = '\n'; private StringTokenizer tokens; public FastIO() { in = new BufferedReader(new InputStreamReader(System.in)); out = new StringBuilder(); } public String next() { while (tokens == null || !tokens.hasMoreElements()) { try { tokens = new StringTokenizer(in.readLine()); } catch (IOException e) { e.printStackTrace(); } } return tokens.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 str = ""; try { str = in.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public FastIO append(Object... objects){ for(Object o: objects) out.append(o); return this; } public FastIO appendln(Object... objects){ return append(objects).append(LINE_BREAK); } public void printAll(){ System.out.println(out.toString()); } } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
5dd5fbadf6c2e9248ffc63338a7eb28b
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.io.*; import java.util.*; public class Main { static int n, m; static char[] s, t; public static void main(String[] args) throws IOException { n = sc.nextInt(); m = sc.nextInt(); s = sc.nextLine().toCharArray(); t = sc.nextLine().toCharArray(); int[] pref = new int[m], suff = new int[m]; int j = 0; for (int i = 0; i < m; i++) { while(s[j] != t[i]) j++; pref[i] = j++; } j = n-1; for (int i = m-1; i >= 0; i--) { while(s[j] != t[i]) j--; suff[i] = j--; } int ans = 0; for (int i = 0; i < m-1; i++) { ans = Math.max(ans, suff[i+1]-pref[i]); } pw.println(ans); pw.close(); } public static void pairSort(Pair[] arr) { ArrayList<Pair> l = new ArrayList<>(); Collections.addAll(l, arr); Collections.sort(l); for (int i = 0; i < arr.length; i++) { arr[i] = l.get(i); } } public static void longSort(long[] arr) { ArrayList<Long> l = new ArrayList<>(); for (long i : arr) l.add(i); Collections.sort(l); for (int i = 0; i < arr.length; i++) { arr[i] = l.get(i); } } public static void intSort(int[] arr) { ArrayList<Integer> l = new ArrayList<>(); for (int i : arr) l.add(i); Collections.sort(l); for (int i = 0; i < arr.length; i++) { arr[i] = l.get(i); } } static class Pair implements Comparable<Pair> { int x; int y; public Pair(int first, int second){ this.x = first; this.y = second; } @Override public int compareTo(Pair p2) { return x - p2.x; } @Override public String toString() { return "("+ x + "," + y + ')'; } } static class Edge implements Comparable<Edge> { int u, v, w; Edge(int a, int b, int c) { u = a; v = b; w = c; } public int compareTo(Edge e) { return w - e.w; } @Override public String toString() { return "("+ u +","+v+"," +w+ ')'; } } static class Triple { int x, y, z; Triple(int a, int b, int c) { x = a; y = b; z = c;} // public int compareTo(Triple t) // { // if(Math.abs(sum - t.sum) < 1e-9) return x > t.x ? 1 : -1; // return sum > t.sum ? 1 : -1; // } public String toString() { return "(" + x + ", " + y + ", " + z + ")"; } } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } 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 String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if (x.charAt(0) == '-') { neg = true; start++; } for (int i = start; i < x.length(); i++) if (x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if (dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg ? -1 : 1); } public boolean ready() throws IOException { return br.ready(); } 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; } public long[] nextLongArr(int n) throws IOException { long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = Long.parseLong(next()); } return arr; } } static PrintWriter pw = new PrintWriter(System.out); static Scanner sc = new Scanner(System.in); static Random random = new Random(); static final long MOD = 1_000_000_000 + 7; static final int INF = Integer.MAX_VALUE; }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
be526655b27c919bdde657c936ce4690
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
// Don't place your source in a package import java.util.*; import java.lang.*; import java.io.*; import java.math.*; // Please name your class Main public class Main { static Scanner in = new Scanner(System.in); public static void main (String[] args) throws java.lang.Exception { PrintWriter out = new PrintWriter(System.out); int T=1; for(int t=0;t<T;t++){ int n=Int(); int m=Int(); String s=Str();String s1=Str(); Solution sol=new Solution(); sol.solution(out,s,s1); } out.flush(); } public static long Long(){ return in.nextLong();} public static int Int(){ return in.nextInt(); } public static String Str(){ return in.next(); } } class Solution{ public void solution(PrintWriter out,String s,String t){ int res=0; ArrayList<Integer>A[]=new ArrayList[26]; for(int i=0;i<A.length;i++){ A[i]=new ArrayList<>(); } for(int i=0;i<s.length();i++){ A[s.charAt(i)-'a'].add(i); } int dp[]=new int[s.length()+1]; int cnt=0; int j=t.length()-1; for(int i=s.length()-1;i>=0;i--){ if(i+1<s.length()){ dp[i]=dp[i+1]; } if(j>=0&&s.charAt(i)==t.charAt(j)){ cnt++;//how many is matched dp[i]=cnt; j--; } } j=0; int pre=-1; for(int i=0;i<s.length();i++){ if(s.charAt(i)==t.charAt(j)){ if(pre==-1){ } else{ List<Integer>list=A[s.charAt(i)-'a']; int l=0,r=list.size()-1; int pos=-1; while(l<=r){ int mid=l+(r-l)/2; int index=list.get(mid); if(index<=pre){ l=mid+1; continue; } if(dp[index+1]>=t.length()-(j+1)){ pos=index; l=mid+1; } else{ r=mid-1; } } res=Math.max(res,pos-pre); } pre=i; j++; } if(j>=t.length())break; } System.out.println(res); } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
2bf6c40a782815560c74675f7e492053
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.util.Scanner; public class Maximumwidth { public static void main(String args[]) { Scanner scan = new Scanner(System.in); int n = scan.nextInt(); int m = scan.nextInt(); String s = scan.next(); String t = scan.next(); int i = 0; int j = 0; int[] l = new int[m]; while (i < n && j < m) { if (s.charAt(i) == t.charAt(j)) { l[j] = i; ++j; } ++i; } i = n - 1; j = m - 1; int[] r = new int[m]; while (i >= 0 && j>=0) { if (s.charAt(i) == t.charAt(j)) { r[j] = i; --j; } --i; } int res = 0; for (int k = 1; k < m; k++) { res = Math.max(res, r[k] - l[k - 1]); } System.out.println(res); } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
6494a34a7959120a27a6482905903666
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.StringTokenizer; public class C { public static void main(String[] args) { FastScanner fs = new FastScanner(); int t = 1;//fs.nextInt(); while (t-- > 0) { int n = fs.nextInt(), m = fs.nextInt(); char[] old = fs.next().toCharArray(); char[] nw = fs.next().toCharArray(); if (n == m && Arrays.equals( old, nw )) { System.out.println( 1 ); continue; } int left[] = new int[m], right[] = new int[m]; int j = 0, i = 0; while (j < m) { while (old[i] != nw[j]) { i++; } left[j] = i; i++; j++; } i = n - 1; j = m - 1; while (j >= 0) { while (old[i] != nw[j]) { i--; } right[j] = i; i--; j--; } int ans = 0; for (int k = 1; k < m; k++) { ans = Math.max( ans, right[k] - left[k - 1] ); } System.out.println( ans ); } } private static class FastScanner { BufferedReader br = new BufferedReader( new InputStreamReader( System.in ) ); StringTokenizer st = new StringTokenizer( "" ); public String next() { while (!st.hasMoreElements()) try { st = new StringTokenizer( br.readLine() ); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } 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
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
c35a169b740d675d2c940d04a39f5c45
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.util.*; import java.lang.*; import java.io.*; public class Main { PrintWriter out = new PrintWriter(System.out); BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer tok = new StringTokenizer(""); String next() throws IOException { if (!tok.hasMoreTokens()) { tok = new StringTokenizer(in.readLine()); } return tok.nextToken(); } int ni() throws IOException { return Integer.parseInt(next()); } long nl() throws IOException { return Long.parseLong(next()); } long mod=1000000007; void solve() throws IOException { for (int tc=1;tc>0;tc--) { int n=ni(),m=ni(); char[]A=next().toCharArray(); char[]B=next().toCharArray(); int[]F=new int[m]; int p=0; for (int i=0;i<m;i++) { while (A[p]!=B[i]) p++; F[i]=p; p++; } int[]L=new int[m]; p=n-1; for (int i=m-1;i>=0;i--) { while (A[p]!=B[i]) p--; L[i]=p; p--; } int ans=0; for (int i=1;i<m;i++) { ans=Math.max(ans,L[i]-F[i-1]); } out.println(ans); } out.flush(); } int gcd(int a,int b) { return(b==0?a:gcd(b,a%b)); } long gcd(long a,long b) { return(b==0?a:gcd(b,a%b)); } long mp(long a,long p) { long r=1; while(p>0) { if ((p&1)==1) r=(r*a)%mod; p>>=1; a=(a*a)%mod; } return r; } public static void main(String[] args) throws IOException { new Main().solve(); } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
1c3678e9cd31b749a038df9346cd854b
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.util.ArrayList; import java.util.Collections; import java.util.Scanner; public class _1492C { public static void main(String args[]){ Scanner in = new Scanner(System.in); int n = in.nextInt(); int m = in.nextInt(); char nn[] = in.next().toCharArray(); char mm[] = in.next().toCharArray(); ArrayList<Integer> list = new ArrayList<>(); int left = 0; int right = m - 1; int start[] = new int[m]; int end[] = new int[m]; for(int i = 0; i < n; i++) if(left < m && nn[i] == mm[left]){ start[left] = i; left++; } for(int i = n - 1; i >= 0; i--){ if(right >= 0 && nn[i] == mm[right]){ end[right] = i; right--; } } int max = 0; for(int i = 0; i < m - 1; i++) max = Math.max(max, end[i + 1] - start[i]); System.out.println(max); } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
3ea6abab350f09bf8b7d2d5ca0268ffc
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; import java.nio.file.Paths; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.StringTokenizer; public class Main3 { public Main3() throws FileNotFoundException { // File file = Paths.get("input.txt").toFile(); // if (file.exists()) { // System.setIn(new FileInputStream(file)); // } long t = System.currentTimeMillis(); FastReader reader = new FastReader(); int n = reader.nextInt(); int m = reader.nextInt(); String str1 = reader.next(); String str2 = reader.next(); List<Integer> list1 = new ArrayList<>(); List<Integer> list2 = new ArrayList<>(); int pos = 0; for (int i = 0; i < m; i++) { while (str1.charAt(pos) != str2.charAt(i)) { pos++; } list1.add(pos); pos++; } pos=n-1; for(int i=m-1;i>=0;i--) { while (str1.charAt(pos) != str2.charAt(i)) { pos--; } list2.add(pos); pos--; } //System.out.println(list1); Collections.reverse(list2); //System.out.println(list2); int max=0; for(int i=0;i<m-1;i++) { max=Math.max(max, list2.get(i+1)-list1.get(i)); } System.out.println(max); // System.out.println("TIME: "+(System.currentTimeMillis()-t)); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static void main(String[] args) throws FileNotFoundException { new Main3(); } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
854b282ab9080f22de220472b9175cc1
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.util.*; public class MaximumWidth { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); sc.nextLine(); String s = sc.nextLine(); String t = sc.nextLine(); int i = 0; int j = 0; int left[] = new int[m]; while (i < n && j < m) { if (s.charAt(i) == t.charAt(j)) { left[j++] = i; } i++; } i = n - 1; j = m - 1; int right[] = new int[m]; while (i >= 0 && j >= 0) { if (s.charAt(i) == t.charAt(j)) { right[j--] = i; } i--; } int max = 0; for (i = 0; i < m - 1; i++) { // System.out.print(idx[i] + " "); max = Math.max(max, right[i + 1] - left[i]); } System.out.println(max); } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
72193dc9800d38906d8e9ff64a1b19ce
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class C { public static void main(String[] args){ MyScanner sc = new MyScanner(); int t = 1; while(t-->0){ int m = sc.nextInt(); int n = sc.nextInt(); String s = sc.next(); String tt = sc.next(); int[] left = new int[n-1]; int[] right = new int[n-1]; int ptr = 0; for(int i=0;i<m&&ptr<n-1;i++){ if(s.charAt(i) == tt.charAt(ptr)){ left[ptr] = i; ptr++; } } ptr = n-1; for(int i=m-1;i>=0&&ptr>=1;i--){ if(s.charAt(i) == tt.charAt(ptr)){ right[ptr-1] = i; ptr--; } } int mx = Integer.MIN_VALUE; for(int i=0;i<n-1;i++){ mx = Math.max(mx,right[i] - left[i]); } System.out.print(mx); } } 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
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
001399c74e75a7ce5f628d0a7eeb57b3
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.util.*; import java.io.*; public class Main2 { 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 FastReader sc = new FastReader(); public static void main (String[] args) { PrintWriter out = new PrintWriter(System.out); int t = 1; // t = sc.nextInt(); while(t-->0) { int n = sc.nextInt(); int m = sc.nextInt(); char[] s = sc.next().toCharArray(); char[] p = sc.next().toCharArray(); int left[] = new int[m]; int right[] = new int[n]; int i=0,j=0; while(j<m) { while(s[i]!=p[j]) { i++; } left[j] = i; i++;j++; } i=n-1; j=m-1; while(j>=0) { while(s[i]!=p[j]) { i--; } right[j] = i; i--;j--; } int res = 0; for(i=1;i<m;i++) res = Math.max(res, right[i]-left[i-1]); out.write(res+"\n"); } out.close(); } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
c322fd57648a705be9ce97178e5d6932
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.awt.Point; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.lang.reflect.Array; import java.math.BigDecimal; import java.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashSet; import java.util.Hashtable; import java.util.LinkedList; import java.util.PriorityQueue; import java.util.Queue; import java.util.Stack; import java.util.StringTokenizer; public class N704 { static PrintWriter out; static Scanner sc; static ArrayList<Integer>q,w; static ArrayList<Integer>adj[]; static HashSet<Integer>primesH; static boolean prime[]; //static ArrayList<Integer>a; static HashSet<Long>h,tmp; static boolean[]v; static int[]a,b,c,d; static int[]l,dp; static char[][]mp; static int A,B,n,m; public static void main(String[]args) throws IOException { sc=new Scanner(System.in); out=new PrintWriter(System.out); //A(); // B(); C(); //D(); //minimum for subarrays of fixed length //F(); out.close(); } private static void A() throws IOException { int t=ni(); while(t-->0) { long p=nl(),a=nl(),b=nl(),c=nl(); long x1=(long)Math.ceil(p/(double)a); long x2=(long)Math.ceil(p/(double)b); long x3=(long)Math.ceil(p/(double)c); // out.println(Math.min(Math.min(x1*1l*a-p, x2*1l*b-p), x3*1l*c-p)); out.println(Math.min(Math.min((a-p%a)%a, (b-p%b)%b), (c-p%c)%c)); } } static void B() throws IOException { int t=ni(); while(t-->0) { int n=ni(); a=nai(n); Stack<Point>q=new Stack<Point>(); int cur=0; for(int i=0;i<n;i++) { int piv=a[i]; cur=i; i++; while(i<n&&a[i]<piv) { i++; } i--; q.add(new Point(cur,i)); } while(!q.isEmpty()) { Point p=q.pop(); for(int i=p.x;i<=p.y;i++) { out.print(a[i]+" "); } } out.println(); } } static void C() throws IOException{ n=ni(); m=ni(); char[] s=sc.next().toCharArray(); char[] t=sc.next().toCharArray(); int[]p=new int[m],su=new int[m]; int j=0; for(int i=0;i<m;i++) { while(j<n&&s[j]!=t[i]) { j++; } p[i]=j++; } j=n-1; for(int i=m-1;i>=0;i--) { while(j>=0&&s[j]!=t[i])j--; su[i]=j--; } int max=0; for(int i=1;i<m;i++) { max=Math.max(su[i]-p[i-1], max); } out.println(max); } static void D() throws IOException { int n=ni(); } static void E() throws IOException { int t=ni(); while(t-->0) { } } static void X() throws IOException { int n=ni(); a=new int[n]; int lo=0,hi=n-1; int locIdx=-1; while(lo<=hi) { int mid=(lo+hi)/2; ol("? "+(mid+1)); out.flush(); int m=ni(),bm=-1,am=-1; a[mid]=m; if(mid>0) { if(a[mid-1]==0) { ol("? "+(mid)); out.flush(); bm=ni(); }else { bm=a[mid-1]; } a[mid-1]=bm; if(bm<m) { hi=mid-1; continue; } } if(mid<n-1) { if(a[mid+1]==0) { ol("? "+(mid+2)); out.flush(); am=ni(); }else { am=a[mid+1]; } a[mid+1]=am; if(am<m) { lo=mid+1; continue; } } locIdx=mid;break; } out.println("! "+(locIdx+1)); out.flush(); } private static void disp(int[] revl) { for(int i=0;i<revl.length;i++) { out.print(revl[i]+" "); } out.println(); } static class Pair implements Comparable<Pair>{ int a,b; Pair(int a,int b){ this.a=a; this.b=b; } @Override public int compareTo(Pair p) { return a-p.a; } public boolean equals(Pair p) { return a==p.a&&b==p.b; } public String toString() { return "("+a+" "+b+")"; } } static int ni() throws IOException { return sc.nextInt(); } static double nd() throws IOException { return sc.nextDouble(); } static long nl() throws IOException { return sc.nextLong(); } static String ns() throws IOException { return sc.next(); } static int[] nai(int n) throws IOException { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = sc.nextInt(); return a; } static long[] nal(int n) throws IOException { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = sc.nextLong(); return a; } static int[][] nmi(int n,int m) throws IOException{ int[][]a=new int[n][m]; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { a[i][j]=sc.nextInt(); } } return a; } static long[][] nml(int n,int m) throws IOException{ long[][]a=new long[n][m]; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { a[i][j]=sc.nextLong(); } } return a; } static void o(String x) { out.print(x); } static void ol(String x) { out.println(x); } static void ol(int x) { out.println(x); } static void disp1(int []a) { for(int i=0;i<a.length;i++) { out.print(a[i]+" "); } out.println(); } static void disp2(Pair []a) { for(int i=0;i<a.length;i++) { out.print(a[i]+" "); } out.println(); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s){ br = new BufferedReader(new InputStreamReader(s));} public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public boolean hasNext() {return st.hasMoreTokens();} public int nextInt() throws IOException {return Integer.parseInt(next());} public double nextDouble() throws IOException {return Double.parseDouble(next());} public long nextLong() throws IOException {return Long.parseLong(next());} public String nextLine() throws IOException {return br.readLine();} public boolean ready() throws IOException {return br.ready(); } } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
c101892367d490038182db2c606cf29a
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.util.*; import java.io.*; public class Main { // static RMQ rmq = new RMQ(); public static void main(String[] args) { FastScanner sc = new FastScanner(); FastOutput out = new FastOutput(System.out); int n = sc.nextInt(), m = sc.nextInt(); char[] chs1 = sc.next().toCharArray(), chs2 = sc.next().toCharArray(); int[] mat1 = new int[m], mat2 = new int[m]; for(int i = 0, j = 0; i < n && j < m; ++i) { if(chs1[i] == chs2[j]) { mat1[j++] = i; } } for(int i = n - 1, j = m - 1; i >= 0 && j >= 0; --i) { if(chs1[i] == chs2[j]) { mat2[j--] = i; } } int res = 0; for(int i = 1; i < m; ++i) { res = Math.max(res, mat2[i] - mat1[i-1]); } out.append(res).println().flush(); } } class RMQ { private int n; private int INF = 100_000 << 2; private int[] dat = new int[INF]; public void init(int[] nums, int n_) { n = 1; while(n < n_) n <<= 1; Arrays.fill(dat, 0, (n<<1)-1, 0); for(int i = 0; i < n_; i++){ update(i, nums[i]); } } public void update(int i, int val) { i += n-1; dat[i] = val; while(i > 0){ i = (i-1)/2; dat[i] = Math.max(dat[i], val); } } public int maxRange(int i, int j) { return maxRange(i, j+1, 0, 0, n); } private int maxRange(int i, int j, int k, int l, int r){ //[l, r) if(r <= i || j <= l) return 0; if(i <= l && r <= j) return dat[k]; return Math.max(maxRange(i, j, k*2+1, l, l+(r-l)/2), maxRange(i, j, k*2+2, l+(r-l)/2, r)); } } 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[] readArrayInt(int fst, int lst) { int[] a = new int[lst]; for(int i = fst; i < lst; ++i) a[i] = nextInt(); return a; } long[] readArrayLong(int fst, int lst) { long[] a = new long[lst]; for(int i = fst; i < lst; ++i) a[i] = nextLong(); return a; } long nextLong() { return Long.parseLong(next()); } } class FastOutput implements AutoCloseable, Closeable, Appendable { private static final int THRESHOLD = 1 << 13; private final Writer os; private StringBuilder cache = new StringBuilder(THRESHOLD * 2); public FastOutput append(CharSequence csq) { cache.append(csq); return this; } public FastOutput append(CharSequence csq, int start, int end) { cache.append(csq, start, end); return this; } private void afterWrite() { if (cache.length() < THRESHOLD) { return; } flush(); } public FastOutput(Writer os) { this.os = os; } public FastOutput(OutputStream os) { this(new OutputStreamWriter(os)); } public FastOutput append(char c) { cache.append(c); afterWrite(); return this; } public FastOutput append(int c) { cache.append(c); afterWrite(); return this; } public FastOutput append(String c) { cache.append(c); afterWrite(); return this; } public FastOutput println(String c) { return append(c).println(); } public FastOutput println() { return append(System.lineSeparator()); } public FastOutput flush() { try { os.append(cache); os.flush(); cache.setLength(0); } catch (IOException e) { throw new UncheckedIOException(e); } return this; } public void close() { flush(); try { os.close(); } catch (IOException e) { throw new UncheckedIOException(e); } } public String toString() { return cache.toString(); } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
0846d9703f1c559b4bb09d088866dc4b
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.util.*; import java.io.*; //import java.math.*; public class Task{ // ..............code begins here.............. static long mod=(long)1e9+7,inf=(long)1e15; static void solve() throws IOException { int[] x=int_arr(); int n=x[0],m=x[1]; char[] s=read().toCharArray(),t=read().toCharArray(); int[] dp1=new int[m+1],dp2=new int[m+1]; int j=0; for(int i=0;i<m;i++){ while(j<n&&s[j]!=t[i]){j++;} dp1[i+1]=j;j++; } j=n-1; for(int i=m-1;i>=0;i--){ while(j>=0&&s[j]!=t[i]){j--;} dp2[m-i]=j;j--; } int res=0; for(int i=1;i<m;i++){ res=Math.max(res,dp2[m-i]-dp1[i]); } out.write(res+""); } public static void main(String[] args) throws IOException{ assign(); int t=1;//int_v(read()); while(t--!=0) solve(); out.flush(); } // taking inputs static BufferedReader s1; static BufferedWriter out; static String read() throws IOException{String line="";while(line.length()==0){line=s1.readLine();continue;}return line;} static int int_v (String s1){return Integer.parseInt(s1);} static long long_v(String s1){return Long.parseLong(s1);} static void sort(int[] a){List<Integer> l=new ArrayList<>();for(int x:a){l.add(x);}Collections.sort(l);for(int i=0;i<a.length;i++){a[i]=l.get(i);}} static int[] int_arr() throws IOException{String[] a=read().split(" ");int[] b=new int[a.length];for(int i=0;i<a.length;i++){b[i]=int_v(a[i]);}return b;} static long[] long_arr() throws IOException{String[] a=read().split(" ");long[] b=new long[a.length];for(int i=0;i<a.length;i++){b[i]=long_v(a[i]);}return b;} static void assign(){s1=new BufferedReader(new InputStreamReader(System.in));out=new BufferedWriter(new OutputStreamWriter(System.out));} //static String setpreciosion(double d,int k){BigDecimal d1 = new BigDecimal(Double.toString(d));return d1.setScale(k,RoundingMode.HALF_UP).toString();}//UP DOWN HALF_UP static int add(int a,int b){int z=a+b;if(z>=mod)z-=mod;return z;} static long gcd(long a,long b){if(b==0){return a;}return gcd(b,a%b);} static long Modpow(long a,long p,long m){long res=1;while(p>0){if((p&1)!=0){res=(res*a)%m;}p >>=1;a=(a*a)%m;}return res%m;} static long Modmul(long a,long b,long m){return ((a%m)*(b%m))%m;} static long ModInv(long a,long m){return Modpow(a,m-2,m);} //static long nck(int n,int r,long m){if(r>n){return 0l;}return Modmul(f[n],ModInv(Modmul(f[n-r],f[r],m),m),m);} //static long[] f; }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
f63327f080c9683422f2572ff1b63fc0
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.util.*; import static java.lang.Math.max; /** * Accomplished using the EduTools plugin by JetBrains https://plugins.jetbrains.com/plugin/10081-edutools */ public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); String s = sc.next(); String t = sc.next(); List<Integer> start = new ArrayList<>(); List<Integer> end = new ArrayList<>(); for (int i = 0, j=0; i < n && j<m; i++) { if (s.charAt(i)==t.charAt(j)) { start.add(i); j++; } } for (int i=n-1, j=m-1; i>=0 && j>=0; i--) { if (s.charAt(i)==t.charAt(j)) { end.add(i); j--; } } Collections.reverse(end); int mx = 1; for (int i = 0; i < m-1; i++) { int x = end.get(i+1); int y = start.get(i); mx = max(mx, x - y); } System.out.println(mx); } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
38756c58d58fd9bb7a50e714b8acaaba
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; import java.util.*; import java.awt.Point; public class Main{ public static void main(String[] args) throws IOException { Scanner input=new Scanner(System.in); while(input.hasNext()){ int n=input.nextInt(); int m=input.nextInt(); String s=input.next(); String t=input.next(); int i=0; int j=0; int[] prefix=new int[m]; int[] suffix=new int[m]; Arrays.fill(prefix, Integer.MAX_VALUE); Arrays.fill(suffix, Integer.MAX_VALUE); while(j<m){ if(s.charAt(i)==t.charAt(j)){ prefix[j]=i; i++; j++; continue; } i++; } i=n-1; j=m-1; while(j>=0){ if(s.charAt(i)==t.charAt(j)){ suffix[j]=i; i--; j--; continue; } i--; } int ans=Integer.MIN_VALUE; for(i=0;i<m-1;i++){ ans=Math.max(ans, suffix[i+1]-prefix[i]); } System.out.println(ans); } } //Credits to SecondThread(https://codeforces.com/profile/SecondThread) for this tempelate static void ruffle_sort(long[] a) { // shandom_ruffle Random r=new Random(); int n=a.length; for (int i=0; i<n; i++) { int oi=r.nextInt(i); long temp=a[i]; a[i]=a[oi]; a[oi]=temp; } // sort Arrays.sort(a); } //Credits to SecondThread(https://codeforces.com/profile/SecondThread) for this tempelate. 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
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
6c4216f49f472164f13703240e2839bb
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.util.*; import java.io.*; public class EdA { static long[] mods = {1000000007, 998244353, 1000000009}; static long mod = mods[0]; public static MyScanner sc; public static PrintWriter out; public static void main(String[] omkar) throws Exception{ // TODO Auto-generated method stub sc = new MyScanner(); out = new PrintWriter(System.out); // String input1 = bf.readLine().trim(); // String input2 = bf.readLine().trim(); // COMPARING INTEGER OBJECTS U DO DOT EQUALS NOT == int n = sc.nextInt(); int m = sc.nextInt(); String s = sc.next(); String t = sc.next(); int[] prefix = new int[m]; int[] suffix = new int[m]; int index = 0; int mi = 0; while(mi < m){ while(s.charAt(index) != t.charAt(mi)){ index++; } prefix[mi] = index; index++; mi++; } int indexs = n-1; int mis = m-1; while(mis >= 0){ while(s.charAt(indexs) != t.charAt(mis)){ indexs--; } suffix[mis] = indexs; indexs--; mis--; } int ans = 0; for(int j = 0;j<m-1;j++){ ans = Math.max(ans, suffix[j+1]-prefix[j]); } out.println(ans); // for(int j = 0;j<array.length;j++){ // out.print(array[j] + " "); // } // out.println(); out.close(); } public static void sort(int[] array){ ArrayList<Integer> copy = new ArrayList<>(); for (int i : array) copy.add(i); Collections.sort(copy); for(int i = 0;i<array.length;i++) array[i] = copy.get(i); } static String[] readArrayString(int n){ String[] array = new String[n]; for(int j =0 ;j<n;j++) array[j] = sc.next(); return array; } static int[] readArrayInt(int n){ int[] array = new int[n]; for(int j = 0;j<n;j++) array[j] = sc.nextInt(); return array; } static int[] readArrayInt1(int n){ int[] array = new int[n+1]; for(int j = 1;j<=n;j++){ array[j] = sc.nextInt(); } return array; } static long[] readArrayLong(int n){ long[] array = new long[n]; for(int j =0 ;j<n;j++) array[j] = sc.nextLong(); return array; } static double[] readArrayDouble(int n){ double[] array = new double[n]; for(int j =0 ;j<n;j++) array[j] = sc.nextDouble(); return array; } static int minIndex(int[] array){ int minValue = Integer.MAX_VALUE; int minIndex = -1; for(int j = 0;j<array.length;j++){ if (array[j] < minValue){ minValue = array[j]; minIndex = j; } } return minIndex; } static int minIndex(long[] array){ long minValue = Long.MAX_VALUE; int minIndex = -1; for(int j = 0;j<array.length;j++){ if (array[j] < minValue){ minValue = array[j]; minIndex = j; } } return minIndex; } static int minIndex(double[] array){ double minValue = Double.MAX_VALUE; int minIndex = -1; for(int j = 0;j<array.length;j++){ if (array[j] < minValue){ minValue = array[j]; minIndex = j; } } return minIndex; } static long power(long x, long y){ if (y == 0) return 1; if (y%2 == 1) return (x*power(x, y-1))%mod; return power((x*x)%mod, y/2)%mod; } static void verdict(boolean a){ out.println(a ? "YES" : "NO"); } 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; } } } //StringJoiner sj = new StringJoiner(" "); //sj.add(strings) //sj.toString() gives string of those stuff w spaces or whatever that sequence is
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
3270868b6d1b88b565857e9e07c8ca76
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
/******************************************************************************* * author : dante1 * created : 23/02/2021 14:35 *******************************************************************************/ 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.util.*; public class c { public static void main(String[] args) { int T = 1; TEST: while (T-- > 0) { int n = in.nextInt(), m = in.nextInt(); char[] s = in.next().toCharArray(); char[] t = in.next().toCharArray(); int[] rms = new int[m]; for (int i = m-1, j = n-1; i >= 0; i--, j--) { while (s[j] != t[i]) { j--; } rms[i] = j; } int res = 0; for (int i = 0, j = 0; i < m-1; i++) { while (s[j] != t[i]) { j++; } res = Math.max(rms[i+1]-j, res); j++; } out.println(res); } out.flush(); } // Handle I/O static int MOD = (int) (1e9 + 7); static FastScanner in = new FastScanner(); static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner() { br = new BufferedReader(new InputStreamReader(System.in)); st = 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()); } int[] readIntArray(int size) { int[] arr = new int[size]; for (int i = 0; i < size; i++) { arr[i] = nextInt(); } return arr; } long nextLong() { return Long.parseLong(next()); } long[] readLongArray(int size) { long[] arr = new long[size]; for (int i = 0; i < size; i++) { arr[i] = nextLong(); } return arr; } double nextDouble() { return Double.parseDouble(next()); } double[] readDoubleArray(int size) { double[] arr = new double[size]; for (int i = 0; i < size; i++) { arr[i] = nextDouble(); } return arr; } } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
876f650a283c590d6aa0c2208e416792
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
/* ⠀⠀⠀⠀⣠⣶⡾⠏⠉⠙⠳⢦⡀⠀⠀⠀⢠⠞⠉⠙⠲⡀⠀ ⠀⠀⠀⣴⠿⠏⠀⠀⠀⠀⠀⠀⢳⡀⠀⡏⠀⠀Y⠀⠀⢷ ⠀⠀⢠⣟⣋⡀⢀⣀⣀⡀⠀⣀⡀⣧⠀⢸⠀⠀A⠀⠀ ⡇ ⠀⠀⢸⣯⡭⠁⠸⣛⣟⠆⡴⣻⡲⣿⠀⣸⠀⠀S⠀ ⡇ ⠀⠀⣟⣿⡭⠀⠀⠀⠀⠀⢱⠀⠀⣿⠀⢹⠀⠀H⠀⠀ ⡇ ⠀⠀⠙⢿⣯⠄⠀⠀⠀⢀⡀⠀⠀⡿⠀⠀⡇⠀⠀⠀⠀⡼ ⠀⠀⠀⠀⠹⣶⠆⠀⠀⠀⠀⠀⡴⠃⠀⠀⠘⠤⣄⣠⠞⠀ ⠀⠀⠀⠀⠀⢸⣷⡦⢤⡤⢤⣞⣁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⢀⣤⣴⣿⣏⠁⠀⠀⠸⣏⢯⣷⣖⣦⡀⠀⠀⠀⠀⠀⠀ ⢀⣾⣽⣿⣿⣿⣿⠛⢲⣶⣾⢉⡷⣿⣿⠵⣿⠀⠀⠀⠀⠀⠀ ⣼⣿⠍⠉⣿⡭⠉⠙⢺⣇⣼⡏⠀⠀⠀⣄⢸⠀⠀⠀⠀⠀⠀ ⣿⣿⣧⣀⣿………⣀⣰⣏⣘⣆⣀⠀⠀ */ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; // System.out is a PrintWriter // import java.util.Arrays; // import java.util.ArrayDeque; // import java.util.ArrayList; // import java.util.Collections; // for ArrayList sorting mainly // import java.util.HashMap; // import java.util.HashSet; // import java.util.Random; import java.util.StringTokenizer; public class C { public static void main(String[] args) throws IOException { FastScanner scn = new FastScanner(); PrintWriter out = new PrintWriter(System.out); for (int tc = 1; tc > 0; tc--) { int N = scn.nextInt(), M = scn.nextInt(), ans = 1; char[] str = scn.next().toCharArray(); char[] tar = scn.next().toCharArray(); int[][] occur = new int[M][2]; // int[][] occurLmt = new int[M][2]; for (int i = 0, j = 0; i < N && j < M; i++, j++) { while (i < N && str[i] != tar[j]) { i++; } occur[j][0] = i; } for (int i = N - 1, j = M - 1; i >= 0 && j >= 0; i--, j--) { while (i >= 0 && str[i] != tar[j]) { i--; } occur[j][1] = i; } // occurLmt[0][0] = occur[0][0]; // occurLmt[M - 1][1] = occur[M - 1][1]; // for (int i = 1; i < M; i++) { // int st = occurLmt[i - 1][0], ed = occur[i][1]; // for (int j = st; j <= ed; j++) { // if (str[j] == tar[i]) { // occurLmt[i][0] = j; // break; // } // } // } // for (int i = M - 2; i >= 0; i--) { // int st = occurLmt[i][0], ed = occurLmt[i + 1][1]; // for (int j = ed; j >= st; j--) { // if (str[j] == tar[i]) { // occurLmt[i][1] = j; // break; // } // } // } for (int i = 1; i < M; i++) { ans = Math.max(occur[i][1] - occur[i - 1][0], ans); } out.println(ans); } out.close(); } 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)); } 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); } private static class FastScanner { BufferedReader br; StringTokenizer st; FastScanner() { this.br = new BufferedReader(new InputStreamReader(System.in)); this.st = new StringTokenizer(""); } String next() { while (!st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException err) { err.printStackTrace(); } } return st.nextToken(); } String nextLine() { if (st.hasMoreTokens()) { return st.nextToken("").trim(); } try { return br.readLine().trim(); } catch (IOException err) { err.printStackTrace(); } return ""; } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
0fd82dccd02fc617da5d826aa5376ffd
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import javax.print.DocFlavor; import javax.swing.text.html.parser.Entity; import java.awt.*; import java.io.*; import java.lang.reflect.Array; import java.util.*; import java.util.List; import java.util.stream.Collectors; public class Main { static FastScanner sc; static PrintWriter pw; static final int INF = 200000000; static final int MOD = 1000000007; static final int N = 1005; static long mul(long a, long b, long mod) { return (a * b) % mod; } public static long pow(long a, long n, long mod) { long res = 1; while(n != 0) { if((n & 1) == 1) { res = mul(res, a, mod); } a = mul(a, a, mod); n >>= 1; } return res; } /*public static int inv(int a) { return pow(a, MOD-2, mod); }*/ public static List<Integer> getPrimes() { List<Integer> ans = new ArrayList<>(); boolean[] prime = new boolean[N]; Arrays.fill(prime, true); prime[0] = prime[1] = false; for(int i = 2; i < N; ++i) { if(prime[i]) { ans.add(i); if (i * 1l * i <= N) for (int j = i * i; j < N; j += i) prime[j] = false; } } return ans; } public static long gcd(long a, long b) { if(b == 0) return a; else return gcd(b, a % b); } public static class A { long x; long y; A(long a, long b) { this.x = a; this.y = b; } } public static void solve() throws IOException { int n = sc.nextInt(), m = sc.nextInt(); String s = sc.next(), t = sc.next(); HashMap<Character, TreeSet<Integer>> map = new HashMap<>(); for(int i = 0; i < n ;++i) { if(map.containsKey(s.charAt(i))) map.get(s.charAt(i)).add(i); else { TreeSet<Integer> tmp = new TreeSet<>(); tmp.add(i); map.put(s.charAt(i), tmp); } } int j = 0; List<Integer> pos = new ArrayList<>(); for(int i = 0; i < m; ++i) { while(s.charAt(j) != t.charAt(i)) j++; pos.add(j); j++; } int ans = 0; for(int i = 1; i < pos.size(); ++i) { ans = Math.max(ans, pos.get(i) - pos.get(i - 1)); } // pw.println(pos); int r = INF; for(int i = m - 1; i > 0; i--) { TreeSet<Integer> set = map.get(t.charAt(i)); pos.set(i, set.lower(r) ); ans = Math.max(ans, pos.get(i) - pos.get(i - 1)); r = pos.get(i); } // pw.println(pos); pw.println(ans); } public static void main(String[] args) throws IOException { sc = new FastScanner(System.in); pw = new PrintWriter(System.out); int xx = 1; while(xx > 0) { solve(); xx--; } pw.close(); } } class FastScanner { static StringTokenizer st = new StringTokenizer(""); static BufferedReader br; public FastScanner(InputStream inputStream) { br = new BufferedReader(new InputStreamReader(inputStream)); } public String next() throws IOException { while (!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()); } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
0e2ed5ca334453d84292a4aed2f1a7d2
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.*; public class Main { static int i, j, k, n, m, t, y, x, sum=0; static long mod = 1000000007; static FastScanner fs = new FastScanner(); static PrintWriter out = new PrintWriter(System.out); static String str; static long ans =0; public static void main(String[] args) { t =1; while (t-- >0){ n = fs.nextInt(); m = fs.nextInt(); String str1 = fs.next(); String str2 = fs.next(); int[] ans = new int[m]; int[] ansr = new int[m]; j=0; for(i=0;i<n;i++){ if(j>=m) break; if(str1.charAt(i)==str2.charAt(j)){ ans[j]=i; j++; } } j=m-1; for(i=n-1;i>=0;i--){ if(j<0) break; if(str1.charAt(i)==str2.charAt(j)){ ansr[j]=i; j--; } } long fans = 0; for(i=1;i<m;i++){ fans = Math.max(fans, ansr[i]- ans[i-1]); } out.println(fans); } out.close(); } private static void initialize(long[] arr, long val){ int n = arr.length; for(int i = 0;i<n;i++){ arr[i]=val; } } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } } static class Pair implements Comparable<Pair> { int first, second; public Pair(int first, int second) { this.first = first; this.second = second; } public int compareTo(Pair o) { return Long.compare(first, o.first); } } static void ruffleSort(int[] a) { //ruffle 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; } //then sort Arrays.sort(a); } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
a207a32789640c78af7b725a24d4525a
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.io.*;import java.util.*;import java.math.*; public class Main { static long mod=1000000007l; static int max=Integer.MAX_VALUE,min=Integer.MIN_VALUE; static long maxl=Long.MAX_VALUE,minl=Long.MIN_VALUE; static BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); static StringTokenizer st; static StringBuilder sb; static public void main(String[] args)throws Exception { st=new StringTokenizer(br.readLine()); int t=1; sb=new StringBuilder(50); o: while(t-->0) { int la=i(); int lb=i(); String a=s(); String b=s(); int aa[]=new int[lb]; int bb[]=new int[lb]; for(int x=0;x<la;x++) { if(a.charAt(x)==b.charAt(0)) { aa[0]=x; break; } } for(int x=la-1;x>=0;x--) { if(a.charAt(x)==b.charAt(lb-1)) { bb[lb-1]=x; break; } } if(lb==2) { sl(bb[lb-1]-aa[0]); continue o; } int i=1; for(int x=aa[0]+1;x<la&&i<lb;x++) { if(a.charAt(x)==b.charAt(i)) { aa[i]=x; i++; } } i=lb-2; for(int x=bb[lb-1]-1;x>=0&&i>=0;x--) { if(a.charAt(x)==b.charAt(i)) { bb[i]=x; i--; } } int v=lb-1; int m=0; for(int x=1;x<v;x++) { m=max(m,aa[x]-aa[x-1],aa[x+1]-aa[x],bb[x+1]-aa[x],aa[x]-bb[x-1], bb[x]-aa[x-1],aa[x+1]-bb[x],bb[x+1]-bb[x],bb[x]-bb[x-1],0); } sl(m); //p(aa); //p(bb); } p(sb); } static int[] so(int ar[]) { Integer r[]=new Integer[ar.length]; for(int x=0;x<ar.length;x++)r[x]=ar[x]; Arrays.sort(r); for(int x=0;x<ar.length;x++)ar[x]=r[x]; return ar; } static long[] so(long ar[]) { Long r[]=new Long[ar.length]; for(int x=0;x<ar.length;x++)r[x]=ar[x]; Arrays.sort(r); for(int x=0;x<ar.length;x++)ar[x]=r[x]; return ar; } static char[] so(char ar[]) { Character r[]=new Character[ar.length]; for(int x=0;x<ar.length;x++)r[x]=ar[x]; Arrays.sort(r); for(int x=0;x<ar.length;x++)ar[x]=r[x]; return ar; } static void s(String s){sb.append(s);} static void s(int s){sb.append(s);} static void s(long s){sb.append(s);} static void s(char s){sb.append(s);} static void s(double s){sb.append(s);} static void ss(){sb.append(' ');} static void sl(String s){sb.append(s);sb.append("\n");} static void sl(int s){sb.append(s);sb.append("\n");} static void sl(long s){sb.append(s);sb.append("\n");} static void sl(char s){sb.append(s);sb.append("\n");} static void sl(double s){sb.append(s);sb.append("\n");} static void sl(){sb.append("\n");} static int max(int ...a){int m=a[0];for(int e:a)m=(m>=e)?m:e;return m;} static int min(int ...a){int m=a[0];for(int e:a)m=(m<=e)?m:e;return m;} static int abs(int a){return Math.abs(a);} static long max(long ...a){long m=a[0];for(long e:a)m=(m>=e)?m:e;return m;} static long min(long ...a){long m=a[0];for(long e:a)m=(m<=e)?m:e;return m;} static long abs(long a){return Math.abs(a);} static int sq(int a){return (int)Math.sqrt(a);} static long sq(long a){return (long)Math.sqrt(a);} static long gcd(long a,long b){return b==0l?a:gcd(b,a%b);} static boolean pa(String s,int i,int j) { while(i<j)if(s.charAt(i++)!=s.charAt(j--))return false; return true; } static int ncr(int n,int c,long m) { long a=1l; for(int x=n-c+1;x<=n;x++)a=((a*x)%m); long b=1l; for(int x=2;x<=c;x++)b=((b*x)%m); return (int)((a*(mul((int)b,m-2,m)%m))%m); } static boolean[] si(int n) { boolean bo[]=new boolean[n+1]; bo[0]=true;bo[1]=true; for(int x=4;x<=n;x+=2)bo[x]=true; for(int x=3;x*x<=n;x+=2) { if(!bo[x]) { int vv=(x<<1); for(int y=x*x;y<=n;y+=vv)bo[y]=true; } } return bo; } static int[] fac(int n) { int bo[]=new int[n+1]; for(int x=1;x<=n;x++)for(int y=x;y<=n;y+=x)bo[y]++; return bo; } static long mul(long a,long b,long m) { long r=1l; a%=m; while(b>0) { if((b&1)==1)r=(r*a)%m; b>>=1; a=(a*a)%m; } return r; } static int i()throws IOException { if(!st.hasMoreTokens())st=new StringTokenizer(br.readLine()); return Integer.parseInt(st.nextToken()); } static long l()throws IOException { if(!st.hasMoreTokens())st=new StringTokenizer(br.readLine()); return Long.parseLong(st.nextToken()); } static String s()throws IOException { if(!st.hasMoreTokens())st=new StringTokenizer(br.readLine()); return st.nextToken(); } static double d()throws IOException { if(!st.hasMoreTokens())st=new StringTokenizer(br.readLine()); return Double.parseDouble(st.nextToken()); } static void p(Object p){System.out.print(p);} static void p(String p){System.out.print(p);} static void p(int p){System.out.print(p);} static void p(double p){System.out.print(p);} static void p(long p){System.out.print(p);} static void p(char p){System.out.print(p);} static void p(boolean p){System.out.print(p);} static void pl(Object p){System.out.println(p);} static void pl(String p){System.out.println(p);} static void pl(int p){System.out.println(p);} static void pl(char p){System.out.println(p);} static void pl(double p){System.out.println(p);} static void pl(long p){System.out.println(p);} static void pl(boolean p){System.out.println(p);} static void pl(){System.out.println();} static void s(int a[]) { for(int e:a) { sb.append(e); sb.append(' '); } sb.append("\n"); } static void s(long a[]) { for(long e:a) { sb.append(e); sb.append(' '); } sb.append("\n"); } static void s(int ar[][]) { for(int a[]:ar) { for(int e:a) { sb.append(e); sb.append(' '); } sb.append("\n"); } } static void s(char a[]) { for(char e:a) { sb.append(e); sb.append(' '); } sb.append("\n"); } static void s(char ar[][]) { for(char a[]:ar) { for(char e:a) { sb.append(e); sb.append(' '); } sb.append("\n"); } } static int[] ari(int n)throws IOException { int ar[]=new int[n]; if(!st.hasMoreTokens())st=new StringTokenizer(br.readLine()); for(int x=0;x<n;x++)ar[x]=Integer.parseInt(st.nextToken()); return ar; } static int[][] ari(int n,int m)throws IOException { int ar[][]=new int[n][m]; for(int x=0;x<n;x++) { if(!st.hasMoreTokens())st=new StringTokenizer(br.readLine()); for(int y=0;y<m;y++)ar[x][y]=Integer.parseInt(st.nextToken()); } return ar; } static long[] arl(int n)throws IOException { long ar[]=new long[n]; if(!st.hasMoreTokens())st=new StringTokenizer(br.readLine()); for(int x=0;x<n;x++) ar[x]=Long.parseLong(st.nextToken()); return ar; } static long[][] arl(int n,int m)throws IOException { long ar[][]=new long[n][m]; for(int x=0;x<n;x++) { if(!st.hasMoreTokens())st=new StringTokenizer(br.readLine()); for(int y=0;y<m;y++)ar[x][y]=Long.parseLong(st.nextToken()); } return ar; } static String[] ars(int n)throws IOException { String ar[]=new String[n]; if(!st.hasMoreTokens())st=new StringTokenizer(br.readLine()); for(int x=0;x<n;x++) ar[x]=st.nextToken(); return ar; } static double[] ard(int n)throws IOException { double ar[]=new double[n]; if(!st.hasMoreTokens())st=new StringTokenizer(br.readLine()); for(int x=0;x<n;x++)ar[x]=Double.parseDouble(st.nextToken()); return ar; } static double[][] ard(int n,int m)throws IOException { double ar[][]=new double[n][m]; for(int x=0;x<n;x++) { if(!st.hasMoreTokens())st=new StringTokenizer(br.readLine()); for(int y=0;y<m;y++)ar[x][y]=Double.parseDouble(st.nextToken()); } return ar; } static char[] arc(int n)throws IOException { char ar[]=new char[n]; if(!st.hasMoreTokens())st=new StringTokenizer(br.readLine()); for(int x=0;x<n;x++)ar[x]=st.nextToken().charAt(0); return ar; } static char[][] arc(int n,int m)throws IOException { char ar[][]=new char[n][m]; for(int x=0;x<n;x++) { String s=br.readLine(); for(int y=0;y<m;y++)ar[x][y]=s.charAt(y); } return ar; } static void p(int ar[]) { StringBuilder sb=new StringBuilder(2*ar.length); for(int a:ar) { sb.append(a); sb.append(' '); } System.out.println(sb); } static void p(int ar[][]) { StringBuilder sb=new StringBuilder(2*ar.length*ar[0].length); for(int a[]:ar) { for(int aa:a) { sb.append(aa); sb.append(' '); } sb.append("\n"); } p(sb); } static void p(long ar[]) { StringBuilder sb=new StringBuilder(2*ar.length); for(long a:ar) { sb.append(a); sb.append(' '); } System.out.println(sb); } static void p(long ar[][]) { StringBuilder sb=new StringBuilder(2*ar.length*ar[0].length); for(long a[]:ar) { for(long aa:a) { sb.append(aa); sb.append(' '); } sb.append("\n"); } p(sb); } static void p(String ar[]) { int c=0; for(String s:ar)c+=s.length()+1; StringBuilder sb=new StringBuilder(c); for(String a:ar) { sb.append(a); sb.append(' '); } System.out.println(sb); } static void p(double ar[]) { StringBuilder sb=new StringBuilder(2*ar.length); for(double a:ar) { sb.append(a); sb.append(' '); } System.out.println(sb); } static void p(double ar[][]) { StringBuilder sb=new StringBuilder(2*ar.length*ar[0].length); for(double a[]:ar) { for(double aa:a) { sb.append(aa); sb.append(' '); } sb.append("\n"); } p(sb); } static void p(char ar[]) { StringBuilder sb=new StringBuilder(2*ar.length); for(char aa:ar) { sb.append(aa); sb.append(' '); } System.out.println(sb); } static void p(char ar[][]) { StringBuilder sb=new StringBuilder(2*ar.length*ar[0].length); for(char a[]:ar) { for(char aa:a) { sb.append(aa); sb.append(' '); } sb.append("\n"); } p(sb); } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
6b6322401d3df1a4493669a6e465ed07
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; import java.util.TreeSet; import java.util.ArrayList; public class C { public void solve(IO io) throws IOException { int n = io.nextInt(); int m = io.nextInt(); String s = io.nextToken(); String t = io.nextToken(); int[] left = new int[m]; int[] right = new int[m]; int k = 0; for (int i = 0; i < n; ++i) { if (k == m) { break; } if (s.charAt(i) == t.charAt(k)) { left[k] = i; k++; } } k = m - 1; for (int i = n - 1; i >= 0; --i) { if (k == -1) { break; } if (s.charAt(i) == t.charAt(k)) { right[k] = i; k--; } } int ans = 0; for (int i = 1; i < m; ++i) { ans = Math.max(ans, Math.max(Math.abs(left[i] - right[i - 1]), Math.abs(left[i - 1] - right[i]))); } io.println(ans + ""); } public void run() throws IOException { IO io = new IO(); solve(io); io.close(); } public static void main(String[] args) throws IOException { new C().run(); } } class IO { public IO() { br = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); } BufferedReader br; StringTokenizer in; PrintWriter out; public String nextToken() throws IOException { while (in == null || !in.hasMoreTokens()) { in = new StringTokenizer(br.readLine()); } return in.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(nextToken()); } public double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } public long nextLong() throws IOException { return Long.parseLong(nextToken()); } public void print(String a) { out.print(a); } public void println(String a) { out.println(a); } public void close() { out.close(); } public void flush() { out.flush(); } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
fdff8760a87ebfab5e0e7da75549ef6a
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.IOException; import java.io.BufferedReader; import java.io.FileReader; import java.io.InputStreamReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Khater */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; Scanner in = new Scanner(inputStream); PrintWriter out = new PrintWriter(outputStream); CMaximumWidth solver = new CMaximumWidth(); solver.solve(1, in, out); out.close(); } static class CMaximumWidth { public void solve(int testNumber, Scanner sc, PrintWriter pw) { int t = 1; // t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); int m = sc.nextInt(); char[] a = sc.nextLine().toCharArray(); char[] b = sc.nextLine().toCharArray(); int max = 1; int[] f = new int[m]; int[] l = new int[m]; int j = 0; for (int i = 0; i < m; i++) { while (b[i] != a[j]) j++; f[i] = j; j++; } j = n - 1; for (int i = m - 1; i >= 0; i--) { while (b[i] != a[j]) j--; l[i] = j; j--; } // pw.println(Arrays.toString(f)); // pw.println(Arrays.toString(l)); for (int i = 1; i < m; i++) { max = Math.max(max, l[i] - f[i - 1]); } pw.println(max); } } } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(FileReader r) { br = new BufferedReader(r); } public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public String nextLine() { try { return br.readLine(); } catch (Exception e) { throw new RuntimeException(e); } } } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
f1506fb738c01303a4bca0e287465d5c
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.io.*; import java.util.*; public class C { static long m = (long) (1e9 + 7); public static void main(String[] args) throws IOException { Scanner scn = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); StringBuilder sb = new StringBuilder(); int T = 1, tcs = 0; C: while (tcs++ < T) { int n = scn.ni(), m = scn.ni(); String s = scn.next(), t = scn.next(); if (m == 1) { sb.append(0 + "\n"); continue; } int j = 0; int pre[] = new int[m], suf[] = new int[m]; for (int i = 0; i < n; i++) if (j < m && s.charAt(i) == t.charAt(j)) { pre[j] = i; j++; } j = m - 1; for (int i = n - 1; i >= 0; i--) if (j >= 0 && s.charAt(i) == t.charAt(j)) { suf[j] = i; j--; } int ans = 0; for (int i = 1; i < m; i++) ans = Math.max(suf[i] - pre[i - 1], ans); sb.append(ans); sb.append("\n"); } out.print(sb); out.close(); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int ni() throws IOException { return Integer.parseInt(next()); } public long nl() throws IOException { return Long.parseLong(next()); } public int[] nia(int n) throws IOException { int a[] = new int[n]; String sa[] = br.readLine().split(" "); for (int i = 0; i < n; i++) a[i] = Integer.parseInt(sa[i]); return a; } public long[] nla(int n) throws IOException { long a[] = new long[n]; String sa[] = br.readLine().split(" "); for (int i = 0; i < n; i++) a[i] = Long.parseLong(sa[i]); return a; } public int[] sort(int[] a) { ArrayList<Integer> l = new ArrayList<>(); for (int v : a) l.add(v); Collections.sort(l); for (int i = 0; i < a.length; i++) a[i] = l.get(i); return a; } public long[] sort(long[] a) { ArrayList<Long> l = new ArrayList<>(); for (long v : a) l.add(v); Collections.sort(l); for (int i = 0; i < a.length; i++) a[i] = l.get(i); return a; } } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
0fd5d057a91f6eb0d7b90ba41d725019
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.util.*; import java.io.*; public class codeforces { public static void main(String[] args) throws Exception { int n=sc.nextInt(); int m=sc.nextInt(); char[]a=sc.next().toCharArray(); char[]b=sc.next().toCharArray(); int ind=n-1; HashMap<Integer, Integer>hm=new HashMap<Integer, Integer>(); for(int i=m-1;i>-1;i--) { while(a[ind]!=b[i]) { ind--; } hm.put(i, ind--); } long ans=0; ind=0; for(int i=0;i<m;i++) { while(a[ind]!=b[i]) { ind++; } if(i!=m-1) { ans=Math.max(ans, hm.get(i+1)-ind); } ind++; } pw.println(ans); pw.close(); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(FileReader r) { br = new BufferedReader(r); } 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 String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if (x.charAt(0) == '-') { neg = true; start++; } for (int i = start; i < x.length(); i++) if (x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if (dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg ? -1 : 1); } public long[] nextlongArray(int n) throws IOException { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public Long[] nextLongArray(int n) throws IOException { Long[] a = new Long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public int[] nextIntArray(int n) throws IOException { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public Integer[] nextIntegerArray(int n) throws IOException { Integer[] a = new Integer[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public boolean ready() throws IOException { return br.ready(); } } static class pair implements Comparable<pair> { long x; long y; public pair(long x, long y) { this.x = x; this.y = y; } public String toString() { return x + " " + y; } public boolean equals(Object o) { if (o instanceof pair) { pair p = (pair) o; return p.x == x && p.y == y; } return false; } public int hashCode() { return new Double(x).hashCode() * 31 + new Double(y).hashCode(); } public int compareTo(pair other) { if (this.x == other.x) { return Long.compare(this.y, other.y); } return Long.compare(this.x, other.x); } } static class tuble implements Comparable<tuble> { int x; int y; int z; public tuble(int x, int y, int z) { this.x = x; this.y = y; this.z = z; } public String toString() { return x + " " + y + " " + z; } public int compareTo(tuble other) { if (this.x == other.x) { if (this.y == other.y) { return this.z - other.z; } return this.y - other.y; } else { return this.x - other.x; } } } static long mod = 1000000007; static Random rn = new Random(); static Scanner sc = new Scanner(System.in); static PrintWriter pw = new PrintWriter(System.out); }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
27b23518ba28126f9869f40ae81c5c22
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
//stan hu tao //join nct ridin by first year culture reps import static java.lang.Math.max; import static java.lang.Math.min; import static java.lang.Math.abs; import java.util.*; import java.io.*; import java.math.*; public class x1492C { 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 M = Integer.parseInt(st.nextToken()); char[] arr = infile.readLine().toCharArray(); char[] brr = infile.readLine().toCharArray(); int dex = 0; for(int i=0; i < N; i++) if(arr[i] == brr[0]) { dex = i; break; } int[] left = new int[M]; left[0] = dex; dex++; for(int i=1; i < M; i++) { while(dex < N && arr[dex] != brr[i]) dex++; left[i] = dex; dex++; } for(int i=0; i < N; i++) if(arr[i] == brr[M-1]) dex = i; int[] right = new int[M]; right[M-1] = dex; dex--; for(int i=M-2; i >= 0; i--) { while(dex >= 0 && arr[dex] != brr[i]) dex--; right[i] = dex; dex--; } int res = 0; for(int i=0; i < M-1; i++) res = max(res, right[i+1]-left[i]); System.out.println(res); } static boolean debug = false; public static void print(int[] arr) { //for debugging only for(int x: arr) System.out.print(x+" "); System.out.println(); } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
ce0b7d33abfd628be8fb94ba4f348689
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.util.Scanner; public class ProblemE { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int m = in.nextInt(); in.nextLine(); String s = in.nextLine(); String t = in.nextLine(); int[] minP = new int[m]; int[] maxP = new int[m]; int index = 0; for (int i = 0; i < n; i++) { if (index == m) { break; } if (t.charAt(index) == s.charAt(i)) { minP[index++] = i; } } index = m - 1; for (int i = n - 1; i >= 0; i--) { if (index < 0) { break; } if (t.charAt(index) == s.charAt(i)) { maxP[index--] = i; } } int result = 0; for (int i = 0; i < m - 1; i++) { result = Math.max(result, maxP[i + 1] - minP[i]); } System.out.println(result); } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
6d1ef91c60d4ab341fcf66a6e9a88e35
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
/* Author:-crazy_coder- */ import java.io.*; import java.util.*; public class cp{ static long mod=998244353; static int ans=0; static int[] par=new int[100005]; static int[] rank=new int[100005]; static BufferedReader br=new BufferedReader (new InputStreamReader(System.in)); static PrintWriter pw=new PrintWriter(System.out); public static void main(String[] args)throws Exception{ // int T=Integer.parseInt(br.readLine()); // int t=1; // comp(); int T=1; while(T-->0){ solve(); } pw.flush(); } public static void solve()throws Exception{ String[] str=br.readLine().split(" "); int n=Integer.parseInt(str[0]); int m=Integer.parseInt(str[1]); String s=br.readLine(); String t=br.readLine(); int[] left=new int[m]; int[] right=new int[m]; HashMap<Character,ArrayList<Integer>> map=new HashMap<>(); for(int i=0;i<m;i++) { char key=t.charAt(i); if(!map.containsKey(key)) { map.put(key,new ArrayList<>()); } } for(int i=0;i<n;i++) { char key=s.charAt(i); if(map.containsKey(key)) { map.get(key).add(i); } } //left array filling left[0]=map.get(t.charAt(0)).get(0); int i=1; while(i<m) { char key=t.charAt(i); ArrayList<Integer> list=map.get(key); int low=0,high=list.size()-1,pans=0; while(low<=high) { int mid=(low+high)/2; int idx=list.get(mid); if(idx>left[i-1]) { pans=idx; high=mid-1; }else { low=mid+1; } } left[i]=pans; i++; } //right array filling ArrayList<Integer> list1=map.get(t.charAt(m-1)); right[m-1]=list1.get(list1.size()-1); i=m-2; while(i>=0) { char key=t.charAt(i); ArrayList<Integer> list=map.get(key); int low=0,high=list.size()-1, pans=0; while(low<=high) { int mid=(low+high)/2; int idx=list.get(mid); if(idx<right[i+1]) { pans=idx; low=mid+1; }else { high=mid-1; } } right[i]=pans; i--; } int max=Integer.MIN_VALUE; for(i=0;i<m-1;i++) { max=Math.max(max,right[i+1]-left[i]); } pw.println(max); } public static int powerOf2(long n ) { int ans=0; while(n%2==0) { n/=2; ans++; } return ans; } public static long NCR(int n,int r){ if(r==0||r==n)return 1; return (fac[n]*invfact[r]%mod)*invfact[n-r]%mod; } static long[] fac=new long[100]; static long[] invfact=new long[100]; public static void comp(){ fac[0]=1; invfact[0]=1; for(int i=1;i<100;i++){ fac[i]=(fac[i-1]*i)%mod; invfact[i]=modInverse(fac[i]); } } public static long modInverse(long n){ return power(n,mod-2); } public static long power(long x,long y){ long res=1; x=x%mod; while(y>0){ if((y&1)==1)res=(res*x)%mod; y=y>>1; x=(x*x)%mod; } return res; } //*************************************DSU************************************ */ public static void initialize(int n){ for(int i=1;i<=n;i++){ par[i]=i; rank[i]=1; } } public static void union(int x,int y){ int lx=find(x); int ly=find(y); if(lx!=ly){ if(rank[lx]>rank[ly]){ par[ly]=lx; }else if(rank[lx]<rank[ly]){ par[lx]=ly; }else{ par[lx]=ly; rank[ly]++; } } } public static int find(int x){ if(par[x]==x){ return x; } int temp=find(par[x]); par[x]=temp; return temp; } //************************************DSU END********************************** */ public static boolean isPresent(ArrayList<Long> zero,long val){ for(long x:zero){ if(x==val)return true; } return false; } public static class Pair implements Comparable<Pair>{ int val; int idx; Pair(int val,int idx){ this.val=val; this.idx=idx; } public int compareTo(Pair o){ return this.val-o.val; } } public static int countDigit(int x){ return (int)Math.log10(x)+1; } //****************************function to find all factor************************************************* public static ArrayList<Long> findAllFactors(long num){ ArrayList<Long> factors = new ArrayList<Long>(); for(long i = 1; i <= num/i; ++i) { if(num % i == 0) { //if i is a factor, num/i is also a factor factors.add(i); factors.add(num/i); } } //sort the factors Collections.sort(factors); return factors; } //*************************** function to find GCD of two number******************************************* public static long gcd(long a,long b){ if(b==0)return a; return gcd(b,a%b); } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 8
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
140444c90001c5a07be8cac379acaced
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.util.*; import java.io.*; public class cf { static Reader sc = new Reader(); static PrintWriter out = new PrintWriter(System.out); static long mod = (long) 1e9 + 7; public static void main(String[] args) { int n = sc.ni(), m = sc.ni(); char[] s = sc.next().toCharArray(); char[] t = sc.next().toCharArray(); int[] left = new int[m], right = new int[m]; ArrayList<Integer>[] indices = new ArrayList[26]; for (int i = 0; i < 26; i++) indices[i] = new ArrayList<>(); for (int i = 0; i < n; i++) { indices[s[i] - 'a'].add(i); } // left[i] = first occ. of t[i] after left[i-1] // right[i] = last occ. of t[i] before right[i+1] int prevStart = -1; for (int i = 0; i < m; i++) { int l = 0, r = indices[t[i] - 'a'].size() - 1, res = -1; while (l <= r) { int mid = l + (r - l) / 2; if (indices[t[i] - 'a'].get(mid) > prevStart) { res = indices[t[i] - 'a'].get(mid); r = mid - 1; } else { l = mid + 1; } } left[i] = res; prevStart = left[i]; } int nextEnd = n; for (int i = m - 1; i >= 0; i--) { int l = 0, r = indices[t[i] - 'a'].size() - 1, res = -1; while (l <= r) { int mid = l + (r - l) / 2; if (indices[t[i] - 'a'].get(mid) < nextEnd) { res = indices[t[i] - 'a'].get(mid); l = mid + 1; } else { r = mid - 1; } } right[i] = res; nextEnd = right[i]; } // print_arr(left); // print_arr(right); int res = 0; for (int i = 0; i < m - 1; i++) { res = Math.max(right[i + 1] - left[i], res); } out.println(res); out.close(); } public static long pow(long a, long b, long m) { if (b == 0) { return 1; } if (b % 2 == 0) { return pow((a * a) % m, b / 2, m) % m; } else { return (a * pow((a * a) % m, b / 2, m)) % m; } } // snippets static class Pair implements Comparable<Pair> { int x; int y; Pair(int x, int y) { this.x = x; this.y = y; } @Override public int compareTo(Pair o) { if (this.x > o.x) return 1; else if (this.x < o.x) return -1; else { if (this.y > o.y) return 1; else if (this.y < o.y) return -1; else return 0; } } public int hashCode() { int ans = 1; ans = x * 31 + y * 13; return ans; } @Override public boolean equals(Object o) { if (this == o) { return true; } if (o == null) { return false; } if (this.getClass() != o.getClass()) { return false; } Pair other = (Pair) o; if (this.x == other.x && this.y == other.y) { return true; } return false; } } static void add_map(HashMap<Integer, Integer> map, int v) { if (!map.containsKey(v)) { map.put(v, 1); } else { map.put(v, map.get(v) + 1); } } static void remove_map(HashMap<Integer, Integer> map, int v) { if (map.containsKey(v)) { map.put(v, map.get(v) - 1); if (map.get(v) == 0) map.remove(v); } } static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static void sort(int[] arr) { // shuffle Random rand = new Random(); for (int i = 0; i < arr.length; i++) { int j = rand.nextInt(i + 1); int tmp = arr[i]; arr[i] = arr[j]; arr[j] = tmp; } Arrays.sort(arr); } static void sort(long[] arr) { // shuffle Random rand = new Random(); for (int i = 0; i < arr.length; i++) { int j = rand.nextInt(i + 1); long tmp = arr[i]; arr[i] = arr[j]; arr[j] = tmp; } Arrays.sort(arr); } static boolean isPrime(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 isPrime(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 void print_arr(int A[]) { for (int i : A) { System.out.print(i + " "); } System.out.println(); } static void print_arr(long A[]) { for (long i : A) { System.out.print(i + " "); } System.out.println(); } static void print_arr(char A[]) { for (char i : A) { System.out.print(i + " "); } System.out.println(); } 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 int max(int A[]) { int max = Integer.MIN_VALUE; for (int i = 0; i < A.length; i++) { max = Math.max(max, A[i]); } return max; } static int min(int A[]) { int min = Integer.MAX_VALUE; for (int i = 0; i < A.length; i++) { min = Math.min(min, A[i]); } return min; } static long max(long A[]) { long max = Long.MIN_VALUE; for (int i = 0; i < A.length; i++) { max = Math.max(max, A[i]); } return max; } static long min(long A[]) { long min = Long.MAX_VALUE; for (int i = 0; i < A.length; i++) { min = Math.min(min, A[i]); } return min; } 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 ArrayList<Integer> sieve(int n) { ArrayList<Integer> primes = new ArrayList<>(); boolean[] isPrime = new boolean[n]; 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; } } } for (int i = 2; i < n; i++) { if (isPrime[i]) primes.add(i); } return primes; } static class Reader { BufferedReader br; StringTokenizer st; Reader() { br = new BufferedReader(new InputStreamReader(System.in)); st = new StringTokenizer(""); } Reader(File f) throws FileNotFoundException { br = new BufferedReader(new FileReader(f)); st = new StringTokenizer(""); } String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { } return st.nextToken(); } int ni() { return Integer.parseInt(next()); } long nl() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } int[] nai(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = ni(); return a; } long[] nal(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nl(); return a; } } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 11
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
11b132bf79aad18da37ea7619cfaa7a2
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.util.*; import java.io.*; public class Main { static InputReader scn = new InputReader(System.in); static OutputWriter out = new OutputWriter(System.out); public static void main(String[] args) { // Running Number Of TestCases (t) int t = 1; while(t-- > 0) solve(); out.close(); } public static void dummy(){ // Dummy Code for Rough out.println((int) 'a'); } public static void solve() { // Main Solution (AC) int n = scn.nextInt(); int k = scn.nextInt(); char[] s = scn.next().toCharArray(); char[] t = scn.next().toCharArray(); int S = s.length; int T = t.length; int[] start = new int[T]; int[] last = new int[T]; int idx = 0; for(int i = 0; i < T; i++){ while(idx < n && s[idx] != t[i]){ idx++; } start[i] = idx; idx++; // because it should br this again } idx = n - 1; for(int i = T - 1; i >= 0; i--){ while(idx >= 0 && s[idx] != t[i]){ idx--; } last[i] = idx; idx--; // because it should br this again } int ans = 0; for(int i = 0; i + 1 < T; i++){ int a = start[i]; int b = last[i+1]; ans = max(ans, b-a); } out.println(ans); } public static int max(int x, int y){ if(x >= y) return x; return y; } public static HashMap<Integer, Integer> sortByValue(HashMap<Integer, Integer> hm) { List<Map.Entry<Integer, Integer> > list = new LinkedList<Map.Entry<Integer, Integer> >(hm.entrySet()); Collections.sort(list, new Comparator<Map.Entry<Integer, Integer> >() { public int compare(Map.Entry<Integer, Integer> o1, Map.Entry<Integer, Integer> o2){ return (o1.getValue()).compareTo(o2.getValue()); } }); HashMap<Integer, Integer> temp = new LinkedHashMap<Integer, Integer>(); for (Map.Entry<Integer, Integer> aa : list) { temp.put(aa.getKey(), aa.getValue()); } return temp; } public static void sortbyColumn(int[][] arr, int col) { Arrays.sort(arr, new Comparator<int[]>() { public int compare(final int[] entry1, final int[] entry2) { if (entry1[col] > entry2[col]) return 1; else return -1; } }); } public static int BaseChange(int n, int r){ int i = 0; int ans = 0; while(n != 0){ int a = n % 10; n /= 10; ans += a * (Math.pow(r, i)); i++; } return ans; } public static HashSet<Long> primeFactors(long n, HashSet<Long> list, int d) { list.add(1L); list.add(n); for (long i = 2; i <= Math.sqrt(n); i += 1) { long a = i; if (n % a == 0) { list.add(a); if (n % (n / a) == 0) list.add(n / a); } } return list; } public static HashMap<Integer, Integer> CountFrequencies(int[] arr) { HashMap<Integer, Integer> map = new HashMap<>(); for (int i : arr) { if (map.containsKey(i)) map.put(i, map.get(i) + 1); else map.put(i, 1); } return map; } public static void ArraySort2D(int[][] arr, int xy) { // xy == 0, for sorting wrt X-Axis // xy == 1, for sorting wrt Y-Axis Arrays.sort(arr, Comparator.comparingDouble(o -> o[xy])); } public static long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } public static long lcm(long a, long b) { return (a * b) / gcd(a, b); } public static boolean isPrime(long n) { if (n < 2) return false; if (n == 2 || n == 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; long sqrtN = (long) Math.sqrt(n) + 1; for (long i = 6L; i <= sqrtN; i += 6) { if (n % (i - 1) == 0 || n % (i + 1) == 0) return false; } return true; } public static int firstOccurence(int array1[], int low, int high, int x, int n) { if (low <= high) { int mid = low + (high - low) / 2; if ((mid == 0 || x > array1[mid - 1]) && array1[mid] == x) return mid; else if (x > array1[mid]) return firstOccurence(array1, (mid + 1), high, x, n); else return firstOccurence(array1, low, (mid - 1), x, n); } return -1; } public static int min(int x, int y){ if(x >= y) return y; return x; } public static int lastOccurence(int array2[], int low, int high, int x, int n) { if (low <= high) { int mid = low + (high - low) / 2; if ((mid == n - 1 || x < array2[mid + 1]) && array2[mid] == x) return mid; else if (x < array2[mid]) return lastOccurence(array2, low, (mid - 1), x, n); else return lastOccurence(array2, (mid + 1), high, x, n); } return -1; } public static void quickSort(int[] arr, int lo, int hi) { if (lo >= hi) { return; } int mid = (lo + hi) / 2; int pivot = arr[mid]; int left = lo; int right = hi; while (left <= right) { while (arr[left] < pivot) { left++; } while (arr[right] > pivot) { right--; } if (left <= right) { int temp = arr[left]; arr[left] = arr[right]; arr[right] = temp; left++; right--; } } quickSort(arr, lo, right); quickSort(arr, left, hi); } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int[] readArrays(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = scn.nextInt(); } return a; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String nextLine() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return nextLine(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) { writer.print(' '); // writer.print(1); } writer.print(objects[i]); } } public void println(Object... objects) { print(objects); writer.println(); } public void close() { writer.close(); } } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 11
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
4c8cb566d2fda261830134526ebd19b5
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.util.*; public class A { public static void main(String[] args) { Scanner sc = new Scanner(System.in); StringBuilder sb = new StringBuilder(); int n = sc.nextInt(); int m = sc.nextInt(); String str1 = sc.next(); String str2 = sc.next(); char a[] = str1.toCharArray(); char b[] = str2.toCharArray(); int first[] = new int[m]; int last[] = new int[m]; int index = 0; for (int i = 0; i < m; i++) { while (a[index] != b[i]) { index++; continue; } first[i] = index; index++; } index = n - 1; for (int i = m - 1; i >= 0; i--) { while (a[index] != b[i]) { index--; continue; } last[i] = index; index--; } int ans = 0; for (int i = 0; i < m - 1; i++) { ans = Math.max(ans, last[i + 1] - first[i]); } sb.append(ans).append("\n"); System.out.print(sb); } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 11
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
f5ab39d31708986b3bc8b2ba9830f071
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.util.*; import java.math.*; import java.io.*; public class B{ static int[] dx={-1,1,0,0}; static int[] dy={0,0,1,-1}; static FastReader scan=new FastReader(); public static PrintWriter out = new PrintWriter (new BufferedOutputStream(System.out)); static ArrayList<Pair>es; static LinkedList<Integer>edges[]; static LinkedList<Integer>edges2[]; static boolean prime[]; static void sieve(int n) { prime = new boolean[n+1]; for(int i=0;i<n;i++) prime[i] = true; for(int p = 2; p*p <=n; p++) { if(prime[p] == true) { for(int i = p*p; i <= n; i += p) prime[i] = false; } } } public static int lowerBound(long[] array, int length, long value) { int low = 0; int high = length; while (low < high) { final int mid = (low + high) / 2; //checks if the value is less than middle element of the array if (value <= array[mid]) { high = mid; } else { low = mid + 1; } } return low; } public static int upperBound(long[] array, int length, long value) { int low = 0; int high = length; while (low < high) { final int mid = low+(high-low) / 2; if ( array[mid]>value) { high = mid ; } else { low = mid+1; } } return low; } static long mod(long x,long y) { if(x<0) x=x+(-x/y+1)*y; return x%y; } static boolean isPowerOfTwo(long n) { if (n == 0) return false; while (n != 1) { if (n % 2 != 0) return false; n = n / 2; } return true; } static boolean isprime(long x) { for(long i=2;i*i<=x;i++) if(x%i==0) return false; return true; } static int dist(int x1,int y1,int x2,int y2){ return Math.abs(x1-x2)+Math.abs(y1-y2); } static long cuberoot(long x) { long lo = 0, hi = 1000005; while(lo<hi) { long m = (lo+hi+1)/2; if(m*m*m>x) hi = m-1; else lo = m; } return lo; } public static int log2(int N) { // calculate log2 N indirectly // using log() method int result = (int)(Math.log(N) / Math.log(2)); return result; } static long gcd(long a, long b) { if(a!=0&&b!=0) while((a%=b)!=0&&(b%=a)!=0); return a^b; } static long LCM(long a,long b){ return (Math.abs(a*b))/gcd(a,b); } static int mid; public static class comp1 implements Comparator<Pair>{ public int compare(Pair o1,Pair o2){ return (int)(o1.y-o2.y); } } public static class comp2 implements Comparator<Pair>{ public int compare(Pair o1,Pair o2){ return (int)((o1.x+o1.y*mid)-(o2.x+o2.y*mid)); } } static boolean can(int m,int s) { return (s>=0&&s<=m*9); } static boolean collinear(long x1, long y1, long x2, long y2, long x3, long y3) { long a = x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2); if(a==0) return true; return false; } static void pythagoreanTriplets(int limit) { //ArrayList<Integer>res=new ArrayList<Integer>(); // triplet: a^2 + b^2 = c^2 int a, b, c = 0; // loop from 2 to max_limitit int m = 2; // Limiting c would limit // all a, b and c int cnt=0; int tmp=1; while (c < limit) { // now loop on j from 1 to i-1 //System.out.println("FUCK"); for ( int n=tmp; n < m; n++) { // Evaluate and print // triplets using // the relation between // a, b and c a = m * m - n * n; b = 2 * m * n; c = m * m + n * n; // System.out.println("FUCK"); if (c > limit) break; // System.out.println(a + " " + b + " " + c); if(c+b==c*c-b*b){ // out.println(n); /// out.println(a+" "+b+" "+c); cnt++; } } tmp++; m++; } out.println(cnt); } static int arr[]; static int n,l; static boolean flag=false; static boolean vis[]=new boolean[(int)2e5+5]; static int res[]; static int idx=0; static StringBuilder s; static void dfs(int x) { vis[x]=true; res[idx++]=x; for(int v:edges[x]) if(!vis[v]) dfs(v); } static void swap(int i,int j) { char tmp=s.charAt(i); s.setCharAt(i,s.charAt(j)); s.setCharAt(j,tmp); } public static void main(String[] args) throws Exception { /*int xx=253; for(int i=1;i*i<=xx;i++) { if(xx%i==0) { System.out.println(i); System.out.println(xx/i); } }*/ //java.util.Scanner scan=new java.util.Scanner(new File("mootube.in")); // PrintWriter out = new PrintWriter (new FileWriter("mootube.out")); //Scannen=new FastReader("moocast.in"); //out = new PrintWriter ("moocast.out"); //System.out.println(3^2); //System.out.println(19%4); //StringBuilder news=new StringBuilder("ab"); //news.deleteCharAt(1); //news.insert(0,'c'); //news.deleteCharAt(0); //System.out.println(news); //System.out.println(can(2,15)); //System.out.println(LCM(2,2)); // System.out.println(31^15); //System.out.println(""); //System.out.println(824924296372176000L>(long)1e16); int tt=1; //rec(2020); //int st=scan.nextInt(); //System.out.println(calc(91)); //sieve(21000); //SNWNSENSNNSWNNW // System.out.println(set.remove(new Pair(1,1))); //System.out.println(count("cccccccccccccccccccccccccooooooooooooooooooooooooodddddddddddddeeeeeeeeeeeeeeeeeeeeeeeeeffffffffffffforrrrrrrrrrrrrcesssssssssssss","codeforces")); //S0ystem.out.println(isPowerOfTwo(446265625L)); //System.out.println("daaa".compareTo("bccc")); //System.out.println(2999000999L>1999982505L); //tt=scan.nextInt(); outer:while(tt-->0) { int n=scan.nextInt(),m=scan.nextInt(); String s=scan.next(),t=scan.next(); ArrayList<Integer>arr[]=new ArrayList[26]; PriorityQueue<Integer>pq[]=new PriorityQueue[26]; PriorityQueue<Integer>pq2[]=new PriorityQueue[26]; TreeSet<Integer>set[]=new TreeSet[26]; for(int i=0;i<26;i++){ pq[i]=new PriorityQueue(); set[i]=new TreeSet(); } for(int i=0;i<26;i++) pq2[i]=new PriorityQueue(Collections.reverseOrder()); for(int i=0;i<s.length();i++){ pq[s.charAt(i)-'a'].add(i); pq2[s.charAt(i)-'a'].add(i); set[s.charAt(i)-'a'].add(i); } int first[]=new int[m]; int second[]=new int[m]; int idx=0; for(int i=0;i<m;i++) { //first[i]=pq[t.charAt(i)-'a'].poll(); if(i==0){ first[i]=set[t.charAt(i)-'a'].first(); idx=set[t.charAt(i)-'a'].first(); } else{ first[i]=set[t.charAt(i)-'a'].higher(idx); idx=first[i]; } } for(int i=m-1;i>=0;i--) { if(i==m-1) { second[i]=first[i]=set[t.charAt(i)-'a'].last(); idx=set[t.charAt(i)-'a'].last(); } else{ second[i]=set[t.charAt(i)-'a'].lower(idx); idx=second[i]; }} //for(int i=0;i<m;i++) // out.println(first[i]+" "+second[i]); int max=0; for(int i=0;i<m-1;i++) max=Math.max(max,second[i+1]-first[i]); out.println(max); } out.close(); } static class dsu{ static int id[]=new int[101]; dsu() { for(int i=0;i<101;i++) id[i]=i; } static int find(int x) { if(x==id[x]) return x; return find(id[x]); } static void connect(int i,int j) { i=find(i); j=find(j); id[i]=j; } static boolean is(int i,int j) { return find(i)==find(j); } } static long binexp(long a,long n,long mod) { if(n==0) return 1; long res=binexp(a,n/2,mod)%mod; res=res*res; if(n%2==1) return (res*a)%mod; else return res%mod; } static class special{ Pair x; Pair y; special(Pair x,Pair y) { this.x=new Pair(x.x,x.y); this.y=new Pair(y.x,y.y); } @Override public int hashCode() { return (int)(x.x + 31 * y.y); } public boolean equals(Object other) { if(other instanceof special) { special e =(special)other; return this.x.x==e.x.x && this.x.y==e.x.y && this.y.x==e.y.x && this.y.y==e.y.y; } else return false; } } static long powMod(long base, long exp, long mod) { if (base == 0 || base == 1) return base; if (exp == 0) return 1; if (exp == 1) return base % mod; long R = powMod(base, exp/2, mod) % mod; R *= R; R %= mod; if ((exp & 1) == 1) { return base * R % mod; } else return R % mod; } public static long pow(long b, long e) { long r = 1; while (e > 0) { if (e % 2 == 1) r = r * b ; b = b * b; e >>= 1; } return r; } private static void sort(int[] arr) { List<Integer> list = new ArrayList<>(); for (int object : arr) list.add(object); Collections.sort(list); for (int i = 0; i < list.size(); ++i) arr[i] = list.get(i); } public static class FastReader { BufferedReader br; StringTokenizer root; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } FastReader(String filename)throws Exception { br=new BufferedReader(new FileReader(filename)); } String next() { while (root == null || !root.hasMoreTokens()) { try { root = new StringTokenizer(br.readLine()); } catch (Exception addd) { addd.printStackTrace(); } } return root.nextToken(); } int nextInt() { return Integer.parseInt(next()); } double nextDouble() { return Double.parseDouble(next()); } long nextLong() { return Long.parseLong(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (Exception addd) { addd.printStackTrace(); } return str; } public int[] nextIntArray(int arraySize) { int array[] = new int[arraySize]; for (int i = 0; i < arraySize; i++) { array[i] = nextInt(); } return array; } } public static class Pair implements Comparable<Pair>{ int x; int y; long ab; int z; public Pair(){} public Pair(int x1, int y1,int z) { x=x1; y=y1; this.z=z; } public Pair(int x1, int y1) { x=x1; y=y1; this.ab=x+y; } @Override public int hashCode() { return (int)(x + 31 * y); } public String toString() { return x + " " + y; } @Override public boolean equals(Object o){ if (o == this) return true; if (o.getClass() != getClass()) return false; Pair t = (Pair)o; return t.x == x && t.y == y; } public int compareTo(Pair o) { if(x==o.x) return (int)(y-o.y); return (int)(x-o.x); } } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 11
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
7f33c2b6da807b6ef4f5ec61b4452f4f
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.io.*; import java.lang.*; import java.util.*; public class ComdeFormces { public static void main(String[] args) throws Exception{ // TODO Auto-generated method stub // Reader.init(System.in); FastReader sc=new FastReader(); BufferedWriter log = new BufferedWriter(new OutputStreamWriter(System.out)); // OutputStream out = new BufferedOutputStream ( System.out ); // int t=sc.nextInt(); // while(t--!=0) { int n=sc.nextInt(); int m=sc.nextInt(); char a[]=sc.next().toCharArray(); char b[]=sc.next().toCharArray(); int l[]=new int[m]; int r[]=new int[m]; int ptr=m-1; for(int i=n-1;i>=0;i--) { if(ptr<0)break; if(a[i]==b[ptr])r[ptr--]=i; } ptr=0; for(int i=0;i<n;i++) { if(ptr>=m)break; if(a[i]==b[ptr])l[ptr++]=i; } int ans=0; for(int i=1;i<m;i++) { ans=Math.max(ans, r[i]-l[i-1]); } log.write(ans+" "); log.flush(); } // } static int ev(int s,int e,int e2,int l) { for(int i=s;i<=e;i++) { int ts=i; int vl2=0; boolean cv=false; int ct=0; while(ts!=0) { int num=ts%10; if(num==0 || num==1 || num==2 || num==5 || num==8) { if(num==2)num=5; else if(num==5)num=2; vl2=vl2*10+num; ct++; } else { cv=true; break; } ts/=10; } if(ct==1) { vl2*=10; } if(cv || vl2>=e2)continue; else return i; } return -1; } static int evv(int cnt[],int cnt2[]) { int ans=0; for(int i=1;i<cnt.length;i++) { if(cnt[i]>cnt2[i]) { ans+=cnt[i]-cnt2[i]; } } return ans; } public static void fb(HashMap<Long,Integer> hm,HashSet<Long> hs, long s) { if(hs.contains(s)) { hm.put(s,hm.get(s)-1); if(hm.get(s)==0)hs.remove(s); return; } else if(s<=1)return; fb(hm,hs,s/2); fb(hm,hs,(s+1)/2); } public static void radixSort(int a[]) { int n=a.length; int res[]=new int[n]; int p=1; for(int i=0;i<=8;i++) { int cnt[]=new int[10]; for(int j=0;j<n;j++) { a[j]=res[j]; cnt[(a[j]/p)%10]++; } for(int j=1;j<=9;j++) { cnt[j]+=cnt[j-1]; } for(int j=n-1;j>=0;j--) { res[cnt[(a[j]/p)%10]-1]=a[j]; cnt[(a[j]/p)%10]--; } p*=10; } } static int bits(long n) { int ans=0; while(n!=0) { if((n&1)==1)ans++; n>>=1; } return ans; } static long flor(ArrayList<Long> ar,long el) { int s=0; int e=ar.size()-1; while(s<=e) { int m=s+(e-s)/2; if(ar.get(m)==el)return ar.get(m); else if(ar.get(m)<el)s=m+1; else e=m-1; } return e>=0?e:-1; } public static int kadane(int a[]) { int sum=0,mx=Integer.MIN_VALUE; for(int i=0;i<a.length;i++) { sum+=a[i]; mx=Math.max(mx, sum); if(sum<0) sum=0; } return mx; } public static int m=1000000007; public static int mul(int a, int b) { return ((a%m)*(b%m))%m; } public static long mul(long a, long b) { return ((a%m)*(b%m))%m; } public static int add(int a, int b) { return ((a%m)+(b%m))%m; } public static long add(long a, long b) { return ((a%m)+(b%m))%m; } //debug public static <E> void p(E[][] a,String s) { System.out.println(s); for(int i=0;i<a.length;i++) { for(int j=0;j<a[0].length;j++) { System.out.print(a[i][j]+" "); } System.out.println(); } } public static void p(int[] a,String s) { System.out.print(s+"="); for(int i=0;i<a.length;i++)System.out.print(a[i]+" "); System.out.println(); } public static void p(long[] a,String s) { System.out.print(s+"="); for(int i=0;i<a.length;i++)System.out.print(a[i]+" "); System.out.println(); } public static <E> void p(E a,String s){ System.out.println(s+"="+a); } public static <E> void p(ArrayList<E> a,String s){ System.out.println(s+"="+a); } public static <E> void p(LinkedList<E> a,String s){ System.out.println(s+"="+a); } public static <E> void p(HashSet<E> a,String s){ System.out.println(s+"="+a); } public static <E> void p(Stack<E> a,String s){ System.out.println(s+"="+a); } public static <E> void p(Queue<E> a,String s){ System.out.println(s+"="+a); } //utils static ArrayList<Integer> divisors(int n){ ArrayList<Integer> ar=new ArrayList<>(); for (int i=2; i<=Math.sqrt(n); i++){ if (n%i == 0){ if (n/i == i) { ar.add(i); } else { ar.add(i); ar.add(n/i); } } } return ar; } static int primeDivisor(int n){ ArrayList<Integer> ar=new ArrayList<>(); int cnt=0; boolean pr=false; while(n%2==0) { pr=true; n/=2; } if(pr)ar.add(2); for(int i=3;i*i<=n;i+=2) { pr=false; while(n%i==0) { n/=i; pr=true; } if(pr)ar.add(i); } if(n>2) ar.add(n); return ar.size(); } static long gcd(long a,long b) { if(b==0)return a; else return gcd(b,a%b); } static int gcd(int a,int b) { if(b==0)return a; else return gcd(b,a%b); } static long factmod(long n,long mod,long img) { if(n==0)return 1; long ans=1; long temp=1; while(n--!=0) { if(temp!=img) { ans=((ans%mod)*((temp)%mod))%mod; } temp++; } return ans%mod; } static int ncr(int n, int r){ if(r>n-r)r=n-r; int ans=1; for(int i=0;i<r;i++){ ans*=(n-i); ans/=(i+1); } return ans; } public static class trip{ int a,b; int c; public trip(int a,int b,int c) { this.a=a; this.b=b; this.c=c; } public int compareTo(trip q) { return this.b-q.b; } } static void mergesort(long[] a,int start,int end) { if(start>=end)return ; int mid=start+(end-start)/2; mergesort(a,start,mid); mergesort(a,mid+1,end); merge(a,start,mid,end); } static void merge(long[] a, int start,int mid,int end) { int ptr1=start; int ptr2=mid+1; long b[]=new long[end-start+1]; int i=0; while(ptr1<=mid && ptr2<=end) { if(a[ptr1]<=a[ptr2]) { b[i]=a[ptr1]; ptr1++; i++; } else { b[i]=a[ptr2]; ptr2++; i++; } } while(ptr1<=mid) { b[i]=a[ptr1]; ptr1++; i++; } while(ptr2<=end) { b[i]=a[ptr2]; ptr2++; i++; } for(int j=start;j<=end;j++) { a[j]=b[j-start]; } } public static class FastReader { BufferedReader b; StringTokenizer s; public FastReader() { b=new BufferedReader(new InputStreamReader(System.in)); } String next() { while(s==null ||!s.hasMoreElements()) { try { s=new StringTokenizer(b.readLine()); } catch(IOException e) { e.printStackTrace(); } } return s.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str=""; try { str=b.readLine(); } catch(IOException e) { e.printStackTrace(); } return str; } boolean hasNext() { if (s != null && s.hasMoreTokens()) { return true; } String tmp; try { b.mark(1000); tmp = b.readLine(); if (tmp == null) { return false; } b.reset(); } catch (IOException e) { return false; } return true; } } public static class pair{ int a; int b; public pair(int a,int b) { this.a=a; this.b=b; } public int compareTo(pair b) { return this.a-b.a; } // public int compareToo(pair b) { // return this.b-b.b; // } @Override public String toString() { return "{"+this.a+" "+this.b+"}"; } } static long pow(long a, long pw) { long temp; if(pw==0)return 1; temp=pow(a,pw/2); if(pw%2==0)return temp*temp; return a*temp*temp; } static int pow(int a, int pw) { int temp; if(pw==0)return 1; temp=pow(a,pw/2); if(pw%2==0)return temp*temp; return a*temp*temp; } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 11
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
309b96ccad979e57404f86612673b1ee
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.io.*; import java.util.*; import java.math.BigDecimal; import java.math.*; // public class Main{ private static int[] input() { int n=in.nextInt(); int[]arr=new int[n]; for(int i=0;i<n;i++) { arr[i]=in.nextInt(); } return arr; } private static int inte() { return in.nextInt(); } private static long lon() { return in.nextLong(); } public static void main(String[] args) { TaskA solver = new TaskA(); // int t = in.nextInt(); // for (int i = 1; i <= t ; i++) { // solver.solve(t, in, out); // } solver.solve(1, in, out); out.flush(); out.close(); } static ArrayList<Integer>[]graph;static int[]arr; static long[][] dp; static class TaskA { public void solve(int testNumber, InputReader in, PrintWriter out) { int n= in.nextInt();int m=in.nextInt(); String s= in.next();String t=in.next(); int[]right =new int[m]; int[]left=new int [m];int ind1=0;int ind2=m-1; for(int i=0;i<n&&ind1<m;i++) { if(s.charAt(i)==t.charAt(ind1)) { left[ind1]=i;ind1++; } } for(int i=n-1;i>=0&&ind2>=0;i--) { if(s.charAt(i)==t.charAt(ind2)) { right[ind2]=i;ind2--; } } int max=0;for(int i=0;i<m-1;i++) { max=Math.max(max, right[i+1]-left[i]); } println(max); } } public static boolean valid(int tim) { String ti=String.valueOf(tim); char[]time=ti.toCharArray(); for(int i=0;i<time.length;i++) { if(time[i]!='1'&&time[i]!='0'&&time[i]!='2'&&time[i]!='5'&&time[i]!='8') { return false; } } return true; } private static int opp(int x) { if(x==1||x==8||x==0) {return x;} else if(x==2) {return 5;} {return 2;} } public static int getFirstSetBitPos(int n) { return (int)((Math.log10(n & -n)) / Math.log10(2)) + 1; } public static long rec(int i,int j,int[]arr) { if(arr.length==1) {return 0;} if(dp[i][j]!=0) {return dp[i][j];} else if(j-i==1) {dp[i][j]=arr[j]-arr[i];return dp[i][j];} else { dp[i][j]=Math.min(rec(i+1,j,arr),rec(i,j-1,arr))+arr[j]-arr[i]; return dp[i][j]; } } private static boolean isPalindrome(String s) { StringBuilder sb=new StringBuilder(s); if(sb.reverse().toString().equals(s)) return true; return false; } private static void dfs(int root,Pair[]p,int parent,int[][]dp) { int ans1=0;int ans2=0; for(int i:graph[root]) { if(i!=parent) { dfs(i,p,root,dp); } } for(int i:graph[root]) { if(i!=parent) { int val1=Math.abs(p[root].first-p[i].first)+dp[i][0]; int val2=Math.abs(p[root].first-p[i].second)+dp[i][1]; ans1+=Math.max(val1, val2); } } for(int i:graph[root]) { if(i!=parent) { int val1=Math.abs(p[root].second-p[i].first)+dp[i][0]; int val2=Math.abs(p[root].second-p[i].second)+dp[i][1]; ans2+=Math.max(val1, val2); } } dp[root][0]=ans1;dp[root][1]=ans2; } private static void printArr(int[] arr) { for (int i = 0; i < arr.length; i++) { print(arr[i] + " "); } print("\n"); } private static String rev(String s) { String st=""; for(int i=s.length()-1;i>=0;i--) { st+=s.charAt(i); } return st; } private static int[] rev(int[]arr) { int[]a=arr.clone(); for(int i=0;i<arr.length;i++) { a[i]=arr[arr.length-i-1]; } return arr; } // for (Map.Entry<Integer,ArrayList<Integer>> entry : hm.entrySet()) { // ArrayList<Integer>a=entry.getValue(); // // } static long ceil(long a,long b) { return (a/b + Math.min(a%b, 1)); } static long pow(long b, long e) { long ans = 1; while (e > 0) { if (e % 2 == 1) ans = ans * b % mod; e >>= 1; b = b * b % mod; } return ans; } static void sortDiff(Pair arr[]) { // Comparator to sort the pair according to second element Arrays.sort(arr, new Comparator<Pair>() { @Override public int compare(Pair p1, Pair p2) { if(p1.first==p2.first) { return (p1.second-p1.first)-(p2.second-p1.first); } return (p1.second-p1.first)-(p2.second-p2.first); } }); } static void sortF(Pair arr[]) { // Comparator to sort the pair according to second element Arrays.sort(arr, new Comparator<Pair>() { @Override public int compare(Pair p1, Pair p2) { if(p1.first==p2.first) { return p1.second-p2.second; } return p1.first - p2.first; } }); } static long[] fac; static long mod = (long) 1000000007; static void initFac(long n) { fac = new long[(int)n + 1]; fac[0] = 1; for (int i = 1; i <= n; i++) { fac[i] = (fac[i - 1] * i) % mod; } } static long nck(int n, int k) { if (n < k) return 0; long den = inv((int) (fac[k] * fac[n - k] % mod)); return fac[n] * den % mod; } static long inv(long x) { return pow(x, mod - 2); } static void sort(int[] a) { ArrayList<Integer> q = new ArrayList<>(); for (int i : a) q.add(i); Collections.sort(q); for (int i = 0; i < a.length; i++) a[i] = q.get(i); } static void sort(long[] a) { ArrayList<Long> q = new ArrayList<>(); for (long i : a) q.add(i); Collections.sort(q); for (int i = 0; i < a.length; i++) a[i] = q.get(i); } public static int bfsSize(int source,LinkedList<Integer>[]a,boolean[]visited) { Queue<Integer>q=new LinkedList<>(); q.add(source); visited[source]=true; int distance=0; while(!q.isEmpty()) { int curr=q.poll(); distance++; for(int neighbour:a[curr]) { if(!visited[neighbour]) { visited[neighbour]=true; q.add(neighbour); } } } return distance; } public static Set<Integer>factors(int n){ Set<Integer>ans=new HashSet<>(); ans.add(1); for(int i=2;i*i<=n;i++) { if(n%i==0) { ans.add(i); ans.add(n/i); } } return ans; } public static int bfsSp(int source,int destination,ArrayList<Integer>[]a) { boolean[]visited=new boolean[a.length]; int[]parent=new int[a.length]; Queue<Integer>q=new LinkedList<>(); int distance=0; q.add(source); visited[source]=true; parent[source]=-1; while(!q.isEmpty()) { int curr=q.poll(); if(curr==destination) { break; } for(int neighbour:a[curr]) { if(neighbour!=-1&&!visited[neighbour]) { visited[neighbour]=true; q.add(neighbour); parent[neighbour]=curr; } } } int cur=destination; while(parent[cur]!=-1) { distance++; cur=parent[cur]; } return distance; } static int bs(int size,int[]arr) { int x = -1; for (int b = size/2; b >= 1; b /= 2) { while (!ok(arr)); } int k = x+1; return k; } static boolean ok(int[]x) { return false; } public static int solve1(ArrayList<Integer> A) { long[]arr =new long[A.size()+1]; int n=A.size(); for(int i=1;i<=A.size();i++) { arr[i]=((i%2)*((n-i+1)%2))%2; arr[i]%=2; } int ans=0; for(int i=0;i<A.size();i++) { if(arr[i+1]==1) { ans^=A.get(i); } } return ans; } public static String printBinary(long a) { String str=""; for(int i=31;i>=0;i--) { if((a&(1<<i))!=0) { str+=1; } if((a&(1<<i))==0 && !str.isEmpty()) { str+=0; } } return str; } public static String reverse(long a) { long rev=0; String str=""; int x=(int)(Math.log(a)/Math.log(2))+1; for(int i=0;i<32;i++) { rev<<=1; if((a&(1<<i))!=0) { rev|=1; str+=1; } else { str+=0; } } return str; } //////////////////////////////////////////////////////// static void sortS(Pair arr[]) { // Comparator to sort the pair according to second element Arrays.sort(arr, new Comparator<Pair>() { @Override public int compare(Pair p1, Pair p2) { return p1.second - p2.second; } }); } static class Pair implements Comparable<Pair> { int first ;int second ; public Pair(int x, int y) { this.first = x ;this.second = y ; } @Override public boolean equals(Object obj) { if(obj == this)return true ; if(obj == null)return false ; if(this.getClass() != obj.getClass()) return false ; Pair other = (Pair)(obj) ; if(this.first != other.first)return false ; if(this.second != other.second)return false ; return true ; } @Override public int hashCode() { return this.first^this.second ; } @Override public String toString() { String ans = "" ;ans += this.first ; ans += " "; ans += this.second ; return ans ; } @Override public int compareTo(Main.Pair o) { { if(this.first==o.first) { return this.second-o.second; } return this.first - o.first; } } } ////////////////////////////////////////////////////////////////////////// static int nD(long num) { String s=String.valueOf(num); return s.length(); } static int CommonDigits(int x,int y) { String s=String.valueOf(x); String s2=String.valueOf(y); return 0; } static int lcs(String str1, String str2, int m, int n) { int L[][] = new int[m + 1][n + 1]; int i, j; // Following steps build L[m+1][n+1] in // bottom up fashion. Note that L[i][j] // contains length of LCS of str1[0..i-1] // and str2[0..j-1] for (i = 0; i <= m; i++) { for (j = 0; j <= n; j++) { if (i == 0 || j == 0) L[i][j] = 0; else if (str1.charAt(i - 1) == str2.charAt(j - 1)) L[i][j] = L[i - 1][j - 1] + 1; else L[i][j] = Math.max(L[i - 1][j], L[i][j - 1]); } } // L[m][n] contains length of LCS // for X[0..n-1] and Y[0..m-1] return L[m][n]; } ///////////////////////////////// boolean IsPowerOfTwo(int x) { return (x != 0) && ((x & (x - 1)) == 0); } //////////////////////////////// static long power(long a,long b,long m ) { long ans=1; while(b>0) { if(b%2==1) { ans=((ans%m)*(a%m))%m; b--; } else { a=(a*a)%m;b/=2; } } return ans%m; } /////////////////////////////// public static boolean repeatedSubString(String string) { return ((string + string).indexOf(string, 1) != string.length()); } static int search(char[]c,int start,int end,char x) { for(int i=start;i<end;i++) { if(c[i]==x) {return i;} } return -2; } //////////////////////////////// static int gcd(int a, int b) { while (b != 0) { int t = b; b = a % b; a = t; } return a; } static long fac(long a) { if(a== 0L || a==1L)return 1L ; return a*fac(a-1L) ; } static ArrayList al() { ArrayList<Integer>a =new ArrayList<>(); return a; } static HashSet h() { return new HashSet<Integer>(); } static void debug(long[][]a) { int n= a.length; for(int i=0;i<a.length;i++) { for(int j=0;j<a[0].length;j++) { out.print(a[i][j]+" "); } out.print("\n"); } } static void debug(int[]a) { out.println(Arrays.toString(a)); } static void debug(ArrayList<Integer>a) { out.println(a.toString()); } static boolean[]seive(int n){ boolean[]b=new boolean[n+1]; for (int i = 2; i <= n; i++) b[i] = true; for(int i=2;i*i<=n;i++) { if(b[i]) { for(int j=i*i;j<=n;j+=i) { b[j]=false; } } } return b; } static int longestIncreasingSubseq(int[]arr) { int[]sizes=new int[arr.length]; Arrays.fill(sizes, 1); int max=1; for(int i=1;i<arr.length;i++) { for(int j=0;j<i;j++) { if(arr[j]<arr[i]) { sizes[i]=Math.max(sizes[i],sizes[j]+1); max=Math.max(max, sizes[i]); } } } return max; } public static ArrayList primeFactors(long n) { ArrayList<Long>h= new ArrayList<>(); // Print the number of 2s that divide n if(n%2 ==0) {h.add(2L);} // n must be odd at this point. So we can // skip one element (Note i = i +2) for (long i = 3; i <= Math.sqrt(n); i+= 2) { if(n%i==0) {h.add(i);} } if (n > 2) h.add(n); return h; } static boolean Divisors(long n){ if(n%2==1) { return true; } for (long i=2; i<=Math.sqrt(n); i++){ if (n%i==0 && i%2==1){ return true; } } return false; } static InputStream inputStream = System.in; static OutputStream outputStream = System.out; static InputReader in = new InputReader(inputStream); static PrintWriter out = new PrintWriter(outputStream); public static void superSet(int[]a,ArrayList<String>al,int i,String s) { if(i==a.length) { al.add(s); return; } superSet(a,al,i+1,s); superSet(a,al,i+1,s+a[i]+" "); } public static long[] makeArr() { long size=in.nextInt(); long []arr=new long[(int)size]; for(int i=0;i<size;i++) { arr[i]=in.nextInt(); } return arr; } public static long[] arr(int n) { long []arr=new long[n+1]; for(int i=1;i<n+1;i++) { arr[i]=in.nextLong(); } return arr; } public static void sort(long arr[], int l, int r) { if (l < r) { // Find the middle point int m = (l+r)/2; // Sort first and second halves sort(arr, l, m); sort(arr , m+1, r); // Merge the sorted halves merge(arr, l, m, r); } } static void println(long x) { out.println(x); } static void print(long c) { out.print(c); } static void print(int c) { out.print(c); } static void println(int x) { out.println(x); } static void print(String s) { out.print(s); } static void println(String s) { out.println(s); } public static void merge(long arr[], int l, int m, int r) { // Find sizes of two subarrays to be merged int n1 = m - l + 1; int n2 = r - m; /* Create temp arrays */ long L[] = new long[n1]; long R[] = new long[n2]; //Copy data to temp arrays for (int i=0; i<n1; ++i) L[i] = arr[l + i]; for (int j=0; j<n2; ++j) R[j] = arr[m + 1+ j]; /* Merge the temp arrays */ // Initial indexes of first and second subarrays int i = 0, j = 0; // Initial index of merged subarry array int k = l; while (i < n1 && j < n2) { if (L[i] <= R[j]) { arr[k] = L[i]; i++; } else { arr[k] = R[j]; j++; } k++; } /* Copy remaining elements of L[] if any */ while (i < n1) { arr[k] = L[i]; i++; k++; } /* Copy remaining elements of R[] if any */ while (j < n2) { arr[k] = R[j]; j++; k++; } } public static void reverse(int[] array) { int n = array.length; for (int i = 0; i < n / 2; i++) { int temp = array[i]; array[i] = array[n - i - 1]; array[n - i - 1] = temp; } } public static long nCr(int n,int k) { long ans=1L; k=k>n-k?n-k:k; for( int j=1;j<=k;j++,n--) { if(n%j==0) { ans*=n/j; }else if(ans%j==0) { ans=ans/j*n; }else { ans=(ans*n)/j; } } return ans; } static int searchMax(int index,long[]inp) { while(index+1<inp.length &&inp[index+1]>inp[index]) { index+=1; } return index; } static int searchMin(int index,long[]inp) { while(index+1<inp.length &&inp[index+1]<inp[index]) { index+=1; } return index; } public class ListNode { int val; ListNode next; ListNode() {} ListNode(int val) { this.val = val; } ListNode(int val, ListNode next) { this.val = val; this.next = next; } } static class Pairr implements Comparable<Pairr>{ private int index; private int cumsum; private ArrayList<Integer>indices; public Pairr(int index,int cumsum) { this.index=index; this.cumsum=cumsum; indices=new ArrayList<Integer>(); } public int compareTo(Pairr other) { return Integer.compare(cumsum, other.cumsum); } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 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()); } } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 11
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
02e9c9e43bedc08524e5614a46f497a4
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.util.*; public class A { public static int gcd(int a, int b) { if (a > b) { int t = b; b = a; a = t; } if (a == 0) { return b; } return gcd(b % a, a); } static int low_bound(LinkedList<Integer> ll, int l, int r, int x) {//l和r为下标,x为目标值 while (l <= r) { int mid = (l + r) >> 1; if (ll.get(mid) >= x) r = mid - 1; else l = mid + 1; } return r; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); sc.nextLine(); String ss = sc.nextLine(); String tt = sc.nextLine(); char[] s = ss.toCharArray(); char[] t = tt.toCharArray(); int cur = 0; int[] dp1 = new int[m]; int[] dp2 = new int[m]; for (int i = 0; i < m; i++) { while (s[cur] != t[i]) { cur++; } dp1[i] = cur++; } cur = n - 1; for (int i = m - 1; i >= 0; i--) { while (s[cur] != t[i]) { cur--; } dp2[i] = cur--; } int re = 0; for (int i = 0; i < m - 1; i++) { re = Math.max(re, dp2[i + 1] - dp1[i]); } System.out.println(re); } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 11
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
ac3f4315dc1ab9b7ac05e69f4f1cd054
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.io.BufferedWriter; import java.io.Writer; import java.io.OutputStreamWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Anubhav */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); CMaximumWidth solver = new CMaximumWidth(); solver.solve(1, in, out); out.close(); } static class CMaximumWidth { public void solve(int testNumber, InputReader in, OutputWriter out) { int n = in.nextInt(); int m = in.nextInt(); char s[] = new char[n]; for (int i = 0; i < n; i++) s[i] = in.nextCharacter(); char t[] = new char[n]; for (int i = 0; i < m; i++) t[i] = in.nextCharacter(); int first[] = new int[m]; int second[] = new int[m]; int f = 0; for (int i = 0; i < n; i++) { if (f == m) break; if (s[i] == t[f]) { first[f] = i; f++; } } f = m - 1; for (int i = n - 1; i >= 0; i--) { if (f < 0) break; if (s[i] == t[f]) { second[f] = i; f--; } } int ans = -1; for (int i = 1; i < m; i++) { int r = second[i] - first[i - 1]; ans = Math.max(ans, r); } out.println(ans); } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private InputReader.SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public char nextCharacter() { int c = read(); while (isSpaceChar(c)) { c = read(); } return (char) c; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void close() { writer.close(); } public void println(int i) { writer.println(i); } } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 11
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
2e007b893169c6e3fe70d8933a0e5ed1
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; import java.io.Writer; import java.io.OutputStreamWriter; import java.io.BufferedReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); CMaximumWidth solver = new CMaximumWidth(); solver.solve(1, in, out); out.close(); } static class CMaximumWidth { public void solve(int testNumber, InputReader in, OutputWriter out) { int n = in.nextInt(), m = in.nextInt(); char[] s = in.next().toCharArray(); char[] t = in.next().toCharArray(); int[] left = new int[m]; int[] right = new int[m]; int j = 0; for (int i = 0; i < m; i++) { while (s[j] != t[i]) j++; left[i] = j; j++; } j = n - 1; for (int i = m - 1; i >= 0; i--) { while (s[j] != t[i]) j--; right[i] = j; j--; } int ans = 0; for (int i = 1; i < m; i++) { ans = Math.max(ans, right[i] - left[i - 1]); } out.println(ans); } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void close() { writer.close(); } public void println(int i) { writer.println(i); } } static class InputReader { BufferedReader reader; StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 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()); } } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 11
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
021f360b87e1cd668bd15487a2cc6129
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.util.*; import java.awt.Point; import java.io.*; import java.math.BigInteger; public class Solutions { //static ArrayList<ArrayList<Integer>>list=new ArrayList<ArrayList<Integer>>(); static FastScanner scr=new FastScanner(); static PrintStream out=new PrintStream(System.out); public static void main(String []args) { // int T=scr.nextInt(); // t:for (int tt=0; tt<T; tt++) { int n=scr.nextInt(); int m=scr.nextInt(); char s[]=scr.next().toCharArray(); char t[]=scr.next().toCharArray(); int left[]=new int[m]; int right[]=new int[m]; int i=0; int j=0; while(i<n && j<m) { if(t[j]==s[i]) { left[j]=i; j++; } i++; } i=n-1; j=m-1; while(i>=0 && j>=0) { if(t[j]==s[i]) { right[j]=i; j--; } i--; } int diff=MIN; for(int k=1;k<m;k++) { diff=Math.max(diff,right[k]-left[k-1]); } out.println(diff); // } } static long modPow(long base,long exp,long mod) { if(exp==0) { return 1; } if(exp%2==0) { long res=(modPow(base,exp/2,mod)); return (res*res)%mod; } return (base*modPow(base,exp-1,mod))%mod; } static long gcd(long a,long b) { if(b==0) { return a; } return gcd(b,a%b); } 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[] sort(int a[]) { Arrays.sort(a); return a; } int []reverse(int a[]){ int b[]=new int[a.length]; int index=0; for(int i=a.length-1;i>=0;i--) { b[index]=a[i]; } return b; } 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()); } } static int MAX=Integer.MAX_VALUE; static int MIN=Integer.MIN_VALUE; }class triplet{ int x; int y; int z; triplet(int fisrt,int second,int third){ this.x=fisrt; this.y=second; this.z=third; } } class pair{ int x=0; int y=0; pair(int first,int second){ this.x=first; this.y=second; } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 11
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
654dca7fda8a1df2ee3a2650ffa83d7f
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
//Some of the methods are copied from GeeksforGeeks Website import java.util.*; import java.lang.*; import java.io.*; public class Main { static FastReader sc=new FastReader(System.in); // abbc // abc static void fill_Min(char sn[],char sm[],int n,int m,int a[]) { int ind=0,i=0; for(i=0;i<n;i++) { if(ind<m && sn[i]==sm[ind]) { a[ind++]=i; } } } static void fill_Max(char sn[],char sm[],int n,int m,int b[]) { int ind=m-1,i=n-1; for(i=n-1;i>=0;i--) { if(ind>=0 && sn[i]==sm[ind]) { b[ind--]=i; } } } public static void main (String[] args) throws java.lang.Exception { try{ int t = 1;// sc.nextInt(); while(t-->0) { int n=sc.nextInt(); int m=sc.nextInt(); String snn=sc.next(); String smm=sc.next(); char sn[]=snn.toCharArray(); char sm[]=smm.toCharArray(); int a[]=new int[m]; int b[]=new int[m]; fill_Min(sn,sm,n,m,a); fill_Max(sn,sm,n,m,b); //print(a); //print(b); int max=0; for(int i=1;i<m;i++) { int dif=Math.abs(b[i]-a[i-1]); max=Math.max(dif,max); } out.println(max); } out.flush(); out.close(); } catch(Exception e) {} } /* ...SOLUTION ENDS HERE...........SOLUTION ENDS HERE... */ static void flag(boolean flag) { out.println(flag ? "YES" : "NO"); out.flush(); } /* Map<Long,Long> map=new HashMap<>(); for(int i=0;i<n;i++) { if(!map.containsKey(a[i])) map.put(a[i],1); else map.replace(a[i],map.get(a[i])+1); } Set<Map.Entry<Long,Long>> hmap=map.entrySet(); for(Map.Entry<Long,Long> data : hmap) { } Iterator<Integer> it = set.iterator(); while(it.hasNext()) { int x=it.next(); } */ static void print(int a[]) { int n=a.length; for(int i=0;i<n;i++) { out.print(a[i]+" "); } out.println(); out.flush(); } static void print(long a[]) { int n=a.length; for(int i=0;i<n;i++) { out.print(a[i]+" "); } out.println(); out.flush(); } static void print_int(ArrayList<Integer> al) { int si=al.size(); for(int i=0;i<si;i++) { out.print(al.get(i)+" "); } out.println(); out.flush(); } static void print_long(ArrayList<Long> al) { int si=al.size(); for(int i=0;i<si;i++) { out.print(al.get(i)+" "); } out.println(); out.flush(); } static class Graph { int v; ArrayList<Integer> list[]; Graph(int v) { this.v=v; list=new ArrayList[v+1]; for(int i=1;i<=v;i++) list[i]=new ArrayList<Integer>(); } void addEdge(int a, int b) { this.list[a].add(b); } } static void DFS(Graph g, boolean[] visited, int u) { visited[u]=true; int v=0; for(int i=0;i<g.list[u].size();i++) { v=g.list[u].get(i); if(!visited[v]) DFS(g,visited,v); } } // static class Pair // { // int x,y; // Pair(int x,int y) // { // this.x=x; // this.y=y; // } // } static long sum_array(int a[]) { int n=a.length; long sum=0; for(int i=0;i<n;i++) sum+=a[i]; return sum; } static long sum_array(long a[]) { int n=a.length; long sum=0; for(int i=0;i<n;i++) sum+=a[i]; return sum; } 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 sort(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 void reverse_array(int a[]) { int n=a.length; int i,t; for (i = 0; i < n / 2; i++) { t = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = t; } } static void reverse_array(long a[]) { int n=a.length; int i; long t; for (i = 0; i < n / 2; i++) { t = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = t; } } static long gcd(long a, long b) { if (a == 0) return b; return gcd(b%a, a); } static int gcd(int a, int b) { if (a == 0) return b; return gcd(b%a, a); } static class FastReader{ byte[] buf = new byte[2048]; int index, total; InputStream in; FastReader(InputStream is) { in = is; } int scan() throws IOException { if (index >= total) { index = 0; total = in.read(buf); if (total <= 0) { return -1; } } return buf[index++]; } String next() throws IOException { int c; for (c = scan(); c <= 32; c = scan()); StringBuilder sb = new StringBuilder(); for (; c > 32; c = scan()) { sb.append((char) c); } return sb.toString(); } int nextInt() throws IOException { int c, val = 0; for (c = scan(); c <= 32; c = scan()); boolean neg = c == '-'; if (c == '-' || c == '+') { c = scan(); } for (; c >= '0' && c <= '9'; c = scan()) { val = (val << 3) + (val << 1) + (c & 15); } return neg ? -val : val; } long nextLong() throws IOException { int c; long val = 0; for (c = scan(); c <= 32; c = scan()); boolean neg = c == '-'; if (c == '-' || c == '+') { c = scan(); } for (; c >= '0' && c <= '9'; c = scan()) { val = (val << 3) + (val << 1) + (c & 15); } return neg ? -val : val; } } 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') break; 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(); } } static PrintWriter out=new PrintWriter(System.out); static int int_max=Integer.MAX_VALUE; static int int_min=Integer.MIN_VALUE; static long long_max=Long.MAX_VALUE; static long long_min=Long.MIN_VALUE; } // Thank You !
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 11
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
e8dcc680a73d3e9e1cf69ee2f3392f6d
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.util.*; import java.io.*; public class C { InputStream is; PrintWriter out; String INPUT = ""; void solve() throws IOException { int n= ni(), m= ni(); char[] s= ns(n), t= ns(m); HashMap<Character, List<Integer>> map = new HashMap<>(); for(int i=0;i<s.length;i++) { if(map.containsKey(s[i])) map.get(s[i]).add(i+1); else { ArrayList<Integer> list= new ArrayList<>(); list.add(i+1); map.put(s[i], list); } } int[] all= new int[26]; for(char c: t) all[c-'a']++; int[] max= new int[m]; for(int i=m-1;i>=0;i--) { char next= t[i]; max[i]= binarySearch(map.get(next), 0, map.get(next).size()-1, i==m-1? n: max[i+1]-1); } int[] min= new int[m]; for(int i=0;i<m;i++) { char next= t[i]; min[i]= binarySearch2(map.get(next), 0, map.get(next).size()-1, i==0? 1: min[i-1]+1); } int ans= 0; for(int i=1;i<m;i++) ans= Math.max(ans, max[i]- min[i-1]); out.println(ans); } private int binarySearch(List<Integer> arr, int start, int last, int find) { int ret= -1; while(start<= last) { int mid= start+ (last- start)/2; if(arr.get(mid)== find) return arr.get(mid); else if(arr.get(mid)< find) { ret= arr.get(mid); start= mid+1; } else last= mid-1; } return ret; } private int binarySearch2(List<Integer> arr, int start, int last, int find) { int ret= -1; while(start<= last) { int mid= start+ (last- start)/2; if(arr.get(mid)== find) return arr.get(mid); else if(arr.get(mid)> find) { ret= arr.get(mid); last= mid-1; } else start= mid+1; } return ret; } void run() throws Exception { is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes()); out = new PrintWriter(System.out); solve(); out.flush(); } public static void main(String[] args) throws Exception { new C().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 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 int[] na(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = ni(); return a; } private int ni() { int num = 0, 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(); } } 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(); } } private void tr(Object... o) { if (INPUT.length() > 0) System.out.println(Arrays.deepToString(o)); } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 11
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
3d689aa63222a176135ec075dcf5dc69
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.io.*; import java.math.*; import java.util.*; // @author : Dinosparton public class test { static class Pair{ long x; long y; Pair(long x,long y){ this.x = x; this.y = y; } } static class Compare { void compare(Pair arr[], int n) { // Comparator to sort the pair according to second element Arrays.sort(arr, new Comparator<Pair>() { @Override public int compare(Pair p1, Pair p2) { if(p1.x!=p2.x) { return (int)(p1.x - p2.x); } else { return (int)(p1.y - p2.y); } } }); // for (int i = 0; i < n; i++) { // System.out.print(arr[i].x + " " + arr[i].y + " "); // } // System.out.println(); } } static class Scanner { BufferedReader br; StringTokenizer st; public Scanner() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static void main(String args[]) throws Exception { Scanner sc = new Scanner(); StringBuffer res = new StringBuffer(); int tc = 1; while(tc-->0) { int n = sc.nextInt(); int m = sc.nextInt(); String s = sc.next(); String t = sc.next(); int start[] = new int[m]; int end[] = new int[m]; int i=0,j=0; int p = 0; while(i<n && j<m) { if(s.charAt(i)==t.charAt(j)) { start[p] = i; p++; i++; j++; } else { i++; } } i = n-1; j = m-1; p = m-1; while(i>=0 && j>=0) { if(s.charAt(i)==t.charAt(j)) { end[p] = i; p--; i--; j--; } else { i--; } } int ans = 0; // for(int k=0;k<m;k++) { // System.out.print(start[k]+" "); // } // System.out.println(); // for(int k=0;k<m;k++) { // System.out.print(end[k]+" "); // } // System.out.println(); for(int k=0;k<m-1;k++) { ans = Math.max(ans, Math.abs(start[k] - end[k+1])); } System.out.println(ans); } System.out.println(res); } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 11
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
2bd80a096a1b4f32972f743b4d8db2b5
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.PriorityQueue; import java.util.Stack; import java.util.StringTokenizer; import java.util.TreeSet; import java.io.IOException; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; public class S2 { public static class Reader { static BufferedReader reader; static StringTokenizer tokenizer; static void init(InputStream input) { reader = new BufferedReader(new InputStreamReader(input)); tokenizer = new StringTokenizer(""); } static String next() throws IOException { while(!tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); } return tokenizer.nextToken(); } static int nextInt() throws IOException { return Integer.parseInt(next()); } static long nextLong() throws IOException { return Long.parseLong(next()); } } private static class Node implements Comparable<Node> { public int ix; public int parents; public ArrayList<Node> children; public boolean visited; public Node(int i) { // TODO Auto-generated constructor stub ix=i+1; parents=0; children = new ArrayList<Node>(); visited = false; } public void addC(Node ch) { children.add(ch); ch.parents++; } public void dfs() { visited=true; int l = children.size(); for(int i=0; i<l; i++) { Node child = children.get(i); if(!child.visited) { child.dfs(); } } } @Override public int compareTo(Node o) { // TODO Auto-generated method stub return ix-o.ix; } } public static Node[] tree; private static class Stac { public int s; public int l; public int[] arr; public Stac(ArrayList<Integer> li) { // TODO Auto-generated constructor stub s=0; l=li.size(); arr = new int[l]; for(int i=0; i<l; i++) { arr[i] = li.get(i); } } public int first() { return arr[s]; } public void remove() { s++; } public int peek() { return arr[l-1]; } public void pop() { l--; } } private static int n; private static long nl; private static long ans; private final static long fd = 998244353; private final static long mod1 = 1000000007; public static void main(String[] args) throws IOException { Reader.init(System.in); PrintWriter out = new PrintWriter(System.out); int tc=1; // tc = Reader.nextInt(); main_loop: for(int tn=0; tn<tc; tn++) { n = Reader.nextInt(); int m = Reader.nextInt(); char[] a = Reader.next().toCharArray(); char[] b = Reader.next().toCharArray(); ArrayList<Integer>[] ch = new ArrayList[26]; for(int i=0; i<26; i++) { ch[i] = new ArrayList<Integer>(); } for(int i=0; i<n; i++) { int cur = a[i]-'a'; ch[cur].add(i); } Stac[] ch1 = new Stac[26]; for(int i=0; i<26; i++) { ch1[i] = new Stac(ch[i]); } ans=0; int[] upos = new int[m]; int c = b[m-1]-'a'; upos[m-1] = ch1[c].peek(); for(int i=m-2; i>=0; i--) { c = b[i]-'a'; while(ch1[c].peek()>=upos[i+1]) { ch1[c].pop(); } upos[i] = ch1[c].peek(); } Stac[] ch2 = new Stac[26]; for(int i=0; i<26; i++) { ch2[i] = new Stac(ch[i]); } ans=0; int[] lpos = new int[m]; c = b[0]-'a'; lpos[0] = ch2[c].first(); for(int i=1; i<m; i++) { c = b[i]-'a'; while(ch2[c].first()<=lpos[i-1]) { ch2[c].remove(); } lpos[i] = ch2[c].first(); } for(int i=0; i<m-1; i++) { int x = upos[i+1]-lpos[i]; if(x>ans) { ans=x; } } out.println(ans); // out.write(("YES").getBytes()); // out.write(("NO").getBytes()); // out.write((ans+" ").getBytes()); // out.print(ans+"\n"); } out.flush(); out.close(); } private static void initTree(int n) throws IOException { tree = new Node[n]; for(int i=0; i<n; i++) { tree[i]=new Node(i); } for(int i=0; i<n-1; i++) { int u = Reader.nextInt()-1; int v = Reader.nextInt()-1; tree[u].addC(tree[v]); tree[v].addC(tree[u]); } } private static long solve(TreeSet<Node> marked, TreeSet<Node> unMarked) { if(marked.size()==n) { return 0; } else { long ret=0; int l = unMarked.size(); int x=0; for(Node cur: unMarked) { TreeSet<Node> tmarked = new TreeSet<S2.Node>(marked); TreeSet<Node> tunMarked = new TreeSet<S2.Node>(unMarked); tmarked.add(cur); tunMarked.remove(cur); cur.visited=true; int l1 = cur.children.size(); for(int i=0; i<l1; i++) { Node childNode = cur.children.get(i); if(childNode.visited) { } else { tunMarked.add(childNode); } } ret+=x; ret%=mod1; ret+=solve(tmarked, tunMarked); ret%=mod1; x++; } return modDiv(ret, l); } } private static long modPow(long a, int p){ long m = mod1; if(p==0){ return 1; } if(a==1){ return 1; } long x = modPow(a, p/2); long y = (x*x); y%=m; if(p%2==1){ y = (y*a); } return (y%m); } private static long modDiv(long p, long q) { long mp = modPow(q, (int)mod1-2); mp%=mod1; p%=mod1; mp*=p; mp%=mod1; return mp; } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 11
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
ac7051fbd568e47ff8426a2e79102628
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.*; import java.text.*; public class Codeforces { static int mod=998244353 ; public static void main(String[] args) throws Exception { PrintWriter out=new PrintWriter(System.out); FastScanner fs=new FastScanner(); int n=fs.nextInt(); int m=fs.nextInt(); char s[]=fs.next().toCharArray(); char t[]=fs.next().toCharArray(); // int n=s.length, m=t.length; int left[]=new int[m]; int right[]=new int[m]; int j=0; int i=0; while(j<m) { while(s[i]!=t[j]) i++; left[j]=i; i++; j++; } j=m-1; i=n-1; while(j>=0) { while(s[i]!=t[j]) i--; right[j]=i; i--; j--; } int ans=0; for(j=0;j<m-1;j++) { ans=Math.max(ans, right[j+1]-left[j]); } out.println(ans); out.close(); } static void add(int arr[]) { int carry=1; int ind=0; while(carry!=0) { if(arr[ind]==1) { arr[ind]=0; ind++; } else { arr[ind]=1; carry=0; } } } static int gcd(int a,int b) { if(b==0) return a; return gcd(b,a%b); } static void sort(int[] a) { //suffle int n=a.length; Random r=new Random(); for (int i=0; i<a.length; i++) { int oi=r.nextInt(n); int temp=a[i]; a[i]=a[oi]; a[oi]=temp; } //then sort Arrays.sort(a); } // Use this to input code since it is faster than a Scanner static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } 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
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 11
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
ca1f725ba58b5ef8e985245d64829ac9
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.io.*; import java.util.Arrays; public class max_width { static StreamTokenizer in; static int nextInt() throws Exception { in.nextToken(); return (int) in.nval; } static String next() throws Exception { in.nextToken(); return (String) in.sval; } public static void main(String[] args) throws Exception { //in = new StreamTokenizer(new BufferedReader(new FileReader("test.in"))); in=new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))); int m=nextInt(), n=nextInt(); String s=next(), t=next(); int []prefix=new int[n], suffix=new int[n]; for(int i=0,j=0;i<n&&j<m;i++,j++) { while(j<m&&s.charAt(j)!=t.charAt(i)) { j++; } prefix[i]=j; } for(int i=n-1,j=m-1;i>=0&&j>=0;i--,j--) { while(j>=0&&s.charAt(j)!=t.charAt(i)) { j--; } suffix[i]=j; } int max=-1; for(int i=0;i<n-1;i++) { int length=n-(i+1); if(suffix[n-length]==Integer.MAX_VALUE || prefix[i]==Integer.MAX_VALUE) continue; // System.out.println(prefix[i]+ " "+suffix[n-length]); max=Math.max(max, Math.abs(prefix[i]-suffix[n-length])); } System.out.println(max); // System.out.println(Arrays.toString(prefix)); // System.out.println(Arrays.toString(suffix)); } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 11
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
6314fa8e069c8c40857d1a0158c4a0e3
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.util.*; import java.io.*; public class Maximum_width { public static void process()throws IOException { int n=I(); int m=I(); String s=S(); String t=S(); int max[]=new int[m]; int min[]=new int[m]; char ch[]=s.toCharArray(); char ch1[]=t.toCharArray(); int pos=0; for(int i=0;i<m;i++) { while(ch[pos]!=ch1[i]) { pos++; } min[i]=pos; pos++; } pos=n-1; for(int i=m-1;i>=0;i--) { while(ch[pos]!=ch1[i]) { pos--; } max[i]=pos; pos--; } // pn(Arrays.toString(max)); // pn(Arrays.toString(min)); int maxi=0; for(int i=1;i<m;i++) { maxi=Math.max(maxi, max[i]-min[i-1]); } pn(maxi); } static Scanner sc = new Scanner(System.in); static PrintWriter out = new PrintWriter(System.out); static void pn(Object o){out.println(o);out.flush();} static void p(Object o){out.print(o);out.flush();} static void pni(Object o){out.println(o);System.out.flush();} static int I() throws IOException{return sc.nextInt();} static long L() throws IOException{return sc.nextLong();} static double D() throws IOException{return sc.nextDouble();} static String S() throws IOException{return sc.next();} static char C() throws IOException{return sc.next().charAt(0);} static int[] Ai(int n) throws IOException{int[] arr = new int[n];for (int i = 0; i < n; i++)arr[i] = I();return arr;} static String[] As(int n) throws IOException{String s[] = new String[n];for (int i = 0; i < n; i++)s[i] = S();return s;} static long[] Al(int n) throws IOException {long[] arr = new long[n];for (int i = 0; i < n; i++)arr[i] = L();return arr;} static void dyn(int dp[][],int n,int m,int z)throws IOException {for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ dp[i][j]=z;}} } // *--------------------------------------------------------------------------------------------------------------------------------*// static class AnotherReader {BufferedReader br;StringTokenizer st;AnotherReader() throws FileNotFoundException {br = new BufferedReader(new InputStreamReader(System.in));} AnotherReader(int a) throws FileNotFoundException {br = new BufferedReader(new FileReader("input.txt"));} String next() throws IOException{while (st == null || !st.hasMoreElements()) {try {st = new StringTokenizer(br.readLine());} catch (IOException e) {e.printStackTrace();}}return st.nextToken();} int nextInt() throws IOException {return Integer.parseInt(next());} long nextLong() throws IOException {return Long.parseLong(next());} double nextDouble() throws IOException {return Double.parseDouble(next());} String nextLine() throws IOException {String str = "";try {str = br.readLine();} catch (IOException e) {e.printStackTrace();}return str;}} public static void main(String[] args)throws IOException{try{boolean oj=true;if(oj==true) {AnotherReader sk=new AnotherReader();PrintWriter out=new PrintWriter(System.out);} else {AnotherReader sk=new AnotherReader(100);out=new PrintWriter("output.txt");} //long T=L();while(T-->0) {process();}out.flush();out.close();}catch(Exception e){return;}}} //*-----------------------------------------------------------------------------------------------------------------------------------*//
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 11
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
83f9cf745bbc5969893628adf5de06f7
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.StringTokenizer; public class Main { 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()); } } public static void main(String[] args) { Reader rd = new Reader(); int n = rd.nextInt(); int m = rd.nextInt(); String s = rd.next(); String t = rd.next(); int[] min = new int[m]; int[] max = new int[m]; Arrays.fill(min, Integer.MAX_VALUE); Arrays.fill(max, Integer.MIN_VALUE); int w = 0; int j = 0; for (int i = 0; i < m; i++) { while (s.charAt(j) != t.charAt(i)) j++; min[i] = j; j++; } j = n - 1; for (int i = m - 1; i > -1; i--) { while (s.charAt(j) != t.charAt(i)) j--; max[i] = j; j--; } for (int i = 0; i < m - 1; i++) { w = Integer.max(w, max[i + 1] - min[i]); } System.out.println(w); } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 11
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
a6707148d24bd5cbb1536656c1446efc
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.util.Scanner; import java.util.TreeSet; public class C { public static void main(String[] args) { Scanner scn = new Scanner(System.in); int lenA = scn.nextInt(); int lenB = scn.nextInt(); char[] a = scn.next().toCharArray(); char[] b = scn.next().toCharArray(); TreeSet<Integer>[] indexSets = new TreeSet[26]; for (int i = 0; i < 26; i++) indexSets[i] = new TreeSet<>(); Pair[] feasibleIndices = new Pair[lenB]; for (int i = 0; i < lenA; i++) { indexSets[a[i] - 'a'].add(i); } int firstChar = b[0] - 'a'; feasibleIndices[0] = new Pair(-1, indexSets[firstChar].first()); for (int i = 1; i < lenB; i++) { int chr = b[i] - 'a'; Pair preP = feasibleIndices[i - 1]; Pair p = new Pair(-1, indexSets[chr].higher(preP.min)); feasibleIndices[i] = p; } int lastChar = b[lenB - 1] - 'a'; feasibleIndices[lenB - 1].max = indexSets[lastChar].last(); for (int i = lenB - 2; i >= 0; i--) { int chr = b[i] - 'a'; Pair sufP = feasibleIndices[i + 1]; feasibleIndices[i].max = indexSets[chr].lower(sufP.max); } int max = 0; for (int i = 1; i < lenB; i++) { Pair p = feasibleIndices[i]; Pair preP = feasibleIndices[i - 1]; int cur = p.max - preP.min; max = Integer.max(max, cur); } System.out.println(max); } } class Pair { int max, min; public Pair(int max, int min) { this.max = max; this.min = min; } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 11
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
67aa1674e10217d5752303ad76b8b03e
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.io.*; import java.util.*; import java.lang.*; public class third { public static void main(String[] args) { try { System.setIn(new FileInputStream("input.txt")); System.setOut(new PrintStream(new FileOutputStream("output.txt"))); } catch (Exception e) { // System.err.println("Error"); // System.out.println("Error"); } Scanner sc = new Scanner(System.in); int t = 1; while(t-->0){ int n = sc.nextInt(); int m = sc.nextInt(); char[] s1 = sc.next().toCharArray(); char [] s2 = sc.next().toCharArray(); int [] l = new int[m]; int [] r = new int[m]; int pos = 0; for(int i=0;i<m;i++){ while(pos<n && s1[pos]!= s2[i] ) pos++; l[i] = pos; pos++; } pos = n-1; for(int i=m-1;i>=0;i--){ while(pos>=0 && s1[pos]!=s2[i]) pos--; r[i] = pos; pos--; } int max = 0; for(int i=0;i<m-1;i++){ max = Math.max(max, r[i+1]-l[i]); } System.out.println(max); } sc.close(); } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 11
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
d13f576b83d72d577b2dc39d8a7ef071
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.io.*; import java.util.ArrayList; import java.util.StringTokenizer; public class C_MaximumWidth { private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); private static final PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); private static StringTokenizer st; private static int readInt() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return Integer.parseInt(st.nextToken()); } public static void main(String[] args) throws IOException { int n = readInt(); int m = readInt(); char[] s = br.readLine().toCharArray(); char[] t = br.readLine().toCharArray(); int answer = maxWidth(n, m, s, t); pw.println(answer); pw.close(); } private static int maxWidth(int ns, int nt, char[] s, char[] t) { ArrayList<Integer>[] indices = new ArrayList[26]; for (int i = 0; i < 26; i++) indices[i] = new ArrayList<>(); for (int i = 0; i < ns; i++) indices[s[i] - 'a'].add(i); int[] pMin = new int[nt]; pMin[0] = -1; for (int i = 1; i < nt; i++) { ArrayList<Integer> arr = indices[t[i - 1] - 'a']; pMin[i] = arr.get(firstGreater(arr, pMin[i - 1])); } int[] sMax = new int[nt]; sMax[nt - 1] = ns; for (int i = nt - 2; i >= 0; i--) { ArrayList<Integer> arr = indices[t[i + 1] - 'a']; sMax[i] = arr.get(lastSmaller(arr, sMax[i + 1])); } int maxW = 0; for (int i = 1; i < nt; i++) { maxW = Math.max(maxW, sMax[i - 1] - pMin[i]); } return maxW; } static int firstGreater(ArrayList<Integer> arr, int key) { int low = -1, high = arr.size(); while (low + 1 < high) { int mid = (low + high) / 2; if (arr.get(mid) > key) high = mid; else low = mid; } return high; } static int lastSmaller(ArrayList<Integer> arr, int key) { int low = -1, high = arr.size(); while (low + 1 < high) { int mid = (low + high) / 2; if (arr.get(mid) < key) low = mid; else high = mid; } return low; } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 11
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
f7bec99ab9f5cac558bba40afc2a2c91
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.io.*; import java.util.ArrayList; import java.util.StringTokenizer; public class C_MaximumWidth { private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); private static final PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); private static StringTokenizer st; private static int readInt() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return Integer.parseInt(st.nextToken()); } public static void main(String[] args) throws IOException { int n = readInt(); int m = readInt(); char[] s = br.readLine().toCharArray(); char[] u = br.readLine().toCharArray(); int answer = maxWidth(n, m, s, u); pw.println(answer); pw.close(); } private static int maxWidth(int ns, int nu, char[] s, char[] u) { ArrayList<Integer>[] indices = new ArrayList[26]; for (int i = 0; i < 26; i++) { indices[i] = new ArrayList<>(); } for (int i = 0; i < ns; i++) { indices[s[i] - 'a'].add(i); } int[] pMin = new int[nu + 1]; pMin[0] = -1; for (int i = 1; i <= nu; i++) { ArrayList<Integer> arr = indices[u[i - 1] - 'a']; pMin[i] = arr.get(firstGreater(arr, 0, arr.size() - 1, pMin[i - 1])); } int[] sMax = new int[nu + 1]; sMax[nu] = ns; for (int i = nu - 1; i >= 0; i--) { ArrayList<Integer> arr = indices[u[i] - 'a']; sMax[i] = arr.get(lastSmaller(arr, 0, arr.size() - 1, sMax[i + 1])); } int maxW = 0; for (int i = 1; i < nu; i++) { maxW = Math.max(maxW, sMax[i] - pMin[i]); } return maxW; } static int firstGreater(ArrayList<Integer> arr, int low, int high, int key) { low--; high++; while (low + 1 < high) { int mid = (low + high) / 2; if (arr.get(mid) > key) high = mid; else low = mid; } return high; } static int lastSmaller(ArrayList<Integer> arr, int low, int high, int key) { low--; high++; while (low + 1 < high) { int mid = (low + high) / 2; if (arr.get(mid) < key) low = mid; else high = mid; } return low; } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 11
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
6b869427408039eaa2960ffcef4ed0a7
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.io.*; import java.text.DecimalFormat; import java.util.ArrayList; import java.util.Arrays; import java.util.Random; import java.util.StringTokenizer; public class Main { static long mod = (long) 1e9 + 7; static long mod1 = 998244353; public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); int n = in.nextInt(); int m = in.nextInt(); char[] a = in.next().toCharArray(); char[] b = in.next().toCharArray(); int[][] dd=new int[m][2]; int i=0; int j=0; int max=0; while(i<n && j<m){ if(a[i]==b[j]){ dd[j][0]=i; i++; j++; } else i++; } i=n-1; j=m-1; while(i>=0 && j>=0){ if(a[i]==b[j]){ dd[j][1]=i; i--; j--; } else i--; } for( i=1;i<m;i++) { // out.println(dd[i-1][0]+" "+dd[i-1][1]); max = Math.max(max, dd[i][1] - dd[i - 1][0]); } out.println(max); out.close(); } static final Random random = new Random(); static void ruffleSort(int[] a) { int n = a.length;//shuffle, then sort for (int i = 0; i < n; i++) { int oi = random.nextInt(n), temp = a[oi]; a[oi] = a[i]; a[i] = temp; } Arrays.sort(a); } static long gcd(long x, long y) { if (x == 0) return y; if (y == 0) return x; long r = 0, a, b; a = Math.max(x, y); b = Math.min(x, y); r = b; while (a % b != 0) { r = a % b; a = b; b = r; } return r; } static long modulo(long a, long b, long c) { long x = 1, y = a % c; while (b > 0) { if (b % 2 == 1) x = (x * y) % c; y = (y * y) % c; b = b >> 1; } return x % c; } public static void debug(Object... o) { System.err.println(Arrays.deepToString(o)); } static String printPrecision(double d) { DecimalFormat ft = new DecimalFormat("0.00000000000"); return String.valueOf(ft.format(d)); } static int countBit(long mask) { int ans = 0; while (mask != 0) { mask &= (mask - 1); ans++; } return ans; } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 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 int[] readArray(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = nextInt(); return arr; } } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 11
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
f2033bdf6b0c3cbc91d89ff3b46e3bcc
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.io.*; import java.util.StringTokenizer; public class codeforces704_C { private static void solve(FastIOAdapter ioAdapter) { int n = ioAdapter.nextInt(); int m = ioAdapter.nextInt(); char[] s = ioAdapter.next().toCharArray(); char[] t = ioAdapter.next().toCharArray(); int[] indicesFirst = new int[m]; int[] indicesLast = new int[m]; int leftS = 0; int rightS = n - 1; int leftT = 0; int rightT = m - 1; for (int i = 0; i < m; i++) { while (s[leftS] != t[leftT]) { leftS++; } indicesFirst[leftT] = leftS; leftS++; leftT++; while (s[rightS] != t[rightT]) { rightS--; } indicesLast[rightT] = rightS; rightS--; rightT--; } int max = 0; for (int i = 0; i < m - 1; i++) { max = Math.max(max, indicesLast[i + 1] - indicesFirst[i]); } ioAdapter.out.println(max); } public static void main(String[] args) throws Exception { try (FastIOAdapter ioAdapter = new FastIOAdapter()) { int count = 1; // count = ioAdapter.nextInt(); while (count-- > 0) { solve(ioAdapter); } } } static class FastIOAdapter implements AutoCloseable { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); public PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter((System.out)))); 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()); } @Override public void close() throws Exception { out.flush(); out.close(); br.close(); } } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 11
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
5b0a1184ae84bb4c871a38b7b1e4ef24
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class MaximumWidth1 { public static void main (String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = 1; while (t-- > 0) { String[] st = br.readLine().split(" "); int n = Integer.parseInt(st[0]); int m = Integer.parseInt(st[1]); String a = br.readLine(); String b = br.readLine(); int[] left = new int[m]; int[] right = new int[m]; int i = 0; for(int j = 0; j < n; j++) { if(i >= m) break; if(a.charAt(j) == b.charAt(i)) { left[i] = j; i++; } } i = m-1; for(int j = n-1; j >= 0; j--) { if(i < 0) break; if(a.charAt(j) == b.charAt(i)) { right[i] = j; i--; } } int ans = 1; for(int j = 0; j < m-1; j++) { ans = Math.max(ans, right[j+1] - left[j]); } System.out.println(ans); } } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 11
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
43be5cbc8ccf0725b60e9affbb868df3
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class MaximumWidth { static int MAXN = 1123456; static boolean[] prime = new boolean[MAXN]; static boolean[] used = new boolean[MAXN]; public static void seive() { for (int i = 2; i < MAXN; ++i) if (!used[i]){ prime[i] = true; for (int j = i; j < MAXN; j += i) { used[j] = true; } } prime[1] = false; } // pair // class Pair implements Comparable<Pair>{ // int ind,val; // Pair(int i,int v){ ind=i;val=v;} // @Override // public int compareTo(Pair o) {return o.val-val ;} // } public static void main (String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int test = 1; while (test-- > 0) { String st[] = br.readLine().split(" "); int n = Integer.parseInt(st[0]); int m = Integer.parseInt(st[1]); String s = br.readLine(); String t = br.readLine(); int[] left = new int[m]; int[] right = new int[m]; int i = 0; for(int j = 0; j < n; j++) { if(i >= m) break; if(t.charAt(i) == s.charAt(j)) { left[i] = j; i++; } } i = m-1; for(int j = n-1; j >= 0; j--) { if(i < 0) break; if(t.charAt(i) == s.charAt(j)) { right[i] = j; i--; } } int max = 1; for(i = 1; i < m; i++){ max = Math.max(max, right[i] - left[i-1]); } System.out.println(max); } } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 11
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
b6a943540f0a7ad7351253c383d04206
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import static java.lang.Math.*; import java.util.*; import java.io.*; public class Main { static long MOD = (long) 1e9 + 7; public static void main(String[] args) { InputReader fs = new InputReader(System.in); PrintWriter pw = new PrintWriter(System.out); int n = fs.readInt(), m = fs.readInt(); String s = fs.readString(), t = fs.readString(); int diff = 1; int last = -1; for (int i = 0; i < n; i++) { if (s.charAt(i) == t.charAt(0)) { last = i; break; } } int[] right = new int[m]; int track = n-1; for (int i = m-1; i >= 0; i--) { while (track >= 0 && s.charAt(track) != t.charAt(i)) track--; right[i] = track--; } for (int i = 1; i < m; i++) { int idx = right[i]; diff = max(diff, idx - last); for (int j = last + 1; j < n; j++) { if (s.charAt(j) == t.charAt(i)) { last = j; break; } } } pw.println(diff); pw.flush(); pw.close(); } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int readInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public double readDouble() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') return res * Math.pow(10, readInt()); if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') return res * Math.pow(10, readInt()); if (c < '0' || c > '9') throw new InputMismatchException(); m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public long readLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public int[] readIntArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = readInt(); } return a; } public int[] readIntArrayShifted(int n) { int[] a = new int[n + 1]; for (int i = 0; i < n; i++) { a[i + 1] = readInt(); } return a; } public long[] readLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) { a[i] = readLong(); } return a; } public String next() { return readString(); } interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 11
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
aadb84cc1fe77f1cad69dad63fb9c9f8
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import static java.lang.Math.*; import java.util.*; import java.io.*; public class Main { static long MOD = (long) 1e9 + 7; public static void main(String[] args) { InputReader fs = new InputReader(System.in); PrintWriter pw = new PrintWriter(System.out); int n = fs.readInt(), m = fs.readInt(); String s = fs.readString(), t = fs.readString(); int[][] m1 = new int[n][26]; int[][] m2 = new int[m][26]; for (int i = n-1; i >=0; i--) { if (i + 1 < n) for (int j = 0; j < 26; j++) m1[i][j] = m1[i+1][j]; m1[i][s.charAt(i) - 'a']++; } for (int i = m-1; i >=0; i--) { if (i + 1 < m) for (int j = 0; j < 26; j++) m2[i][j] = m2[i+1][j]; m2[i][t.charAt(i) - 'a']++; } int diff = 1; int last = -1; for (int i = 0; i < n; i++) { if (s.charAt(i) == t.charAt(0)) { last = i; break; } } int[] right = new int[m]; int track = n-1; for (int i = m-1; i >= 0; i--) { while (track >= 0 && s.charAt(track) != t.charAt(i)) track--; right[i] = track--; } for (int i = 1; i < m; i++) { int idx = right[i]; // System.out.println("last: " + last); // System.out.println("idx: " + idx); diff = max(diff, idx - last); for (int j = last + 1; j < n; j++) { if (s.charAt(j) == t.charAt(i)) { last = j; break; } } } pw.println(diff); pw.flush(); pw.close(); } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int readInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public double readDouble() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') return res * Math.pow(10, readInt()); if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') return res * Math.pow(10, readInt()); if (c < '0' || c > '9') throw new InputMismatchException(); m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public long readLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public int[] readIntArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = readInt(); } return a; } public int[] readIntArrayShifted(int n) { int[] a = new int[n + 1]; for (int i = 0; i < n; i++) { a[i + 1] = readInt(); } return a; } public long[] readLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) { a[i] = readLong(); } return a; } public String next() { return readString(); } interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 11
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
9630e452c7743b58de88ed96682190da
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.util.*; import java.io.*; public class Practice { static boolean multipleTC = false; final static int mod = 1000000007; final static int mod2 = 998244353; final double E = 2.7182818284590452354; final double PI = 3.14159265358979323846; int MAX = 100005; void pre() throws Exception { } int mask[]; // All the best void solve(int TC) throws Exception { int n = ni(), m = ni(); char s[] = nln().toCharArray(); char t[] = nln().toCharArray(); TreeMap<Integer, Integer> first = new TreeMap<>(); TreeMap<Integer, Integer> last = new TreeMap<>(); int idx = 0; for (int i = 0; i < n; i++) { if (idx < m && s[i] == t[idx]) { first.put(idx, i); idx++; } } idx--; for (int i = n - 1; i >= 0; i--) { if (idx >= 0 && s[i] == t[idx]) { last.put(idx, i); idx--; } } int ans = 1; for (int i = 0; i + 1 < m; i++) { ans = max(ans, last.get(i + 1) - first.get(i)); } pn(ans); } int[] readArr(int n) throws Exception { int arr[] = new int[n]; for (int i = 0; i < n; i++) { arr[i] = ni(); } return arr; } void sort(int arr[], int left, int right) { ArrayList<Integer> list = new ArrayList<>(); for (int i = left; i <= right; i++) list.add(arr[i]); Collections.sort(list); for (int i = left; i <= right; i++) arr[i] = list.get(i - left); } void sort(int arr[]) { ArrayList<Integer> list = new ArrayList<>(); for (int i = 0; i < arr.length; i++) list.add(arr[i]); Collections.sort(list); for (int i = 0; i < arr.length; i++) arr[i] = list.get(i); } public long max(long... arr) { long max = arr[0]; for (long itr : arr) max = Math.max(max, itr); return max; } public int max(int... arr) { int max = arr[0]; for (int itr : arr) max = Math.max(max, itr); return max; } public long min(long... arr) { long min = arr[0]; for (long itr : arr) min = Math.min(min, itr); return min; } public int min(int... arr) { int min = arr[0]; for (int itr : arr) min = Math.min(min, itr); return min; } public long sum(long... arr) { long sum = 0; for (long itr : arr) sum += itr; return sum; } public long sum(int... arr) { long sum = 0; for (int itr : arr) sum += itr; return sum; } String bin(long n) { return Long.toBinaryString(n); } String bin(int n) { return Integer.toBinaryString(n); } static int bitCount(int x) { return x == 0 ? 0 : (1 + bitCount(x & (x - 1))); } static void dbg(Object... o) { System.err.println(Arrays.deepToString(o)); } int bit(long n) { return (n == 0) ? 0 : (1 + bit(n & (n - 1))); } int abs(int a) { return (a < 0) ? -a : a; } long abs(long a) { return (a < 0) ? -a : a; } void p(Object o) { out.print(o); } void pn(Object o) { out.println(o); } void pni(Object o) { out.println(o); out.flush(); } String n() throws Exception { return in.next(); } String nln() throws Exception { return in.nextLine(); } int ni() throws Exception { return Integer.parseInt(in.next()); } long nl() throws Exception { return Long.parseLong(in.next()); } double nd() throws Exception { return Double.parseDouble(in.next()); } public static void main(String[] args) throws Exception { new Practice().run(); } FastReader in; PrintWriter out; void run() throws Exception { in = new FastReader(); out = new PrintWriter(System.out); int T = (multipleTC) ? ni() : 1; pre(); for (int t = 1; t <= T; t++) solve(t); out.flush(); out.close(); } class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } public FastReader(String s) throws Exception { br = new BufferedReader(new FileReader(s)); } String next() throws Exception { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { throw new Exception(e.toString()); } } return st.nextToken(); } String nextLine() throws Exception { String str = ""; try { str = br.readLine(); } catch (IOException e) { throw new Exception(e.toString()); } return str; } } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 11
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
2df59417a9510b0b8a9071d43b1c6737
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class Maximum_width { static class RealScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } public static void main(String[] args) { RealScanner sc = new RealScanner(); int n, m; n = sc.nextInt(); m = sc.nextInt(); String s1 = sc.next(); String s2 = sc.next(); int[] first = new int[m]; { int p = 0; for (int i = 0; i < m; i++) { while (p < n && s1.charAt(p) != s2.charAt(i)) { p++; } first[i] = p; p++; } } int[] last = new int[m]; { int p = n - 1; for (int i = m - 1; i >= 0; i--) { while (p >= 0 && s1.charAt(p) != s2.charAt(i)) { p--; } last[i] = p; p--; } } //System.out.println(Arrays.toString(first) + " " + Arrays.toString(last)); int res = 0; for (int i = 0; i < m - 1; i++) { res = Math.max(res, last[i + 1] - first[i]); } System.out.println(res); } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 11
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output
PASSED
b84da348196fe93818edc8538e1592cf
train_109.jsonl
1614071100
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \ldots, p_m$$$, where $$$1 \leq p_1 &lt; p_2 &lt; \ldots &lt; p_m \leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\max\limits_{1 \le i &lt; m} \left(p_{i + 1} - p_i\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.
512 megabytes
/** * @Created_by : Lucent868 * @Date : 30-08-2021 * @Time : 21:18 */ import java.util.*; import java.io.*; import java.math.*; public class C { static InputReader in; static PrintWriter out; public static void main(String args[]) { new Thread(null, new Runnable() { public void run() { try { solve(); out.flush(); out.close(); } catch (Exception e) { e.printStackTrace(); } } }, "1", 1 << 26).start(); } public static void solve() { in = new InputReader(System.in); out = new PrintWriter(System.out); int n = in.nextInt(); int m = in.nextInt(); String s = in.readString(); String t = in.readString(); int pre[] = new int[m]; int suf[] = new int[m]; int x = 0; for (int i = 0; i < m; i++) { while (x<n && s.charAt(x)!=t.charAt(i))x++; pre[i] = x; x++; } x = n-1; for (int i = m-1; i >=0; i--) { while (x>-1 && s.charAt(x)!=t.charAt(i))x--; suf[i] = x; x--; } int ans = 0; for (int i = 0; i < m-1; i++) { ans =Math.max(ans,suf[i+1]-pre[i]); } out.println(ans); } static class InputReader { private final InputStream stream; private final byte[] buf = new byte[8192]; private int curChar, numChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public String nextLine() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndOfLine(c)); return res.toString(); } public String readString() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public long nextLong() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public int nextInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nextIntArray(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = nextInt(); } return arr; } public long[] nextLongArray(int n) { long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = nextLong(); } return arr; } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } public static int[] sort(int[] arr) { List<Integer> temp = new ArrayList(); for (int i : arr) temp.add(i); Collections.sort(temp); int start = 0; for (int i : temp) arr[start++] = i; return arr; } public static long[] sort(long[] arr) { List<Long> temp = new ArrayList(); for (long i : arr) temp.add(i); Collections.sort(temp); int start = 0; for (long i : temp) arr[start++] = i; return arr; } public static int bs(int[] a, int fromIndex, int toIndex, double key) { int low = fromIndex; int high = toIndex - 1; while (low <= high) { int mid = (low + high) >>> 1; long midVal = a[mid]; if (midVal < key) low = mid + 1; else if (midVal > key) high = mid - 1; else return mid; // key found } return -(low + 1); // key not found. } }
Java
["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"]
2 seconds
["3", "4", "1", "1"]
NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\{1, 2, 5\}$$$ and $$$\{1, 4, 5\}$$$.In the second example the beautiful sequence with the maximum width is $$$\{1, 5\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\{1, 2, 3, 4, 5\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\{1, 2\}$$$.
Java 11
standard input
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
17fff01f943ad467ceca5dce5b962169
The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq m \leq n \leq 2 \cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.
1,500
Output one integer — the maximum width of a beautiful sequence.
standard output