Skip to content

Instantly share code, notes, and snippets.

@saisumit
Created December 11, 2017 14:33
Show Gist options
  • Save saisumit/e0ffda48049fa35671347ac6e18993c1 to your computer and use it in GitHub Desktop.
Save saisumit/e0ffda48049fa35671347ac6e18993c1 to your computer and use it in GitHub Desktop.
#include <bits/stdc++.h>
using namespace std ;
#define REP(i, a, b) for (int i = a; i <= b; i++)
#define FOR(i, n) for (int i = 0; i < n; i++)
#define foreach(it, ar) for ( typeof(ar.begin()) it = ar.begin(); it != ar.end(); it++ )
#define fill(ar, val) memset(ar, val, sizeof(ar))
#define PI 3.1415926535897932385
#define uint64 unsigned long long
#define Int long long
#define int64 long long
#define all(ar) ar.begin(), ar.end()
#define pb push_back
#define ff first
#define ss second
#define bit(n) (1<<(n))
#define Last(i) ( (i) & (-i) )
#define sq(x) ((x) * (x))
#define mp make_pair
const int MAXN = 351 ;
int dp[ MAXN ][ MAXN ][ MAXN ] ;
char A[ MAXN ] ;
char B[ MAXN ] ;
int N , M , K ;
const int INF = 1e8 ;
int comp ( char a , char b )
{
return ( a - 'a' )^( b - 'a' ) ;
}
int rec( int i , int j , int LCS )
{
if( LCS >= K )
return 0 ;
if( i == N or j == M )
return INF ;
int cost = INF ;
if( dp[i][j][LCS] != -1 )return dp[i][j][LCS] ;
if( A[i] == B[j] )
cost = min(cost,rec( i + 1 , j + 1 , LCS + 1 )) ;
cost = min( cost , comp(A[i],B[j] ) + rec( i + 1 , j + 1 , LCS + 1 ) ) ;
cost = min( cost , rec(i,j+1,LCS) ) ;
cost = min( cost , rec(i+1,j,LCS) ) ;
dp[i][j][LCS] = cost ;
return cost ;
}
int main()
{
memset( dp, -1 , sizeof(dp) ) ;
scanf("%d%d%d",&N,&M,&K) ;
scanf(" %s",A) ;
scanf(" %s",B) ;
int ans = rec( 0 , 0 , 0 ) ;
if( ans >= INF )ans = -1 ;
printf("%d\n",ans );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment