To solve this problem, we need to determine the minimal cost required to make a specific DNA sequence t a substring of another DNA sequence s by inserting characters with associated costs. The approach can be broken down into several steps:
-
Understanding the Problem:
- We need to embed the string
t into s by potentially inserting characters. The goal is to minimize the total cost of these insertions.
- Each character
A, C, G, and T has a specific cost associated with inserting it into s.
-
Formulating the Solution:
- The total cost of embedding
t entirely via insertions is the sum of the costs of all characters in t.
- However, if some characters in
t already match characters in s when overlaid at certain positions, we can save on the total cost by not having to insert those matching characters.
- The problem reduces to finding the optimal alignment of
t within s such that the sum of costs for characters that need to be inserted is minimized.
-
Using Convolution for Efficient Calculation:
- To efficiently calculate the number of matching characters for every possible alignment of
t in s, we can use the convolution operation.
- For each character type (
A, C, G, T), we create binary arrays indicating the presence of that character in s and t.
- We then perform convolution between these arrays to determine the number of matches for each alignment.
- By summing up the convolution results for all character types, we can determine the total number of matches for each alignment.
- The maximum number of matches across all alignments allows us to compute the minimal insertion cost.
-
Implementation Details:
- Implement the Fast Fourier Transform (FFT) to perform efficient convolutions.
- Sum the costs of characters that are not matched in the optimal alignment to determine the minimal total cost.
Here is the Java implementation that follows this approach:
import java.io.*;
import java.util.*;
public class Main {
static class Complex {
double re, im;
Complex(double re, double im){
this.re = re;
this.im = im;
}
Complex add(Complex o){
return new Complex(this.re + o.re, this.im + o.im);
}
Complex sub(Complex o){
return new Complex(this.re - o.re, this.im - o.im);
}
Complex mul(Complex o){
return new Complex(this.re * o.re - this.im * o.im, this.re * o.im + this.im * o.re);
}
Complex scale(double s){
return new Complex(this.re * s, this.im * s);
}
}
static void fft(Complex[] a, boolean invert){
int n = a.length;
for(int i=1,j=0;i<n;i++){
int bit = n >>1;
while(j >= bit){
j -= bit;
bit >>=1;
}
if(j < bit) j += bit;
if(i < j){
Complex temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
for(int len=2; len<=n; len <<=1){
double angle = 2 * Math.PI / len * (invert ? -1 : 1);
Complex wlen = new Complex(Math.cos(angle), Math.sin(angle));
for(int i=0;i<n;i+=len){
Complex w = new Complex(1,0);
for(int j=0; j<len/2; j++){
Complex u = a[i+j];
Complex v = a[i+j+len/2].mul(w);
a[i+j] = u.add(v);
a[i+j+len/2] = u.sub(v);
w = w.mul(wlen);
}
}
}
if(invert){
for(int i=0;i<n;i++) {
a[i] = a[i].scale(1.0 / n);
}
}
}
static long[] convolution(long[] a, long[] b){
int n1 = a.length;
int n2 = b.length;
int n = 1;
while(n < n1 + n2) n <<=1;
Complex[] fa = new Complex[n];
Complex[] fb = new Complex[n];
for(int i=0;i<n;i++){
fa[i] = new Complex(i < a.length ? a[i] : 0, 0);
fb[i] = new Complex(i < b.length ? b[i] : 0, 0);
}
fft(fa, false);
fft(fb, false);
for(int i=0;i<n;i++) fa[i] = fa[i].mul(fb[i]);
fft(fa, true);
long[] result = new long[n];
for(int i=0;i<n;i++) result[i] = Math.round(fa[i].re);
return result;
}
public static void main(String[] args)throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s = br.readLine();
String t = br.readLine();
String[] costStr = br.readLine().trim().split("\\s+");
int costA = Integer.parseInt(costStr[0]);
int costC = Integer.parseInt(costStr[1]);
int costG = Integer.parseInt(costStr[2]);
int costT = Integer.parseInt(costStr[3]);
int n = s.length();
int m = t.length();
long sum_cost = 0;
int[] tChars = new int[m];
for(int i=0;i<m;i++){
char c = t.charAt(i);
tChars[i] = c;
switch(c){
case 'A': sum_cost += costA; break;
case 'C': sum_cost += costC; break;
case 'G': sum_cost += costG; break;
case 'T': sum_cost += costT; break;
}
}
// For each character, compute convolution
long max_sum = 0;
for(char X : new char[]{'A','C','G','T'}){
// Build S_X
long[] S_X = new long[n];
for(int i=0;i<n;i++) if(s.charAt(i)==X) S_X[i] = 1;
// Build T_X_rev
long[] T_X_rev = new long[m];
for(int i=0;i<m;i++) {
char tc = t.charAt(m-1-i);
if(tc == X){
switch(tc){
case 'A': T_X_rev[i] = costA; break;
case 'C': T_X_rev[i] = costC; break;
case 'G': T_X_rev[i] = costG; break;
case 'T': T_X_rev[i] = costT; break;
}
}
else{
T_X_rev[i] = 0;
}
}
// Convolve S_X and T_X_rev
long[] conv = convolution(S_X, T_X_rev);
// conv[k] corresponds to shift i = k - (m -1)
for(int k=0; k < conv.length; k++) {
if(k - (m -1) < -m +1 || k - (m -1) > n -1) continue;
max_sum += conv[k];
}
}
// Now, find the maximum sum_matches over all shifts
// To do this correctly, we need to reset max_sum and recompute per shift
// Instead, let's compute per character and keep track
// Reset max_sum
max_sum = 0;
// Initialize an array to store sum_matches for each shift
int total_shifts = n + m -1;
long[] sum_matches = new long[total_shifts];
for(char X : new char[]{'A','C','G','T'}){
// Build S_X
long[] S_X = new long[n];
for(int i=0;i<n;i++) if(s.charAt(i)==X) S_X[i] = 1;
// Build T_X_rev
long[] T_X_rev = new long[m];
for(int i=0;i<m;i++) {
char tc = t.charAt(m-1-i);
if(tc == X){
switch(tc){
case 'A': T_X_rev[i] = costA; break;
case 'C': T_X_rev[i] = costC; break;
case 'G': T_X_rev[i] = costG; break;
case 'T': T_X_rev[i] = costT; break;
}
}
else{
T_X_rev[i] = 0;
}
}
// Convolve S_X and T_X_rev
long[] conv = convolution(S_X, T_X_rev);
// Add to sum_matches
for(int k=0; k < conv.length; k++) {
if(k >= sum_matches.length) break;
sum_matches[k] += conv[k];
}
}
// Now find the maximum sum_matches over all possible shifts
for(int k=0; k < sum_matches.length; k++) {
int i = k - (m -1);
if(i < -m +1 || i > n -1) continue;
if(sum_matches[k] > max_sum) max_sum = sum_matches[k];
}
long min_cost = sum_cost - max_sum;
System.out.println(min_cost);
}
}