একটি বিন্যাসে বিজোড় চরিত্রটি সন্ধান করুন


20

ইনপুট

প্রথম লাইনটি একটি নির্দিষ্ট স্ট্রিং হবে যেকোন পরিমাণ বারবার পুনরাবৃত্তি হবে। উদাহরণস্বরূপ, এটি হতে পারে abcabcabcabc, [];[];[];, ইত্যাদিতে কেটে যেতে পারে; উদাহরণস্বরূপ: 1231231231। সর্বদা সংক্ষিপ্ততম স্ট্রিংটি সন্ধান করুন; উদাহরণস্বরূপ, লাইনটি যদি থাকে 22222তবে স্ট্রিংটি হয় 2না 22বা 22222অন্য কিছু। স্ট্রিং সর্বদা কমপক্ষে 2 টি পুরো বার পুনরাবৃত্তি হবে।

সমস্ত পরবর্তী লাইনগুলি কোনও সংখ্যার মাধ্যমে সেই প্যাটার্নটি অফসেট হবে। উদাহরণস্বরূপ, এটি হতে পারে:

abcabcabc
cabcabcab
bcabcabca

(1 দ্বারা অফসেট), বা এটি হতে পারে:

abcdefabcdefabcdefabc
cdefabcdefabcdefabcde
efabcdefabcdefabcdefa

(অফসেট 4)

ইনপুটটির একটি চরিত্র ভুল হবে। (এটি প্রথম লাইনে না থাকার গ্যারান্টিযুক্ত)) উদাহরণস্বরূপ, এই ইনপুটটিতে:

a=1a=1a=1
=1a=1a=1a
1a=11=1a=
a=1a=1a=1
=1a=1a=1a

1লাইন 3 অদ্ভুত এক হয়ে গেছে।

আউটপুট

আপনাকে বিজোড়ের বাইরে (অবশ্যই শূন্য-ভিত্তিক, উপরের বাম থেকে শুরু করে) স্থানাঙ্কগুলি আউটপুট করতে হবে। উদাহরণস্বরূপ, উপরের ইনপুটটিতে, সংশ্লিষ্ট আউটপুটটি 4,2। আপনি আউটপুট 4 2, বা "4""2", বা এমনকি [[4],[2]], বা অন্য কোনও ফর্ম্যাটও করতে পারেন, যতক্ষণ আপনি আউটপুটটি অনুমিত হওয়ার কথা।

পরীক্ষার মামলা

ইনপুট:

codegolfcodegolfco
egolfcodegolfcodeg
lfcodegolfcodegoff
odegolfcodegolfcod
golfcodegolfcodego
fcodegolfcodegolfc

আউটপুট: 16,2

