Sie erhalten eine Zeichenfolge, die Sie erstellen müssen. Beginnen Sie mit der leeren Zeichenfolge und verwenden Sie die Kosten für das Anhängen und Klonen

17

Ihre Aufgabe ist es, die angegebene Zielzeichenfolge zu erstellen. Beginnen Sie mit einer leeren Zeichenfolge, und fügen Sie ihr Zeichen hinzu, bis Ihre Zeichenfolge mit der gewünschten übereinstimmt. Sie können entweder ein Zeichen an das Ende Ihrer Zeichenfolge mit den Kosten x anfügen oder die Zeichenfolge mit den Kosten y klonen. Was wir wollen, ist der billigste Weg, dies zu tun.

Testfälle

targetString , appendcost, clonecost -> totalcost

"bb", 1, 2 -> 2
"bbbb", 2, 3 -> 7
"xzxpcxzxpy", 10, 11 -> 71
"abababab", 3, 5 -> 16
"abababab", 3, 11 -> 23

quelle
1
Wie sind die Kosten definiert? Sind sie positive ganze Zahlen?
Arnauld
1
Ich denke, Sie wollen nur Code Golf (kürzester Code) herausfordern, deshalb habe ich die Code-Herausforderung entfernt und Puzzle-Tags programmiert, die eine alternative Art der Wertung anzeigen.
xnor
7
Ich denke, es würde helfen, mehr Testfälle zu haben, da es wahrscheinlich ist, dass jemand ein Programm schreiben könnte, das gute Heuristiken hat, die für alle Testfälle funktionieren, aber im Allgemeinen nicht optimal sind. Insbesondere hat keiner der Testfälle mehrere Klone oder Klone von Teilzeichenfolgen, die nicht am Anfang stehen. Ich denke, es wäre auch gut, ein Beispiel zu haben, bei dem eine Änderung nur der Kosten die Ausgabe ändert.
xnor
6
Übrigens eine schöne erste Herausforderung!
Erik der Outgolfer
Wird das Klonen eines einzelnen Buchstabens immer noch als Klonvorgang betrachtet?
digEmAll

Antworten:

2

Schale , 25 Bytes

φ?ö▼z+:⁴∞²m⁰§:h§δf`€otṫḣ0

Probieren Sie es online!

Eingaben sind in der Reihenfolge Kosten anhängen, Kosten klonen, Ziel.

Erläuterung

φ?ö▼z+:⁴∞²m⁰§:h§δf`€otṫḣ0  Two explicit inputs and one implicit.
                           Example: 2, 3, s="abab"
φ                          Make a recursive function and call it on s:
 ?                      0   If s is empty, return 0.
  ö▼z+:⁴∞²m⁰§:h§δf`€otṫḣ    Otherwise do this.
                       ḣ    Prefixes: ["a","ab","aba","abab"]
                    otṫ     Suffixes except the first one: ["bab","ab","b"]
               §δf`€        Keep those prefixes that have the corresponding suffix as substring: ["ab","aba"]
            §:h             Prepend s minus last character: ["aba","ab","aba"]
          m⁰                Recurse on each: x=[6,4,6]
        ∞²                  Repeat the clone cost: [3,3,3,..
      :⁴                    Prepend append cost: [2,3,3,3,..
    z+                      Add component-wise to x: [8,7,9]
   ▼                        Minimum: 7
Zgarb
quelle
1

Python 2 , 112 Bytes

f=lambda s,a,c,i=0:i<len(s)and min([a+f(s,a,c,i+1)]+[c+f(s,a,c,n)for n in range(i+1,len(s)+1)if s[i:n]in s[:i]])

Probieren Sie es online!

ovs
quelle
1

JavaScript (ES6), 123 111 Bytes

Übernimmt die Eingabe als (x)(y)(s).

x=>y=>m=g=([s,...r],o='',c=0)=>s?[...r,g(r,o+s,c+x)].map(_=>s+=r.shift(~o.search(s)&&g(r,o+s,c+y)))|m:m=m<c?m:c

Probieren Sie es online!

Kommentiert

x => y =>                    // x = 'append' cost; y = 'clone' cost
m =                          // m = minimum cost, initialized to a non-numeric value
                             //     this is what will eventually be returned
g = (                        // g = recursive function taking:
  [s,                        //   - the input string split into s = next character
      ...r],                 //     and r[] = array of remaining characters
  o = '',                    //   - o = output string
  c = 0                      //   - c = current cost
) =>                         //
  s ?                        // if s is defined:
    [ ...r,                  //   split a copy of r
      g(r, o + s, c + x)     //   do a recursive call with an 'append' operation
    ].map(_ =>               //   iterate as many times as there are remaining characters
                             //   in r[], + 1
      s +=                   //     append to s
        r.shift(             //     the next character picked from the beginning of r[]
          ~o.search(s) &&    //     if s is found in o,
          g(r, o + s, c + y) //     do a recursive call with a 'clone' operation
        )                    //     (note that both s and r are updated *after* the call)
    ) | m                    //   end of map(); return m
  :                          // else:
    m = m < c ? m : c        //   update m to min(m, c)
Arnauld
quelle
1

R , 192 185 Bytes

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})

Probieren Sie es online!

Abgerollter Code und Erklärung:

# 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
  }
}
digEmAll
quelle