Cerinta completa
Oh!! Mankind is in trouble again. This time, it’s a deadly disease spreading at a rate never seen before. The need of the hour is to set up efficient virus detectors. You are the lead at Central Hospital and you need to find a fast and reliable way to detect the footprints of the virus DNA in that of the patient.
The DNA of the patient as well as of the virus consists of lowercase letters. Since the collected data is raw, there may be some errors. You will need to find all substrings in the patient DNA that either exactly match the virus DNA or have at most one mismatch, i.e., a difference in at most one location.
For example, “aa” and “aa” are matching, “ab” and “aa” are matching, while “abb” and “bab” are not.
Function Description
Complete the virusIndices function in the editor below. It should print a list of space-separated integers that represent the starting indices of matching substrings in increasing order, or No match!.
virusIndices has the following parameter(s):
- p: a string that represents patient DNA
- v: a string that represents virus DNA
Input Format
The first line contains an integer , the number of test cases.
.
Each of the next lines contains two space-separated strings (the patient DNA) and (the virus DNA).
Constraints
- All characters in and .
Output Format
For each test case, output a single line containing a space-delimited list of starting indices (-indexed) of substrings of which are matching with according to the condition mentioned above. The indices have to be in increasing order. If there is no matching substring, output No Match!.
Sample Input 0
3
abbab ba
hello world
banana nan
Sample Output 0
1 2
No Match!
0 2
Explanation 0
For the first case, the substrings of starting at indices and are “bb” and “ba” and they are matching with the string which is “ba“.
For the second case, there are no matching substrings so the output is No Match!.
For the third case, the substrings of starting at indices and are “ban” and “nan” and they are matching with the string which is “nan“.
Sample Input 1
3
cgatcg gc
atcgatcga cgg
aardvark ab
Sample Output 1
1 3
2 6
0 1 5
Explanation 1
For the first case, the substrings of starting at indices and are “ga” and “gc” and they are matching with the string which is “gc“.
For the second case, the substrings of starting at indices and are “cga” and “cga” and they are matching with the string which is “cgg“.
For the third case, the substrings of starting at indices , and are “aa“, “ar” and “ar” and they are matching with the string which is “ab“.
Limbajul de programare folosit: java8
Cod:
import java.io.*;
import java.util.*;
public class Solution {
private static class FastScanner {
private final InputStream in;
private final byte[] buffer = new byte[1 << 16];
private int ptr = 0, len = 0;
FastScanner(InputStream is) {
this.in = is;
}
private int read() throws IOException {
if (ptr >= len) {
len = in.read(buffer);
ptr = 0;
if (len <= 0) return -1;
}
return buffer[ptr++];
}
String next() throws IOException {
int c;
do {
c = read();
} while (c <= ' ' && c != -1);
if (c == -1) return null;
StringBuilder sb = new StringBuilder();
while (c > ' ') {
sb.append((char) c);
c = read();
}
return sb.toString();
}
int nextInt() throws IOException {
return Integer.parseInt(next());
}
}
private static int[] zFunction(String s) {
int n = s.length();
int[] z = new int[n];
int l = 0, r = 0;
for (int i = 1; i < n; i++) {
if (i <= r) z[i] = Math.min(r - i + 1, z[i - l]);
while (i + z[i] < n && s.charAt(z[i]) == s.charAt(i + z[i])) z[i]++;
if (i + z[i] - 1 > r) {
l = i;
r = i + z[i] - 1;
}
}
return z;
}
private static String reverse(String s) {
return new StringBuilder(s).reverse().toString();
}
private static String solveOne(String p, String v) {
int n = p.length();
int m = v.length();
if (m > n) return "No Match!";
String s1 = v + "#" + p;
int[] z1 = zFunction(s1);
String rp = reverse(p);
String rv = reverse(v);
String s2 = rv + "#" + rp;
int[] z2 = zFunction(s2);
ArrayList<Integer> ans = new ArrayList<>();
for (int i = 0; i + m <= n; i++) {
int pref = Math.min(z1[m + 1 + i], m);
int j = n - i - m;
int suf = Math.min(z2[m + 1 + j], m);
if (pref == m || pref + suf >= m - 1) {
ans.add(i);
}
}
if (ans.isEmpty()) return "No Match!";
StringBuilder out = new StringBuilder();
for (int i = 0; i < ans.size(); i++) {
if (i > 0) out.append(' ');
out.append(ans.get(i));
}
return out.toString();
}
public static void main(String[] args) throws Exception {
FastScanner fs = new FastScanner(System.in);
int t = fs.nextInt();
StringBuilder out = new StringBuilder();
for (int tc = 0; tc < t; tc++) {
String p = fs.next();
String v = fs.next();
out.append(solveOne(p, v)).append('\n');
}
String outputPath = System.getenv("OUTPUT_PATH");
if (outputPath != null && !outputPath.isEmpty()) {
try (BufferedWriter bw = new BufferedWriter(new FileWriter(outputPath))) {
bw.write(out.toString());
}
} else {
System.out.print(out);
}
}
}
Scor obtinut: 1.0
Submission ID: 464614243
Link challenge: https://www.hackerrank.com/challenges/save-humanity/problem
