लपेटें-आसपास के परिणाम


11

परिचय

इस चुनौती में, आपका काम स्ट्रिंग्स के सामान्यीकृत बाद का पता लगाना है। अनुवर्ती जरूरी नहीं है कि वे सन्निहित हों, और वे स्ट्रिंग को "चारों ओर लपेटें" भी कर सकते हैं, इसके अंत में जा रहे हैं और शुरुआत से फिर से शुरू कर सकते हैं। आप हालांकि, रैप की संख्या को कम करना चाहते हैं।

अधिक औपचारिक रूप से, चलो uऔर vकिसी भी दो तार, और हो सकता है k ≥ 0एक पूर्णांक। हम कहते हैं कि uएक के kबाद की घटना है v, अगर वहाँ ऐसे अलग सूचकांक हैं , और अधिकांश सूचकांक संतुष्ट हैं । इसका मतलब यह है कि रास्ते में इसके कुछ पात्रों को चुनते हुए, बाएं से दाएं की ओर जाते हुए और अधिकतम समय पर चारों ओर लपेटते हुए (समान रूप से, अधिकांश स्वीप्स करते हुए ) अंदर पाया जा सकता है । ध्यान दें कि कोई भी चरित्र एक रैप-अराउंड के बाद भी एक से अधिक बार नहीं चुना जा सकता है, और उस -wrapping बाद बिल्कुल साधारण अनुवर्ती है कि हम सभी परिचित हैं।i1, i2, ..., ilen(u)u == v[i1] v[i2] ... v[ilen(u)]kijij > ij+1uvkk+1v0

काम

आपके इनपुट दो नॉन-खाली अल्फ़ान्यूमेरिक स्ट्रिंग्स हैं uऔर v, और आपका आउटपुट सबसे छोटा पूर्णांक है, kजो कि uएक k-wrapping के बाद का है v। यदि ऐसा नहीं kहै, तो आउटपुट होगा -1

उदाहरण

इनपुट पर विचार करें u := xyzyxzzxyxऔर v := yxzzazzyxxxyz। अगर हम के पात्रों के लिए तलाश शुरू uमें vएक लालची फैशन में, हम 3 बार लपेट होगा:

 yxzzazzyxxxyz
>─x─────y────z┐
┌─────────────┘
└y───────x────┐
┌─────────────┘
└──zz─────x─y─┐
┌─────────────┘
└──────────x──>

इस प्रकार सही आउटपुट अधिकतम 3 पर है। ध्यान दें कि एक बार बाएं-सबसे अधिक वर्ण xका चयन कैसे किया जाता है, और फिर दूसरी स्वीप पर ध्यान नहीं दिया जाता है, क्योंकि इसे फिर से उपयोग नहीं किया जा सकता है। हालांकि, केवल 2 रैप-अराउंड के साथ एक छोटी विधि मौजूद है:

 yxzzazzyxxxyz
>──────────xyz┐
┌─────────────┘
└yxzz────x────┐
┌─────────────┘
└───────y─x───>

यह पता चलता है कि एक रैप-अराउंड (यानी दो स्वीप) पर्याप्त नहीं है, इसलिए सही आउटपुट है 2

नियम और बोनस

आप एक फ़ंक्शन या पूर्ण प्रोग्राम लिख सकते हैं, और यदि आवश्यक हो तो आप इनपुट के क्रम को भी बदल सकते हैं। सबसे कम बाइट गिनती जीतता है, और मानक खामियों को रोक दिया जाता है।

वहाँ एक है -10% के बोनस में के तहत 10 सेकंड कुल परीक्षण मामले के सभी की गणना के लिए। मैं अपनी मशीन पर अस्पष्ट मामलों का परीक्षण करूंगा; पायथन में मेरे संदर्भ कार्यान्वयन में लगभग 0.6 सेकंड लगते हैं। मेरे पास 1.86 गीगाहर्ट्ज़ डुअल कोर सीपीयू के साथ 7 साल पुराना लैपटॉप है, जिसे आप ध्यान में रख सकते हैं।

परीक्षण के मामलों

"me" "moe" -> 0
"meet" "metro" -> -1
"ababa" "abaab" -> 1
"abaab" "baabaa" -> 1
"1c1C1C2B" "1111CCCcB2" -> 3
"reverse" "reserved" -> 2
"abcdefg" "gfedcba" -> 6
"xyzyxzzxyx" "yxzzazzyxxxyz" -> 2
"aasdffdaasdf" "asdfddasdfsdaafsds" -> 2

1
क्या यह भी उदाहरण के लिए एक वैध समाधान होगा? यह एक लालची दृष्टिकोण है।
orlp

