R , 192 185 octets
f=function(s,a,c,k=0,x="",R=substring,N=nchar,p=R(s,1,1:N(s)))'if'(!N(s),k,{y={};for(i in c(R(s,1,1),p[mapply(grepl,p,x)]))y=min(y,f(R(s,N(i)+1),a,c,k+'if'(any(y),c,a),paste0(x,i)));y})
Essayez-le en ligne!
Code déroulé et explication:
# s is the current remaining string (at the beginning is equal to the target string)
# a is the append cost
# c is the clone cost
# k is the current cost (at the beginning is zero)
# x is the partially constructed string (at the beginning is empty)
f=function(s,a,c,k=0,x=""){
# store in p all the possible prefixes of s
p = substring(s,1,1:nchar(s))
# if s is empty return the current cost k
if(!nchar(s))
k
else{
y={}
# prepend the first letter of s (=append operation) to
# the prefixes in p that are contained in x (=clone operations)
for(i in c(substring(s,1,1),p[mapply(grepl,p,x)])){
# perform first the append then the clone operations and recurse,
# storing the cost in y if lower than previous
# (if y is NULL is an append operation otherwise is a clone, we use the right costs)
y = min(y,f(substring(s,nchar(i)+1),a,c,k+'if'(any(y),c,a),paste0(x,i)))
}
# return the current cost
y
}
}