Code golf 代码:删除字符串中的重复字符

Code golf 代码:删除字符串中的重复字符,code-golf,Code Golf,挑战:最短的代码,通过字符计数,检测并删除字符串中的重复字符。删除包括复制字符的所有实例(所以,如果找到3个n,则所有三个都必须删除),并且需要保留原始字符顺序 示例输入1: nbHHkRvrXbvkn 示例输出1: RrX 示例输入2: nbHHkRbvnrXbvkn 示例输出2: RrX (第二个示例删除了出现三次的字母;某些解决方案无法解释这一点) (这是基于我需要在C#中以最快的方式完成这项工作的地方,但我认为它可以很好地跨语言编写代码。)C# 71个字符Python: k[256],

挑战:最短的代码,通过字符计数,检测并删除字符串中的重复字符。删除包括复制字符的所有实例(所以,如果找到3个n,则所有三个都必须删除),并且需要保留原始字符顺序

示例输入1:
nbHHkRvrXbvkn

示例输出1:
RrX


示例输入2:
nbHHkRbvnrXbvkn

示例输出2:
RrX

(第二个示例删除了出现三次的字母;某些解决方案无法解释这一点)

(这是基于我需要在C#中以最快的方式完成这项工作的地方,但我认为它可以很好地跨语言编写代码。)

C# 71个字符

Python:
k[256],o[100000],p,c;main(){while((c=getchar())!=-1)++k[o[p++]=c];for(c=0;c<p;c++)if(k[o[c]]==1)putchar(o[c]);}
#ifdef NICE_LAYOUT
#include <stdio.h>

/* global variables are initialized to 0 */
int char_count[256];                          /* k in the other layout */
int char_order[999999];                       /* o ... */
int char_index;                               /* p  */

int main(int ch_n_loop, char **dummy)         /* c  */
                                              /* variable with 2 uses */
{

  (void)dummy; /* make warning about unused variable go away */

  while ((ch_n_loop = getchar()) >= 0) /* EOF is, by definition, negative */
  {
    ++char_count[ ( char_order[char_index++] = ch_n_loop ) ];
    /* assignment, and increment, inside the array index */
  }
  /* reuse ch_n_loop */
  for (ch_n_loop = 0; ch_n_loop < char_index; ch_n_loop++) {
    (char_count[char_order[ch_n_loop]] - 1) ? 0 : putchar(char_order[ch_n_loop]);
  }
  return 0;
}
#else
k[256],o[999999],p;main(c){while((c=getchar())>=0)++k[o[p++]=c];for(c=0;c<p;c++)k[o[c]]-1?0:putchar(o[c]);}
#endif
s=原始输入()
打印过滤器(lambda c:s.count(c)Perl
perl的21个字符,调用31个,总共36次击键(计算移位和最终返回):

C89(106个字符) 这篇文章使用了一种与我最初的答案完全不同的方法。有趣的是,在写了这篇文章之后,我发现这些方法非常相似。感谢caf在我之前提出了这种方法

b[256];l;x;main(c){while((c=getchar())>=0)b[c]=b[c]?1:--l;
for(;x-->l;)for(c=256;c;)b[--c]-x?0:putchar(c);}
一行是58+48=106字节

C89(173个字符) 这是我最初的答案。正如评论中所说,它不太好用

#include<stdio.h>
main(l,s){char*b,*d;for(b=l=s=0;l==s;s+=fread(b+s,1,9,stdin))b=realloc(b,l+=9)
;d=b;for(l=0;l<s;++d)if(!memchr(b,*d,l)&!memchr(d+1,*d,s-l++-1))putchar(*d);}
#包括
main(l,s){char*b,*d;for(b=l=s=0;l==s;s+=fread(b+s,1,9,stdin))b=realloc(b,l+=9)
;d=b;for(l=0;l1.8
s.split(“”).filter(函数(o,i,a)a.filter(函数(p)o==p).length VB.NET
对于s中的每个c:s=IIf(s.LastIndexOf(c)s.IndexOf(c),s.Replace(CStr(c),Nothing),s):下一步
诚然,VB并不是保存字符的最佳语言,但行的长度为98个字符。

Haskell 在Haskell中肯定有较短的方法可以做到这一点,但是:

Prelude Data.List> let h y=[x|x<-y,(<2).length$filter(==x)y]
Prelude Data.List> h "nbHHkRvrXbvkn"
"RrX"
Prelude Data.List>让hy=[x | xTCL
123个字符。也许可以缩短一些,但这对我来说已经足够好了

proc h {i {r {}}} {foreach c [split $i {}] {if {[llength [split $i $c]]==2} {set r $r$c}}
return $r}
puts [h [gets stdin]]
红宝石 63查尔

puts (t=gets.split(//)).map{|i|t.count(i)>1?nil:i}.compact.join
VB.NET/LINQ 96个字符表示完整的工作报表

Dim p=新字符串(从“nbHHkRvrXbvkn”组c中的c按c进入i=计数,其中i=1选择c)。ToArray)

完成工作语句,原始字符串和VB特定的“Pretty listing(代码重新格式化)”关闭,96个字符,没有原始字符串的非工作语句关闭,84个字符

(回答前请确保您的代码正常工作。谢谢。)

Scala 54个字符仅用于方法体,66个字符带有(静态类型)方法声明:

def s(s:String)=(""/:s)((a,b)=>if(s.filter(c=>c==b).size>1)a else a+b)
C C语言的完整程序,141字节(计算换行符)

#包括
c、 n[256],o,i=1;main(){for(;c-EOF;c=getchar())c-EOF?n[c]=n[c]?-1:o++:0;for(;iC)#
65个字符:

new String(h.Where(x=>h.IndexOf(x)==h.LastIndexOf(x)).ToArray());
(((1+ρx)-(ϕx)ιx)=xιx)/x
(1+=/¨(x∘∊¨x))/x
重新分配的67个字符:

h=new String(h.Where(x=>h.IndexOf(x)==h.LastIndexOf(x)).ToArray());
var a=new string(s.Where(c=>s.Count(h=>h==c)<2).ToArray());
Ruby-6153515635 “61个字符,”统治者说。(给了我另一个编码高尔夫的想法…)

put((i=gets.split(“”))-i.select{c|i.to_.s.count(c)PowerShell
61个字符。其中,
$s=“nbHHkRvrXbvkn”
$a
是结果

$h=@{}
($c=[char[]]$s)|%{$h[$_]++}
$c|%{if($h[$_]-eq1){$a+=$_}}
全功能参数化脚本:

param($s)
$h=@{}
($c=[char[]]$s)|%{$h[$_]++}
$c|%{if($h[$_]-eq1){$a+=$_}}
$a
PHP(136个字符)
C

(第1版:112个字符;第2版:107个字符)

k[256],o[100000],p,c;main(){while((c=getchar())!=-1)++k[o[p++]=c];for(c=0;cAPL
23个字符:

new String(h.Where(x=>h.IndexOf(x)==h.LastIndexOf(x)).ToArray());
(((1+ρx)-(ϕx)ιx)=xιx)/x
(1+=/¨(x∘∊¨x))/x
我是一个APL新手(昨天学的),所以请友善一点——这肯定不是最有效的方法。我很惭愧我没有在很大程度上打败Perl

再说一次,当一个新手用APL解决这个问题的最自然的方法仍然比迄今为止任何语言的任何其他解决方案都要简洁时,也许这说明了什么。

Haskell

(只是把马克·拉沙科夫的作品删掉几个字符,我宁愿把它作为对他的评论发布)

执行相同的筛选,然后将结果列表作为唯一的元素放入列表中。现在箭头(看起来像集合包含!)表示“对于RHS列表中的每个元素,依次调用该元素
[z]
”。
[z]
是包含单个元素
z
,因此元素“
过滤器(=x)y
”如果它只包含一个元素,则只能称为“
[z]
”。否则它将被丢弃,并且永远不会用作
z
的值。因此
z
的值(在列表中的
|
左侧返回)正是使
过滤器返回长度为1的列表的
x

这是我的第二个版本,我的第一个版本返回
x
,而不是
z
——因为它们是一样的——并将
z
重命名为
,这是Haskell符号,表示“这个值不会被使用,所以我不会通过给代码命名来使代码复杂化”。

LabVIEW 7.1 一个字符,即方框图中的蓝色常量“1”。 我发誓,输入是复制粘贴;-)

PHP 118个字符的实际代码(加上PHP块标记的6个字符):

另一个APL解决方案
作为动态函数(18个字符)

假设输入为变量x(16个字符)的行:

Javascript 1.6 比之前发布的Javascript 1.8解决方案短(71个字符vs 85个字符)

C:83 89 93 99 101个字符
  • O(n2)时间
  • 限制为999个字符
  • 仅在32位模式下工作(由于不包含
    -ing
    (花费18个字符),使得
    的返回类型被解释为
    int
    ,并截断一半地址位)
  • 在Mac上显示友好的“警告:此程序使用gets(),这是不安全的。”

(此类似的82-chars版本通过命令行获取输入:

main(char*c,char**S){for(c=*++S;*c;c++)strchr(*S,*c)-strrchr(*S,*c)||putchar(*c);}
)C#(53个字符) 其中s是您的输入字符串:

new string(s.Where(c=>s.Count(h=>h==c)<2).ToArray());
新字符串(其中c=>s.Count(h=>h==c)s.Count(h=>h==c)J(16-12个字符)<
param($s)
$h=@{}
($c=[char[]]$s)|%{$h[$_]++}
$c|%{if($h[$_]-eq1){$a+=$_}}
$a
<?PHP
function q($x){return $x<2;}echo implode(array_keys(array_filter(
array_count_values(str_split(stream_get_contents(STDIN))),'q')));
<?PHP
echo implode(array_keys(array_filter(array_count_values(str_split(
stream_get_contents(STDIN))),function($x){return $x<2;})));
k[256],o[100000],p,c;main(){while((c=getchar())!=-1)++k[o[p++]=c];for(c=0;c<p;c++)if(k[o[c]]==1)putchar(o[c]);}
/* #include <stdio.h> */
/* int */ k[256], o[100000], p, c;
/* int */ main(/* void */) {
  while((c=getchar()) != -1/*EOF*/) {
    ++k[o[p++] = /*(unsigned char)*/c];
  }
  for(c=0; c<p; c++) {
    if(k[o[c]] == 1) {
      putchar(o[c]);
    }
  }
  /* return 0; */
}
#ifdef NICE_LAYOUT
#include <stdio.h>

/* global variables are initialized to 0 */
int char_count[256];                          /* k in the other layout */
int char_order[999999];                       /* o ... */
int char_index;                               /* p  */

int main(int ch_n_loop, char **dummy)         /* c  */
                                              /* variable with 2 uses */
{

  (void)dummy; /* make warning about unused variable go away */

  while ((ch_n_loop = getchar()) >= 0) /* EOF is, by definition, negative */
  {
    ++char_count[ ( char_order[char_index++] = ch_n_loop ) ];
    /* assignment, and increment, inside the array index */
  }
  /* reuse ch_n_loop */
  for (ch_n_loop = 0; ch_n_loop < char_index; ch_n_loop++) {
    (char_count[char_order[ch_n_loop]] - 1) ? 0 : putchar(char_order[ch_n_loop]);
  }
  return 0;
}
#else
k[256],o[999999],p;main(c){while((c=getchar())>=0)++k[o[p++]=c];for(c=0;c<p;c++)k[o[c]]-1?0:putchar(o[c]);}
#endif
(((1+ρx)-(ϕx)ιx)=xιx)/x
h y=[x|x<-y,[_]<-[filter(==x)y]]
h y=[z|x<-y,[z]<-[filter(==x)y]]
(<2).length $ filter (==x) y
[z] <- [filter(==x)y]
<?php
$s=trim(fgets(STDIN));$x='';while(strlen($s)){$t=str_replace($s[0],'',substr($s,1),$c);$x.=$c?'':$s[0];$s=$t;}echo$x;
{(1+=/¨(ω∘∊¨ω))/ω}
(1+=/¨(x∘∊¨x))/x
s.match(/(.)(?=.*\1)/g).map(function(m){s=s.replace(RegExp(m,'g'),'')})
main(){char s[999],*c=gets(s);for(;*c;c++)strchr(s,*c)-strrchr(s,*c)||putchar(*c);}
main(char*c,char**S){for(c=*++S;*c;c++)strchr(*S,*c)-strrchr(*S,*c)||putchar(*c);}
new string(s.Where(c=>s.Count(h=>h==c)<2).ToArray());
var a=new string(s.Where(c=>s.Count(h=>h==c)<2).ToArray());
(~.{~[:I.1=#/.~) (~.{~[:I.1=#/.~) 'nbHHkRvrXbvkn' RrX (~.#~1=#/.~) 'nbHHkRvrXbvkn' RrX
import Data.List
import Control.Monad
import Control.Arrow
main=interact$liftM2(\\)nub$ap(\\)nub
(liftM2(\\)nub$ap(\\)nub) "nbHHkRvrXbvkn"
main=interact$liftM2 ap liftM2 ap(\\)nub
fold -w1|sort|uniq -u|paste -s -d ''
    xchg cx,bp
    std
    mov al,2
    rep stosb
    inc cl
l0: ; to save a byte, I've encoded the instruction to exit the program into the
    ; low byte of the offset in the following instruction:
    lea si,[di+01c3h] 
    push si
l1: mov dx,bp
    mov ah,6
    int 21h
    jz l2
    mov bl,al
    shr byte ptr [di+bx],cl
    jz l1
    inc si
    mov [si],bx
    jmp l1
l2: pop si
l3: inc si
    mov bl,[si]
    cmp bl,bh
    je l0+2
    cmp [di+bx],cl
    jne l3
    mov dl,bl
    mov ah,2
    int 21h
    jmp l3
 programname < input > output
  .`{\{=}+,,(!}+,
+-------------------------------------------------------------------------+
||    |    |    |    |    |    |    |    |    |    |    |    |    |    |  |
|0         10        20        30        40        50        60        70 |
|                                                                         |
+-------------------------------------------------------------------------+