@orlp यह मान्य नहीं है, क्योंकि पहले xका उपयोग तीन अलग-अलग स्वीपों में किया जाता है। इसे केवल एक बार ही इस्तेमाल किया जा सकता है।
ज़र्गब

आह, मैं अब देख रहा हूं।
orlp

जवाबों:


4

पायथ, 34 बाइट्स

Mh+Smssm>.ukC,dtdfqGsm@HkT.PUHlG_1

यह एक फ़ंक्शन को परिभाषित करता है g, जो पैरामीटर के रूप में दो तार लेता है। इसे ऑनलाइन आज़माएं: पायथ कंपाइलर / एक्ज़ीक्यूटर

यह कोड बहुत अक्षम है। यह एक समय और स्मृति की जटिलता है len(v)!/(len(v)-len(u))!। यह 10 सेकंड से कम समय में परीक्षण मामलों को हल करने में सक्षम नहीं है। (यह भी बहुत संभावना दुर्घटनाग्रस्त हो जाएगा, क्योंकि यह स्मृति से बाहर चला जाएगा।)

M                                    define g(G, H): return _
                          .PUHlG        all permutations of [0, 1, ..., len(H)-1] of length len(G)
                 fqGsm@HkT              filter the permutations which form the string G
    mssm>.ukC,dtd                       compute the number of wraps for each of the remaining permutations
  +S                            _1      sort the numbers and append -1
 h                                      return the first element

4

हास्केल, 160 * 0.9 = 144 बाइट्स

a#(-1)=a
a#b=min a b
f y=w(y++" ")0$length y
w _ n _[]=n
w(c:d)n o g@(a:b)|n>o=(-1)|a==c=z#w y n z g|c==' '=w y(n+1)o g|1<2=w y n o g where z=w d n o b;y=d++[c]

सभी परीक्षण मामलों के लिए समय (ध्यान दें: तर्क फ़्लिप किए गए हैं):

*Main> map (uncurry f) [
             ("moe", "me"),
             ("metro", "meet"),
             ("abaab", "ababa"),
             ("baabaa", "abaab"),
             ("1111CCCcB2", "1c1C1C2B"),
             ("reserved", "reverse"),
             ("gfedcba", "abcdefg"),
             ("yxzzazzyxxxyz", "xyzyxzzxyx"),
             ("asdfddasdfsdaafsds", "aasdffdaasdf")]
[0,-1,1,1,3,2,6,2,2]
(0.08 secs, 25794240 bytes)

यह कैसे काम करता है (लघु संस्करण): सरल जानवर बल जो एक मिलान चरित्र का उपयोग करने और इसे लंघन करने की न्यूनतम लेता है। मैं या तो समाप्त होने पर खोज को रोक देता हूं (चक्रों की संख्या लौटाता हूं) या जब अब तक की तुलना में अधिक साइकिल चला जाता हूं (वापसी -1)।

अपने पहले संस्करण की तुलना में बहुत सारे बाइट्स सहेजे गए, मुख्यतः क्योंकि मैंने एक पूर्ण कार्यक्रम से एक फ़ंक्शन पर स्विच किया।

कुछ टिप्पणियों और उचित रिक्ति के साथ गोल्फ हास्केल काफी पठनीय है:

-- a minimum function that ignores a -1 in the right argument to prevent
-- "not solvable" cases in parts of the recursive search to dominate low numbers
-- of solvable parts. If the case isn't solvabale at all, both arguments are
-- -1 and are carried on.
a # (-1) = a
a # b    = min a b

-- the main function f calls the worker funktion w with arguments
-- * the string to search in (STSI), appended by a space to detect cycles
-- * the number of cycles so far
-- * the minimum of cycles needed so far, starting with the length of STSI
-- * the string to search for (STSF) (partial applied away and therefore invisible)
f y = w (y++" ") 0 (length y)

-- the worker function 
w _ n _ [] = n          -- base case: if STSF is empty the work is done and the 
                        -- number of cycles is returned

w (c:d) n o g@(a:b)     -- "c" is first char of STSI, "d" the rest
                        -- "n" number of cycles, "o" minimum of cycles so far
                        -- "g" is the whole STSF, "a" the 1st char, "b" the rest
  | n>o    = (-1)             -- if current cycle is more than a previous result,
                              -- indicate failure
  | a==c   = z # w y n z g    -- if there's a character match, take the min of
                              -- using it and skipping it
  | c==' ' = w y (n+1) o g    -- cycle detected, repeat and adjust n
  | 1<2    = w y n o g        -- otherwise try next char in STSI

  where                 -- just some golfing: short names for common subexpressions
  z = w d n o b;        -- number of cycles if a matching char is used
  y = d ++ [c]          -- rotated STSI

संदर्भ के लिए: पुराना संस्करण, पूर्ण कार्यक्रम, 187 बाइट्स

main=interact$show.f.lines
a#(-1)=a
a#b=min a b
f[x,y]=w x(y++" ")0 0
w[]_ n _=n
w g@(a:b)(c:d)n m|a==c=w b d n 1#y|c==' '&&m==1=w g(d++" ")(n+1)0|c==' '=(-1)|1<2=y where y=w g(d++[c])n m

@Zbb: ने मेरे समाधान को फिर से काम किया। यह अब तेज और छोटा है।
nimi

0.6s में जब व्याख्या की जाती है, 0.01s संकलित होने पर।
जर्गर्ब

2

जावास्क्रिप्ट (ईएस 6) 174 (193 - 10%)

पुनरावर्ती खोज, @ nimi के उत्तर की तरह, रैप के मिनट को ध्यान में रखते हुए। समाधान स्थान बड़ा है (पिछले उदाहरण के लिए सब से ऊपर), लेकिन वर्तमान में मिले मिनट में खोज को काटने से समय कम रहता है। संपादित करें 1 एक लापता परीक्षण मामला जोड़ें, थोड़ा छोटा करें संपादित करें 2 के आसपास पैरामाम को पारित करने की कोई आवश्यकता नहीं है, यह तय हो गया है

K=(w,s,x)=>
  ~-(R=(r,l,p=0,q=1,z=w[p],i=0)=>
  {
    if(z&&!(q>x)){
      if(~(r+l).indexOf(z))
        for(t=l?R(l+r,'',p,q+1):x;x<t?0:x=t,i=~r.indexOf(z,-i);)
          t=R(r.slice(-i),l+r.slice(0,~i),p+1,q);
      q=x
    }
    return q
  })(s,'')

Ungolfed

K=(word, astring)=>
{
  var minWraps // undefined at first. All numeric comparison with undefined give false 
  var R=(right, left, pos, wraps)=>
  {
    var cur = word[pos]
    var i,t;
    if (! cur) // when all chars of word are managed
      return wraps;
    if (wraps > minWraps) // over the minimum wrap count already found, stop search
      return wraps; 
    if ( (right+left).indexOf(cur) < 0 ) // if the current char is not found in the remaining part of the string
      return minWraps; // return the current min, could still be undefined (that means 'no way')
    if ( left ) // if there is a left part, try a wrapping search with the current char
    {
      t = R(left+right, '', pos, wraps+1)
      if ( !(minWraps < t)) minWraps = t; // set current min if t is less than current min or current min is still undefined
    }
    // find all occurrences of current char in the remaining part
    // for each occurrence, start a recursive search for the next char
    for(i = 0; (i = right.indexOf(cur, i)) >= 0; i++)
    {
      var passed = right.slice(0,i) // the passed chars go in the left part
      var rest = right.slice(i+1) 
      t = R(rest, left+passed, pos+1, wraps) // try next char in the remaining part, no wrap
      if ( !(minWraps < t)) minWraps = t; // set current min if t is less than current min or current min is still undefined
    }
    return minWraps
  }
  var result = R(astring, '', 0, 1) // start with right=string and left empty
  return ~-result; // decrement. convert undefined to -1
}

फ़ायरफ़ॉक्स / फायरबग कंसोल में टेस्ट करें

time=~new Date;
[['me','moe']
,['meet','metro']
,['ababa','abaab']
,['abaab','baabaa']
,['1c1C1C2B','1111CCCcB2']
,['reverse','reserved']
,['abcdefg','gfedcba']
,['xyzyxzzxyx','yxzzazzyxxxyz']
,['aasdffdaasdf','asdfddasdfsdaafsds']]
.forEach(s=>console.log(s,r=K(...s)))
time-=~new Date

आउटपुट (अंतिम पंक्ति एमएस में निष्पादन का समय है)

["मुझे", "मो" "0
[" मिलना "," मेट्रो "] -1
[" अबाबा "," अबाब "] 1
[" अबआब "," बाबा "] 1
[" 1c1C1b2B "," 1111CCCcB2 "] 3
["रिवर्स", "आरक्षित"] 2
["एब्सडेफग", "गफेड्ब्बा"] 6
["xyzyxzzxyx", "yxzzazzyxxxyz"] 2
["एसाडफैडसैड्सफीड", "एसडीडासफैड्सडाफैड्स"] 2
116


फायरबग के साथ परीक्षण किया, मेरी मशीन पर 175ms में चलता है।
जर्गर्ब

@Zgarb में तब सुधार की गुंजाइश है: मैं इसे धीमा और छोटा करने की कोशिश करूँगा
edc65
हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.