ইনপুট:

][[][][[][][[][][[][][[
[][][[][][[][][[][][[][
[][[][][[][][[][][[][][
[[][][[]]][[][][[][][[]

আউটপুট: 8,3

ইনপুট:

...
. .
...

আউটপুট: 1,1

ইনপুট:

ababa
babab
ababb
babab

আউটপুট: 4,2

যাওয়া!


স্ট্রিং এর মধ্যে কোন অক্ষর থাকতে পারে? মুদ্রণযোগ্য এএসসিআইআই? হওয়া ASCII? ইউনিকোড?
ডেনিস

@ ডেনিস জাস্ট প্রিন্টযোগ্য এএসসিআইআই (এটি মূলত স্ট্রিংগুলির সাথে জড়িত যে কোনও চ্যালেঞ্জের জন্য অনুমান করা যেতে পারে; অন্যথায় আমাদের প্রায় প্রতিটি চ্যালেঞ্জের জন্য এটি নির্দিষ্ট করতে হবে: পি)
ডোরকনব

আমি অনুমান করেছি। আমি এমন একটি পদ্ধতির কথা ভাবছি যা অব্যবহৃত চর প্রয়োজন, তাই আমি ভেবেছিলাম আমি জিজ্ঞাসা করব।
ডেনিস

আমাদের কি এখানে abc/cab/abcকেসটি পরীক্ষা করা উচিত: - এবং 0 2এখানে আউটপুট ?
ব্যবহারকারী 2846289

@ ভাদিমআর না, যেহেতু কেবলমাত্র একটি চরিত্রই ভুল হবে।
ডোরকনবব

উত্তর:


7

বাশ পার্ল, 231 229 218 178 164 166 138 106 74 বাইট

/^(((.*).*)\2+)\3$/;$_.=$1x2;$.--,die$+[1]if/^(.*)(.)(.*)
.*\1(?!\2).\3/

স্ক্রিপ্টটির জন্য -nস্যুইচটি ব্যবহার করা দরকার , যা দুটি বাইটের জন্য দায়ী।

এমটি 0 এর উত্তর থেকে প্যাটার্নের সমস্ত সম্পূর্ণ পুনরাবৃত্তির দুটি অনুলিপি যুক্ত করার ধারণা নেওয়া হয়েছে ।

অন্যান্য সমস্ত উত্তরের বিপরীতে, এই পদ্ধতির প্রতিটি পুনরাবৃত্তির মধ্যে বর্তমান ইনপুট লাইনের ধরণটি বের করার চেষ্টা করা হয়; এটি বিজোড় অক্ষরযুক্ত লাইনে ব্যর্থ হবে (এবং এর পরিবর্তে পূর্ববর্তী লাইনের প্যাটার্নটি ব্যবহার করবে)। লুপের মধ্যে প্যাটার্ন নিষ্কাশন অন্তর্ভুক্ত করার জন্য এটি করা হয়, যা কয়েকটি বাইট সংরক্ষণ করতে সক্ষম হয়।

উদার সংস্করণ

#!/usr/bin/perl -n

# The `-n' switch makes Perl execute the entire script once for each input line, just like
# wrapping `while(<>){…}' around the script would do.

/^(((.*).*)\2+)\3$/;

# This regular expression matches if `((.*).*)' - accessible via the backreference `\2' -
# is repeated at least once, followed by a single repetition of `\3" - zero or more of the
# leftmost characters of `\2' - followed by the end of line. This means `\1' will contain
# all full repetitions of the pattern. Even in the next loop, the contents of `\1' will be
# available in the variable `$1'.

$_.=$1x2;

# Append two copies of `$1' to the current line. For the line, containing the odd
# character, the regular expression will not have matched and the pattern of the previous
# line will get appended.
#
# Since the pattern is repeated at least two full times, the partial pattern repetition at
# the end of the previous line will be shorter than the string before it. This means that
# the entire line will the shorter than 1.5 times the full repetitions of the pattern, 
# making the two copies of the full repetitions of the pattern at least three times as 
# long as the input lines.

$.-- , die $+[1] if

# If the regular expression below matches, do the following:
#
#   1. Decrement the variable `$.', which contains the input line number.
#
#      This is done to obtain zero-based coordinates.
#
#   2. Print `$+[1]' - the position of the last character of the first subpattern of the
#      regular expression - plus some additional information to STDERR and exit.
#
#      Notably, `die' prints the (decremented) current line number.

/^(.*)(.)(.*)
.*\1(?!\2).\3/;

# `(.*)(.)(.*)', enclosed by `^' and a newline, divides the current input line into three
# parts, which will be accesible via the backreferences `\1' to `\3'. Note that `\2'
# contains a single character.
#
# `.*\1(?!\2).\3' matches the current input line, except for the single character between
# `\1' and `\3' which has to be different from that in `\2', at any position of the line
# containing the pattern repetitions. Since this line is at least thrice as long as
# `\1(?!\2).\3', it will be matched regardless of by how many characters the line has been
# rotated.

উদাহরণ

পরীক্ষার ক্ষেত্রে

codegolfcodegolfco
egolfcodegolfcodeg
lfcodegolfcodegoff
odegolfcodegolfcod
golfcodegolfcodego
fcodegolfcodegolfc

গল্ফ সংস্করণ আউটপুট হয়

16 at script.pl line 1, <> line 2.

অর্থাত্ বিজোড় চরিত্রের সমন্বয় রয়েছে 16,2

এই নির্মম অপব্যবহারগুলি উদার আউটপুট ফর্ম্যাটটির সুবিধা নেয়।

প্রস্থান করার ঠিক আগে, পার্লের কিছু বিশেষ ভেরিয়েবলের বিষয়বস্তু হ'ল:

$_  = lfcodegolfcodegoff\ncodegolfcodegolfcodegolfcodegolf
$1  = lfcodegolfcodego
$2  = f
$3  = f

( $nব্যাকরেফারেন্সের মাধ্যমে অ্যাক্সেসযোগ্য সাব-প্যাটার্নগুলির মিল রয়েছে \n))


উত্তর ইউনিটের চালাক ধরা এটি একটি বাইট দ্বারা অপ্টিমাইজ করা যেতে পারে:^((.*?)(.*?))(?=\1+\2$)
হাইকো ওবারডিক

জনপ্রিয় বাচ্চারা যে ভাষাটি ব্যবহার করছে তা আমি স্যুইচ করেছি। সম্ভবত আরও গল্ফ করা যেতে পারে; এক দশকেরও বেশি সময় এটি আমার প্রথম পার্ল স্ক্রিপ্ট ...
ডেনিস

2
... এবং আপনি যদি ভাবেন যে জনপ্রিয় জনপ্রিয় বাচ্চারা কী ব্যবহার করছে
দশক

এই উত্তরটি তার প্রাপ্য প্রেম পাচ্ছে না। আমার কাছে বিজয়ী @ ডুরকনব-এর মতো দেখতে
আর্দনে নিউ

8

পার্ল, 212 191 181 168 বাইট

$_=<>;/^(((.*?)(.*?))\2+)\3$/;$x=$1x4;while(<>){chop;$x=~/\Q$_\E/&&next;for$i(0..y///c-1){for$r(split//,$x){$b=$_;$b=~s/(.{$i})./$1$r/;$x=~/\Q$b\E/&&die$i,$",$.-1,$/}}}
  • এই সংস্করণটি ডেনিসের উত্তরে শিখে নেওয়া উত্তর ইউনিটটি ধরার জন্য একটি অনুকূলিত কৌশল ব্যবহার করে ।
  • সমস্ত লাইনের সমান দৈর্ঘ্য রয়েছে এমন বৈশিষ্ট্যটি ব্যবহার করে অনুকূলকরণ।
  • শেষ লাইনের জন্য লাইন এন্ডেরও প্রয়োজন, অন্যথায় chompপরিবর্তে chopব্যবহার করা উচিত।
  • আরডনিউর মন্তব্যে অপ্টিমাইজেশন যুক্ত করা হয়েছে।

পুরানো সংস্করণ, 212 বাইট:

$_=<>;chop;/^(.+?)\1+(??{".{0,".(-1+length$1).'}'})$/;$;=$1;while(<>){$x=$;x length;chop;$x=~/\Q$_\E/&&next;for$i(0..-1+length$_){for$r(split//,$;){$b=$_;$b=~s/(.{$i})./$1$r/;$x=~/\Q$b\E/&&exit print$i,$",$.-1}}}

অবরুদ্ধ সংস্করণ:

$_ = <>;  # read first line
/^(((.*?)(.*?))\2+)\3$/;
# The repeat unit \2 consists of \3 and \4,
# and the start part \2 can be added at the end (as partial or even full unit).
$x = $1 x 4; # $x is long enough to cover each following line

# Old version:
# /^(.+?)\1+(??{ ".{0," . (-1 + length $1) . '}' })$/;
# $a = $1; # $a is the repeat unit.
# The unit is caught by a non-greedy pattern (.+?) that is
# repeated at least once: \1+
# The remaining characters must be less than the unit length.
# The unit length is known at run-time, therefore a "postponed"
# regular expression is used for the remainder.

# process the following lines until the error is found
while (<>) {
    # old version:
    # $x = $a x length;
    # $x contains the repeated string unit, by at least one unit longer
    # than the string in the current line
    chop; # remove line end of current line
    $x =~ /\Q$_\E/ && next;
          # go to next line, if current string is a substring of the repeated units;
          # \Q...\E prevents the interpretation of special characters
    # now each string position $x is checked, if it contains the wrong character:
    for $i (0 .. y///c - 1) {  # y///c yields the length of $_
        for $r (split //, $x) { #/ (old version uses $a)
            # replace the character at position $i with a
            # character from the repeat unit
            $b = $_;
            $b =~ s/(.{$i})./$1$r/;
            $x =~ /\Q$b\E/
               && die $i, $", $. - 1, $/;
               # $" sets a space and the newline is added by $/;
               # the newline prevents "die" from outputting line numbers
        }
    }
}

দুর্দান্ত সমাধান এবং মন্তব্যগুলি, আমাকে আরও
রেইজেক্স

1
প্রথমটি chopঅপ্রয়োজনীয়- মুছে ফেলা উচিত। ফাইনালটি exit printপ্রতিস্থাপন করা যেতে পারে die( ,$/অতিরিক্ত জিনিসগুলি আড়াল করার জন্য যুক্ত করুন (যদি প্রয়োজন হয়))। এছাড়াও length$_সঙ্গে প্রতিস্থাপিত হতে পারেy///c
ardnew

@ অর্ডনিউ: অনেক ধন্যবাদ, আমি প্রথমটি সরিয়েছি chop, কারণ $স্ট্রিংয়ের শেষে নিউলাইনের আগে মিল রয়েছে। dieযুক্ত হওয়া নতুন লাইনের মাধ্যমে অতিরিক্ত জিনিসপত্র গোপন করা আমার কাছে প্রয়োজনীয় বলে মনে হচ্ছে। এছাড়াও অপ্রয়োজনীয় ছাড়া y///cতুলনায় অনেক কম length$_এবং এক বাইট ছোট । length$_
হাইকো ওবারডিক

1
@ardnew: আমি সম্পর্কে ভুলে গিয়েছিলেন ডাই 'র ভারবোসিটি। এটি এমনকি লাইন নম্বর প্রিন্ট অন্তর্ভুক্ত! আমি এটি আমার পরবর্তী আপডেটে ব্যবহার করব।
ডেনিস

3

সি, 187 বাইট

সীমাবদ্ধতা।

  • 98 টি অক্ষরের চেয়ে বেশি ইনপুট স্ট্রিং ব্যবহার করবেন না :)

গল্ফ সংস্করণ

char s[99],z[99],*k,p,i,I,o,a;c(){for(i=0;k[i]==s[(i+o)%p];i++);return k[i];}main(){for(gets(k=s);c(p++););for(;!(o=o>p&&printf("%d,%d\n",I,a))&&gets(k=z);a++)while(o++<p&&c())I=I<i?i:I;}

উদার সংস্করণ

char s[99],z[99],*k,p,i,I,o,a;

c()
{
    for(i=0
       ;k[i]==s[(i+o)%p]
       ;i++)
       ;
    return k[i];
}

main()
{
    for(gets(k=s);c(p++);)
         ;
    for(;!(o=o>p&&printf("%d,%d\n",I,a)) && gets(k=z);a++)
           while(o++ < p && c())
            I=I<i?i:I;
}

2

পাইথন, 303 292

r=raw_input
R=range
s=r()
l=len(s)
m=1
g=s[:[all((lambda x:x[1:]==x[:-1])(s[n::k])for n in R(k))for k in R(1,l)].index(True)+1]*l*2
while 1:
 t=r()
 z=[map(lambda p:p[0]==p[1],zip(t,g[n:l+n]))for n in R(l)]
 any(all(y)for y in z)or exit("%d,%d"%(max(map(lambda b:b.index(False),z)),m))
 m+=1

ইনপুট স্টিডিন দিয়ে যায়। কোনও দাবি থাকলে আমি এটি ব্যাখ্যা করব, তবে দেখে মনে হচ্ছে না যে আমি যাই হোক জিতে যাব।


1

পার্ল, 157 154

সম্পাদনা করুন : -3 আরডনেউ'র পরামর্শকে ধন্যবাদ।

<>=~/^(((.*?).*?)\2+)\3$/;$p=$2;$n=$+[1];while(<>){s/.{$n}/$&$&/;/(\Q$p\E)+/g;$s=$p;1while/./g*$s=~/\G\Q$&/g;print$n>--($m=pos)?$m:$m-$n,$",$.-1,$/if pos}

এটি আমাকে কিছুটা সময় নিয়েছিল (অবশ্যই এবং চালু, অবশ্যই, 5 দিন নয় ;-)), এবং অ্যালগরিদম সম্পর্কে ধারণাটি প্রাথমিকভাবে অধরা ছিল (যদিও আমি অনুভব করেছি যে এটি সেখানে ছিল) তবে শেষ পর্যন্ত (এবং হঠাৎ) এটি সমস্ত পরিষ্কার হয়ে গেল।

স্ট্রিংয়ের দৈর্ঘ্য যদি প্যাটার্ন দৈর্ঘ্যের একাধিক হয় এবং স্ট্রিংটি প্যাটার্নের শুরু দিয়ে শুরু না হয় তবে নিজেই যুক্ত করে স্ট্রিংটি কনটেন্টেশনের জায়গায় প্যাটার্ন তৈরি করে (বৃত্তাকার ফিতাতে কোনও শব্দের অসীম পুনরাবৃত্তি কল্পনা করুন - এর স্থান ldালাই গুরুত্বপূর্ণ নয়)। সুতরাং, ধারণাটি হ'ল একাধিক ইউনিটের দৈর্ঘ্যে লাইনটি ছাঁটাই করা এবং এটিতে মূলটি মিলিত করুন। ফলাফলটি এমনকি ভুল চরিত্রযুক্ত স্ট্রিংয়ের জন্যও অন্তত একবার প্যাটার্নের সাথে মেলে গ্যারান্টিযুক্ত। সেখান থেকে আপত্তিজনক চরিত্রের অবস্থানটি পাওয়া সহজ।

প্রথম লাইনটি নির্লজ্জভাবে হাইকো ওবারডিকের উত্তর থেকে ধার করা হয়েছে :-)

<>=~/^(((.*?).*?)\2+)\3$/;      # Read first line, find the repeating unit
$p=$2;                          # and length of whole number of units.
$n=$+[1];                       # Store as $p and $n.
while(<>){                      # Repeat for each line.
    s/.{$n}/$&$&/;              # Extract first $n chars and
                                # append original line to them.
    /(\Q$p\E)+/g;               # Match until failure (not necessarily from the
                                # beginning - doesn't matter).
    $s=$p;                      # This is just to reset global match position
                                # for $s (which is $p) - we could do without $s,
                                # $p.=''; but it's one char longer.
                                # From here, whole pattern doesn't match -
    1while/./g*$s=~/\G\Q$&/g;   # check by single char.
                                # Extract next char (if possible), match to 
                                # appropriate position in a pattern (position 
                                # maintained by \G assertion and g modifier).
                                # We either exhaust the string (then pos is 
                                # undefined and this was not the string we're
                                # looking for) or find offending char position.

    print$n>--($m=pos)?$m:$m-$n,$",$.-1,$/if pos
}

1
চমৎকার কাজ. আমি মনে করি আপনি প্রতিস্থাপন করতে পারেন /.{$n}/;$_=$&.$_;সঙ্গেs/.{$n}/$&$&/;
ardnew

1

জাভাস্ক্রিপ্ট (ES6) - 147 133 136 অক্ষর

s.split('\n').map((x,i)=>(v=/^(.*)(.)(.*)᛫.*\1(?!\2).\3/.exec(x+'᛫'+(a=/^(((.*).*)\2+)\3\n/.exec(s)[1])+a))&&console.log(v[1].length,i))

আশা করে যে স্ট্রিংটি ভেরিয়েবলের মধ্যে পরীক্ষা করা হবে এবং sফলাফলটি কনসোলে আউটপুট করে।

var repetitionRE = /^(((.*).*)\2+)\3\n/;
                                        // Regular expression to find repeating sequence
                                        // without any trailing sub-string of the sequence.
var sequence = repetitionRE.exec(s)[1]; // Find the sequence string.
s.split('\n')                           // Split the input into an array.
 .map(
   ( row, index ) =>                    // Anonymous function using ES6 arrow syntax
   {
     var testStr = row + '᛫'+ sequence + sequence;
                                        // Concatenate the current row, a character which won't
                                        // appear in the input and two copies of the repetitions
                                        // of the sequence from the first line.
     var match = /^(.*)(.)(.*)᛫.*\1(?!\2).\3/.exec(testStr);
                                        // Left of the ᛫ finds sub-matches for a single
                                        // character and the sub-strings before and after.
                                        // Right of the ᛫ looks for any number of characters
                                        // then the before and after sub-matches with a
                                        // different character between.
      if ( match )
       console.log( match[1].length, index );
                                        // Output the index of the non-matching character
                                        // and the row.
   }         
 );

পরীক্ষার মামলা ১

s="codegolfcodegolfco\negolfcodegolfcodeg\nlfcodegolfcodegoff\nodegolfcodegolfcod\ngolfcodegolfcodego\nfcodegolfcodegolfc"
s.split('\n').map((x,i)=>(v=/^(.*)(.)(.*)᛫.*\1(?!\2).\3/.exec(x+'᛫'+(a=/^(((.*).*)\2+)\3\n/.exec(s)[1])+a))&&console.log(v[1].length,i))

আউটপুট

16 2

পরীক্ষার মামলা 2

s="][[][][[][][[][][[][][[\n[][][[][][[][][[][][[][\n[][[][][[][][[][][[][][\n[[][][[]]][[][][[][][[]"
s.split('\n').map((x,i)=>(v=/^(.*)(.)(.*)᛫.*\1(?!\2).\3/.exec(x+'᛫'+(a=/^(((.*).*)\2+)\3\n/.exec(s)[1])+a))&&console.log(v[1].length,i))

আউটপুট

8 3

পরীক্ষার মামলা 3

s="...\n. .\n..."
s.split('\n').map((x,i)=>(v=/^(.*)(.)(.*)᛫.*\1(?!\2).\3/.exec(x+'᛫'+(a=/^(((.*).*)\2+)\3\n/.exec(s)[1])+a))&&console.log(v[1].length,i))

আউটপুট

1 1

টেস্ট কেস 4

s="ababa\nbabab\nababb\nbabab"
s.split('\n').map((x,i)=>(v=/^(.*)(.)(.*)᛫.*\1(?!\2).\3/.exec(x+'᛫'+(a=/^(((.*).*)\2+)\3\n/.exec(s)[1])+a))&&console.log(v[1].length,i))

আউটপুট

4 2

পরীক্ষার মামলা 5

s="xyxy\nyyxy"
s.split('\n').map((x,i)=>(v=/^(.*)(.)(.*)᛫.*\1(?!\2).\3/.exec(x+'᛫'+(a=/^(((.*).*)\2+)\3\n/.exec(s)[1])+a))&&console.log(v[1].length,i))

আউটপুট

0 1

পরীক্ষার মামলা 6

s="ababaababa\nababaaaaba"
s.split('\n').map((x,i)=>(v=/^(.*)(.)(.*)᛫.*\1(?!\2).\3/.exec(x+'᛫'+(a=/^(((.*).*)\2+)\3\n/.exec(s)[1])+a))&&console.log(v[1].length,i))

আউটপুট

6 1

দুঃখের বিষয়, এই পদ্ধতির ব্যর্থতা যদি, যেমন s="xyxy\nyyxy",। দ্বিতীয় লাইনের জন্য, match[4]হবে yy; এটা ন্যায়বিচার করা উচিত y
ডেনিস

পুনরায় কাজ করেছেন এবং 14 টি অক্ষর দ্বারা সংক্ষিপ্ত করেছেন।
এমটি0

খুব সুন্দর! আমি কিছু সময়ে খুব একই দ্বিতীয় রেজেক্স চেষ্টা করেছিলাম, তবে আমি সর্বনিম্ন একটির পরিবর্তে দু'বার ন্যূনতম প্যাটার্ন সংযুক্ত করেছি (এবং এইভাবে, কৃপণভাবে ব্যর্থ হয়েছে)। একটি ছোটখাটো ইস্যু: প্রথম রেজেক্সের ababপ্যাটার্নটি চমকে দেবে ababaababa; আপনি ব্যবহার করতে হবে ^…$
ডেনিস

/^…\n/কাজ বা/^…$/m
এমটি 0

1
এটির জন্য নেতৃস্থানীয় প্রয়োজন হতে পারে না ^(অন্ততপক্ষে আমি তালিকাভুক্ত test টি পরীক্ষার মামলার কোনওটির জন্য এটির প্রয়োজন নেই - তবে সম্ভবত একটি পাল্টা উদাহরণ রয়েছে যেখানে এটি এটি রেখে গেছে)।
এমটি0
আমাদের সাইট ব্যবহার করে, আপনি স্বীকার করেছেন যে আপনি আমাদের কুকি নীতি এবং গোপনীয়তা নীতিটি পড়েছেন এবং বুঝতে পেরেছেন ।
Licensed under cc by-sa 3.0 with attribution required.