string - Code Golf : "Color highlighting" of repeated text

标签 string language-agnostic code-golf rosetta-stone

锁定。这个问题及其答案是locked因为这个问题是题外话,但具有历史意义。它目前不接受新的答案或互动。








(感谢下面的 greg0ire 帮助解决关键概念)

挑战:
构建一个程序来查找所有子字符串并用颜色属性“标记”它们(在 XML 中有效地突出显示它们)。

规则:

  • 这应该只对长度为 2 或更多的子字符串进行。
  • 子字符串只是连续字符的字符串,其中可能包括非字母字符。请注意,空格和其他标点符号不会分隔子字符串。
  • 字符大小写不能忽略。
  • “突出显示”应该通过在 XML 中标记子字符串来完成。您的标签应采用 <TAG#>theSubstring</TAG#> 的形式哪里#是该子串和相同子串唯一的正数。
  • 该算法的优先级是找到最长的子串,而不是它在文本中匹配的次数。

  • 注意:以下示例中显示的标记顺序并不重要。为清楚起见,OP 仅使用它。

    示例输入:
    LoremIpsumissimplydummytextoftheprintingandtypesettingindustry.LoremIpsumhasbeentheindustry'sstandarddummytexteversincethe1500s,whenanunknownprintertookagalleyoftypeandscrambledittomakeatypespecimenbook.
    

    部分正确的输出(在此示例中,OP 可能没有完全替换)
    <TAG1>LoremIpsum</TAG1>issimply<TAG2>dummytext</TAG2>of<TAG5>the</TAG5><TAG3>print</TAG3>ingand<TAG4>type</TAG4>setting<TAG6>industry</TAG6>.<TAG1>LoremIpsum</TAG1>hasbeen<TAG5>the</TAG5><TAG6>industry</TAG6>'sstandard<TAG2>dummytext</TAG2>eversince<TAG5>the</TAG5>1500s,whenanunknown<TAG3>print</TAG3>ertookagalleyof<TAG4>type</TAG4>andscrambledittomakea<TAG4>type</TAG4>specimenbook.
    

    您的代码应该能够处理边缘情况,例如:

    示例输入 2:
    hello!TAG!</hello.TAG.</
    

    示例输出 2:
    <TAG1>hello</TAG1>!<TAG2>TAG</TAG2>!<TAG3></</TAG3><TAG1>hello</TAG1>.<TAG2>TAG</TAG2>.<TAG3></</TAG3>
    

    赢家:
  • 最优雅的解决方案获胜(由
    其他评论,点赞)
  • 奖金
    解决方案的要点/考虑
    使用 shell 脚本


  • 小说明:
  • 输入可以硬编码或从文件中读取
  • 标准仍然是“优雅”,诚然这有点模糊,但它也包含了简单的字符/行数。其他人的评论和/或点赞也表明 SO 社区如何看待挑战
  • 最佳答案

    Perl 206、189、188、199、157 个字符,不包括原始字符串和最后打印。

     #New algorithm that produces correct ouputs for many cases
    
    
    
        push@z,q/LoremIpsumissimplydummytextoftheprintingandtypesettingindustry.LoremIpsumhasbeentheindustry'sstandarddummytexteversincethe1500s,whenanunknownprintertookagalleyoftypeandscrambledittomakeatypespecimenbook/;
    
        push@z,q/oktooktobookokm/;
        push@z,q!dino1</dino2</!;
        push@z,q!dino1TAG2dino3TAG!;
    
        ## loop for tests doesn't count
        for $z(@z) {
        print "input : $z\n";
        $i=0;@r=();
        #### begin count
        $c=127;$l=length($_=$z);while($l>1){if(/(.{$l}).*\1/){push@r,$1;++$c;s/$1/chr($c)/eg}else{$l--}}$z=$_;map{++$i;$x=chr(127+$i);$z=~s:$x:<TAG$i>$_</TAG$i>:g}@r
        #### end count 157 chars
        ## output doesn't count
        ;print "output : $z\n","="x80,"\n"
        }
    
    __END__
    perl tags2.pl
    input : LoremIpsumissimplydummytextoftheprintingandtypesettingindustry.LoremIpsumhasbeentheindustry'sstandarddummytexteversincethe15
    00s,whenanunknownprintertookagalleyoftypeandscrambledittomakeatypespecimenbook
    
    output : <TAG1>LoremIpsum</TAG1>i<TAG11>ss</TAG11><TAG12>im</TAG12>ply<TAG2>dummytext</TAG2><TAG6>oft</TAG6><TAG13>he</TAG13><TAG4>p
    rint</TAG4><TAG7>ing</TAG7><TAG8>and</TAG8><TAG5>types</TAG5>e<TAG14>tt</TAG14><TAG7>ing</TAG7><TAG3>industry</TAG3>.<TAG1>LoremIpsu
    m</TAG1>hasbe<TAG15>en</TAG15><TAG9>the</TAG9><TAG3>industry</TAG3>'<TAG11>ss</TAG11>t<TAG8>and</TAG8>ard<TAG2>dummytext</TAG2>ev<TA
    G16>er</TAG16>since<TAG9>the</TAG9>1500s,w<TAG13>he</TAG13>nanunknown<TAG4>print</TAG4><TAG16>er</TAG16>t<TAG10>ook</TAG10>agal<TAG1
    7>le</TAG17>y<TAG6>oft</TAG6>y<TAG18>pe</TAG18><TAG8>and</TAG8>scramb<TAG17>le</TAG17>di<TAG14>tt</TAG14>omakea<TAG5>types</TAG5><TA
    G18>pe</TAG18>c<TAG12>im</TAG12><TAG15>en</TAG15>b<TAG10>ook</TAG10>
    ================================================================================
    input : oktooktobookokm
    output : <TAG1>okto</TAG1><TAG1>okto</TAG1>bo<TAG2>ok</TAG2><TAG2>ok</TAG2>m
    ================================================================================
    input : dino1</dino2</
    output : <TAG1>dino</TAG1>1<TAG2></</TAG2><TAG1>dino</TAG1>2<TAG2></</TAG2>
    ================================================================================
    input : dino1TAG2dino3TAG
    output : <TAG1>dino</TAG1>1<TAG2>TAG</TAG2>2<TAG1>dino</TAG1>3<TAG2>TAG</TAG2>
    ================================================================================
    

    关于string - Code Golf : "Color highlighting" of repeated text,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3171552/

    相关文章:

    javascript - JS : how to get resulting value out of its function?

    java - 创建并有机会从字符串池中创建 String 对象有什么意义

    language-agnostic - 您什么时候在该领域使用过动态规划?

    language-agnostic - 静态内容的部署

    code-golf - Code Golf : Tic Tac Toe

    system.reactive - 检测 IObservable 上的 IsAlive

    language-agnostic - Code-Golf:友好数字缩写

    android - 如何按字符串删除 sqlite 表行条目?

    string - 如何从 Go 中的字符串中替换第 n 个字符

    multithreading - 多线程意义上的机器指令假设