Skip to content

Instantly share code, notes, and snippets.

@chermehdi
Created October 14, 2016 22:21
Show Gist options
  • Save chermehdi/5b7953b146d26a477c006890fbb94421 to your computer and use it in GitHub Desktop.
Save chermehdi/5b7953b146d26a477c006890fbb94421 to your computer and use it in GitHub Desktop.
Spoj Blast Solution
package com.problems;
/**
* @Author Mehdi Maick
*
*/
import java.io.*;
import java.util.Arrays;
import java.util.StringTokenizer;
public class Blast {
static char[] A, B;
static int k, a, b;
static int[][] memo;
static int f(int i, int j) {
if (i <= 0)
return j * k;
if (j <= 0)
return i * k;
if (memo[i][j] != -1)
return memo[i][j];
int ans = 9999999;
ans = Math.min(f(i - 1, j - 1) + Math.abs(A[i - 1] - B[j - 1]), ans);
ans = Math.min(ans, f(i - 1, j) + k);
ans = Math.min(ans, f(i, j - 1) + k);
return memo[i][j] = ans;
}
public static void solve(FastReader fs, PrintWriter pw) {
A = fs.next().toCharArray();
B = fs.next().toCharArray();
k = fs.nextInt();
int m = B.length;
int n = A.length;
memo = new int[n + 2][m + 2];
for (int i = 0; i <= n + 1; i++) {
Arrays.fill(memo[i], -1);
}
pw.println(f(A.length, B.length));
}
public static int dpSolve(int m, int n) {
int[][] dp = new int[m + 1][n + 1];
for (int i = 1; i <= m; i++) {
dp[i][0] = k * i;
}
for (int j = 1; j <= n; j++) {
dp[0][j] = k * j;
}
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
dp[i][j] = Math.min(Math.min(k + dp[i - 1][j], k + dp[i][j - 1]), dp[i - 1][j - 1]
+ Math.abs(A[j - 1] - B[i - 1]));
}
}
return dp[m][n];
}
public static void main(String[] args) throws Exception {
FastReader fs = new FastReader(System.in);
PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out));
solve(fs, pw);
fs.close();
pw.close();
}
static class FastReader {
BufferedReader reader;
StringTokenizer st;
FastReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream));
st = null;
}
String next() {
while (st == null || !st.hasMoreTokens()) {
try {
String line = reader.readLine();
if (line == null) {
return null;
}
st = new StringTokenizer(line);
} catch (Exception e) {
throw new RuntimeException();
}
}
return st.nextToken();
}
String nextLine() {
String s = null;
try {
s = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return s;
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
char nextChar() {
return next().charAt(0);
}
int[] nextIntArray(int n) {
int[] arr = new int[n];
int i = 0;
while (i < n) {
arr[i++] = nextInt();
}
return arr;
}
long[] nextLongArray(int n) {
long[] arr = new long[n];
int i = 0;
while (i < n) {
arr[i++] = nextLong();
}
return arr;
}
int[] nextIntArrayOneBased(int n) {
int[] arr = new int[n + 1];
int i = 1;
while (i <= n) {
arr[i++] = nextInt();
}
return arr;
}
long[] nextLongArrayOneBased(int n) {
long[] arr = new long[n + 1];
int i = 1;
while (i <= n) {
arr[i++] = nextLong();
}
return arr;
}
void close() {
try {
reader.close();
} catch (IOException e) {
System.err.println("There's been an error trying closing the reader ");
e.printStackTrace();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment